代写CHC5223 Data Structures and Algorithms 2023–2024 Semester 2 Assignment 2代做Java程序

CHC5223 Data Structures and Algorithms

2023–2024 Semester 2

Assignment 2

Value 60% of coursework: Part A is 30% and Part B is 30%

Individual work

Learning outcomes

Students will be able to understand

1.1 Data structures

1.2 The applications of data structures

1.3 Object-oriented programming concepts

1.4 Methods for program testing

Students will have acquired skills in:

2.1 Data abstraction

2.2 The use of data structures

2.3 Programming at a more advanced level in a high-level object-oriented language 2.4 Program testing and documentation

Students will have acquired skills in:

3.1 Self-management

3.2 Learning

3.3 Communication

3.4 Problem solving

3.5 Information technology

Submission requirements

The assignment submitted should compressed into a  .zip file, the following files should be contained in the compressed file:

•    a report as a Microsoft Word document containing text of all your classes. filename format: student ID+CHC5223_CW2_Report.docx

•    a .zip file containing the project: the runnable jar file (if available) and all the program’s sourcetexts (.java), including those provided filename format: student ID +CHC5223_ CW2_Files.zip

Part A - binary search tree

General requirements

All your programming must conform. to “Java Conventions and Programming Guidelines” – see module Moodle site.

You must paste the source code of all your classes into your report, as text or images.

Introduction

The topic of this part of the assignment is binary search trees.

The interface IMemberDB describes methods for an abstract data type (ADT) which holds a database of Member objects.

You must implement IMemberDB as a binary search tree for Assignment 2.

You must not make any changes to these interfaces.

Requirements

Basic rules

You must create one executable project after completing all tasks.

One Java class should be defined in one .java file respectively.

In the report, the source code of each task, together with the corresponding explanation, should be presented separately.

Failure to comply with these rules will result in zero marks.

Task 1

You must create a Java class called MemberBST that implements the interface IMemberDB.

You must use a binary search tree but it does not need to be self-balancing.

You must not encapsulate existing implementations of collections in your submission. For

example, you must not create a TreeMap object and call methods on that object from your class. Failure to comply with this will result in zero marks for that part.

Tip: use String.compareTo to compare strings lexicographically. You can treat uppercase and lowercase as different. (Hash codes have no place in this assignment.)

Methods can be implemented either iteratively or recursively. You must not implement the method remove by just building a new tree.

You may make use of the supplied source code for the method remove, based on Object-

Oriented Programming in Oberon-2, Hanspeter Mössenböck Springer-Verlag 1993, page 78, transcribed into Java by David Lightfoot (see Appendix).

The constructor for MemberBST must print the string  Binary Search Tree” to System.out.

Take care that you have not used a linear search O(n) where you should have used a binary search tree, aiming towardsO(logn). 5 marks

You must give clear rationales and detailed explanations of your design and implementation in the report. 5 marks

Task 2

You must make appropriate use of assertions (assert statements) to protect preconditions of the operations. Remember to enable assertion checking for your project.

You must give clear rationales and detailed explanations of your design and implementation in the report.  2 mark

Task 3

You must make your class log monitoring information, either to a text file or by calls of System.out.println.

It must log (at least):

•    for every addition of a Member (put), for every retrieval of a Member(get),for every deletion of a Member(remove):

o the Member name;

o the sequence of nodes of the tree visited.

•    Paste your loginto your report.

We have supplied a main program CHC5223.java for your use with this assignment.

The name of the file is set in the static variable filename.

Sample files sampleMembersUK.csv and sampleMembersUS.csv each contain 500 members in this format. 3 marks

You must give clear rationales and detailed explanations of your design and implementation in the report.  3 mark

Task 4

You must devise a test plan for your implementation. Be sure to check (among many other cases).

•    that deleting a leaf node works correctly

•    that deleting a node with one descendant works correctly

•    that deleting a node with two descendants works correctly 2 marks

You must give clear rationales and detailed explanations of your design and implementation in the report. 3 mark

Task 5

By using the supplied main program, or by other means, you must test your MemberBST.

You must give clear evidence and detailed explanations of your test in the report, including your test plan, test data used, expected results and actual results. You must show your actual results  and the logging information copied from your log file or the output pane of your IDE. Do not simply state “test passed”, or similar.  5 mark

Task 6

You must comment on the time efficiency and space efficiency of your implementation of the binary search tree.

You must give clear rationales and detailed explanations in the report.  2 mark

total 30 marks

Part B - graphs and pathfinding

The topic of this part of Assignment 2 is graphs and pathfinding.

General requirements

All your programming must conform. to “Java Conventions and Programming Guidelines” . You must paste the source code of all your classes into your report, as textnot images.

You must implement all necessary data structures using only arrays.

