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

1) Inheritance: You must determine what (if anything) should be abstract, what s

ID: 3540271 • Letter: 1

Question

1) Inheritance: You must determine what (if anything) should be abstract, what should be inherited, and what should be overridden
a) Create class Shape. The class should have a variable for the shape's area and perimeter. There should also be methods to "get" these variables. Also create a method called calculate, whose purpose is to calculate the appropriate values for both of these variable and then set them (do not create a "set" method for these variables).
b) Create classes Circle, Rectangle, and RightTriangle that each will inherit from class Shape, and class Square, decide if class Square should inherit from class Rectangle or class Shape. Include whatever additional variables that are appropriate to each of these shapes.
Create method bodies for any abstract methods that might have been inherited.
c) The constructors for these objects must take in some int value (which will determine the size of the shape). If that parameter (or parameters) are not positive (zero or negative), throw an IllegalNegativeArgumentException. (see part d). The String passed to the constructor of the exception class should be a meaningful description of the context in which the exception is being thrown.
d) Create class IllegalNegativeArgumentException, which should inherit from IllegalArgumentException.
e) Create method resetDimensions in each of these classes (hint, rely on inheritance). The method does not take in any parameters. It should generate a JOptionPane to ask the user what the new value (or values) should be for the variable (or variables) that determine the size of this object. Put the line of code that would convert the string into an int in a try/catch block to catch for a NumberFormatException. In the same try block, throw an IllegalNegativeArgumentExcpetion if the value is not positive. Catch here for this Exception too, but not in the same catch block that the NumberFormatException was caught. If either Exception is thrown, do not change the value (or values) of the variables. Do not ask the user to input again a new value. A JOptionPane should be created to inform the user of the type of error made in his input, and that no changes were made.
f) Create another class that has only a main method, call this class Assignment5. This method should create an array of type Shape, with room for 10 shape objects. Fill the array with a variety (at least one of each of the 4) of Shape objects. Then, loop through the array, calling calculate on each of the shape objects. Rely on polymorphism, do not determine what each object type is. Output (to the console or with a JOptionPane) the average area of all 10 shapes

Explanation / Answer

Hey, It all fit in 1 post this time...

Anyways, heres the updated version with very minor alterations. Read my comment to your original to get a full understanding.


import javax.swing.JOptionPane;

public class Assignment5{
    public static void main(String[] args)
    {
        Shape[] shapeArray = new Shape[10];
        shapeArray[0] = new Circle(5);
        shapeArray[1] = new Circle(1);
        shapeArray[2] = new Rectangle(3,8);
        shapeArray[3] = new RightTriangle(2,4,4.47);
       
        //Testing resetDimensions
        shapeArray[1].resetDimensions();
       
        double totalArea=0;
        for (int i=0; i<4; i++)
        {
            shapeArray[i].calculateArea();
            totalArea += shapeArray[i].getArea();
        }
        JOptionPane.showMessageDialog(null, "The average area of the shapes is "+totalArea/4.0);
    }
}

import java.util.Scanner;

public abstract class Shape {
    protected double area, perimeter;
    Scanner kb = new Scanner(System.in);
   
    public double getArea()
    {
        return area;
    }
    public double getPerimeter()
    {
        return perimeter;
    }
    public double calculateArea()
    {
        return 0;
    }
    public double calculatePerimeter()
    {
        return 0;
    }
   
    public void resetDimensions(){}

}

import javax.swing.JOptionPane;

public class Circle extends Shape {
    protected double radius;
   
    public Circle(double r){
        if (r<=0)
            throw new IllegalNegativeArgumentException();
        radius = r;
    }

    public double getArea() {
        return super.getArea();
    }

    public double getPerimeter() {
        return super.getPerimeter();
    }

    public double calculateArea() {
        area = radius *radius *3.14;
        return area;
    }

    public double calculatePerimeter() {
        perimeter = 2 * 3.14 * radius;
        return perimeter;
    }
   
    public void resetDimensions(){
        JOptionPane.showMessageDialog(null, "Please enter in new radius.");
        System.out.print("Enter new Radius here: ");
        String temp = kb.nextLine();
        try{
            radius = Double.parseDouble(temp);
        }
        catch (NumberFormatException e){
            System.out.println("Input error.");
            return;
        }
       
        try{
            new Circle(radius);
        }
        catch (NumberFormatException e){
            System.out.println(e.toString());
        }
    }
}

import javax.swing.JOptionPane;

public class Rectangle extends Shape {
    protected double height, width;
   
    public Rectangle(double h, double w){
        if (h<0 || w<0)
            throw new IllegalNegativeArgumentException();
        height = h;
        width = w;
    }

    public double getArea() {
        return super.getArea();
    }

    public double getPerimeter() {
        return super.getPerimeter();
    }

    public double calculateArea() {
        area = height * width;
        return area;
    }

    public double calculatePerimeter() {
        perimeter = 2* (height + width);
        return perimeter;
    }
   
   
    public void resetDimensions(){
        JOptionPane.showMessageDialog(null, "Please enter in new height then new width.");
        System.out.print("Enter new Height here: ");
        String tempHeight = kb.nextLine();
        System.out.print("Enter new Width here: ");
        String tempWidth = kb.nextLine();
        try{
            height = Double.parseDouble(tempHeight);
            width = Double.parseDouble(tempWidth);
        }
        catch (NumberFormatException e){
            System.out.println("Input error.");
            return;
        }
       
        try{
            new Rectangle(height,width);
        }
        catch (NumberFormatException e){
            System.out.println(e.toString());
        }
    }
}

import javax.swing.JOptionPane;

public class RightTriangle extends Shape {
    protected double a,b,c;
   
    public RightTriangle(double a, double b, double c){
        if (a<0 || b<0 || c<0)
            throw new IllegalNegativeArgumentException();
        this.a=a;
        this.b=b;
        this.c=c;
    }
   
    public double getArea() {
        return super.getArea();
    }

    public double getPerimeter() {
        return super.getPerimeter();
    }

    public double calculateArea() {
        area = 0.5 * (a*b);
        return area;
    }

    public double calculatePerimeter() {
        perimeter = a+b+c;
        return perimeter;
    }
   
    public void resetDimensions(){
        JOptionPane.showMessageDialog(null, "Please enter in new A, B, then C.");
        System.out.print("Enter new A here: ");
        String tempA = kb.nextLine();
        System.out.print("Enter new B here: ");
        String tempB = kb.nextLine();
        System.out.print("Enter new C here: ");
        String tempC = kb.nextLine();
        try{
            a = Double.parseDouble(tempA);
            b = Double.parseDouble(tempB);
            c = Double.parseDouble(tempC);
        }
        catch (NumberFormatException e){
            System.out.println("Input error.");
            return;
        }
       
        try{
            new RightTriangle(a,b,c);
        }
        catch (NumberFormatException e){
            System.out.println(e.toString());
        }
    }
}

public class IllegalNegativeArgumentException extends IllegalArgumentException {
    public String toString() {
        return "Parameters must be greater than 0.";
    }
}