You need to create two classes. FastFoodWorker in FastFoodWorker.java Create fie
ID: 640087 • Letter: Y
Question
You need to create two classes.
FastFoodWorker in FastFoodWorker.java
Create fields for name, hours and pay
Create get methods: getName(), getHours() and getPay()
Create set methods: setName(), setHours()
Set the pay field when the hours are set. Pay is $10 an hour
Create a child class Cashier in Cashier.java
Create field: register and the methods to set and get this field
Override the parent's setHours() method to set the pay to $12 an hour.
Below is the code I will use to test your programs. Copy this code and create a DemoFastFoodWorker.java program.
public class DemoFastFoodWorkers {
public static void main(String args[]){
FastFoodWorker aWorker = new FastFoodWorker();
Cashier aCashier = new Cashier();
aWorker.setName("Lisa");
aWorker.setHours(10.0);
aCashier.setName("Jane");
aCashier.setHours(10);
aCashier.setRegister(30);
System.out.println(aWorker.getName() + " worked " + aWorker.getHours() + " hours. Pay is $" + aWorker.getPay());
System.out.println(aCashier.getName() + " worked " + aCashier.getHours() + " hours on Register " + aCashier.getRegister() + ". Pay is $" + aCashier.getPay());
}
}
You need to create two classes.
FastFoodWorker in FastFoodWorker.java
Create fields for name, hours and pay
Create get methods: getName(), getHours() and getPay()
Create set methods: setName(), setHours()
Set the pay field when the hours are set. Pay is $10 an hour
Create a child class Cashier in Cashier.java
Create field: register and the methods to set and get this field
Override the parent's setHours() method to set the pay to $12 an hour.
Below is the code I will use to test your programs. Copy this code and create a DemoFastFoodWorker.java program.
public class DemoFastFoodWorkers {
public static void main(String args[]){
FastFoodWorker aWorker = new FastFoodWorker();
Cashier aCashier = new Cashier();
aWorker.setName("Lisa");
aWorker.setHours(10.0);
aCashier.setName("Jane");
aCashier.setHours(10);
aCashier.setRegister(30);
System.out.println(aWorker.getName() + " worked " + aWorker.getHours() + " hours. Pay is $" + aWorker.getPay());
System.out.println(aCashier.getName() + " worked " + aCashier.getHours() + " hours on Register " + aCashier.getRegister() + ". Pay is $" + aCashier.getPay());
}
}
Explanation / Answer
//FastFoodWorker.java
public class FastFoodWorker
{
private String name;
//set access specifier protectd to access in the Cashier class
protected int hours ;
protected int pay;
public FastFoodWorker()
{
name="";
hours=0;
pay=0;
}
//setter metods for name and hours
public void setName(String name)
{
this.name=name;
}
public void setHours(int hours)
{
this.hours=hours;
//set pay to 10$
pay=hours*10;
}
//Getter methods for name ,hours
//and pay
public String getName()
{
return name;
}
//Returns hours
public int getHours()
{
return hours;
}
//Returns pay
public int getPay()
{
return pay;
}
}
---------------------------------------------------------
//Cashier.java
public class Cashier extends FastFoodWorker
{
private int register;
//constructor of the class Cashier
public Cashier()
{
register=0;
}
//sets the register vlaue
public void setRegister(int register)
{
this.register=register;
}
//Returns the register value
public int getRegister()
{
return register;
}
//Override the super class setHours method
//and set pay 12 dollars
@Override
public void setHours(int hours)
{
super.setHours(hours);
//set super class pay value 12$ per hour
pay=hours*12;
}
}
----------------------------------------------------------------
//Demo class that tests the FastFoodWorker and Cashier
//and test the metods and print results of each method calling
public class DemoFastFoodWorkers
{
public static void main(String args[])
{
//Create an instantiate object FastFoodWorker
FastFoodWorker aWorker = new FastFoodWorker();
//set name and hours and register
aWorker.setName("Lisa");
aWorker.setHours(10);
//print name ,hours and pay value
System.out.println(aWorker.getName() + " worked " +
aWorker.getHours() + " hours. Pay is $" + aWorker.getPay());
//Create an instantiate Cashier object
Cashier aCashier = new Cashier();
//set name and hours and register
aCashier.setName("Jane");
aCashier.setHours(10);
aCashier.setRegister(30);
//print name ,hours and register
System.out.println(aCashier.getName() + " worked " +
aCashier.getHours() + " hours on Register " +
aCashier.getRegister() + ". Pay is $" + aCashier.getPay());
}
}
------------------------------------------------------------------------------
Sample output:
Lisa worked 10 hours. Pay is $100
Jane worked 10 hours on Register 30. Pay is $120
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.