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: 3676457 • 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:

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

import java.lang.Math; import java.io.*; // Utility Class Point // Stores x,y coordinates of a point class Point { private int x,y; // Constructor public Point( int newX, int newY ) { // initialize x and y x = newX; y = newY; } // Returns value of the X coordinate public int X() { return x; } // Returns value of the Y coordinate public int Y() { return y; } // Utility Function // Calculates the linear distance between // this point and the given point pt. // Used in Triangle to calculate length of sides public double DistanceBetween(Point pt) { double distance,xd,yd; xd = x - pt.X(); yd = y - pt.Y(); distance = Math.sqrt( xd*xd + yd*yd ); return distance; } } // End of Point class //============================================ // Shape Class // Abstract class from which other shapes inherit abstract class Shape { // All shapes have a perimeter, so define abstract method // returns a double that is the perimeter of the shape abstract double Perimeter(); } //=================================================== // Triangle class, inherits from abstract Shape class // Has child classes : RightTriangle and EquilateralTriangle class Triangle extends Shape { protected Point vertexA, vertexB, vertexC; // Length of the 3 sides protected double sideAB, sideBC, sideAC; public Triangle() { sideAB = sideBC = sideAC = 0; } // Constructor : Takes 3 vertices, calculates sides public Triangle (Point newA, Point newB, Point newC) { vertexA = newA; vertexB = newB; vertexC = newC; sideAB = vertexA.DistanceBetween(vertexB); sideBC = vertexB.DistanceBetween(vertexC); sideAC = vertexA.DistanceBetween(vertexC); } // calculates and returns perimeter of this triangle public double Perimeter() { return (sideAB + sideBC + sideAC); } // calculates and returns the length of // the longest side of this triangle public double LongestSide() { return (sideAB > sideBC ? ( sideAB > sideAC ? sideAB : sideAC) : (sideBC>sideAC ? sideBC : sideAC) ); } //calculates and returns the length of //the shortest side of this triangle public double ShortestSide() { if ( sideAB