(must be written in java) 10.1 (The Triangle class) Design a class named Triangl
ID: 3616830 • Letter: #
Question
(must be written in java)10.1
(The Triangle class) Design a class named Triangle that extendsGeometricObject. The class contains:
- Three double data fields named side1, side2, side3 with defaultvalues 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 thistriangle.
- A method named getPerimeter() that returns the perimeter of thistriangle.
- A method named toString() that returns a string description forthe triangle.
s = (side1 + side2 + side3)/2
area = Squareroot of s(s - side1)(s - side2)(s - side3)
Write a test program that creates a Triangle object with sides1,1.5,1, setting color yellow and filled true, and displaying thearea, perimeter, color, and whether filled or not.
Thanks in Advance Aphareus
Explanation / Answer
import java.io.*; public class Triangle{ public static double side1 =1.0; public static double side2 =1.0; public static double side3 =1.0; public String color =" Red"; public boolean filled =true; /* constructors */ public Triangle() { this.side1 = 1; this.side2 = 1; this.side3 = 1; this.color ="Red"; this.filled = true; } public Triangle(double a , double b , double c, String col,boolean bol) { this.side1 = a; this.side2 = b; this.side3 = c; this.color= col; this.filled= bol; } /* accessor methods */ public double getSide1() { return side1 ; } public double getSide2() { return side2 ; } public double getSide3() { return side3 ; } /* methods */ public static double getPerimeter() { double per = (side1 + side2 +side3); return per; } public double getArea() { double s = ((side1 + side2 +side3)/2); double ar = (s * (s- side1) * (s- side2) * (s-side3)); return Math.sqrt(ar); } public String toString() { String s =""; s +="***************************************** "; s +="Side1 = " + side1 + " Side2 = " +side2 + " Side3 = " + side3; s += " Color is: " + color; s += " Is Filled: " + filled; s += " Perimeter is: " + (getPerimeter()); s += " Area is: " + (getArea()); s+=" *****************************************"; return s; } public static void main(String [] args) { Triangle t = new Triangle(1, 1.5, 2,"Yellow", true); System.out.println(t.toString()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.