初学者帮助超载顾问和方法

时间:2016-11-03 21:06:54

标签: java

我正在为学校编写代码,需要3个班级(主要,机场,坐标)。机场计算两个机场之间的距离。最初是2班(主要,机场)。其中双精度数由用户在Main类中输入,并通过Airport类中的distanceCalc方法传递。添加Coordinates类以保持Latitude和Longitude值,我们必须使用Airport类中的重载构造函数和方法通过distanceCalc方法传递值。

我遇到的问题是,当我调用distanceCalc方法接受类参数时,它使用两个机场的相同纬度和经度值,并返回0.0作为它们之间的距离。希望这是有道理的,有人可以帮助我找出原因。以下是代码的各个部分。

public class MainClass {
    public static void main(String[] args) {

    // Variable Declaration
    Airport Airport1, Airport2;
    Scanner objKB = new Scanner(System.in);
    DecimalFormat objFmt = new DecimalFormat("#,###,##0.0");
    double Lat1, Long1, Lat2, Long2, nautMiles, statueMiles;
    String depAirport, arrAirport;
    Coordinates coord = new Coordinates();

    // User Inputs
    System.out.println("Enter three digit airport ID of departing airport: ");
    depAirport = objKB.nextLine();
    System.out.println("Enter three digit airport ID of arriving airport: ");
    arrAirport = objKB.nextLine();
    System.out.println("Enter latitude of departing airport: ");
    Lat1 = objKB.nextDouble();
    System.out.println("Enter longitude of departing airport: ");
    Long1 = objKB.nextDouble();
    System.out.println("Enter latitude of arriving airport: ");
    Lat2 = objKB.nextDouble();
    System.out.println("Enter longitude of arriving airport: ");
    Long2 = objKB.nextDouble();

    Airport1 = new Airport(Lat1, Long1);
    Airport2 = new Airport(Lat2, Long2);

    //Calculations from Airport class
    nautMiles = Airport.getDistance(Lat1, Long1, Lat2, Long2); //original method that calculates properly
    nautMiles = Airport1.getDistance(Airport2); //new method that will calculate 0.0

机场等级:

public class Airport {
    public static double getDistance(double Lat1, double Long1, double Lat2, double Long2) {

    // Local Variable Declaration
    double distance;
    double greatCircle;
    double radius = 10800 / Math.PI; 

    //Calculations
    Lat1 = Math.toRadians(Lat1);
    Long1 = Math.toRadians(Long1);
    Lat2 = Math.toRadians(Lat2);
    Long2 = Math.toRadians(Long2);
    greatCircle = Math.acos(Math.sin(Lat1) * Math.sin(Lat2) + Math.cos(Lat1) * Math.cos(Lat2) 
            * Math.cos(Long1 - Long2));
    distance = radius * greatCircle;

    //Return
    return distance;
}
// Method overloads

public double getDistance(Airport a){
    double lat1, lat2, long1, long2;
    lat1 = coord.getLat();
    long1 = coord.getLong();
    lat2 = a.getLat();
    long2 = a.getLong();
    return getDistance(lat1, long1, lat2, long2);
    }

public double getDistance(Coordinates c){
    double lat1, long1, lat2, long2;
    lat1 = coord.getLat();
    long1 = coord.getLong();
    lat2 = c.getLat();
    long2 = c.getLong();
    return getDistance(lat1, long1, lat2, long2);
    }

public double getDistance(double lat, double lng){
    double lat2, long2;
    lat2 = coord.getLat();
    long2 = coord.getLong();
    return getDistance(lat, lng, lat2, long2);
    }

    //Fields
    private Coordinates coord;

    //Constructors
    public Airport() {
        coord = new Coordinates();          
    }

    public Airport(double Latitude, double Longitude){
        this();
        coord.setLat(Latitude);
        coord.setLong(Longitude);   
    }

    public Airport(Coordinates values){
        this(values.getLat(), values.getLong());    
    }

    //Mutators and Accessors
    public double getLat() {return coord.getLat();}
    public void setLat(double lat) {
        coord.setLat(lat);
    }

    public double getLong() {return coord.getLong();}
    public void setLong(double lng) {
        coord.setLong(lng);
    }

协调班级

public class Coordinates {

//Fields
private static double Latitude, Longitude;

//Mutators and Accessors
public double getLat() {return Latitude;}
public void setLat(double lat) {
    Latitude = lat;
}

public double getLong() {return Longitude;}
public void setLong(double lng) {
    Longitude = lng;
}

}

1 个答案:

答案 0 :(得分:1)

您的问题出在此处:private static double Latitude, Longitude; 通过制作此static,意味着每个坐标实例具有相同的纬度和经度。 使某些东西静态意味着类的所有实例将共享相同的数据。