Help with Java program

Have to build a program to check location and distance from 1 object t

Page 1 of 1

3 Replies - 933 Views - Last Post: 09 November 2007 - 04:53 PM Rate Topic: -----

#1 saxontrooper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 07

Help with Java program

Post icon  Posted 09 November 2007 - 09:53 AM

Evening all,
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 :) . I am having trouble taking the input and storing it as a string (double latitude, double longitude, "and location name") :blink: . The program compiles as is, and passes all the junit tests but one (distance method). That one I know has something to do with the output from the method not being called correctly in the test, or initialized in the program correctly, but can't figure out which :angry: .

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 :crazy: . All the other tests pass except that one, and I can not get the program to compile.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Java program

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Help with Java program

Posted 09 November 2007 - 10:14 AM

You distance function should not be returning a 0. Well, unless they're completely equal. I'd write is something like so:
	public double getDistanceFrom(Location otherLocation) {
		double latDiff = (this.latitude - otherLocation.latitude);
		double longDiff = (this.longitude - otherLocation.longitude);
		if (latDiff == 0.0) {
			return Math.abs(longDiff);
		} else if (longDiff == 0.0) {
			return Math.abs(latDiff);
		} else {
			return Math.sqrt((latDiff * latDiff) + (longDiff * longDiff));
		}
	}

Make sense? If lat or lon is zero, that just means they're on the same line. Rather than using a distance formula, you just return difference of two points. If both are zero, a zero is returned, they are the same location!

Of course, the only reason you'd do a zero check is too skip the relatively minor sqrt calculation. This should work just fine, too:
	public double getDistanceFrom(Location otherLocation) {
		double latDiff = (this.latitude - otherLocation.latitude);
		double longDiff = (this.longitude - otherLocation.longitude);
		return Math.sqrt((latDiff * latDiff) + (longDiff * longDiff));
	}


Hope this helps.

This post has been edited by baavgai: 09 November 2007 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

#3 saxontrooper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 07

Re: Help with Java program

Posted 09 November 2007 - 11:47 AM

I really appreciate the help, that did clear up some of the confusion on the distance method, but I still can not get the tester to work, it gives me errors when i use this.distance, location.distance, etc... This is getting really frustrating :crazy: . And of course the text is written by someone who has been programming for years and can't spell beginner, let alone distance education. :D . Any suggestions will be most helpful.

Thanks





View Postbaavgai, on 9 Nov, 2007 - 10:14 AM, said:

You distance function should not be returning a 0. Well, unless they're completely equal. I'd write is something like so:
	public double getDistanceFrom(Location otherLocation) {
		double latDiff = (this.latitude - otherLocation.latitude);
		double longDiff = (this.longitude - otherLocation.longitude);
		if (latDiff == 0.0) {
			return Math.abs(longDiff);
		} else if (longDiff == 0.0) {
			return Math.abs(latDiff);
		} else {
			return Math.sqrt((latDiff * latDiff) + (longDiff * longDiff));
		}
	}

Make sense? If lat or lon is zero, that just means they're on the same line. Rather than using a distance formula, you just return difference of two points. If both are zero, a zero is returned, they are the same location!

Of course, the only reason you'd do a zero check is too skip the relatively minor sqrt calculation. This should work just fine, too:
	public double getDistanceFrom(Location otherLocation) {
		double latDiff = (this.latitude - otherLocation.latitude);
		double longDiff = (this.longitude - otherLocation.longitude);
		return Math.sqrt((latDiff * latDiff) + (longDiff * longDiff));
	}


Hope this helps.

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Help with Java program

Posted 09 November 2007 - 04:53 PM

To be honest, the first time I ever used junit was testing your code. :P

Not that unit tests are bad, far from it. It's just before all the formalized packaged stuff, programmers just used to write their own test suites; no extras required.

Anyway, I made a NetBeans project from your code. And put the LocationTest class in the Test Packages of the project. I also had to be clear with it what package I'd put Location under. And I had to set output level to high/debug to get any form of useful feedback.

The result. Even though I called the method "getDistanceFrom" for clarity, it has to be the original "distance", because the test expects it.

When I got testing to run, it failed with:
Testcase: testDistance(loctest.LocationTest):	FAILED
expected:<171.83> but was:<171.78919756492257>


Obviously, they want some rounding? Still, even rounded, it's interesting that it's so far off, the distance formula is pretty straight forward.

Note, the test for this had to be fixed, looks like this on mine:
public void testDistance() {
   Location location1 = new Location(-33.86, 151.21, "Sydney");
   Location location2 = new Location(40.42, -3.69, "Madrid");
   assertEquals(171.83, location1.distance(location2));
}


Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1