[JAVA] Write codes using Type 1 Inner Classes (All outer classes are implicitly
ID: 3803635 • Letter: #
Question
[JAVA]
Write codes using Type 1 Inner Classes (All outer classes are implicitly static). This means that all the classes created for this program will be created inside a single Outer class.
(Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject and then implement the Triangle class. Write a test program that 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.
(Please don't use ArrayList class!)
Explanation / Answer
Please find my implementation.
Please let me know in case of any issue.
public abstract class GeometricObjects {
private String color;
private boolean filled;
public GeometricObjects(){
this.color = "white";
this.filled = false;
}
/*Construct Geometric Object with specified color and filled value*/
public GeometricObjects(String color, boolean filled){
this.color = color;
this.filled = filled;
}
/* Return Color*/
public String getColor(){
return color;
}
/*Return filled. since filled is boolean we name it isFilled*/
public boolean isFilled(){
return filled;
}
/*Set new color*/
public void setColor(String color) {
this.color = color;
}
/*Set new filled*/
public void setFilled(boolean filled){
this.filled = filled;
}
// abstract methods
public abstract double getArea();
public abstract double getPerimeter();
/* toString method that returns the string representation of object*/
public String toString(){
return "Object color is: " + color + " object filled is: " + filled ;
}
}
############## Triangle.java #############
class Triangle extends GeometricObjects {
private double side1, side2, side3;
private double width;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public Triangle(double side1, double side2, double side3, String color, boolean filled) {
super(color, filled);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double getArea() {
double s = (side1+side2+side3)/2.0;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
return area;
}
@Override
public double getPerimeter() {
return (side1+side2+side3);
}
@Override
public String toString() {
return super.toString()+ "; Triangle Area is: " + getArea() + "; Perimeter is: " + getPerimeter() ;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.