Write a program that is able to store an employee’s information. An employee can
ID: 3755222 • Letter: W
Question
Write a program that is able to store an employee’s information. An employee can have a first name, last name, address and a boss. It is possible for an employee to not have a boss. Employee class (All variables are private) fname String lname String boss Employee Addr Address Employee(fname,lname,boss, city, state) getName() getAddress() setBoss(Employee object) getBoss() Address class (All variables are private) city String state String Address(city,state) getAddress() Driver class Create two employee objects. You can hardcode the employee data. User input not necessary For the first employee, there is no boss which means that in the constructor you would pass in null (Use the keyword null) For the second employee, the boss will be the first employee; Display all the employees’ information. You will assume that you have no idea which employee has a boss and which does not. So when displaying information, you have to check if the instance variable, boss, is set to null or not. Only if it is not set to null do you want to display the boss’s information which would be the first name and last name of the boss. If there is no boss, then you don’t display any boss information Example: 1st employee : John Jones lives in Sacramento California and boss is null (Does not have a boss) 2nd employee James bond lives in Davis California and his boss is john jones (point to the first employee)
Explanation / Answer
Employee class:
public class Employee {
//instance variables
private String fname,lname;
private Employee boss;
private Address address;
//constructor
Employee(String fname,String lname,Employee boss,String city,String state)
{
this.fname=fname;
this.lname=lname;
this.boss=boss;
address = new Address(city,state);
}
//getter and setter methods
public String getName() {
return fname+ " "+ lname;
}
public String getAddress() {
return address.city+" "+address.state;
}
public void setBoss(Employee object) {
this.boss=object;
}
public String getBoss() {
String result=null;
if(this.boss!=null) {
result= boss.fname+" "+boss.lname;
}
return result;
}
}
Address class:
public class Address
{
//instance variables
public String city,state;
//constructor
Address(String city,String state)
{
this.city=city;
this.state=state;
}
//getter method
public String getAddress() {
return city+" "+state;
}
}
Test class or Drive class:
public class TestEmployeeApp {
//main method to run application
public static void main(String[] args)
{
//creating first employee object
Employee emp1 = new Employee("John","Johnes",null,"Sacramento","California");
//displaying the first employee inform
System.out.println("Employee Name:"+ emp1.getName());
System.out.println("Employee Address(City & State):"+ emp1.getAddress());
//checking whether the employee has boss or not
System.out.println("Employee Boss::"+ emp1.getBoss());
System.out.println(" ");
//creating second employee object
Employee emp2 = new Employee("James","bond",emp1,"Davis","California");
//displaying the first employee inform
System.out.println("Employee Name:"+ emp2.getName());
System.out.println("Employee Address(City & State):"+ emp2.getAddress());
System.out.println("Employee Boss::"+ emp2.getBoss());
}
}
Output:
Employee Name:John Johnes
Employee Address(City & State):Sacramento California
Employee Boss::null
Employee Name:James bond
Employee Address(City & State):Davis California
Employee Boss::John Johnes
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.