When programming in Java it is usual to make use of collection classes from the Java class library. However, if you need to program in some other language such classes, or their equivalents, will not necessarily be available.

Task 1

Find or devise a transport network – it need not be real, but it must be realistic. It must bean undirected, connected graph, with no loops. It must have at least eight nodes.

Each node should have a name (with no spaces in it) and x andy positions (0 < x < 256 and 0 < y < 256) indicating the approximate position of the node on amap with a 256 × 256 grid (y increasing downwards).

The links must contain information about the distance between the nodes it connects. The distance can be measured in suitable units of length, such as km, or time, such as minutes.

The links between them must be such that there are several pairs of nodes that are linked by more than one route.

You must sketch your network and include it in your report. The sketch must show each node annotated with its name and located on the sketch at its x andy position. The sketch must show each link annotated with its distance. You can make the sketch by hand but if you do so you will  need to scan it to include in your report. 2 marks

Task 2

You must express your network as a text file using the syntax:

“station” name xy

linkstation station distance

Each station must have been defined in a station line before being cited in a link line.

Include the content of the text file in your report. 2 marks

Task 3

You must implement Java classes StackInt, QueueIntListInt and SetInt. These will be subclasses  respectively of abstract classes AbsStackInt, AbsQueueInt, AbsListInt, AbsSetInt, whose source is provided and shown in the appendixes. These are given as abstract classes (to be ‘sub-classed’)  rather than as interfaces (to be ‘implemented’) partly because they are all ‘bounded’, that is, with limited capacity because of being implemented by arrays, and also to allow us to give you hints on how to implement them.

The required behaviour of the methods of the classes is indicated as pre- and post-conditions in comments.

You must give clear rationales and detailed explanations of your design and implementation in the report. 4 marks

Task 4

You must make appropriate use of assertions (assert statements) to protect preconditions of the operations. Remember to enable assertion checking for your project. 1 mark

Task 5

By using JUnit or otherwise you must test your implementations of StackIntQueueIntListInt and SetInt. To do this, create objects of each class in which capacity is set to a low value, such as 10. You will set it to something larger when deploying the classes later.

You must give clear evidence and detailed explanations of your design and implementation in the report, including the test plan, test data used, expected results, and actual results. Your actual results must be shown either as screenshot images or as text copied from the output pane of the development toolkit. 4 marks

Task 6

You must create a Java class StationInfo to implement the Java interface IStationInfo (supplied). 1 mark

Task 7

You must create a Java class to define an adjacency matrix

final double NO_LINK = Double.MAX_VALUE; // was erroneous double.MAX_VALUE

int numStations; // 0 <= numStations <= capacity

double distance[ ][ ];

where,for all i,j in 0 <= numStations <= capacity

distance [i][j] is the distance from station with number i to station with number j

distance [i][j] is equal to distance [j][i]

distance [i][i] = NO_LINK

Task 8

and also define a list of objects of class StationInfo held in an array.

Task 9

You must create a Java method in this class that reads a text file containing the textual description of your network and builds the corresponding list of stations and the matrix of links.

You must perform. as many validity checks in this as you can and report any errors. Include the text of this class in your report.

You must give clear rationales and detailed explanations of your design and implementation in the report. 2 marks

Task 10

Use print statements to show the contents of the list and the matrix after using your program to build them from the data held in your text file. Copy the results into your report.

You must give clear rationales and detailed explanations of your design and implementation in the report. 2 marks

Task 11

You must write Java methods (as in Appendixes) to perform. a depth-first traversal from a given node of your network and a breadth-first traversal from the same node, making use of the algorithms given in the appendix. Include the methods text and the resulting sequence of node names in your report.

You must give clear rationales and detailed explanations of your design and implementation in the report. 4 marks

Task 12

You must implement Dijkstra’s algorithm (as in Appendix) making use of the data structures you have constructed, to find and display the shortest path between two stations in your network. You must also ‘instrument’ your implementation to count the number of iterations of the while  loop

You must give clear rationales and detailed explanations of your design and implementation in the report.   4 marks

Task 13

You must give clear rationales and detailed explanations of the difference between Dijkstra’s algorithm and A* algorithm and state how their behavior. would differ in the case of your network. 4 marks

total 30 marks

Obtaining help

It is encouraged to request further clarification on what is required for this assignment. Please  try to do this during normal contact time and avoid asking for such help in the last week before the deadline.

You can discuss the requirements and the material covered in the assignment with others but what you create must be all your own work. Be careful to avoid collusion.

Declare in your report any help you have received other than that from the module teaching team.

Feedback

In addition to the written feedback that we aim to provide within the normal interval, you will be able to obtain fast, brief, verbal formative feedback and help on correcting your work at your practical classes.

Appendix: remove

