Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

***********IN JAVA************* Design a class named Triangle that extends provi

ID: 3872263 • Letter: #

Question

***********IN JAVA*************

Design a class named Triangle that extends provided 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.

Note : first determine whether three lengths form a triangle. If not, just use default values for all the sides.

The accessor(getters and setters) 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.

The toString() method is implemented as follows:

                         return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3 + super.toString();

The test program, prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.

Input1

2

2

2

red

true

output1

1.73

6.00

Triangle: side1 = 2.0 side2 = 2.0 side3 = 2.0 color: red and filled: true

Input2

22

3

5

green

false

output2

0.43

3.00

Triangle: side1 = 1.0 side2 = 1.0 side3 = 1.0 color: green and filled: false

*******Use the following class driver and class with no modifications to it!********

import java.util.*;
import java.lang.*;
import java.io.*;

class GeometricObject{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

/** Construct a default geometric object */
public GeometricObject() {
dateCreated = new java.util.Date();
}

/** Construct a geometric object with the specified color
* and filled value */
public GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}

/** Return color */
public String getColor() {
return color;
}

/** Set a new color */
public void setColor(String color) {
this.color = color;
}

/** Return filled. Since filled is boolean,
its getter method is named isFilled */
public boolean isFilled() {
return filled;
}

/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}

public double getArea(){
return 0;
}
  
public double getPerimeter() {
return 0;
}
  
/** Return a string representation of this object */
public String toString() {
return " color: " + color + " and filled: " + filled;
}
}

class DriverMain{

public static void main(String args[]){

Scanner input = new Scanner(System.in);

double side1 = input.nextDouble();

double side2 = input.nextDouble();

double side3 = input.nextDouble();

String color = input.next();

boolean filled = input.nextBoolean();

Triangle triangle = new Triangle(side1, side2, side3);

triangle.setColor(color);

triangle.setFilled(filled);

System.out.printf("%.2f",triangle.getArea());

System.out.println();

System.out.printf("%.2f",triangle.getPerimeter());

System.out.println();

System.out.println(triangle);

}

}

***********************************************************************************

---And use the following class that was refered to----

class Triangle extends GeometricObject{

}

***********************************************************************************

Leaving comments in the code would be much appreciated to help me understand the code more!

Explanation / Answer

public class TriangleTest { // Driver Class public static void main(String[] args) { Triangle tri1 = new Triangle(); System.out.println(tri1); } } // Subclass Triangle class Triangle extends GeometricObject{ private double side1, side2, side3; // Constructors public Triangle() { // Default generates 1x1x1 triangle this(1.0, 1.0, 1.0); } public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } // Accessors (No mutators defined by class) public double getSide1() { return side1; } public double getSide2() { return side2; } public double getSide3() { return side3; } // Data methods public double getPerimeter(){ // Perimeter is sum of all sides return side1+side2+side3; } public double getArea(){ // Area calculated using Heron's Rule double semiPer = this.getPerimeter()/2; return Math.sqrt(semiPer*(semiPer-side1)*(semiPer-side2)*(semiPer-side3)); } // Output @Override public String toString(){ return String.format("Triangle with sides %.2f, %.2f, %.2f has a perimeter of %.2f, and an area of %.3f units.",side1,side2,side3,this.getPerimeter(),this.getArea()); } }