Assignment 4, due Sep 15Solutions
Part of
the homework for CS:2820, Fall 2017
|
a)
System.out.println( "Start" ); for (int i = 1; i < 10; i++) { System.out.println( "i = " + i ); } System.out.println( "End" );
System.out.println( "Start" ); { int i = 1; while (i < 10) { System.out.println( "i = " + i ); i++; } } System.out.println( "End" );
b)
for (Road r:roads) { System.out.println( r.toString() ); }
{ Iteratorit = roads.iterator(); while (it.hasNext()) { Road r = it.next(); System.out.println( r.toString() ); } }
a) Give an appropriate declaration for the list of roads inside class Road. (0.3 points)
private static LinkedListroads = new LinkedList ();
Strictly speaking, the initial value (every thing after the equals sign) is not required by this question.
b) Give the code you would use inside the Road constructor so that the constructed road automatically adds itself to the list. (0.3 points)
roads.add( this );
c) The main class still needs to iterate over all the roads in order to print out the textual representation of the road network. Suggest how class roads could permit this without exporting the list itself. (0.4 points)
One solution is to put a print-roads public static method in class Road that prints all the roads.
Another solution is put a road-iterator public static method in class Road that returns an iterator over the private variable roads.
There are, of course, other solutions.
a) Identify all of the distinct paths through this code. (0.5 points)
The adequacy of the above solution is fairly obvious. Note that there are 2 boolean tests in the control structure, one testing for loop termination and one testing for a hit or miss. With 2 booleans, there are 4 possible combinations of true and false, so it should be no surprise that we found 4 paths. This does not mean that there must be 4 paths; in fact, we can actually execute each statement at least once with just 3 paths:
b) Describe how you could test these paths using input to the road-network program. Merely giving the input file(s) is not sufficient without an explanation of how what each input file or each input line tests. (0.5 points)
intersection i -- searches an empty list to see if i already defined road i j -- searches for and finds i, fails to find j
That's actually a sufficient test!