Question

Java: Distance calc. This question is fairly straightforward. Design implement (source code) a program to compute...

Java: Distance calc. This question is fairly straightforward. Design implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2] Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs. Note: for C++, #include and then call sqrt(). For Java, you’ll use Math.sqrt() and for C# it’s Math.Sqrt(). See the short appendix below for the general form.

Sample run 1: Entered X1: 1.5 Entered Y1: -3.4 Entered X2: 4 Entered Y2: 5 Distance: 8.764131445842194

Sample run 2: Entered X1: -5.5 Entered Y1: -8.8 Entered X2: 25 Entered Y2: 4.5 Distance: 33.27371334852784

Sample run 3: Entered X1: 2 Entered Y1: 2 Entered X2: 10 Entered Y2: 10 Distance: 11.313708498984761  

Homework Answers

Answer #1
//Distance2.java
import java.util.Scanner;
public class Distance2 {
    public static void main(String args[]){
        double x1,x2,y1,y2, distance;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter X1: ");
        x1 = scanner.nextDouble();
        System.out.print("Enter Y1: ");
        y1 = scanner.nextDouble();
        System.out.print("Enter X2: ");
        x2 = scanner.nextDouble();
        System.out.print("Enter Y2: ");
        y2 = scanner.nextDouble();
        distance = Math.pow(Math.pow(x2-x1,2)+Math.pow(y2-y1,2),0.5);
        System.out.println("Distance: "+ distance);
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(Center of a triangle) In Java, write the following method that returns the center of a...
(Center of a triangle) In Java, write the following method that returns the center of a triangle: (MAKE SURE TO USE THIS METHOD) public static Point getCenterPoint(Point p1, Point p2, Point p3); Write a test program that prompts the user to enter three points and displays the center point. Here is a sample run: (Hint: Use what you created for the previous problem). Enter x1, y1, x2, y2, x3, y3: 2.5 2 5 -1.0 4.0 2.0 The center point is...
Java: All Hail Modulus Agustus! The modulus operator is used all the time. Realize that if...
Java: All Hail Modulus Agustus! The modulus operator is used all the time. Realize that if you “mod” any number by a number “n”, you’ll get back a number between 0 and n-1. For example, “modding” any number by 20 will always give you a number between 0-19. Your job is to design implement (source code) a program to sum the total of all digits in an input integer number between 0 and 1000, inclusive. Notice that you need to...
(Use Java ) please write comment on every line I need to understand the code Problem...
(Use Java ) please write comment on every line I need to understand the code Problem Description: Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5. For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle, as shown in the Figure. (Hint: A point is in the rectangle if its horizontal distance...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT