Greetings from the land of perpetual Sun and Sand, did I mention the sand. I am new to Java and having difficulty with this assignment for a class. We are asked to build a program that uses locations to figure how far away an object is from another and figure which one is closer. I am enclosing the code I have so far, including the Junit tester



public class Location { // add instance variables private double latitude; private double longitude; private double distance; private String tag; /** * Default constructor for objects of class Location. */ public Location() { // your code here tag = null; latitude = 0.0; longitude = 0.0; } /** * Second constructor for objects of class Location. */ public Location(double inLatitude, double inLongitude, String inTag) { // your code here this.latitude = inLatitude; this.longitude = inLongitude; this.tag = inTag; // do not think think this is correct } /** * sets the location latitude. * * @param inLatitude Location latitude value. */ public void setLatitude(double inLatitude) { // your code here this.latitude = inLatitude; } /** * gets the location latitude. * * @return double The location latitude. */ public double getLatitude() { //if (this.tag == null) //the hint sheet had a return value of 0.0 but that doesn't make sense { //return 0.0; } //else { return this.latitude; } } /** * sets the location longitude. * * @param inLongitude Location longitude value. */ public void setLongitude(double inLongitude) { // your code here this.longitude = inLongitude; } /** * gets the location longitude. * * @return double The location longitude. */ public double getLongitude() { //if (longDiff == this.longitude) // same thing return value of 0.0 doesn't make sense { // return 0.0; } //else { return this.longitude; } } /** * sets the location tag string. * * @param inTag Location tag string. */ public void setTag(String inTag) { // not sure if this is right to store the value from location to a string this.tag = inTag; } /** * gets the location tag string. * * @return String The location tag string. */ public String getTag() { // I am not sure if this is correct fro what I need return this.tag; /** * calculates the relative distance to a location. * * @param newLocation The location to determine distance to. * * @return double The relative distance to the new location. */ public double distance(Location newLocation) { double latDiff = (latitude - newLocation.latitude); double longDiff = (longitude - newLocation.longitude); if (latDiff == 0.0 || longDiff == 0.0) { return 0.0; } else { distance = Math.sqrt((latDiff * latDiff) + (longDiff * longDiff)); return this.distance; } } /** * determines which of two locations is closer. * If the two objects passed as parameters are the same distance * from the location, a value of 0 is returned. If the first object * passed is closer, a value of 1 is returned. If the second object * is closer, a value of 2 is returned. * * @param location1 The location of the first object. * @param location2 The location of the second object. * * @return int 0 if equal distance, 1 or 2 otherwise as per description. */ public int closer(Location location1, Location location2) { // replace this line with your code double distance1 = this.distance(location1); double distance2 = this.distance(location2); if (Math.abs(distance1 - distance2) <= 1E-14) { return 0; } else if (distance1 < distance2) { return 1; } else //distance2 < distance1 { return 2; } } /** * provides String containing location information. * * @return String shows latitude, longitude, and tag value. */ public String toString() { // replace this line with your code return ("The Latitude is " + this.getLatitude() + ", " + "the longitude is " + this.getLongitude()); } }
In the Junit tester the problem occurs when trying to check the distance method

public class LocationTest extends junit.framework.TestCase { /** * Default constructor for test class LocationTest. */ public LocationTest() { // default constructor } /** * Sets up the test fixture. * * Called before every test case method. */ protected void setUp() { // anything needed prior to each test case } /** * Tears down the test fixture. * * Called after every test case method. */ protected void tearDown() { // anything needed after test cases complete } /** * Test the default constructor. */ public void testConstructor() { Location location = new Location(); assertNotNull(location); } /** * Test the second constructor. */ public void testConstructor2() { Location location1 = new Location(-33.86, 151.21, "Sydney"); assertNotNull(location1); } /** * Test the setLatitude method. */ public void testSetLatitude() { Location home = new Location(); home.setLatitude(31.10); assertEquals(31.10, home.getLatitude(), 0.01); } /** * Test the getLatitude method. */ public void testGetLatitude() { Location home = new Location(); home.setLatitude(15.50); assertEquals(15.50, home.getLatitude(), 0.01); } /** * Test the SetLongitude method. */ public void testSetLongitude() { Location home = new Location(); home.setLongitude(115.50); assertEquals(115.50, home.getLongitude(), 0.01); } /** * Test the GetLongitude method. */ public void testGetLongitude() { Location home = new Location(); home.setLongitude(1.25); assertEquals(1.25, home.getLongitude(), 0.01); } /** * Test the SetTag method. */ public void testSetTag() { Location first = new Location(131.00, -26.00, "Livingston"); first.setTag("Seattle"); assertEquals("Seattle", first.getTag()); } /** * Test the GetTag method. */ public void testGetTag() { Location first = new Location(31.00, 26.00, "Woodville"); first.setTag("New York"); assertEquals("New York", first.getTag()); } /** * Test the Distance method. */ public void testDistance() { Location location1 = new Location(-33.86, 151.21, "Sydney"); Location location2 = new Location(40.42, -3.69, "Madrid"); assertEquals(171.83, this.distance); //does not recognise the variable or method distance } /** * Test the Closer method. */ public void testCloser() { Location location1 = new Location(-33.86, 151.21, "Sydney"); Location location2 = new Location(40.42, -3.69, "Madrid"); Location location3 = new Location(51.51, -0.13, "London"); Location location4 = new Location(41.42, -3.69, "Madrid2"); Location location5 = new Location(39.42, -3.69, "Madrid3"); Location location6 = new Location(40.42, -2.69, "Madrid4"); Location location7 = new Location(40.42, -4.69, "Madrid5"); // test same distance (using same location) assertEquals(0, location1.closer(location2, location2)); } /** * Test the ToString method. */ public void testToString() { Location location1 = new Location(-33.86, 151.21, "Sydney"); assertTrue(location1.toString().contains("151.21")); } }
I would really appreciate any help with this. I am about ten hours ahead here in Iraq, so if I don't respond immediately I apologize.