private class Node {

private Member data;

private Node left, right;

public Node(Members) {data = s; left = null; right = null;}

} // Node

private Node root;

public Member remove(String name){

// based on Object-Oriented Programming in Oberon-2

// Hanspeter Mössenböck Springer-Verlag 1993, page 78

// transcribed into Java by David Lightfoot

// put assert statement for preconditions here

Node parent = null, del, p = null, q = null;

Member result;

del = root;

while (del != null && !del.data.getName().equals(name)) {

parent = del;

if (name.compareTo(del.data.getName()) < 0)

del = del.left;

else

del = del.right;

}// del == null  || del.data.getName().equals(name))

if(del != null) { // del.data.getName().equals(name)

// find the pointer p to the node to replacedel

if (del.right == null) p = del.left;

elseif (del.right.left == null) {

p = del.right; p.left = del.left;

} else {

p = del.right;

while (p.left != null) {q = p; p = p.left;}

q.left = p.right; p.left = del.left; p.right = del.right;

}

if(del == root) root = p;

elseif (del.data.getName().compareTo(parent.data.getName()) < 0)

parent.left = p;

else parent.right = p;

// reduce size of tree by one

result = del.data;

}

else result = null;

return result;

} // remove

Appendix Abstract class AbsListInt

package CHC5223; // or whatever

/**

* Abstract class to be sub-classed by class(es) that represent lists of ints *

* You may change the package name for this, but you should not

* modify it in any other way. *

*/

abstract public class AbsListInt {

protected int list[];

protected int size; // 0 <= size <= capacity

protected final int capacity;

/**

* @param capacity -- maximum capacity of this list

* @post new list of current size zero has been created */

public AbsListInt(int capacity){

// implements abounded list of int values

this.capacity = capacity;

this.size = 0;

list = new int[capacity];

}

public int getCapacity() {return capacity;}

public int getSize() {return size;}

/**

* @param n node to be added

* @pre getSize() != getCapacity()

* @post nhas been appended to list */

abstract public void append(int n);

/**

* @param x -- value to besought

* @pre true

* @return true if x is in list*/

abstract public boolean contains(intx);

}

Appendix Abstract class AbsQueueInt

package CHC5223; // or whatever

/**

* Abstract class to be sub-classed by class(es) that represent stacks of ints *

* You may change the package name for this, but you should not

* modify it in any other way. *

*/

abstract public class AbsQueueInt {

protected int queue[];

protected int size; // 0 <= size <= capacity

protected final int capacity;

public AbsQueueInt(int capacity){

this.capacity = capacity;

this.size = 0;

this.queue = new int[capacity];

}

public int getCapacity() {return capacity;}

public int getSize() {return size;}

/**

* @param n node to be added

* @pre getSize() != getCapacity()

* @post n has been added to back of queue */

abstract public void addToBack(int n);

/**

* @pre getSize() != 0

* @post element at front of queue has been removed

* @return value that has been removed */

abstract public int removefromFront();

}

Appendix Abstract class AbsSetInt

package CHC5223; // or whatever

/**

* Abstract class to be sub-classed by class(es) that represent stacks of ints *

* You may change the package name for this, but you should not

* modify it in any other way. *

*/

abstract public class AbsSetInt {

protected int set[];

protected int size; // 0 <= size <= capacity

protected final int capacity;

/**

* @param capacity -- maximum capacity of this queue

* @pre capacity >= 0

* @post new set of current size zero has been created */

public AbsSetInt(int capacity){

this.capacity = capacity;

this.size = 0;

this.set = new int[capacity];

}

public int getCapacity() {return capacity;}

public int getSize() {return size;}

/**

* @param x -- value to besought

* @pre true

* @return true iffx is in list*/

abstract public boolean contains(intx);

/**

* @param n node to be added

* @pre contains(n)  || getSize() != getCapacity()

* @post contains(n) */

abstract public void include(int n);

/**

* @pre true

* @post !contains(n) */

abstract public void exclude(int n);

}

Appendix Abstract class AbsStackInt

package CHC5223; // or whatever

/**

* Abstract class to be sub-classed by class(es) that represent stacks of ints *

* You may change the package name for this, but you should not

* modify it in any other way. *

*/

abstract public class AbsStackInt {

protected int stack[];

protected int size; // 0 <= size <= capacity

protected final int capacity;

/**

* @param capacity -- maximum capacity of this queue

* @pre capacity >= 0

* @post new stack of current size zero has been created */

public AbsStackInt(int capacity){

this.capacity = capacity;

this.size = 0;

stack = new int[capacity];

}

public int getCapacity() {return capacity;}

public int getSize() {return size;}

/**

* @param n node to be added

* @pre getSize() != getCapacity()

* @post n has been pushed on to top of stack */

abstract public void push(int n);

/**

* @pre getSize() != 0

* @post element on top of stack has been removed

* @return value that has been removed */ abstract public int pop();

/**

* @pre getSize() != 0

* @return value on top of stack */

abstract public int peek() ;

}

