Background: Now consider the problem of representing, inside the computer, a con
ID: 3871116 • Letter: B
Question
Background: Now consider the problem of representing, inside the computer, a connection from a road to or from a particular incoming connection to an intersection. There are numerous solutions! First consider going perhaps a bit overboard and having a separate object for each lane in and out of an interesection. Where we used to have just one intersection object, we how have a constellation of objects, one central one for the intersection itself, and one for each lane in and out of that intersection.
A Problem: How would this change the definition of class Road. Concentrate on changes to the data, ignore changes in the methods.
import java. io. File; import java. util. LinkedList; import java. util. Scanner; Roads are one-way streets linking intersections ee Intersection class Road //measured in seconds float travelTime; Intersection destination; //where the road goes Intersection source; //name of road is source-destination //where the comes from Intersections join roads see Road class Intersection String name; LinkedList Road» outgoing LinkedList Road» incoming new LinkedList Road» () ; new LinkedList Road () ; Bug: multiple types of intersections (uncontrolled, stoplight) RoadNetwork, the main class to build a network of roads and intersections see Road ee Intersection k/ public class RoadNetwork public static void main (String[] args) [ //Bug: Must add code to see if there is a file name Scanner scnew Scanner ( new File( args[0]) // Bug: What if the file doesn' t exist?Explanation / Answer
Please find comments inline
PROGRAM CODE:
import java.io.File;
import java.util.LinkedList;
import java.util.Scanner;
/** Roads are one-way streets linking intersections
* @see Intersection
*/
class Road {
float travelTime; //measured in seconds
Intersection destination; //where the road goes
Intersection source; //where the comes from
//All the stops can be added to another linkedlist
LinkedList <Intersection> stops = new LinkedList <Intersection> ();
// name of road is source-destination
}
/** Intersections join roads
* @see Road
*/
class Intersection {
String name;
LinkedList <Road> outgoing = new LinkedList <Road> ();
LinkedList <Road> incoming = new LinkedList <Road> ();
// Bug: multiple types of intersections (uncontrolled, stoplight)
//This bug is addressed in Road class by adding multiple stops to a Road
/** RoadNetwork, the main class to build a network of roads and intersections.
* @see Road
* @see Intersection
*/
public class RoadNetwork {
public static void main(String[] args) {
// Bug: Must add code to see if there is a file name
// Bug: What if the file doesn't exist?
// both the bbugs mentioned above have been fixed using loops below
//Checking if arguments are provided
if(args.length > 0)
{
//checking if the file is found. If not output to svcreen that file was not found
try
{
Scanner sc = new Scanner( new File( args[0] ) );
}catch(FileNotFoundException fe)
{
System.out.println( args[0] + " not found");
}
}
else
{
System.out.println( "File name not provided.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.