ooo cricket 12:03 PM K Lab6Polymorphism.pdf variables and sets the inherited sha
ID: 3820451 • 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;
String getName()
{
return shapeName;
}
abstract double getArea();
}
======================================================================
Square.java
public class Square extends Shape{
double side;
Square(double a)
{
shapeName="Square";
if(a>=0)
{
side=a;
}
else
System.out.println("Side is less than 0");
}
@Override
double getArea() {
// TODO Auto-generated method stub
return side*side;
}
}
==========================================================================
Rectangle.java
public class Rectangle extends Shape{
double l,h;
Rectangle(double a,double b)
{
l=a;
h=b;
}
@Override
double getArea() {
// TODO Auto-generated method stub
return l*h;
}
}
======================================================================
Application.java
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape s[]=new Shape[2];
Scanner sc=new Scanner(System.in);
double a,l,h;
System.out.println("Input the side of square: ");
a=sc.nextDouble();
System.out.println("Input the length of rectangle: ");
l=sc.nextDouble();
System.out.println("Input the width of rectangle: ");
h=sc.nextDouble();
s[0]=new Square(a);
s[1]=new Rectangle(l,h);
System.out.println("The square has an area of "+s[0].getArea());
System.out.println("The Rectangle has an area of "+s[1].getArea());
}
}
=====================================================================
Output:
Input the side of square:
10.5
Input the length of rectangle:
2.5
Input the width of 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.