Design a class named Triangle that extends provided GeometricObject. The class c
ID: 3586061 • Letter: D
Question
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 1 import java.util.*; 2 import java.lang.*; 3 import java.io.; 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 5 class Geometricobjectf 6 private String color="white". 7 private boolean filled; 8 private java.util.Date dateCreated; The toString method is implemented as follows: return "Triangle: side. " + side 1 +" 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. 10 Construct a default geometric object */ 11 public Geometricobject) t 12 dateCreated=newjava.util.Date(); 15 *Construct a geometric object with the specified color 16 and filled value / 17 public Geometricobject (String color, boolean filled) 18 dateCreated=newjava.util.Date(); 19 this.color= color; 20 this.filled=filled output1 1.73 6.00 Triangle: side! = 2.0 side2 2.0 side3 = 2.0 color: red and filled: true 23 *Return color 24 public String getColor) 25 return color; 26 27 28 *Set a new color/ 29 public void setColor(String color) f 30 this.color color; green false output2 0.43 3.00 Triangle: side! = 1.0 side2 33 Return filled. Since filled is boolean, 34 its getter method is named isFilled*/ 35 public boolean isFilled) 36 return filled 1.0 side3 = 1.0 color: green and filled: false 39 1* Set a new filled 0 public void setFilled (boolean filled) 1 this.filled-filled; 44 1* Get dateCreated/ 45 public java.util.Date getDateCreated) f 46 return dateCreated; public double getArea)A return 0; public double getPerimeter) return 0; 57 Return a string representation of this object */ 58 public String toString) { 59 return" color:"color +"and filled:" filled 63 class Triangle extends Geometricobject 4 class DriverMain public static void main(String args[]) scanner input new Scanner(SysteM.in); double side! = input.nextDouble(); double side2 = input.nextDouble(); double side3 = input.nextDouble(); string color = input.next(); boolean filled 1nput.nextBoolean(); 9 Triangle triangle new Triangle(side1, side2, side3); triangle.setColor(color); triangle.setFilled(filled); System.out.printt(%. 2f",triangle. getArea()); System.out.println(); System. out .printf("X.2f",triangle. getPerimeter()) ; System.out.println(); System.out.printin(triangle)Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class GeometricObject{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject(){
Date mydate = new Date();
dateCreated = mydate;
}
public GeometricObject(String color, boolean filled){
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled = filled;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
public double getArea(){
return 0;
}
public double getPerimeter(){
return 0;
}
public String toString(){
return "color: "+ color+" and filled: " + filled;
}
}
class Triangle extends GeometricObject{
double side1;
double side2;
double side3;
Triangle(){
side1 =1;
side2=1;
side3=1;
}
Triangle(double side1, double side2, double side3){
if(side3<=(side1+side2) && side2<=(side1+side3) && side1<=(side3+side2)){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;}
else this.side1 = 1;
this.side2 = 1;
this.side3 = 1;
}
public double getSide1(){
return side1;
}
public double getSide2(){
return side2;
}
public double getSide3(){
return side3;
}
public void setSide1(double side1){
this.side1 = side1;
}
public void setSide2(double side2){
this.side2 = side2;
}
public void setSide3(double side3){
this.side3 = side3;
}
public double getArea(){
double s= (side3+side2+side1)/2;
double h = s*(s-side1)*(s-side2)*(s-side3);
return Math.pow(h,0.5);
}
public double getPerimeter(){
return side1+side2+side3;
}
public String toString(){
return "Triangle: side1 = "+ side1 +" side2 = " + side2 + " side3 = "+side3+" " + super.toString();
}
}
public class Main{
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.