Results 1 to 7 of 7
  1. #1
    fscj is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default How to you write a java program using the distance formula?

    import java.util.Scanner;

    public class Distance
    {

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

    System.out.print("Enter first two points (x1, y1) :");
    double x1 = input.nextDouble();
    double y1 = input.nextDouble();

    System.out.print("Enter second two points (x2, y2) :");
    double x2 = input.nextDouble();
    double y2 = input.nextDouble();

    double distance = Math.pow(((x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1)), 0.5);

    System.out.print("The distance of the two points is " + distance); //I'm using JGRASP AND my error is here!
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to you write a java program using the distance formula?

    What exact error are you getting?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    fscj is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to you write a java program using the distance formula?

    import java.util.Random;
    public class Distance {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter first two points (x1, y1) :");
    double firstPoints = input.nextDouble();

    System.out.print("Enter second two points (x2, y2) :");
    double secondPoints = input.nextDouble();

    double distance = Math.pow((x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1), 0.5;

    System.out.print("The distance of the two points is " + distance);
    }
    }
    Last edited by fscj; 09-26-2013 at 07:53 PM. Reason: Wrong program

  4. #4
    fscj is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to you write a java program using the distance formula?

    Distance.java:14: error: ')' expected
    double distance = Math.pow((x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1), 0.5;
    ^
    1 error

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to you write a java program using the distance formula?

    Count your opening and closing parenthesis. You should have the same number of each.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: How to you write a java program using the distance formula?

    It has to do with the fact hat you can't multiply in Java like (x2-x1)(x2-x1). It has to be (x2-x1)*(x2-x1).
    And BTW, there is a square root method in the Math class.

    Regards,
    Jim
    Last edited by jim829; 09-27-2013 at 04:58 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: How to you write a java program using the distance formula?

    Moving to New to Java.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Distance Formula and Random numbers code
    By granslime in forum New To Java
    Replies: 10
    Last Post: 09-26-2013, 07:27 PM
  2. Distance formula
    By cost63 in forum New To Java
    Replies: 2
    Last Post: 07-09-2013, 03:35 AM
  3. diving distance program
    By maximumammo in forum New To Java
    Replies: 4
    Last Post: 04-05-2012, 08:09 PM
  4. program for a quadratic formula
    By bbtgirl in forum New To Java
    Replies: 9
    Last Post: 01-30-2009, 06:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •