Question subject: Need Help with Java Source code.
Posted: Tue Oct 26, 2010 1:35 am
Joined: Tue Oct 26, 2010 1:32 am Posts: 2 Has thanked: 0 time Have thanks: 0 time
Okay so Im trying to make a method that fits this criteria. A public method named distanceTo that accepts a parameter of type Point and returns a double value representing the distance between the two points (i.e., between the Point object on which the method was called and the other Point object that was passed as a parameter to the method). Recall that to compute the distance between two points in 2D space you must: { Find the dierence between the two x-coordinates { Compute the square of that dierence { Find the dierence between the two y-coordinates { Compute the square of that dierence { Add those two squares together { Take the square root of the result
and.... so far I have this /** * Changes the distance between the two points */ public void distanceTo(double xPosition, double yPosition) {
}
Im at a lost and don't know where to start any help would be great.
Wizkid390
Question subject: Re: Need Help with Java Source code.
Posted: Tue Oct 26, 2010 11:16 pm
Joined: Tue Oct 26, 2010 1:32 am Posts: 2 Has thanked: 0 time Have thanks: 0 time
Bump....anyone that can help would be really appreciated.
apeter
Question subject: Re: Need Help with Java Source code.
Posted: Wed Nov 03, 2010 3:57 pm
Joined: Wed Nov 03, 2010 3:48 pm Posts: 4 Has thanked: 0 time Have thanks: 1 time
Hi Wizkid390,
one possible solution would be:
Code:
import java.lang.Math;
public class xPoint {
/** * Makes the class executable */ public static void main(String[] args) { xPoint p1 = new xPoint(5, 7); xPoint p2 = new xPoint(8, 4); System.out.println("Distance = " + p1.distanceTo(p2)); }
/** * Private member containing the x coordinate */ private double x;
/** * Private member containing the y coordinate */ private double y;
/** * Method to compute the distance to another xPoint instance * @param xPosition * @param yPosition */ public xPoint(double xPosition, double yPosition) { x = xPosition; y = yPosition; }
/** * Method to compute the distance to another xPoint instance. * @param other Instance to compute distance to. * @return Distance between the current instance of xPoint and other. */ public double distanceTo(xPoint other) { return Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2)); } }