CSc 127A Quiz Section Leader __________ Your Name



Quiz: Chapter 4 Answer at end Name ___________________________

Implement a class named WeeklyEmployee. Each instance of WeeklyEmployee must keep track of the employee's name, the hourly rate of pay, and the number of hours worked during the current week. Each instance of WeeklyEmployee must be able to compute its own gross pay, which is defined as (hours worked * hourly rate of pay). A programmer must be able to set the hours worked each week since this changes from week to week. Because BogusBurgers' time clock measures hours worked in tenths of an hour, hours worked must have a fractional part (could make this a double). Implement the constructor a toString method to show the name and dollars per hour, a getName method to return the name, a getGrossPay method to return the gross pay, and a setHours method to change the hours worked. The Java code the left must generate the output to the right (use the back of this page if necessary).

|// This employee worked 30.0 hours at $10.00 per hour | |

|WeeklyEmployee anEmp = new WeeklyEmployee( "Bob Berger", 30.0, 10.00 ); |Output: |

| | |

|System.out.println(anEmp.toString()); |Bob Berger: $10.0 |

| |Bob Berger grossed 300.0 |

|System.out.println(anEmp.getName() + " grossed " + anEmp.getGrossPay()); |Bob Berger grossed 400.0 |

| | |

|anEmp.setHours( 40.0 ); | |

| | |

|System.out.println(anEmp.getName() + " grossed " + anEmp.getGrossPay()); | |

Answer

public class WeeklyEmployee

{

private String my_name;

private double my_hours;

private double my_rate;

public WeeklyEmployee( String initName, double initHours, double initRate )

{ // Five argument constructor

my_name = initName;

my_hours = initHours;

my_rate = initRate;

}

public void setHours(double thisWeeksHours)

{ // post: set the hours worked for a given week

my_hours = thisWeeksHours;

}

public String getName( )

{

return my_name;

}

public double getGrossPay( )

{

return my_hours * my_rate;

}

public String toString( )

{

return my_name + " $" + my_rate;

}

}

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download