Design a class named Triangle that extends GeometricObject. Theclass contains: /
ID: 3607793 • Letter: D
Question
Design a class named Triangle that extends GeometricObject. Theclass contains:
//Three double data fields named side1, side2, and side3 withdefault values 1.0 to denote three sides of the triangle.
//A no-arg constructor that creates a default triangle.
//A constructor that creates a rectangle with the specified side1,side2, and side3.
//The accessor methods for all three data fields.
//A method named getArea() that returns the area of thistriangle.
//A method named getPerimeter() that returns the perimeter of thistriangle.
//A method named toString() that returns a string description forthe triangle.
The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2= "side3 =" + side3;
//Implement the class. Write a test program that creates a Triangleobject with sides 1, 1.5, 1, sets color yellow and filled true, anddisplays the area, perimeter, color, and whether filled or not.
Explanation / Answer
abstract class GeometricObject{protected String color;protected double weight;protected GeometricObject(){color = "white";weight = 1.0;}protected GeometricObject(String color, double weight){this.color = color;this.weight = weight;}public String getColor(){return color;}public void setColor(String color){this.color = color;}public double getWeight(){return weight;}public void setWeight(double weight){this.weight = weight;}public abstract double findArea();public abstract double findPerimeter();}class Triangle extends GeometricObject{double side1,side2,side3;boolean filled;public double findArea(){double area,s;s = ( side1 + side2 + side3 ) /2;area = Math.sqrt(s * (s-side1) * (s-side2) * (s-side3));return area;}public Triangle(double s1,double s2,double s3){side1=s1;side2=s2;side3=s3;}public void setFilled(boolean t){filled = t;}public boolean getFilled(){return filled;}public double findPerimeter(){double peri;peri = side1 + side2 + side3;return peri;}public String toString(){return "Triangle: side1 = " + side1 + " side2 = " + side2 + "side3 = " + side3;}public static void main(String args[]){Triangle t = new Triangle(1, 1.5, 1);t.setColor("yellow");t.setFilled(true);System.out.println("Area is :" + t.findArea());System.out.println("Perimeter is :" + t.findPerimeter());System.out.println("color is :" + t.getColor());System.out.println("Filled :" + t.getFilled());}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.