Exercise P10.5
Make a class Employee with a name and salary. Make a class Manager inherit from Employee. Add an instance field, named department, of type String. Supply a method toString that prints the manager's name, department, and salary. Make a class Executive inherit from Manager. Supply appropriate toString methods for all classes. Supply a test program that tests these classes and methods.
Make a class Employee with a name and salary. Make a class Manager inherit from Employee. Add an instance field, named department, of type String. Supply a method toString that prints the manager's name, department, and salary. Make a class Executive inherit from Manager. Supply appropriate toString methods for all classes. Supply a test program that tests these classes and methods.
Use the following class as your tester class:
/**
This program tests the Employee class and its subclasses.
*/
public class EmployeeTester
{
public static void main(String[] args)
{
Employee e = new Employee(''Edgar'', 65000);
Manager m = new Manager(''Mary'', 85000, ''Engineering'');
Executive v = new Executive(''Veronica'', 105000, ''Engineering'');
System.out.println(e);
System.out.println(''Expected: Employee[name=Edgar,salary=65000.0]'');
System.out.println(m);
System.out.println(''Expected: Manager[super=Employee[
name=Mary,salary=85000.0],department=Engineering]'');
System.out.println(v);
System.out.println(''Expected: Executive[super=Manager[
super=Employee[name=Veronica,salary=105000.0],
department=Engineering]]'');
}
}
Exercise P10.6
Write a superclass Worker and subclasses HourlyWorker and SalariedWorker. Every worker has a name and a salary rate. Write a method computePay(int hours) that computes the weekly pay for every worker. An hourly worker gets paid the hourly wage for the actual number of hours worked, if hours is at most 40. If the hourly worker worked more than 40 hours, the excess is paid at time and a half. The salaried worker gets paid the hourly wage for 40 hours, no matter what the actual number of hours is. Supply a test program that uses polymorphism to test these classes and methods.
Use the following class as your tester class:
/**
This class tests class Worker and its subclasses.
*/
public class WorkerTester
{
public static void main(String[] args)
{
Worker s = new SalariedWorker(''Sally'', 40);
Worker h = new HourlyWorker(''Harry'', 40);
System.out.println(s.computePay(30));
System.out.println(''Expected: 1600'');
System.out.println(h.computePay(30));
System.out.println(''Expected: 1200'');
System.out.println(s.computePay(50));
System.out.println(''Expected: 1600'');
System.out.println(h.computePay(50));
System.out.println(''Expected: 2200'');
}
}
Due February 27, 2009
No comments:
Post a Comment