SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Interfaces And Inner Classes
Exercise:
Programming Projects
Question:9 | ISBN:9780132830317 | Edition: 5

Question

Redo the class Employee and the class HourlyEmployee in Displays 7.2 and 7.3 so that the class Date is an inner class of the class Employee and an inherited inner class of the class HourlyEmployee. Also, do a suitable test program.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

NOTE: Please refer the textbook for the classes Employee, Date, and HourlyEmployee

// Employee.java
import java.util.Scanner;
public class Employee
{
	// inner class
	public class Date
	{
		// Refer the textbook for the code of this Date class
	}

	private String name;
	private Date hireDate;

	// Refer the textbook for the code of this Employee class
}
// HourlyEmployee.java
public class HourlyEmployee extends Employee
{
	// Refer the textbook for the code of this HourlyEmployee class
}

 


// TestHourlyEmployee.java
public class TestHourlyEmployee
{
	public static void main(String[] args)
	{
		Employee employee1 = new Employee();
		Employee.Date date1 = employee1.new Date();

		HourlyEmployee hourlyEmployee1 = new HourlyEmployee(
				"John Smith", date1, 45.45, 172);

		HourlyEmployee hourlyEmployee2 = new HourlyEmployee();
		hourlyEmployee2.setName("Steven Lee");
		hourlyEmployee2.setHireDate(employee1.new Date(4, 28,
				2015));
		hourlyEmployee2.setRate(50.50);
		hourlyEmployee2.setHours(168);

		System.out.println("Hourly Employee #1");
		System.out.println(hourlyEmployee1);
		System.out.printf("Total pay: $%.2f\n", hourlyEmployee1.getPay());

		System.out.println("\nHourly Employee #2");
		System.out.println(hourlyEmployee2);
		System.out.printf("Total pay: $%.2f\n", hourlyEmployee2.getPay());
	}
}

Output:

Hourly Employee #1
John Smith January 1, 1000
$45.45 per hour for 172.0 hours
Total pay: $7817.40

Hourly Employee #2
Steven Lee April 28, 2015
$50.5 per hour for 168.0 hours
Total pay: $8484.00

 

0 0

Discussions

Post the discussion to improve the above solution.