Create an abstract class PizzaWorker Fields: name, pay, hours; Constructor that
ID: 640061 • Letter: C
Question
Create an abstract class PizzaWorker
Fields: name, pay, hours;
Constructor that requires a name as a parameter.
Two abstract methods: getPay(), getInfo()
Two child classes: Cook and Driver
Cook constructor will have two parameters, the name and the hours
The pay rate for a cook is $15 an hour
The getInfo method will display the name, the pay rate, the hours worked and the final pay
The Driver constructor will have three parameters, the name, the hours and the mileage
It will have an additional field for miles
The getinfo method will display the name, pay rate, hours worked, mileage and final pay
The pay rate for a driver is $10 an hour
The mileage is .50 per mile
Here is what you need to test your code:
public class DemoPizzaWorkers {
public static void main(String args[]){
Driver aDriver = new Driver("Joe",40,70);
Cook aCook = new Cook("John",40);
aDriver.getInfo();
aCook.getInfo();
PizzaWorker workers[] = new PizzaWorker[5];
workers[0] = new Driver("Driver 1",35,60);
workers[1] = new Driver("Driver 2",20,70);
workers[2] = new Cook("Cook 1",45);
workers[3] = new Cook("Cook 2",40);
workers[4] = new Driver("Driver 3", 20,10);
for (int i=0; i < workers.length; i++)
workers[i].getInfo();
}
}
Create an abstract class PizzaWorker
Fields: name, pay, hours;
Constructor that requires a name as a parameter.
Two abstract methods: getPay(), getInfo()
Two child classes: Cook and Driver
Cook constructor will have two parameters, the name and the hours
The pay rate for a cook is $15 an hour
The getInfo method will display the name, the pay rate, the hours worked and the final pay
The Driver constructor will have three parameters, the name, the hours and the mileage
It will have an additional field for miles
The getinfo method will display the name, pay rate, hours worked, mileage and final pay
The pay rate for a driver is $10 an hour
The mileage is .50 per mile
Here is what you need to test your code:
public class DemoPizzaWorkers {
public static void main(String args[]){
Driver aDriver = new Driver("Joe",40,70);
Cook aCook = new Cook("John",40);
aDriver.getInfo();
aCook.getInfo();
PizzaWorker workers[] = new PizzaWorker[5];
workers[0] = new Driver("Driver 1",35,60);
workers[1] = new Driver("Driver 2",20,70);
workers[2] = new Cook("Cook 1",45);
workers[3] = new Cook("Cook 2",40);
workers[4] = new Driver("Driver 3", 20,10);
for (int i=0; i < workers.length; i++)
workers[i].getInfo();
}
}
Explanation / Answer
//Abstract class PizzaWorker
//PizzaWorker.java
public abstract class PizzaWorker
{
//create protected fields
//of clas PizzaWorker
//Protected can be accessible be to
//inherited classes
protected String name;
protected int pay;
protected int hours;
//Constructor to set name,pay nd hours
public PizzaWorker(String name,int pay,int hours)
{
this.name=name;
this.pay=pay;
this.hours=hours;
}
//abstract metods for getPay and getInfo
public abstract double getPay();
public abstract String getInfo();
}
------------------------------------------------------------------
//Cook implments the PizzaWorker class
public class Cook extends PizzaWorker
{
public Cook(String name,int pay,int hours)
{
super(name,pay,hours);
}
//Returns the fina pay of Cook
public double getPay()
{
return pay*hours;
}
@Override
public String getInfo() {
return "Name:"+name+
" Pay Rate: "+pay+
" Hours Worked: "+hours+
" Final Pay: "+getPay();
}
}
----------------------------------------------------------------
public class Driver extends PizzaWorker
{
private double miles;
//Parameterized constructor with nam,pay,hours and miles
public Driver(String name, int pay, int hours,double miles)
{
super(name, pay, hours);
this.miles=miles;
}
//Returns the fina pay of Driver
public double getPay()
{
return pay*hours;
}
public String getInfo()
{
return "Name:"+name+
" Pay Rate: "+pay+
" Hours Worked: "+hours+
" Mileage: "+miles+
" Final Pay: "+getPay();
}
}
------------------------------------------------------------------------------
//Demo program that tests the classes Cook and Driver
//and calls the getInfo method
//DemoPizzaWorkers.java
public class DemoPizzaWorkers
{
public static void main(String args[])
{
//Cook object with name ,pay rate and hours
Cook aCook = new Cook("John",15,40);
//call getInfo method
System.out.println(aCook.getInfo());
//Cook object with name ,pay rate and hours and miles
Driver aDriver = new Driver("Joe",10,40,0.50);
//call getInfo method
System.out.println(aDriver.getInfo());
//Create an arrya of abstract class PizzaWorker of size 5
PizzaWorker workers[] = new PizzaWorker[5];
//set Driver and Cook objects to the PizzaWorker
workers[0] = new Driver("Driver 1",35,60,0.50);
workers[1] = new Driver("Driver 2",20,70,0.50);
workers[2] = new Cook("Cook 1",45,5);
workers[3] = new Cook("Cook 2",40,6);
workers[4] = new Driver("Driver 3", 20,10,.50);
//call getInfor on Workers obeject
for (int i=0; i < workers.length; i++)
System.out.println(workers[i].getInfo());
}
}
------------------------------------------------------------------------------------
Sample output:
Name:John
Pay Rate: 15
Hours Worked: 40
Final Pay: 600.0
Name:Joe
Pay Rate: 10
Hours Worked: 40
Mileage: 0.5
Final Pay: 400.0
Name:Driver 1
Pay Rate: 35
Hours Worked: 60
Mileage: 0.5
Final Pay: 2100.0
Name:Driver 2
Pay Rate: 20
Hours Worked: 70
Mileage: 0.5
Final Pay: 1400.0
Name:Cook 1
Pay Rate: 45
Hours Worked: 5
Final Pay: 225.0
Name:Cook 2
Pay Rate: 40
Hours Worked: 6
Final Pay: 240.0
Name:Driver 3
Pay Rate: 20
Hours Worked: 10
Mileage: 0.5
Final Pay: 200.0
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.