Background: In the code distributed with Lecture ll on February 10, instances of
ID: 3793813 • Letter: B
Question
Background: In the code distributed with Lecture ll on February 10, instances of class Intersection are declared to have a field outgoing that is a list of the roads leading away from that intersection. This list is never used in the code that was distributed. It was only included because we speculated that we would need it later. The fact is, we will need it. So, when a road is defined from one intersection to another, there must be something in the initializer for class Road like if (source null Bug: need code here to add this road to source. outgoing We are likely to also want to add something like this to class Intersection: public addoutgoing( Road r) t outgoing .add a) Why does the code distributed to the class force us to add a method to class Intersection? (0.5 points) b) Write the code to call addoutgoing from class Road. This code replaces the bug notice given above. (0.5 points)Explanation / Answer
Question a :
--------------------------------
In Intersection.java "outgoing" List is Private . So we can't access this from any other class
So we need to add a public method to add to this list
Question b :
--------------------------------
Please check below modified files
Intersection.java :-
------------------------------------
import java.util.LinkedList;
import java.util.Scanner;
/** Intersections are linked by one-way roads
* @see Road
* @see RoadNetwork#findRoad(String)
*/
class Intersection {
private final LinkedList <Road> outgoing = new LinkedList <Road> ();
// Bug: Does the intersection need a list of incoming roads?
public final String name; // the name of the intersection
/** initializer scans and processes one intersection definition
*/
public Intersection( Scanner sc, LinkedList <Intersection> inters ) {
name = sc.next();
if (RoadNetwork.findIntersection( name ) != null) {
Errors.warn( "Intersection '" + name + "' redefined.");
}
sc.nextLine();
}
/** output this Intersection in a format like that used for input
*/
public String toString() {
return(
"intersection " +
name
);
}
/*
* New Method to Add Outgoing road
*/
public void addOutgoing(Road r){
outgoing.add(r) ;
}
}
Road.java :-
----------------------
import java.util.LinkedList;
import java.util.Scanner;
class Road {
private final float travelTime; // how long to travel down road
private final Intersection destination; // where road goes, or null
private final Intersection source; // source of road, or null
// Road name is the source-destination names
/** initializer scans and processes one road definition
*/
public Road( Scanner sc, LinkedList <Intersection> inters ) {
String srcName = sc.next();
String dstName = sc.next();
source = RoadNetwork.findIntersection( srcName );
if (source == null) {
Errors.warn(
"Road '" + srcName +
"' '" + dstName +
"' source undefined."
);
}
/**
* Add road to source.outgoing list
*
*/
if(source != null){
source.addOutgoing(this);
}
destination = RoadNetwork.findIntersection( srcName );
if (destination == null) {
Errors.warn(
"Road '" + srcName +
"' '" + dstName +
"' destination undefined."
);
}
// Bug: What if there is no next float?
travelTime = sc.nextFloat();
sc.nextLine();
}
/** output this road in a format like that used for input
*/
public String toString() {
String srcName;
String dstName;
if (source == null) {
srcName = "???";
} else {
srcName = source.name;
}
if (destination == null) {
dstName = "???";
} else {
dstName = destination.name;
}
return(
"road " +
srcName + " " +
dstName + " " +
travelTime
);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.