Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write this program in Java. a. Write a class called Point, with integer x and y

ID: 3676460 • Letter: W

Question

Write this program in Java.

a. Write a class called Point, with integer x and y as data members of the class.

b. Define a no-argument constructor that sets both data members to zero.

c. Define an overloaded constructor that sets the x and y to data provided by user.

d. Add any getters, setters needed and a toString method that displays the point coordinates.

e. Define a class Line that uses “start” and “end”, of the type Point, as data members of the class.

f. Define a constructor, using four integer data to define the “start” and “end” point of a line.

g. Define a constructor, using two Point data to define the “start” and “end” point of a line.

h. Write a method that calculates the length of a Line, given “start” and “end” point of a line.

i. Write a class Equilateral_Triangle that uses the coordinates of three points provided by user.

j. Calculate the height of the triangle from P2(5,2), and from P3(3,4).

k. Calculate the height of the triangle from P1(2,1).

l. Write methods to calculate the perimeter and area of the Equilateral triangle.

m. Write a driver program that defines vertices of an equilateral triangle as P1(2,1), P2(5,2), P3(3,4) and calculates its heights, perimeter, and area, as shown here:

sameple output:

Sample Output:

Point 1: (2,1), Point 2: (5,2),   Point 3: (3,4)

Line from (2,1) to (5,2) Side_1 = 3.1623

Line from (2,1) to (3,4) Side_2 = 3.1623

Line from (5,1) to (3,4) Side_3 = 2.8284

Height (from P1) = 2.8284   Height (from P2 & P3) = 2.5298

Perimeter = 9.1530   Area = 4.000

Explanation / Answer

Hi, Since class name is Equilateral. So length of each side should be equal. But in example it is ioscales traingle. eight of equilateral triangle from any vertex is same. I have hard coded value of point.

You can change the code to read from user or let me know for any change

import java.text.DecimalFormat;

public class Point {
   private int x;
   private int y;
  
   public Point(){
       x=0;
       y=0;
   }
  
   public Point(int x, int y){
       this.x=x;
       this.y=y;
   }

   public int getX() {
       return x;
   }

   public void setX(int x) {
       this.x = x;
   }

   public int getY() {
       return y;
   }

   public void setY(int y) {
       this.y = y;
   }
  
   @Override
   public String toString() {
       return "("+x+","+y+")";
   }
}

class Line{
   private Point start;
   private Point end;
  
   public Line(int x1, int y1, int x2, int y2) {
       start = new Point(x1, y1);
       end = new Point(x2, y2);
   }
  
   public Line(Point start, Point end) {
       this.start = start;
       this.end = end;
   }
  
   public double lenght(){
       double xDiff = Math.pow((end.getX() - start.getX()), 2);
       double yDiff = Math.pow((end.getY() - start.getY()), 2);
       return Math.sqrt(xDiff + yDiff);
   }
}

class Equilateral_Triangle{
   public static void main(String[] args) {
       Point p1 = new Point(2, 1);
       Point p2 = new Point(5, 2);
       Point p3 = new Point(3,4);
       Line l1 = new Line(p1, p2);
       double side_1 = l1.lenght();
       Line l2 = new Line(p1, p3);
       double side_2 = l2.lenght();
       Line l3 = new Line(p2, p3);
       double side_3 = l3.lenght();
       // this is to format double number upto 3 decimal point
           DecimalFormat df= new DecimalFormat("#0.0000");
       System.out.println("Point 1: "+p1.toString()+", Point 2: "+p2.toString()+", Point 3: "+p3.toString());

       System.out.println("Line from "+p1.toString()+" to "+p2.toString()+" Side_1 = "+df.format(side_1));

       System.out.println("Line from "+p1.toString()+" to "+p3.toString()+" Side_2 = "+df.format(side_2));

       System.out.println("Line from "+p2.toString()+" to "+p3.toString()+" Side_3 = "+df.format(side_3));
      
       double heightFromP1 = Math.sqrt((side_1*side_2)-(1/4.0)*side_3*side_3);

       System.out.println("Height (from P1) = "+df.format(heightFromP1));
       //Height (from P2 & P3) = 2.5298

       System.out.println("Perimeter = "+df.format((side_1+side_2+side_3)));
       System.out.println("Area = "+df.format((0.5*heightFromP1*side_3)));
      
      
   }
}


/*

Output:

Point 1: (2,1), Point 2: (5,2), Point 3: (3,4)
Line from (2,1) to (5,2) Side_1 = 3.1623
Line from (2,1) to (3,4) Side_2 = 3.1623
Line from (5,2) to (3,4) Side_3 = 2.8284
Height (from P1) = 2.8284
Perimeter = 9.1530
Area = 4.0000

*/