9 Replies - 6652 Views - Last Post: 28 February 2011 - 10:49 PM Rate Topic: -----

#1 umrk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-February 11

distance between two points in java

Posted 28 February 2011 - 10:01 PM


package unit5;


import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;

public class Distance
{       //instantiates variables
	private int x1,y1,x2,y2;

	public Distance()
        // default coordinates
	{
            setCoordinates(0,0,0,0);
            


	}

	public Distance(int xOne, int yOne, int xTwo, int yTwo)
	//.
        {
           setCoordinates(x1,y1,x2,y2);
           

	}

	public void setCoordinates(int xOne, int yOne, int xTwo, int yTwo)
	//sets the 
        {
            x1=xOne;
            y1=yOne;
            x2=xTwo;
            y2=yTwo;


	}

	public String determineClosest( )
	{
		double distanceA = 0.0, distanceB = 0.0;
		String result="";






		return result;
	}
	
	public String toString()
	{
		return "";
	}
}


The directions are to find which coordinates are closer to the point of origin(0,0).
I am confused on how to write an If statement that would accomplish the  task. As this is my homework, please do not just give me the answer. Try to address where my problem is and suggest a possible solution.

here is the runner:

package unit5;
//� A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Class -
//Lab  -

import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;

public class Lab05e
{
	public static void main( String[] args )
	{
		Scanner keyboard = new Scanner( System.in );

		out.print("Enter X1 :: ");
		int xOne = keyboard.nextInt();
		out.print("Enter Y1 :: ");
		int yOne = keyboard.nextInt();
		out.print("Enter X2 :: ");
		int xTwo = keyboard.nextInt();
		out.print("Enter Y2 :: ");
		int yTwo = keyboard.nextInt();

		Distance test = new Distance();
		test.setCoordinates(xOne, yOne, xTwo, yTwo);
		out.println(test.determineClosest());
		
		//add more test cases
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: distance between two points in java

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: distance between two points in java

Posted 28 February 2011 - 10:07 PM

http://en.wikipedia....agorean_theorem


   x1,y1
    | \
    |  \
  A |   \ C
    |    \
    |---- x2,y2
       B


This post has been edited by pbl: 28 February 2011 - 10:08 PM

Was This Post Helpful? 1
  • +
  • -

#3 umrk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-February 11

Re: distance between two points in java

Posted 28 February 2011 - 10:09 PM

hmm. let me try to work it out. remember, i am slow at Java aha
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: distance between two points in java

Posted 28 February 2011 - 10:11 PM

View Postumrk, on 01 March 2011 - 12:09 AM, said:

hmm. let me try to work it out. remember, i am slow at Java aha

This is only math

A = y2 - y1
B = x2 - x1

C = Math.sqrt(A^2 + b^2)
Was This Post Helpful? 0
  • +
  • -

#5 umrk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-February 11

Re: distance between two points in java

Posted 28 February 2011 - 10:23 PM

would this also work?
public String determineClosest( )
	{
		double distanceA = 0.0, distanceB = 0.0;
		distanceA = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); 
                
                
                String result="";






		return result;
	}


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: distance between two points in java

Posted 28 February 2011 - 10:25 PM

String result="" + distanceA;
Was This Post Helpful? 0
  • +
  • -

#7 umrk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-February 11

Re: distance between two points in java

Posted 28 February 2011 - 10:29 PM

that would only print the distance of the points, for my assignment, i need to write an If statement that would basically state,
if {distanceA to (0,0)> than distanceB to (0,0)}
{
String result = B is closer to (0,0)
}



that is what i am trying to do. sorry if that's confusing.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: distance between two points in java

Posted 28 February 2011 - 10:35 PM

Even easier with one point being 0,0:

distanceA = Math.sqrt((x2 * x2) + (y2 * y2));
distanceB = Math.sqrt((x1 * x1) + (y1 * y1));

now you can set your String result accordingly
Was This Post Helpful? 0
  • +
  • -

#9 umrk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-February 11

Re: distance between two points in java

Posted 28 February 2011 - 10:40 PM

public String determineClosest( )
	{
		double distanceA = 0.0, distanceB = 0.0;
		distanceA = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
                
                distanceB = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
                
                if(distanceA>distanceB)
                {
                    String result=(distanceA + " is closer to (0,0)");
                    
                }
               
                
                if(distanceA<distanceB)
                {
                    String result=(distanceA + " is closer to (0,0)");
                }




something like this?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: distance between two points in java

Posted 28 February 2011 - 10:49 PM

Yes and no :)
Now you declare String inside a {} it can't be seen oustide it
You use twice distanceA in result
You will have to declare it outside
Also as you are playing with int they may be at the same distance

public String determineClosest( )
	{
		double distanceA = 0.0, distanceB = 0.0;
		distanceA = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
                
                distanceB = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
                String result = "";       
                if(distanceA>distanceB)
                {
                    result= distanceA + " is closer to (0,0)";
                    
                }
                else if(distanceA<distanceB)
                {
                    result= distanceB + " is closer to (0,0)";
                }
                else
                {
                    result = "They are a the same distance";
                }
           return result;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1