public class Formal2{
	String name;
	double longitude;
	double latitude;
        double distanceFrom;

public double getLongitude(){
	return longitude;

	}
	public void setLongitude(double longitude){
	this.longitude = longitude;
	}
	public double getLatitude(){
	return latitude;

	}
	public void setLatitude(double latitude){
	this.latitude = latitude;
	}
	public String getName(){
	return name;

	}
	public void setName(String name) {
	this.name = name;
	}
        public double getDistanceFrom(){
            return distanceFrom;
        }
        public void setDistanceFrom(double distanceFrom){
            this.distanceFrom=distanceFrom;
        }
        
        public String toString(){
            return ("City Name = " + name + "\nLatitude = " + latitude + "\nLongitude = " + longitude);
        }
}

import java.lang.Math;
        
public class GreatCircleDistance{
     
// CONSTANTS USED INTERNALLY
final double DEGREES_TO_RADIANS = ( Math.PI/180.0 );
// Mean radius in KM
final double EARTH_RADIUS = 6371.0;
/** Method to compute Great Circle distance between 
  * two points. Please note that this algorithm  
  * assumes the Earth to be a perfect sphere, whereas
  * in fact the equatorial radius is about 30Km 
  * greater than the Polar.
  *
  * @param alt other point to compute distance to
  * @return The distance in Kilometres
  */

public double GreatCircleDistance(lat, lon, alt) {

// There is no real reason to break this lot into 
// 4 statements but I just feel it's a little more 
// readable
  
    
  double p1 = Math.cos(lat)*Math.cos(lon) 
              *Math.cos(alt.lat)*Math.cos(alt.lon);
  double p2 = Math.cos(lat)*Math.sin(lon) 
              *Math.cos(alt.lat)*Math.sin(alt.lon);
  double p3 = Math.sin(lat)*Math.sin(alt.lat);

  return(Math.acos(p1+p2+p3)*EARTH_RADIUS);

}

}

public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) 
	{
	Formal2 l1 = new Formal2();
		double longitude;
		double latitude;
		String name;
		l1.name = "Toronto";		//set state variables
		l1.longitude = 2.2;
		l1.latitude = 1.6;
		longitude = l1.getLongitude();	//calculate longitude
		latitude = l1.getLatitude();	//calculate latitude
		name = l1.getName();

		System.out.println(l1);
                
		
    }
	
}

Recommended Answers

All 3 Replies

I am having trouble getting this to compile, the I wrote everything but the GreatCircleDistance method ( that one the teacher gave us)

purpose: the calculation of geographic distances between locations

Hi ladyjade555 and welcome to DaniWeb :)

Ok there are a couple of issues with our code that I can see with a quick read-through. Line 40 is

import java.lang.Math;

You don't need to explicitly import any classes or packages that start with java.lang - these are the classes that are automatically "imported" with the java virtual machine. They form the base classes that the other packages in java (including the ones that you write) build upon.

Lines 92-94 are probably the lines that are causing you grief when you attempt to compile. These lines are explicitly trying to set the variables in your Formal2 object, but you are doing it incorrectly. You can't access the variables directly from the Main class because the variables are not open to the "public". However you can call your "set" methods because they have been made public. For example, by changing line 92 to

l1.setName("Toronto");

you will set the name of your object to "Toronto" and this is legal. I'll let you work out how to set the longitude and latitude.

As an aside, it would probably be better to set the variables in a constructor method in the Formal2 class rather than the way that I have explained. However, that might be a little further down the track in your learning path, but I thought I'd mention it anyways.

Let me know how you go with my suggestions. :)

I also don't think that this correct:

public double GreatCircleDistance(lat, lon, alt) {

}

Shouldn't be :

public double GreatCircleDistance(double lat,double lon,double alt) {

}
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.