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

Create a new class called Triangle that extends Shape and implement the followin

ID: 3838803 • Letter: C

Question

Create a new class called Triangle that extends Shape and implement the following con- structor.

• protected Triangle(Point[] aPoints) This constructor calls the superclass’s con- structor and passes it the name "Triangle". It then passes a copy of the first three entries in the aPoints array to the setPoints method.

Override the following methods.
1. public double getPerimeter()

This method calculates the perimeter of the Triangle. The perimeter of a triangle is the sum of the lengths its sides (to get the length of a side, calculate the distance between the two of its vertices).

2. public double getArea()

3

This method calculates the area of the Triangle. If the vertices of a triangle are A = (ax,ay), B = (bx,by), and C = (cx,cy), then the area of the triangle is given by the expression

[ax(by cy)+bx(cy ay)+cx(ay by) ] / 2

Here is my Shape code:

import java.awt.Point;

public abstract class Shape {

   private String name;

   private Point[] points;

   protected Shape(String aName) {

   this.name = aName;

   }

   public final String getName() {

   return this.name;

   }

   protected final void setPoints(Point[] thePoints) {

   this.points = thePoints;

   }

   public final Point[] getPoints() {

   return this.points;

   }

   public static double getDistance(Point p1, Point p2) {

   double x1 = p1.getX();

   double x2 = p2.getX();

   double y1 = p1.getY();

   double y2 = p2.getY();

   double add1 = x1 - x2;

   double sqr1 = add1 * add1;

   double add2 = y1 - y2;

   double sqr2 = add2 * add2;

   double sqrt = sqr2 + sqr1;

   return Math.sqrt(sqrt);

   }

  

   abstract double getPerimeter();// Just Remove Method body

  

}

Explanation / Answer

Shape.java

public abstract class Shape {

   private String name;

   private Point[] points;

     

     

     

   public final String getName() {

   return this.name;

   }

     

   protected final void setPoints(Point[] thePoints) {

   this.points = thePoints;

   }

     

   public final Point[] getPoints() {

   return this.points;

   }

     

   public static double getDistance(Point p1, Point p2) {

         

       //to get the values of x and y coordinates of each points

   double x1 = p1.getX();

   double x2 = p2.getX();

   double y1 = p1.getY();

   double y2 = p2.getY();

   // to find the distance between two points

   double add1 = x1 - x2;

   double sqr1 = add1 * add1;

   double add2 = y1 - y2;

   double sqr2 = add2 * add2;

   double sqrt = sqr2 + sqr1;

   return Math.sqrt(sqrt);

   }

     

  

   abstract double getPerimeter(Point a,Point b,Point c);// Just Remove Method body

   abstract double getArea(Point a,Point b,Point c);

   }

Triangle.java

import java.util.Scanner;

public class Triangle extends Shape {

  

   protected Triangle() {

      

       // TODO Auto-generated constructor stub

   }

  

   public static void main(String[] args) {

       // TODO Auto-generated method stub

       Triangle ob=new Triangle();

       // to get the user input

       Scanner num = new Scanner(System.in);

          System.out.println("Enter the coordinates of triangle");

         

          System.out.println("First Coordinate");

          System.out.println("Enter the x coordinate of first point");

      

          double x1=0;

       //assigned the value of x coordinate of Ist point to x1

          x1 = Double.parseDouble(num.nextLine());

      

          System.out.println("Enter the y coordinate of first point");

      

          double y1=0;

          //assigned the value of y coordinate of Ist point to y1

          y1 = Double.parseDouble(num.nextLine());

   // point1 is used to store the first point

          Point point1 = new Point(x1,y1);

  

          point1.getPoint();

      

         

  

          System.out.println(" Enter 2nd Coordinate");

      

          System.out.println(" Enter the x coordinate of second point");

      

          double x2=0;

          //assigned the value of x coordinate of 2nd point to x2

          x2 = Double.parseDouble(num.nextLine());

      

          System.out.println("Enter the y coordinate of second point");

      

          double y2=0;

          //assigned the value of y coordinate of 2nd point to y2

          y2 = Double.parseDouble(num.nextLine());

      

          Point point2 = new Point(x2,y2);

      

          //getPoint method in Point is invoked to set the value

          point2.getPoint();

      

         

      

          System.out.println("Enter the 3rd Coordinate");

          System.out.println("Enter the x coordinate of third point");

      

          double x3=0;

          //assigned the value of x coordinate of 3rd point to x3

          x3 = Double.parseDouble(num.nextLine());

      

          System.out.println("Enter the y coordinate of third point");

      

          double y3=0;

          //assigned the value of y coordinate of 3rd point to y3

          y3 = Double.parseDouble(num.nextLine());

      

          Point point3 = new Point(x3,y3);

      

          point3.getPoint();

   //getPerimeter is called by passing the 3 points as the arguments

       double s=ob.getPerimeter(point1,point2,point3);

       //perimeter is printed

       System.out.println("perimeter "+s);

       //getAraea method is called by passing the 3 points

       double aa=ob.getArea(point1,point2,point3);

       System.out.println("area "+aa);

      

   }

   @Override

   double getPerimeter(Point point1,Point point2,Point point3) {

       // TODO Auto-generated method stub

          double perimeter=0;

      

       //getDistance method is invoked by passing the 2 points

       perimeter = getDistance(point1, point2)+getDistance(point1, point3)+getDistance(point2, point3);

         

       return perimeter;

  

   }

   double getArea(Point point1,Point point2,Point point3){

       double area=0;

       //x coordinate of Ist point is assigned to ax

       double ax=point1.getX();

       //y coordinate of Ist point is assigned to ay

       double ay=point1.getY();

       //x coordinate of 2nd point is assigned to bx

       double bx=point2.getX();

       //y coordinate of 2nd point is assigned to by

       double by=point2.getY();

       //x coordinate of 3rd point is assigned to cx

       double cx=point3.getX();

       //y coordinate of 3rd point is assigned to cy

       double cy=point3.getY();

       //given equation for finding the area of the triangle

       area=(ax*(by -cy)+bx*(cy-ay)+cx*(ay-by))/2;

       return area;

  

   }

}

Point.java

public class Point {

   private double X;

      private double Y;

     

   public Point(double x, double y) {

  

      this.X=x;

      this.Y=y;

      }

   public double getX(){

  

   return X;

  

   }

  

   public double getY(){

   return Y;

  

  

  

   }

  

   public void getPoint(){

  

   System.out.println("The point is ("+X+","+Y+")");

   }

}

OUTPUT

Enter the coordinates of triangle

First Coordinate

Enter the x coordinate of first point

7

Enter the y coordinate of first point

4

The point is (7.0,4.0)

Enter 2nd Coordinate

Enter the x coordinate of second point

8

Enter the y coordinate of second point

6

The point is (8.0,6.0)

Enter the 3rd Coordinate

Enter the x coordinate of third point

2

Enter the y coordinate of third point

6

The point is (2.0,6.0)

perimeter 13.621232784634294

area 6.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote