ooo cricket 12:03 PM K Lab6Polymorphism.pdf variables and sets the inherited sha
ID: 3822973 • Letter: O
Question
ooo cricket 12:03 PM K Lab6Polymorphism.pdf variables and sets the inherited shapeName variable to string "Rectangle". Ensure that the length and width are both greater than or equal to 0 G. The Rectangle class should also implement the getArea method of its abstract superclass; this implementation should compute the area of the rectangle and return the result. H. Write an application (driver class) that tests Square and Rectangle classes Create an array of type Shape that holds an instance of Square and an instance of Rectangle. The program should polymorphically compute and display the areas of both the objects. Allow a user to enter the values for the side of the square and the length and width of the rectangle Sample output: Input the side of the square 10.5 Input the length of the rectangle :2.5 Input the width of the rectangle 3.5 The Square has an area of 110.25 The Rectangle has an area of 8.75 Problem 2: Payroll System Modification Modify the payroll system (discussed in the class) to include an additional Employee subclass PieceWorker that represents an employee whose pay is based on the number of pieces of merchandise produced. Class PieceWorker should contain private instance variable wage to store the employee's wage per piece) and pieces to store the number of pieces produced). Provide a concrete implementation of method earnings in class PieceWorker that calculates the employee's earnings by multiplying the number of pieces produced by the wage per piece. Create an array of Employee variables to store references to objects of each concrete class in the new Employee hierarchy. For each Employee, display its string representation and earnings. CSC 115 Lab 5 Page 2 of 3 Calendar Notifications Courses MessagesExplanation / Answer
// Shape.java
public abstract class Shape {
protected String shapeName;
public String getName()
{
return shapeName;
}
abstract double getArea();
}
// Square.java
import javax.management.InvalidAttributeValueException;
public class Square extends Shape {
private double side;
public Square(double side) throws InvalidAttributeValueException
{
shapeName = "Square";
if (side <= 0)
{
throw new InvalidAttributeValueException("Side value should be greater than 0");
}
this.side = side;
}
@Override
double getArea() {
return side*side;
}
}
// Rectange.java
import javax.management.InvalidAttributeValueException;
public class Rectangle extends Shape {
private double length;
private double breadth;
public Rectangle(double length, double breadth) throws InvalidAttributeValueException
{
shapeName = "Rectangle";
if (length <= 0)
{
throw new InvalidAttributeValueException("Length value should be greater than 0");
}
if (breadth <= 0)
{
throw new InvalidAttributeValueException("Breadth value should be greater than 0");
}
this.length = length;
this.breadth = breadth;
}
@Override
double getArea() {
return length*breadth;
}
}
// TestShape.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestShape {
public static void main(String[] args)
{
List<Shape> shapes = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while(true)
{
try{
System.out.print("Input the side of the square: ");
double side = sc.nextDouble();
shapes.add(new Square(side));
break;
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
while(true)
{
try{
System.out.print("Input the length of the rectangle: ");
double length = sc.nextDouble();
System.out.print("Input the breadth of the rectangle: ");
double breadth = sc.nextDouble();
shapes.add(new Rectangle(length, breadth));
break;
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println();
System.out.println("The " + shapes.get(0).getName() + " has an area of " + shapes.get(0).getArea());
System.out.println("The " + shapes.get(1).getName() + " has an area of " + shapes.get(1).getArea());
sc.close();
}
}
// output
Input the side of the square: 10.5
Input the length of the rectangle: 2.5
Input the breadth of the rectangle: 3.5
The Square has an area of 110.25
The Rectangle has an area of 8.75
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.