Write this program in Java. a. Write a class called Point, with integer x and y
ID: 3680076 • 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.util.scanne;
public class DistTriangle
{
double distance(double x1 , double y1 , double x2 , dopuble y2)
{
rreturn math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y1));
double x1,x2,y1,y2;
double distance;
scanner scan = new scanner(system.in);
System.out.pintln("enter the x coordinate for point 1 : ");
x1=scan.nextDouble();
System.out.println("enter the y coordinate for point 1 :");
y1=scan.nextDouble();
System.out.println("enter the x coordinate for point 2 :");
x2=scan.nextDouble();
System.out.println("enter the y coordinate point 2:");
y2=scan.nextDouble();
distance=distance(x1,y1,x2,y2);
System.out.println("the distance between two points is : + distance + ".");
}
public class AreaOfTriangle
{
Scanner scanner=new Scanner(System.in);
System.out.println("please enter base width of triangle");
float base=scanner.nextFloat();
System.out.pintln("please enter height of taingle");
float area=area(base,height);
System.out.pintln("area of triangle is :" + area);
}
public static float area(float base,float height)
{
return (base * height )/2;
}
class Calculation
{
public static void main(Sting args[])
{
DistTriangle dist = new DistTriangle(double x1,double y1,double x2,double y2);
AreaOfTriangle area=new AreaOfTriangle(float base,float base);
dist.distance();
area.area();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.