-
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
-
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:
Code:
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:
Code:
public double distance(Point p) {
//code
You can also overload these to return double or int
Similar Threads
-
By Rob Abbe in forum Talk to the Editors
Replies: 44
Last Post: 01-13-2003, 02:57 PM
-
By Lori Piquet in forum Talk to the Editors
Replies: 114
Last Post: 10-10-2002, 06:01 AM
-
By Glen Kunene in forum Talk to the Editors
Replies: 17
Last Post: 03-23-2002, 12:43 AM
-
Replies: 7
Last Post: 02-01-2001, 12:54 PM
-
Replies: 0
Last Post: 11-16-2000, 12:45 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks