1) Inheritance: You must determine what (if anything) should be abstract, what s
ID: 3540326 • 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. you did not create class square in part b .
Explanation / Answer
Hey, its me again. Sorry I didn%u2019t do the class Square; I totally missed it! Here%u2019s the missing class I owe you along with the updated class that tests Square. Also, there is no simpler way of getting a user%u2019s input other than Scanner, so I believe your teacher wanted you to learn it.
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);
shapeArray[4] = new Square(7);
//Testing resetDimensions
shapeArray[1].resetDimensions();
double totalArea=0;
for (int i=0; i<5; i++)
{
shapeArray[i].calculateArea();
totalArea += shapeArray[i].getArea();
}
JOptionPane.showMessageDialog(null, "The average area of the shapes is "+totalArea/5.0);
}
}
import javax.swing.JOptionPane;
public class Square extends Shape {
protected double side;
public Square(double s){
side=s;
}
public double getArea() {
return super.getArea();
}
public double getPerimeter() {
return super.getPerimeter();
}
public double calculateArea() {
return side*side;
}
public double calculatePerimeter() {
return side*4;
}
public void resetDimensions() {
JOptionPane.showMessageDialog(null, "Please enter in new square side.");
System.out.print("Enter new side here: ");
String tempSide = kb.nextLine();
try{
side = Double.parseDouble(tempSide);
}
catch (NumberFormatException e){
System.out.println("Input error.");
return;
}
try{
new Square(side);
}
catch (NumberFormatException e){
System.out.println(e.toString());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.