Close Lab S: Create and use a class Problem Description: Design a class named Tr
ID: 3751262 • Letter: C
Question
Close Lab S: Create and use a class Problem Description: Design a class named Triangle that extends Geometricobiect. The class contains: default values to denote three sides of the triangle A constructor that creates a triangle with the specified Three double data fields named sidel. side. and side3 with . A no-arg constructor that creates a default triangle sidel, side2, and side3 . The accessor methods for all three data fields .A method named getAreal0 that returns the area of this .A sethod named getPerieto that returns the perimeter of .A method named tostring) that returns a string description triangle this triangle. for the triangle For the formula to compute the area of a triangle, see Exercise 5.19. The toStringmethod is implemented as Eollows: Draw the UML diagran that involves the classes Triangle and Gecmetricobiet Implement the class. Write a test program that creates a Triangle object with sides 1. 1.5, 1. color yellow and filled true, and displays the area, perimeter, color, and hether filled or not Design: Draw the UML class diagram here Coding: publie elass Exerese11 01 public static wold main(Stringtl args) Triangle triangle-new Trianglei. 2.5, ) trsangle.setColor "yelLow) triangle.setFilled(tzue) a System.out-printinttriangle) ystem.out-printin(The area lariangle.getArea0 Systen.out-printin The perimeter is triangle.getPerimeter O) ystem.out-printinttriangle)a elass Geonete Lecbject Copy it from the book class Triangle extends Geometricobject Implement it Submission Follow our class coding standard to complete this lab, check out for creditExplanation / Answer
Step1: Copy the code for GeometricObject from your text book (as instructed by professor)
Step2: Copy the following code only after Step1, otherwise you will get errors
Triangle.java
--------------
public class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public Triangle() {
this.side1 = 1;
this.side2 = 1;
this.side3 = 1;
}
public Triangle(double side1, double side2, double side3) throws IllegalTriangleException {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
isValidTriangle();
}
public double getArea() {
double s = (side1 + side2 + side3) / 2.0;
return Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
}
public double getPerimeter() {
return side1 + side2 + side3;
}
public String toString() {
return "Triangle: "
+ " side1 = " + side1
+ " side2 = " + side2
+ " side3 = " + side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
}
Exercise11_01.java
-------------------
public class Exercise11_01 {
public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is " + triangle.getPerimeter());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.