Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help with a java program

I am having trouble figuring out what I am doing. If anyone can help me it would be appreciated. Here are the requirements:

Write a Java program that computes the distance between two points on a number plane (of x and y). In the server class, define the overloaded methods that computes distances of 1) two integer coordinates, 2) two real number coordinates, 3) two whole number coordinates defined as two Point objects, and 4) an integer coordinates and the origin of the number plane.

Here is the code I have so far but I don't think I am doing it right.

//Start Code

import java.util.Random;

public class Distance
{

public static void main (String[] args) {
int x1, y1, x2, y2;
double dist;
final int num;

Random generator = new Random();

x1 = generator.nextInt();
x2 = generator.nextInt();
y1 = generator.nextInt();
y2 = generator.nextInt();

dist = Math.sqrt(Math.pow(x1 - x2, 2) +
Math.pow(y1 - y2, 2));

//Integer
System.out.println("The distance between (" + x1 +
"," + y1 + ") and (" + x2 + "," +
y2 + ") is " + (int)dist);

//Real
System.out.println("The distance between (" + x1 +
"," + y1 + ") and (" + x2 + "," +
y2 + ") is " + (double)dist);

}
}

//End Code
[1351 byte] By [Domokun] at [2007-11-11 7:05:41]
# 1 Re: Help with a java program
Instead of casting the result into an int or double, it says that you should overload the methods, that means that you would have something like:

public int distance(int x, int y) {
//code
}

public double distance(double x, double y) {
//code
}

As with the Point objects, you could define a point simply as having an x and y coordinate, and would calculate the distance using the same method, but having a new method which receives a Point object instead of variables:

public double distance(Point p) {
//code

You can also overload these to return double or int
chimps at 2007-11-11 22:39:23 >