
Create a Faculty subclass of Employee which has the additional data of rank and expertise. Rank could be one of Assistant Professor, Associate Professor, or Professor. Expertise would be a String such as ?Computer Science?, ?Music?, ?History? etc. Create a Staff subclass of Employee which has the additional data of Category (Full or Part time). Provide suitable constructors for your classes. Each of these sub classes must have a changestatus() method that changes the rank or category respectively. For example, an Associate Professor could be promoted to Professor; a Full time classified could be demoted to part time. Add a changestatus() method to the Employee base class that simply outputs a message ?Cannot change this Employee?s status?.
Demonstrate dynamic binding in your main program. Declare an array of Employees (compile time type) but assign different subclasses to each array element (run time type). Show that getinfo() and changestatus() uses the methods determined by their run time types and displays all the relevant data.
The previous java program is this,
import java.util.*;
class EmployeeSalaries {
static Scanner input = new Scanner(System.in);
String name;
static int amt,sal=0;
EmployeeSalaries(String name, int sal) {
this.name = name;
this.sal = sal;
}
public void setName(String name){
this.name=name;
}
public static void setSal(int sal){
sal=sal;
}
static int increase() {
System.out.print("Enter amount to increase:");
amt = input.nextInt();
if (amt < 0) {
System.out.println("Invalid Amount");
return 1;
}
sal = sal + amt;
return 0;
}
static int decrease() {
System.out.println("Your Balance=" + sal);
System.out.print("Enter amount to decrease:");
amt = input.nextInt();
if (sal < amt) {
System.out.println("Not sufficient balance.");
return 1;
}
if (amt < 0) {
System.out.println("Invalid Amount");
return 1;
}
sal = sal - amt;
return 0;
}
void getInfo() {
System.out.println("Name:" + name);
System.out.println("Balance:" + sal);
}
void quit() {
EmployeeSalaries.setSal(0);
}
void salarychange(){
int menu;
System.out.println("1. Increase Salary Amount");
System.out.println("2. Decrease Salary Amount");
boolean quit = false;
System.out.print("Please enter your choice: ");
menu = input.nextInt();
switch (menu) {
case 1:
EmployeeSalaries.increase();
break;
case 2:
EmployeeSalaries.decrease();
break;
}
}
public static void main(String args[]) {
System.out.println("Enter your Name: ");
String nn = input.nextLine();
System.out.println("Enter Initial Salary: ");
int sal = input.nextInt();
EmployeeSalaries b1 = new EmployeeSalaries(nn,sal);
b1.salarychange();
b1.getInfo();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.