Help with a java program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Help with a java program

  1. #1
    Join Date
    Oct 2005
    Posts
    1

    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

  2. #2
    Join Date
    Aug 2005
    Posts
    43
    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

  1. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 03:57 PM
  2. Has Sun Given Up on the Desktop?
    By Lori Piquet in forum Talk to the Editors
    Replies: 114
    Last Post: 10-10-2002, 07:01 AM
  3. .NET vs. Enterprise Java: Who's Got Better Security?
    By Glen Kunene in forum Talk to the Editors
    Replies: 17
    Last Post: 03-23-2002, 01:43 AM
  4. making java program into exe file???
    By Herman in forum Java
    Replies: 7
    Last Post: 02-01-2001, 01:54 PM
  5. Replies: 0
    Last Post: 11-16-2000, 01: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center