Question

(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 at (3.8333333333333335, 1.0)

Homework Answers

Answer #1

Point.java

public class Point {
    private double p1;
    private double p2;

    public double getP1() {
        return p1;
    }

    public void setP1(double p1) {
        this.p1 = p1;
    }

    public double getP2() {
        return p2;
    }

    public void setP2(double p2) {
        this.p2 = p2;
    }
    
    
}

Driver Class

Centroid.java

import java.util.Scanner;

public class Centroid {
    public static Point getCenterPoint(Point p1, Point p2, Point p3){
        Point p = new Point();
        double x = (p1.getP1() + p2.getP1() + p3.getP1()) / 3;
        double y = (p1.getP2() + p2.getP2() + p3.getP2()) / 3;
        
        p.setP1(x);
        p.setP2(y);
        
        return p;
    }
    public static void main(String[] args) {
        Point p1 = new Point();
        Point p2 = new Point();
        Point p3 = new Point();
        Point p4 = new Point();
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter x1 and y1");
        p1.setP1(sc.nextDouble());
        p1.setP2(sc.nextDouble());
        
        System.out.println("Enter x2 and y2");
        p2.setP1(sc.nextDouble());
        p2.setP2(sc.nextDouble());
        
        System.out.println("Enter x3 and y3");
        p3.setP1(sc.nextDouble());
        p3.setP2(sc.nextDouble());
        
        p4 = getCenterPoint(p1, p2, p3);
        System.out.println("Center Point = " + p4.getP1() + ", " + p4.getP2());
    }
}

Output:-

Make sure to put the two files in the same package.

Note:- Please comment if you face any problem. :)

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
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
Write a complete C program that prompts the user for the coordinates of two 3- D...
Write a complete C program that prompts the user for the coordinates of two 3- D points ( x1 , y1 , z1 ) and ( x2 , y2 , z2 ) and displays the distance between them computed using the following formula: distance= square root of (x1-x2)2+(y1-y2)2+(z1-z2)2
(Java) Write the missing equals method needed for the Point class shown on the screen to...
(Java) Write the missing equals method needed for the Point class shown on the screen to work with this program. The missing method would be added to the Point class. class PointTestP3 {   public static void main(String[] args) {    Point p1 = new Point(5, 6);     Point p2 = new Point(5, 6);        System.out.println(p1.equals(p2)); // prints "true"   } }
Write a method using JAVA called "maximum4" that returns the maximum of 4 decimal numbers. Use...
Write a method using JAVA called "maximum4" that returns the maximum of 4 decimal numbers. Use the "math.max" method to implement "maximum4". The user should enter the 4 values and the program determines the maximum and then display the result. The steps for the Project could be the following: Create the Maximum4 method that includes the math.max Define and enter the variables Apply the Maximum4 method you created to find the maximum of the 4 decimal numbers you entered and...
Write code in JAVA Write a program that will output a right triangle based on user...
Write code in JAVA Write a program that will output a right triangle based on user specified input height (int) and specified input symbol (char).The first line will have one user-specified character, such as % or *. Each subsequent line will have one additional user-specified character until the number in the triangle's base reaches specified input height. Output a space after each user-specified character, including after the line's last user-specified character. Hint: Use a nested for loop. Ex:If the input...
PLEASE USE JAVA Write a program that draws the circumscribed circle (also known as the circumcircle)...
PLEASE USE JAVA Write a program that draws the circumscribed circle (also known as the circumcircle) of a given triangle ABC; this circle passes through points A, B, and C. These points will be specified by the user by clicking the mouse button. Remember, the three perpendicular bisectors of the three edges of a triangle all pass through one point, the circumcenter, which is the center of the circumscribed circle.
(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...
You are given the following Java files: EvaluateExprission that you will use to add a method....
You are given the following Java files: EvaluateExprission that you will use to add a method. In EvaluateExprission.java write a recursive method    public static int EvaluateE (int x, int y) The method should evaluate the expression bellow and take the values x, y from the user. Update the main to call EvaluateE method properly . E = xy +x* (x-1)(y-1) +x* (x-2)(y-2) + . . . +1
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...