(The Triangle class) Design a class named Triangle that extends GeometricObject.
ID: 3830443 • Letter: #
Question
(The Triangle class) Design a class named Triangle that extends GeometricObject. The class contains:
Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
A no-arg constructor that creates a default triangle.
A constructor that creates a triangle with the specified side1, side2, and side3.
The accessor methods for all three data fields.
A method named getArea() that returns the area of this triangle.
A method named getPerimeter() that returns the perimeter of this triangle.
A method named toString() that returns a string description for the triangle. For the formula to compute the area of a triangle, see Programming Exercise 2.19. The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
"Please use Eclipse to write this program"
Explanation / Answer
GeometricObject.java
public abstract class GeometricObject {
//Declaring abstract methods
public abstract double getPerimeter();
public abstract double getArea();
}
__________________
Triangle.java
public class Triangle extends GeometricObject {
//Declaring instance variables
double side1,side2,side3;
//Zero argumented constructor
public Triangle() {
super();
this.side1 = 1.0;
this.side2 = 1.0;
this.side3 = 1.0;
}
//Parameterized constructor
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
//getters and setters
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
//This method will calculates the perimeter of the Triangle
@Override
public double getPerimeter() {
return side1+side2+side3;
}
//This method will calculates the area of the Triangle
@Override
public double getArea() {
double p=(side1+side2+side3)/2;
double area=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
return area;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Triangle :side1=" + side1 + ", side2=" + side2 + ", side3="+ side3;
}
}
_______________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
double side1,side2,side3;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//getting the inputs entered by the user
System.out.print("Enter side1 :");
side1=sc.nextDouble();
System.out.print("Enter side2 :");
side2=sc.nextDouble();
System.out.print("Enter side3 :");
side3=sc.nextDouble();
//Creating an object of Triangle class by pasing the user entered inputs
Triangle t=new Triangle(side1, side2, side3);
//Displaying the area of the Triangle
System.out.printf("Area of The Triangle is :%.2f ",t.getArea());
//Displaying the Perimeter of the Triangle
System.out.println("Perimeter of The Triangle is :"+t.getPerimeter());
}
}
______________________
Output:
Enter side1 :4.5
Enter side2 :5.0
Enter side3 :5.5
Area of The Triangle is :10.61
Perimeter of The Triangle is :15.0
______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.