CS 160

Fall 2007
September 24, 2007
Old Test Question Example
(20 points)Implement a class Employee with the following properties. An employee has a certain wage per hour and a timecard that holds the hours the employee has worked since the last payday. The wage per hour is specified in the constructor, and the timecard’s initial value is 0. Supply a method work that simulates the employee working a certain amount of hours, which will increase the value of the timecard. Also you will need provide the methods for returning the timecard current value, and payday, a method that will print the amount owed to the employee and reset the timecard. Here is a sample usage of creating an instance of the Employee class and calling the methods, this is what I would write if I was writing a program that uses the class you write from the paragraph above. (sample):

Employee e1 = new Employee (5.25);
e1.work(5);
e1.work(7);
System.out.println(e1.getHours());
System.out.println(e1.getPay());

Write your class here:





































 
public class Employee

{

                private double wage;

                private double hours;

                

                public Employee(double w)

                {

                    wage = w;

                    hours = 0;

                }

                

                public void work(double h)

                {

                    hours += h;   

                }

                public double getHours()

                {

                    return hours;

                }

                public double getPay()

                {

                    return hours * wage;

                }

                public double actuallyPay()

                {

                    double pay = wage*hours;

                    hours = 0;

                    return pay;

                }

}