Question
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
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:
-
A no-arg constructor that initializes all three sides to 0 .
-
A constructor that creates a triangle with the specified side1, side2, and side3.
-
The setter methods for three data fields.
-
The getter methods for three data fields.
-
A method named getPerimeter() that returns the perimeter of this triangle.
-
A method named toString() that returns a string description for the triangle.
Write a test program that does the following tasks:
-
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.
-
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started