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:
Inheritance
Exercise:
Programming Projects
Question:4 | ISBN:9780132830317 | Edition: 5

Question

Define a class called Administrator, which is a derived class of the class SalariedEmployee in Display 7.5 You are to supply the following additional instance variables and methods:

  • An instance variable of type String that contains the administrator’s title (such as "Director" or "Vice President" ).

  • An instance variable of type String that contains the administrator’s area of

  • responsibility (such as "Production" , "Accounting" , or "Personnel" ).

  • An instance variable of type String that contains the name of this administrator’s immediate supervisor.

  • Suitable constructors, and suitable accessor and mutator methods.

  • A method for reading in an administrator’s data from the keyboard.

Override the definitions for the methods equals and toString so they are app- ropriate to the class Administrator.

Also, write a suitable test program.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

NOTE: Refer the textbook for the classes SalariedEmployee, Employee, and Date

// Administrator.java
import java.util.Scanner;
public class Administrator extends SalariedEmployee
{
	private String title;
	private String responsibility;
	private String supervisor;
	
	public Administrator()
	{
		super();
		this.title = "";
		this.responsibility = "";
		this.supervisor = "";
	}
		
	public Administrator(String name, Date date, double salary, 
			String title, String responsibility, String supervisor)
	{
		super(name, date, salary);
		this.title = title;
		this.responsibility = responsibility;
		this.supervisor = supervisor;
	}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	public String getResponsibility()
	{
		return responsibility;
	}

	public void setResponsibility(String responsibility)
	{
		this.responsibility = responsibility;
	}

	public String getSupervisor()
	{
		return supervisor;
	}

	public void setSupervisor(String supervisor)
	{
		this.supervisor = supervisor;
	}
	
	public void readData()
	{
		Scanner keyboard = new Scanner(System.in);
		
		System.out.print("Enter the name of administrator: ");
		String name = keyboard.nextLine();
		
		System.out.print("Enter the salary of administrator: ");
		double salary = Double.parseDouble(keyboard.nextLine());
		
		System.out.print("Enter valid month of hire date: ");
		int month = Integer.parseInt(keyboard.nextLine());
		
		System.out.print("Enter valid day of hire date: ");
		int day = Integer.parseInt(keyboard.nextLine());
		
		System.out.print("Enter valid year of hire date: ");
		int year = Integer.parseInt(keyboard.nextLine());
		
		setName(name);
		setSalary(salary);
		setHireDate(new Date(month, day, year));
				
		System.out.print("Enter title of administrator: ");		
		title = keyboard.nextLine();
		
		System.out.print("Enter responsibility of administrator: ");
		responsibility = keyboard.nextLine();
		
		System.out.print("Enter supervisor for administrator: ");
		supervisor = keyboard.nextLine();
		System.out.println();
	}
	
	public boolean equals(Administrator other)
	{
		return super.equals(other) &&(title.equals(other.title) 
				&& responsibility.equals(other.responsibility) 
				&& supervisor.equals(other.supervisor)); 
	}
	
	public String toString()
	{
		return super.toString() + "\n" + title + ", "  
				+ responsibility + ", supervised by " + supervisor;
	}	
}
// SalariedEmployee.java
public class SalariedEmployee extends Employee
{
	// please refer the textbook for the code of this SalariedEmployee class
}
// Employee.java
public class Employee
{
	// please refer the textbook for the code of this Employee class
}
// Date.java
import java.util.Scanner;
public class Date
{
	// please refer the textbook for the code of this Date class
}
// AdministratorTest.java
public class AdministratorTest
{
	public static void main(String[] args)
	{
		Administrator admin1 = new Administrator();
		Administrator admin2 = new Administrator();
		
		admin1.readData();
		admin2.readData();
		
		System.out.println("Administrator 1:");
		System.out.println(admin1);
		
		System.out.println("\nAdministrator 2:");
		System.out.println(admin2);
	}
}

Output:

Enter the name of administrator: John Smith
Enter the salary of administrator: 20000
Enter valid month of hire date: 10
Enter valid day of hire date: 10
Enter valid year of hire date: 2010
Enter title of administrator: Vice President
Enter responsibility of administrator: Accounting
Enter supervisor for administrator: Steven Steve

Enter the name of administrator: Charles Chars
Enter the salary of administrator: 15000
Enter valid month of hire date: 5
Enter valid day of hire date: 5
Enter valid year of hire date: 2012
Enter title of administrator: Director
Enter responsibility of administrator: Production
Enter supervisor for administrator: Mikhale Mike

Administrator 1:
John Smith October 10, 2010
$20000.0 per year
Vice President, Accounting, supervised by Steven Steve

Administrator 2:
Charles Chars May 5, 2012
$15000.0 per year
Director, Production, supervised by Mikhale Mike
1 0

Discussions

Post the discussion to improve the above solution.