package javaapplication10; /** * @param args the command line arguments */ import javax.swing.*; /** * * @author mpjames */ public class Stations { public static void main(String[] args) { // declairing variables double tempBearing; double tempLength; double distanceToClosest; double distanceToClosestDest; double tempBearingDest; double tempLengthDest; //an array of stations Station[] stations = new Station[16]; //populate the array with stations and values stations[0] = new Station("windmillHill",new Location(60.0,12.0), new Line ("Docks Line")); stations[1] = new Station("trim bridge",new Location(63.0,6.0), new Line ("Docks Line")); stations[2] = new Station("piracCresent",new Location(80.0,8.0), new Line ("Gyratory Line")); stations[3] = new Station("easton",new Location(85.0,12.0), new Line ("Brunel Line")); stations[4] = new Station("parkway",new Location(102.0,9.0), new Line ("Brunel Line")); stations[5] = new Station("templeFields",new Location(140.0,7.0), new Line ("Gyratory Line")); stations[6] = new Station("stDennis",new Location(180.0,1.0), new Line ("Docks Line")); stations[7] = new Station("moxbridge",new Location(180.0,2.0), new Line ("Brunel Line")); stations[8] = new Station("shakespeareCourt",new Location(192.0,5.0), new Line ("Gyratory Line")); stations[9] = new Station("weston-on-shore",new Location(232.0,14.0), new Line ("Docks Line")); stations[10] = new Station("jacobsWell",new Location(240.0,3.0), new Line ("Docks Line")); stations[11] = new Station("central",new Location(243.0,5.0), new Line ("Docks Line")); stations[12] = new Station("newbridge",new Location(245.0,11.0), new Line ("Docks Line")); stations[13] = new Station("tivoli",new Location(275.0,6.0), new Line ("Brunel Line")); stations[14] = new Station("cliftonStreet",new Location(285.0,11.0), new Line ("Brunel Line")); stations[15] = new Station("stJudesHill",new Location(355.0,4.0), new Line ("Gyratory Line")); //To get users current bearings and and locations and creating objects //String temp = JOptionPane.showInputDialog("Bearing to current location?"); tempBearing = Double.parseDouble(JOptionPane.showInputDialog("Bearing to current location?")); //temp = JOptionPane.showInputDialog("distance from origin of current?"); tempLength = Double.parseDouble(JOptionPane.showInputDialog("distance from origin of current?")); //String temp = JOptionPane.showInputDialog("Bearing to current location?"); tempBearingDest = Double.parseDouble(JOptionPane.showInputDialog("Bearing to Destination?")); //temp = JOptionPane.showInputDialog("distance from origin of current?"); tempLengthDest = Double.parseDouble(JOptionPane.showInputDialog("distance to end Destination?")); Location current = new Location(tempBearing, tempLength); Station closest = stations[0]; distanceToClosest = current.calcDistance(stations[0].location); for(int i = 1; i < stations.length; i++){ double tempDist = current.calcDistance(stations[i].location); if(tempDist < distanceToClosest){ closest = stations[i]; distanceToClosest = tempDist; }//if (tempDist < distanceToClosest) }//for loop //2nd loop for end destination Location currentDest = new Location(tempBearingDest, tempLengthDest); Station closestDest = stations[0]; distanceToClosestDest = currentDest.calcDistance(stations[0].location); for(int i = 1; i < stations.length; i++){ double tempDist = currentDest.calcDistance(stations[i].location); if(tempDist < distanceToClosestDest){ closestDest = stations[i]; distanceToClosestDest = tempDist; }//if (tempDist < distanceToClosest) }//for loop JOptionPane.showMessageDialog(null, "the closest station is " + closest.name + closest.line.lname + "\nIt is " + distanceToClosest + " away."); JOptionPane.showMessageDialog(null, "the closest station to your destination is " + closestDest.name + "\nIt is " + distanceToClosestDest + " away."); } } class Station{ // a station class Location location; String name; Line line; public Station(String n, Location l, Line r){ name = n; location = l; line = r; } class Line { String lname; public Line (String String) { lname = String; } }//class station class Location{ double bearing; double length; public Location(){ bearing = 0.0; length = 0.0; } public Location(double b, double l){ bearing = b; length = l; } public double calcDistance(Location l){ double angle = Math.abs(bearing - l.bearing);//calculating the angle between the bearings double temp = (length*length)+(l.length*l.length)-(2*length*l.length*Math.cos(Math.toRadians(angle))); return Math.pow(temp, 0.5); } } }