[JAVA] Write codes using Type 1 Inner Classes (All outer classes are implicitly
ID: 3861070 • 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.
Explanation / Answer
This is abstract GeometricObject Class
package com.GeometricShape1;
public abstract class GeometricObject {
String color;
boolean filled;
//0 arg constructor
GeometricObject(){
}
//2 arg constructor
GeometricObject(String color,boolean filled){
this.color=color;
this.filled=filled;
}
//4 arg constructor
GeometricObject( int X,int Y,String color,boolean filled){
this.color=color;
this.filled=filled;
}
//static inner class
public static class Triangle extends GeometricObject {
double firstSide;
double secondSide;
double thirdSide;
Double Perimeter;
//0 arg constructor
Triangle(){
}
//3 arg constructor
public Triangle(double firstSide, double secondSide, double thirdSide) {
this.firstSide = firstSide;
this.secondSide = secondSide;
this.thirdSide = thirdSide;
}
//5 arg constructor
public Triangle(String color, boolean filled,double firstSide, double secondSide, double thirdSide) {
super(color, filled);
this.firstSide = firstSide;
this.secondSide = secondSide;
this.thirdSide = thirdSide;
}
//setter & getter
public double getFirstSide() {
return firstSide;
}
public void setFirstSide(double firstSide) {
this.firstSide = firstSide;
}
public double getSecondSide() {
return secondSide;
}
public void setSecondSide(double secondSide) {
this.secondSide = secondSide;
}
public double getThirdSide() {
return thirdSide;
}
public void setThirdSide(double thirdSide) {
this.thirdSide = thirdSide;
}
public String getName(){
return "Triangle";
}
public Double getArea(){
Double semiperimeter=Perimeter/2;
Double Area= Math.sqrt(semiperimeter*(semiperimeter-firstSide)*(semiperimeter-secondSide)*(semiperimeter-thirdSide));;
return Area;
}
public Double getPerimeter(){
Perimeter=firstSide+secondSide+thirdSide;
return Perimeter;
}
};
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 String toString(){
return null;
}
abstract String getName();
abstract Double getArea();
abstract Double getPerimeter();
}
This is Driver class
//This is test class to check above classes
package com.GeometricShape1;
import java.util.ArrayList;
import com.GeometricShape1.GeometricObject.Triangle;
public class ShapeTest {
public static void main(String[] args) {
ArrayList<Triangle> shapes=new ArrayList();
Triangle triangle1=new Triangle("White",true,2.0,3.0,4.0);
Triangle triangle2=new Triangle("Red",true,3.0,5.0,6.0);
shapes.add(triangle1);
shapes.add(triangle2);
//call method to print all
for(Triangle currentshape:shapes){
System.out.println(currentshape.getName()+" Color:"+currentshape.getColor()+" filled:"+currentshape.isFilled());
if(currentshape instanceof Triangle){
//perimeter
System.out.printf("%s's perimeter is %s ",currentshape.getName(),currentshape.getPerimeter());
//(Triangle) currentShape;
System.out.printf("%s's area is %f ",currentshape.getName(),currentshape.getArea());
System.out.println("---------------------------");
}//end if
}//end for
}
}
Output:
Triangle Color:White filled:true
Triangle's perimeter is 9.0
Triangle's area is 2.904738
---------------------------
Triangle Color:Red filled:true
Triangle's perimeter is 14.0
Triangle's area is 7.483315
---------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.