Interface IStationInfo

package CHC5223; // or whatever

/**

* Interface to be implemented by class(es) that represent

* information about stations *

* You may change the package name for this, but you should not

* modify it in any other way. *

*/

public interface IStationInfo {

/**

* @return the name of the station

*/

String getName();

/**

* @return x position -- 0 <= getxPos() < 256

*/

intgetxPos();

/**

* @return y position -- 0 <= getyPos() < 256

*/

int getyPos();

}

Appendix: breadth-first traversal, beginning with a specified start station

Let Q bean empty queue of Stations

Let L be an empty list of Stations

Add the start station to the back of Q

while Q is not empty do

Remove the Station at the front of Q, call this Station S

if S is not in list L then

Append S to L

for each station S2 that is adjacent to S do

if S2 is not in the list L then

Add S2 to the back of queue Q

endif

endfor

endif

endwhile

return L

Appendix: depth-first traversal, beginning with a specified start station.

Let S be a stack of Stations

Create an initially empty list of Stations, which we will call L

Push the start station on to the stack S

while S is not empty do

Pop the Station at the top of the stack. Call this Station T.

if T is not already in list L then

add it to the back of that list

endif

for each station T2 that is adjacent to T do

if T2 is not in the list L then

push it on to the top of the stack S

endif

endfor

endwhile

return L

Appendix: Dijkstra’s algorithm for shortest path in a network

set Closed to be empty

add all nodes in the graph to Open.

set the g-value of Start to 0, and the g-value of all the other nodes to ∞

set previous to be none for all nodes.

while End is not in Closed do

let X be the node in Open that has the lowest g-value (highest priority)

remove X from Open and add it to Closed.

if X is not equal to End then

for each node N that is adjacent to X in the graph, and also in Open do

let g’ = g-value of X + cost of edge from N to X

if g’ is less than the current g-value of N then

change the g-value of N to g’

make N’s previous pointer point to X

endif

endfor

endif

endwhile

reconstruct the shortest path from Start to End by following “previous” pointers to find the previous node to End, the previous node to that previous node, and soon.





热门主题

课程名

ib3960 csci-ua.0480-003 econ12-200 ectb60h3f cs247—assignment comp9414 tk3163 ce335 math2010 ec3450 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 econ7230 math2014 eec180 msinm014/msing014/msing014b math350-real stat141b econ2101 fit2004 comp643 econ20120/econ30320 bu1002 cm2030 mn7182sr ectb60h3s ib2d30 ohss7000 fit3175 acct7104 compsci 369 math226 127.241 info1110 37007 math137a mgt4701 comm1180 fc300 ectb60h3 comm1170 llp120 bio99 econ7030 csse2310/csse7231 125.330 110.309 comm1190 csc3100 bu1007 comp 636 qbus3600 compx222 stat437 kit317 hw1 ag942 fit3139 115.213 ipa61006 econ214 envm7512 6010acc fit4005 fins5542 slsp5360m 119729 cs148 hld-4267-r csci410 comp4002/gam cava1001 or4023 cosc2758/cosc2938 cse140 fu010055 finc3017 comp9417 24309 bsys702 mgec61 cive9831m pubh5010 fsc60504 5bus1037 plana4310 info90004 p6769 bsan3209 plan6392 625.609.81 csmai21 fnce6012 misy262 ifb106tc csci910 502it comp603/ense600 4035 csca08 8iar101 caes1000 econ0060 ap/adms4540 ast101h5f bsd131 msci242l 158225 7014mhr econ60822 ecn302 csci 4261 elec51020 blaw1002 ec3044 acct40115 csi2108–cryptographic philo225-24a acst2001 fit9132 comp1117b ad654 comp3221 st332 cs170 n1608 msim3101 badp2003 mth002 econ0033 engr228-digital law-10027u fit5057 ve311 sle210 6012acc 072243a 3809ict amath 483 ifn556 cven4051 2024 comp9024 158.739-2024 comp 3023 ecs122a com63004 infs704 bms5021 comp1028 genc3004 phil2617 civl2700/9700 mth116 ee401 eco2011 fit5225 econ7021 cpt106 clim1001 emet8002 bsys602 orbs7120 comp2001j nzditts5 ecs797p musc2700 hudm5122 bafi3271 env101 ap/adms3900a busi1271 it5015 eng4052 econ60662 math2003j finc6010 125.320 aic2100 fit3152 prog2003 mast20004 mgts2604 com71404 civl5266 ecf2721 civ6746 cdlc001 bsys501 mcs.200 xjco1921 comp3009j ecs797p/u econ0027
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图