Question: I need help with this Java program, Class specifications: Table 1: City Class Class City implements Comparable Private Instance Variables Description String city City name
I need help with this Java program, Class specifications:
Table 1: City Class
| Class City implements Comparable | |
| Private Instance Variables | Description |
| String city | City name |
| double gpsX | GPS Longitude in Decimal Degrees |
| double gpsY | GPS Latitude in Decimal Degrees |
| int population | Population |
| int vertIndex | Index in Vertex ArrayList |
| Public Instance Methods | Description |
| City (String name, double x, double y, int size, int index) | Constructor to initialize each instance variable. Do not round these numbers, here do the rounding in printCity(). |
| String getCity() | Getter for city |
| double getLongitude() | Getter for gpsX |
| double getLatitude() | Getter for gpsY |
| int getPopulation() | Getter for population |
| int getIndex() | Getter for vertIndex |
| void setIndex (int index) | Setter for vertIndex |
| int compareTo (City c) | Compares population |
| boolean equals (Object c) | Override equals comparing city name |
| String toString() | Return City name |
| String printCity() | Return City object information with the GPS coordinates rounded to 2 decimal places, as Mars Hill: [1]:[-82.55, 35.83]:(1869) |
Table 2: Road Class
| Class Road extends WeightedEdge implements Comparable< WeightedEdge> | |
| Private Instance Vars | Description |
| City startCity | City at (x1, y1) |
| City endCity | City at (x2, y2) |
| double distance | Miles from startCity to endCity as crow flies |
| String direction | Map direction: N, S, E, W, etc |
| Public Instance Methods | Description |
| Road (City c1, City c2) | Constructor to initialize each instance variable: |
| double getDistance() | Getter for distance |
| String getDirection() | Getter for direction |
| int compareTo (Road r) | Compares distance between startCity and endCity; the weight |
| String toString() | Return City names at either end of Road |
| String printRoad() | Return Road object information with the distance rounded to 2 decimal places For example: Gastonia to Hickory traveling N for 34.84 miles |
| Private Instance Methods | Description |
| int findQuadrant (double x1, double y1, double x2, double y2) | Return the quadrant in which the direction of travel takes you from PointA (x1,y1) (startCity) to PointB:(x2,y2) (endCity): return 1, 2, 3, or 4 |
| double compAngle (double sideA, double sideB, double sideC) | Return the angle at PointA: (x1,y1) in degrees: where sideA is across from PointA: (x1,y1), side B is across from PointB: (x2,y2) and sideC is the path from PointA to PointB |
| double distance (double x1, double y1, double x2, double y2) | Return the distance from (x1,y1) to (x2,y2) DO NOT round the answer here |
| String compDirection (double angle, int quad) | Return the direction of travel using angle and quadrant |
| void computeDirection () | Called from the constructor; use all the other private instance methods and City instance variables to set the values of the the distance and direction instance variables for the Road object. You must start this function by changing the GPS coordinates to radians. At the end of the method change the distance to miles before storing it. (multiply by 3956). |
Table 3: City Information
| City | Longitude | Latitude | Population |
| Murphy | -84.029924 | 35.089848 | 1627 |
| Mars Hill | -82.547843 | 35.828496 | 1869 |
| Mooresville | -80.820139 | 35.584337 | 32711 |
| Morrisville | -78.828930 | 35.827493 | 18576 |
| Morehead City | -76.746748 | 34.727700 | 8661 |
| Manteo | -75.669385 | 35.904595 | 1434 |
Table 4: Road Information
|
| City1 | City2 |
| City1 | City2 |
| 0 | Murphy | Mars Hill | 3 | Morrisville | Mars Hill |
| 0 | Murphy | Mooresville | 3 | Morrisville | Mooresville |
| 1 | Mars Hill | Murphy | 3 | Morrisville | Morehead City |
| 1 | Mars Hill | Mooresville | 3 | Morrisville | Manteo |
| 1 | Mars Hill | Morrisville | 4 | Morehead City | Mooresville |
| 2 | Mooresville | Murphy | 4 | Morehead City | Morrisville |
| 2 | Mooresville | Mars Hill | 4 | Morehead City | Manteo |
| 2 | Mooresville | Morrisville | 5 | Manteo | Morrisville |
| 2 | Mooresville | Morehead City | 5 | Manteo | Morehead City |
Given Code:
City Class:
public class City implements Comparable
{
private String city; // City name
private double gpsX; // Longitide in degrees
private double gpsY; // Latitide in degrees
private int population; // Population size
private int vertIndex; // index in the Vertex ArrayList
/** Construct a City object and initialize the instance variables */
public City (String name, double x, double y, int size, int index)
{
// add code
}
/** Return the City name */
public String getCity()
{
// add code
}
/** Return the City longitude */
public double getLongitude()
{
// add code
}
/** Return the City latitude */
public double getLatitude()
{
// add code
}
/** Return the City poulation */
public int getPopulation()
{
// add code
}
/** Return the City index in the vertex ArrayList */
public int getIndex()
{
// add code
}
/** Set the City index of the vertex ArrayList */
public void setIndex (int index)
{
// add code
}
/** Compare the City poulations */
@Override
public int compareTo (City c)
{
// add code
}
/** Return true, when the City names are the same */
@Override
public boolean equals (Object c)
{
// add code
}
/**
* Return the City info as a String
* Example: Mars Hill: [1]:[82.55 W, 35.83 N]:(1869)
* Round the GPS coordinates to 2 decimal places for display
*/
public String printCity()
{
// add code as above
}
/** Return the City name as a String */
public String toString()
{
// add code
}
}
NCCitiesRoads Class:
public class NCCitiesRoads
{
/**
* For this program:
* 1. Create the City and Road ArrayLists
* 2. Add 6 City objects to the City Vertex ArrayList.
* 3. Add 18 Road objects to the Road Edge ArrayList.
* 4. Create a WeightedGraph passing in the
* City and Road ArrayLists.
* 5. Next, display each of the items requested below:
* (look at the Graph/WeightedGraph methods)
* a. The number of Cities in the map.
* b. The City object information for the 4th City
* added, using a graph method to retrieve the City.
* c. The City with the largest number of Roads ending there.
* d. The Road edge information for each Road using a
* graph method.
* e. The complete Road information for each Road from the
* Road ArrayList. Cast the WeightedEdge from the
* ArrayList to a Road object
* f. Provide an ArrayList with the Cities sorted
* and print it - (use Collections.sort)
*/
public static void main (String[] args)
{
// Create an ArrayList called cities of City objects
// Create an ArrayList called roads of WeightedEdge Road objects
// Create 6 Cities
City city0 = new City ("Murphy", -84.029924, 35.089848, 1627, 0);
City city1 = new City ("Mars Hill", -82.547843, 35.828496, 1869, 1);
// Do the rest yourself
// Add them to ArrayList
cities.add (city0);
cities.add (city1);
// Do the rest yourself
// Create 18 roads and add them to ArrayList
roads.add (new Road (city0, city1));
// Do the rest yourself
// Create Weighted Graph
// The number of cities in the map.
// The City object information for the 4th City added,
// using a graph method to retrieve the City.
// The City with the largest number of Roads ending there.
// This requires you to loop thru arraylist
// The Road edge information for each Road using a graph method.
// The complete Road information for each Road from the Road ArrayList.
// Cast the WeightedEdge from the ArrayList to a Road object
// This requires you to loop thru arraylist
// Provide an additional ArrayList with the Cities sorted and print it
// (use Collections.sort)
// This requires you to loop thru arraylist to create new arraylist
}
}
Road Class:
public class Road extends WeightedEdge implements Comparable
{
private City startCity; // The city at the start of the road
private City endCity; // The city at the end of the road
private double distance; // The distance in miles from startCity to endCity
private String direction; // The direction of travel on the road
// from startCity to endCity
/** Construct a Road object and initialize all the instance variables */
public Road (City c1, City c2)
{
// Pass the indices of the vertices from the
// Vertex List to the grandparent Edge
super (c1.getIndex(), c2.getIndex(), 0);
// Initialize the endpoint cities
// add code here
// Calling this method computes the direction of the Road
// object and the distance between the City vertices and
// sets the corresponding instance variables and the
// weight instance variable of the parent WeightedEdge
computeDirection();
}
/** Return the distance */
public double getDistance()
{
// add code here
}
/** Return the direction */
public String getDirection()
{
// add code here
}
/** Compare two Road edges by their weights */
public int compareTo (Road edge)
{
// add code here
}
/**
* Calling computeDirection computes the direction of the Road
* object and the distance between the City vertices and
* sets the corresponding instance variables:
* distance
* weight
* direction
*
* Note: Do NOT round any values (especially GPS) in this method
* we want them to have their max precision.
* Only do rounding when displaying values
*
*/
private void computeDirection()
{
// Convert the startCity (PointA) and endCity (PointB)
// GPS coordinates from degrees into radians
// Point A: (x1, y1): startCity
double x1 = Math.toRadians (startCity.getLongitude());
double y1 = Math.toRadians (startCity.getLatitude());
// Point B: (x2, y2): endCity
double x2 = Math.toRadians (endCity.getLongitude());
double y2 = Math.toRadians (endCity.getLatitude());
// Find quadrant
int quad = findQuadrant (x1, y1, x2, y2);
// Uses quadrant the determine coordinates of Point C: (x3, y3)
// These are for Quad 2 or Quad 4
double x3 = x1;
double y3 = y2;
if (quad == 1 || quad == 3)
{
// These are for Quad 1 or Quad 4
x3 = x2;
y3 = y1;
}
// Compute length of sides a, b, and c which are across
// from points (and angles) A, B and C, respectively
double sideA = distance (x2, y2, x3, y3);
double sideB = distance (x1, y1, x3, y3);
double sideC = distance (x1, y1, x2, y2);
// Compute the angle at pointA in degrees.
// The order of these three sides is important:
// sideA: across from angle A - the direction being calculated
// sideB: across from angle B
// sideC: across from angle C which is the right angle
// and the side that is the path from PointA (x1, y1)
// to PointB (x2, y2)
double angleA = compAngle (sideA, sideB, sideC);
// Set distance, weight and direction
// Note: Dont round anything here, we want max precision
// Convert the sideC length (distance from PtA to PtB) into miles
distance = sideC * 3956;
// The distance is also the weight
weight = distance;
// Use angleA and the quadrant to find
// the direction of travel from PointA to PointB
direction = compDirection (angleA, quad);
}
/**
* Assuming that PointA (x1, y1) is at the origin,
* Find the Quadrant containing PointB (x2, y2)
* This is accomplished by comparing both x1 to x2 and y1 to y2
*/
private int findQuadrant (double x1, double y1, double x2, double y2)
{
int quad = 0;
if (x1 < x2 && y1 <= y2)
{
quad = 1;
}
else if (x1 >= x2 && y1 < y2)
{
quad = 2;
}
else if (x1 > x2 && y1 >= y2)
{
quad = 3;
}
else if (x1 <= x2 && y1 > y2)
{
quad = 4;
}
return quad;
}
/**
* Compute the distance between PointA (x1,y1) and PointB (x2,y2)
* Return the distance rounded to two decimal places
*/
private double distance (double x1, double y1, double x2, double y2)
{
return Math.sqrt ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
/** Compute AngleA at PointA from the sides of the triangle */
private double compAngle (double sideA, double sideB, double sideC)
{
return Math.toDegrees (Math.acos(((sideB * sideB) +
(sideC * sideC) - (sideA * sideA)) /
(2 * sideB * sideC)));
}
/**
* Compute the direction of the line between PointA to PointB,
* Using the angle at PointA and the quadrant PointA is in
*/
private String compDirection (double angle, int quadrant)
{
String dirStr = "None";
switch (quadrant)
{
case 1:
{
if (angle <= 11.25)
{
dirStr = "E";
}
else if (angle <= 33.75)
{
dirStr = "ENE";
}
else if (angle <= 56.25)
{
dirStr = "NE";
}
else if (angle <= 78.75)
{
dirStr = "NNE";
}
else // angle 78.75 -> 90
{
dirStr = "N";
}
break;
}
case 2:
{
if (angle <= 11.25)
{
dirStr = "N";
}
else if (angle <= 33.75)
{
dirStr = "NNW";
}
else if (angle <= 56.25)
{
dirStr = "NW";
}
else if (angle <= 78.75)
{
dirStr = "WNW";
}
else // angle 78.75 -> 90
{
dirStr = "W";
}
break;
}
case 3:
{
if (angle <= 11.25)
{
dirStr = "W";
}
else if (angle <= 33.75)
{
dirStr = "WSW";
}
else if (angle <= 56.25)
{
dirStr = "SW";
}
else if (angle <= 78.75)
{
dirStr = "SSW";
}
else // angle 78.75 -> 90
{
dirStr = "S";
}
break;
}
case 4:
{
if (angle <= 11.25)
{
dirStr = "S";
}
else if (angle <= 33.75)
{
dirStr = "SSE";
}
else if (angle <= 56.25)
{
dirStr = "SE";
}
else if (angle <= 78.75)
{
dirStr = "ESE";
}
else // angle 78.75 -> 90
{
dirStr = "E";
}
break;
}
}
return dirStr;
}
/**
* Return the Road info as a String:
* Round distance to 2 places for display.
* "Barco to Elizabeth City traveling WSW for 17.41 miles"
*/
public String printRoad()
{
// code this as above
}
/**
* Return the Road info as a String containing City names
* Road: Charlotte to Greensboro
*/
public String toString()
{
// code this as above
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
