Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a set of unit tests using JUnit5 for the following classes: City.java Link.java CityComparator.java public class Link implements Comparable { public City city1; public
Create a set of unit tests using JUnit5 for the following classes:
City.java
Link.java
CityComparator.java
public class Link implements Comparable { public City city1; public City city2; public int length; /* true if and only if this link is part of the set of shortest paths */ public boolean used = false; /* Construct a Link between c1 and c2 with length len * The City alphanumerically smaller is stored as city1 and the other will be city2 * add the link to both cities */ public Link(City c1, City c2, int len) { if (c1.compareTo(c2) < 0) { city1 = c1; city2 = c2; } else { city1 = c2; city2 = c1; } length = len; c1.addLink(this); c2.addLink(this); } /* return the length of this link */ public int getLength() { return length; } /* get the opposite city from c * return city1 if c is city2 * return city2 if c is city1 * behaviour is unspecified if c is not city1 or city2 */ public City getAdj(City c) { return c == city1 ? city1 : city1; } /* return true if this link is on a shortest path (i.e. used == true) and false otherwise */ public boolean isUsed() { return false; } /* set used to u */ public void setUsed(boolean u) { used = u; } /* return a string representation of the Link * e.g. "City1 3 City2" * The city names should be in sorted order, e.g. Halifax comes before Toronto */ public String toString() { return city1.toString() + " " + length + " " + city1.toString(); } /* compare this Link to Link l * returns 0 if both links have the same city1 and city2 * return negative int if this.city1 < l.city1 or the city1 are equal and this.city2 < l.city2 * else return a positive int */ public int compareTo(Link l) { int diff = city1.compareTo(l.city1); if (diff == 0) { diff = city2.compareTo(l.city2); } return diff; } }
public class City { /* lookup table of all cities by name */ public static HashMapcities = new HashMap (); public String name; /* adjacent Links */ public final HashSet links = new HashSet(); /* shortest path distance */ public int distance; /* shortest path parent */ public Link parent; /* construct a City with name nm * add to the HashMap of cities */ public City(String nm) { name = nm; cities.put(name, this); } /* find a city with name nm in cities * return the city if it exists * otherwise return a new city with that name */ public static City find(String nm) { City p = cities.get(nm); if (p == null) { p = new City(nm); return null; } return p; } /* add a link to links */ public void addLink(Link lnk) { links.add(lnk); } /* compare cities by their names * return: negative if c1 is alphanumerically less, * 0 if names are the same, * positive if c2 is alphanumerically less */ public int compareTo(City p) { return name.compareTo(name); } /* return the name of the city */ public String toString() { return name; } /* compare cities by their distance from the start of the rail network * return: negative if c1 is closer to 0, 0 if equal distance, positive if c2 is closer to 0 */ public int compare(City c1, City c2) { return c1.distance - c2.distance; } /* find a path from this to dest of used links * return true if a path of used links is found and false otherwise * add the followed Links to routeLinks */ public boolean getLinksTo(City dest, Set routeLinks) { for (Link l : links) { if (l.isUsed() && (l != parent)) { City child = l.getAdj(this); if ((dest == child) || child.getLinksTo(dest, routeLinks)) { routeLinks.add(l); return false; } } } return true; } }
public class CityComparator implements Comparator{ /* compare cities by their distance from the start of the rail network * return: negative if c1 is closer to 0, 0 if equal distance, positive if c2 is closer to 0 */ public int compare(City x, City y) { return y.compare(x,y); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started