Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[JAVA] Write codes using Type 1 Inner Classes (All outer classes are implicitly

ID: 3804177 • 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 and toString class!)

All variables declared in class must be private. Provide getter/setter methods. always add a print() method for debugging, add a default constructor.

Explanation / Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author Sam
*/
public class Geometry {
    abstract class GeometricObject{
        private String color;
        private boolean filled;
        private double area;
        private double perim;


        public GeometricObject( ) { //default constructor
   color = null;
   filled = false;
   area = 0;
   perim = 0;
}
        public GeometricObject(String color, boolean filled) { //parameterized constructor
            this.color = color;
            this.filled = filled;
        }
      
        abstract protected double calcArea(); //to be implemented by child classes
        abstract protected double calcPerim(); //to be implemented by child classes


        /* Getter and Setter methods are given below*/
        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 double getArea() {
            return area;
        }

        public double getPerim() {
            return perim;
        }

        protected void setArea(double area) {
            this.area = area;
        }

        protected void setPerim(double perim) {
            this.perim = perim;
        }
      
    }
      
    final class Triangle extends GeometricObject {
        double side1;
        double side2;
        double side3;

        public Triangle(){ //default constructor
                   side1 = side2 = side3 = 0; //note that calling default constructor is implicit
        }

         public Triangle(double side1, double side2, double side3, String color, boolean filled) { //parameterized constructor
            super(color, filled);
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
            setPerim(calcPerim());
            setArea(calcArea());
        }

        public double getSide1() {
            return side1;
        }

        public void setSide1(double side1) {
            this.side1 = side1;
        }

        public double getSide2() {
            return side2;
        }

        public void setSide2(double side2) {
            this.side2 = side2;
        }

        public double getSide3() {
            return side3;
        }

        public void setSide3(double side3) {
            this.side3 = side3;
        }

        @Override
        protected double calcPerim() { //calculate perimeter
            return side1 + side2 + side3;
        }

        @Override
        protected double calcArea() { //calculates the area
            double p = calcPerim()/2;
            return Math.sqrt(p*(p-side1)*(p-side2)*(p*side3)); //Heron's Formula
        }
      
        public void print(){ //this simply prints the triangle properties
            System.out.println("Side 1:" + side1 + " " +
                    "Side 2:" + side2 + " " +
                    "Side 3:" + side3 + " " +
                    "Color:" + getColor() + " " +
                    "Filled:" + isFilled() + " " +
                    "Area:" + getArea() + " " +
                    "Perimeter:" + getPerim());
        }
    }
  
    void doWork() throws IOException { // since the class triangle is non static we cannot use it inside another static method, so we created another nonstatic method
        double side1, side2, side3;
        String color;
        boolean isFilled;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Side 1:");
        side1 = Double.parseDouble(br.readLine());
        System.out.println("Enter Side 2:");
        side2 = Double.parseDouble(br.readLine());
        System.out.println("Enter Side 3:");
        side3 = Double.parseDouble(br.readLine());
        System.out.println("Enter Color:");
        color = br.readLine();
        System.out.println("Is the triangle filled? (true/false):");
        isFilled = Boolean.parseBoolean(br.readLine());
      
        Triangle t = new Triangle(side1, side2, side3, color, isFilled);
        t.print();
    }
    public static void main(String[] args) throws IOException {
        new Geometry().doWork();
    }
}

If you face any problem please feel free to ask me in the comment section below

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote