Question: Write the code in JAVA Design a class named Triangle that extends GeometricObject ( as shown below ) . The class should contain the following

Write the code in JAVA
Design a class named Triangle that extends GeometricObject (as shown below). The class should contain the following three private double data fields: side1, side2, side3 with default values 1.0 to denote three sides of the triangle.
In addition, the class should have the following constructors and methods:
1. A no-arg constructor that initializes all three sides to 0.
2. A constructor that creates a triangle with the specified side1, side2, and side3.
3. The setter methods for three data fields.
4. The getter methods for three data fields.
5. A method named getPerimeter() that returns the perimeter of this triangle.
6. A method named toString() that returns a string description for the triangle.
Write a test program that does the following tasks:
1. Prompts the user to enter three sides of the triangle. The program creates a Triangle object with these sides. Prompt the user to enter a color and a Boolean value to indicate whether the triangle is filled. Use these two input values to set the data fields color and filled.
2. Invoke the Triangle objects getPerimeter() and toString() methods.
public 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 get 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;
}
/** Return a string representation of this object */
public String toString(){
return "created on "+ dateCreated +"
color: "+ color +
" and filled: "+ filled;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!