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.
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
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)
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...
I need a Flowchart and Code for the following Java Program Write a method that converts...
I need a Flowchart and Code for the following Java Program Write a method that converts milliseconds to hours, minutes, and seconds using the following header: public static String convertMillis(long millis) The method returns a string as hours:minutes:seconds. For example,convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Write a test program that prompts the user to enter a long integer for milliseconds and displays a string in the format of hours:minutes:seconds.
In Java, create a program and Flowchart, Write a method that converts milliseconds to hours, minutes,...
In Java, create a program and Flowchart, Write a method that converts milliseconds to hours, minutes, and seconds using the following header: public static String convertMillis(long millis) The method returns a string as hours:minutes:seconds. For example, convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Write a test program that prompts the user to enter a long integer for milliseconds and displays a string in the format of hours:minutes:seconds.
(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.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT