Hi, basically ive been trying to write code to work out where the nearest station for a person is based on their current location.
I did have it working to display nearest station name and distance from that station but something went wrong I must have accidently deleted something or done something to it.

Anyways I would appreciate it if someone could help me out and tell me where I've gone wrong

When trying to build the code it says no main classes exist

The code is

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculatedistance;
import javax.swing.*;


/**
 *
 * @author Jade
 */
public class CalculateDistance {
    private static int i;
   

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args, String bearingToClosest) {
       
        double tempBearing;
        double tempLength;
        double distanceToClosest;
               
        // array of stations
        Station[]stations = new Station[16];
        
        //populate array list
        stations[0] = new Station("Trim Bridge", new Location(65.0,2600.0));
        stations[1] = new Station("Windmill Hill", new Location(65.0,4800.0));
        stations[2] = new Station("Pirac Cresent", new Location(82.0,3400.0));
        stations[3] = new Station("Easton", new Location(84.0,4800.0));
        stations[4] = new Station("Parkway", new Location(105.0,3400.0));
        stations[5] = new Station("Temple Fields", new Location(149.0,3000.0));
        stations[6] = new Station("St Judes Hill", new Location(344.0,1800.0));
        stations[7] = new Station("Clifton Street", new Location(287.0,4400.0));
        stations[8] = new Station("Tivoli", new Location(277.0,2200.0));
        stations[9] = new Station("Jacobs Well", new Location(251.0,1200.0));
        stations[10] = new Station("Central", new Location(240.0,2000.0));
        stations[11] = new Station("Newbridge", new Location(244.0,4400.0));
        stations[12] = new Station("Weston-On-Shore", new Location(235.0,5800.0));
        stations[13] = new Station("Shakespeare Court", new Location(183.0,2000.0));
        stations[14] = new Station("St Dennis", new Location(180.0,400.0));
        stations[15] = new Station("Maxbridge", new Location(180.0,800.0));
        
        //get current location
        String temp = JOptionPane.showInputDialog("Bearing to current location");
        tempBearing = Double.parseDouble(temp);
        
        temp= JOptionPane.showInputDialog("Distance from origin of current location");
        tempLength = Double.parseDouble(temp);
        
        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
        
        

      
        JOptionPane.showMessageDialog(null, "the closest station is "+closest.name + "\nIt is " +distanceToClosest + " m away  on a bearing of");
       
    }
}
class Station{
    Location location;
    String name;
    
    public Station (String n, Location l){
        name = n;
        location = l;
    }
}//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); // calculate angle between bearings
        double temp = (length*length)+(l.length*l.length)-(2*length*l.length*Math.cos(Math.toRadians(angle)));
        return Math.pow(temp, 0.5);
    }
          
   
}//location

Recommended Answers

All 3 Replies

Hi, basically ive been trying to write code to work out where the nearest station for a person is based on their current location.
I did have it working to display nearest station name and distance from that station but something went wrong I must have accidently deleted something or done something to it.

Anyways I would appreciate it if someone could help me out and tell me where I've gone wrong

When trying to build the code it says no main classes exist

The code is

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculatedistance;
import javax.swing.*;


/**
 *
 * @author Jade
 */
public class CalculateDistance {
    private static int i;
   

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args, String bearingToClosest) {
       
        double tempBearing;
        double tempLength;
        double distanceToClosest;
               
        // array of stations
        Station[]stations = new Station[16];
        
        //populate array list
        stations[0] = new Station("Trim Bridge", new Location(65.0,2600.0));
        stations[1] = new Station("Windmill Hill", new Location(65.0,4800.0));
        stations[2] = new Station("Pirac Cresent", new Location(82.0,3400.0));
        stations[3] = new Station("Easton", new Location(84.0,4800.0));
        stations[4] = new Station("Parkway", new Location(105.0,3400.0));
        stations[5] = new Station("Temple Fields", new Location(149.0,3000.0));
        stations[6] = new Station("St Judes Hill", new Location(344.0,1800.0));
        stations[7] = new Station("Clifton Street", new Location(287.0,4400.0));
        stations[8] = new Station("Tivoli", new Location(277.0,2200.0));
        stations[9] = new Station("Jacobs Well", new Location(251.0,1200.0));
        stations[10] = new Station("Central", new Location(240.0,2000.0));
        stations[11] = new Station("Newbridge", new Location(244.0,4400.0));
        stations[12] = new Station("Weston-On-Shore", new Location(235.0,5800.0));
        stations[13] = new Station("Shakespeare Court", new Location(183.0,2000.0));
        stations[14] = new Station("St Dennis", new Location(180.0,400.0));
        stations[15] = new Station("Maxbridge", new Location(180.0,800.0));
        
        //get current location
        String temp = JOptionPane.showInputDialog("Bearing to current location");
        tempBearing = Double.parseDouble(temp);
        
        temp= JOptionPane.showInputDialog("Distance from origin of current location");
        tempLength = Double.parseDouble(temp);
        
        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
        
        

      
        JOptionPane.showMessageDialog(null, "the closest station is "+closest.name + "\nIt is " +distanceToClosest + " m away  on a bearing of");
       
    }
}
class Station{
    Location location;
    String name;
    
    public Station (String n, Location l){
        name = n;
        location = l;
    }
}//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); // calculate angle between bearings
        double temp = (length*length)+(l.length*l.length)-(2*length*l.length*Math.cos(Math.toRadians(angle)));
        return Math.pow(temp, 0.5);
    }
          
   
}//location

That is because main has a set structure:

public static void main(String[] args) {}

and yours looks like this:

public static void main(String[] args, String bearingToClosest) {}

thanks for the help, its actually working again now

I have another problem i'm not sure how to go about calculating the distance between the bearing the person currently is (as input by them) and the bearing of the nearest station and displaying this any help?

thanks for the help, its actually working again now

I have another problem i'm not sure how to go about calculating the distance between the bearing the person currently is (as input by them) and the bearing of the nearest station and displaying this any help?

Try this:http://www.movable-type.co.uk/scripts/latlong.html or this http://en.wikipedia.org/wiki/Haversine_formula it gives you formula's etc to calculate the distance bewtween two bearings, if this isnt of help just do a Google on 'calculate distance between two bearings' or ‘haversine’ formula'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.