In: Computer Science
We have to solve only problem 2, I'm sharing Problem 1 only for reference purpose.
Problem 1. (25 points)
Alice is going to create a notebook including the profile of all
her friends. For each of her friends, she would
like to keep first-name, last-name, cell number, and birthdate
(month and day). At the beginning, she doesn’t
know how many spaces she should reserve for recording her friends,
so she gradually inputs her friends’
information into it.
• Please help her by first creating a Java class, called Person,
with the required information from
description above, along with any required setter/getter methods.
(5 points)
• Then create, a Java class, called FriendsList, that keeps the
list of some persons as someone’s
friends, using the Person class above, and performs the required
operations, such as add new friends,
delete/ modify an existing one, return the list of her friends
sorted by last-names, report the number of
her friends, report the list of her friends who born in a given
month sorted by their day of birth, and
report the list of her friends who born in a given day of a month,
sorted by their last-names. She also
needs to get the cell number of a friend by giving her/his
first-name and last-name. (10 points)
• Create a tester class, called MyFriends, to test the FriendList
class by creating a new object of this
class, adding some friends, modifying and deleting some existing
friends, retrieving and printing all
friends, retrieving and printing the list of friends who born in a
given month, retrieving and printing
the list of friends who born in a given day of a month, and finding
the cell number of an existing
friend by giving her/his first-name and last-name. Also, create a
separate copy of the FriendList
object you have already created, and remove all friends with
first-name “Shane” from the new object.
(10 points)
Java file names: Person.Java, FriendList.java, and
MyFriends.java.
Problem 2. (40 points)
In this problem, we are going to reuse the Person class created in
the previous problem. Therefore, first make
2
sure that you have already created Person.java based on the
description provided above.
• Create a subclass of Person, called Employee, such that it can
keep extra information of a person
including year of hiring, annual salary, vacation days, and unused
vacation days. Provide required
setter/getter methods for this class. (10 points)
• Create a subclass of Employee, called Manager, which also has
monthly bonus, and the list of all
her/his employees. Provide required setter/getter methods for this
class. (10 points)
• Create a class, called Company, with the following information:
name of the company, starting year,
and the list of all its employees/managers. Provide required
setter/getter methods for this class. (10
points)
• Create a tester class, called MyCompany, in which you should test
all the above classes by creating a
company, adding some employees/managers into it. (10 points)
• Bonus: Provide two listings of the company’s employees/managers,
one sorted by last-names, and
one sorted by each manager and then her/his employees. (5
points)
Java file names: Person.Java, Employee.java, Manager.java,
Company.java, and MyCompany.java.
Short Summary:
**************Please do upvote to appreciate our time. Thank you!******************
Source Code:
Person.java
//This class holds person details
public class Person implements Comparable<Person>{
//Instance variables
private String firstName;
private String lastname;
private String cellnumber;
private String birthdate ;
//Constructor
public Person(String firstName, String lastname,
String cellnumber, String birthdate) {
super();
this.firstName = firstName;
this.lastname = lastname;
this.cellnumber = cellnumber;
this.birthdate = birthdate;
}
//Compare using lastname
@Override
public int compareTo(Person arg0) {
return this.lastname.compareTo(arg0.lastname);
}
@Override
public String toString() {
return "firstName=" + firstName +
", lastname=" + lastname + ", cellnumber=" + cellnumber
+ ", birthdate=" + birthdate ;
}
//Getters
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getCellnumber() {
return cellnumber;
}
public void setCellnumber(String cellnumber) {
this.cellnumber = cellnumber;
}
public String getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
}
Employee.java
package com.core.java;
//This class extends person
public class Employee extends Person {
//Instance variables
private int yearOfHiring;
private double annualSalary;
private int vacationDays;
private int unusedVacationDays;
//Constructor
public Employee(String firstName, String lastname,
String cellnumber, String birthdate,int yearOfHiring, double
annualSalary, int vacationDays, int unusedVacationDays) {
super(firstName, lastname,
cellnumber, birthdate);
this.yearOfHiring =
yearOfHiring;
this.annualSalary =
annualSalary;
this.vacationDays =
vacationDays;
this.unusedVacationDays =
unusedVacationDays;
}
@Override
public String toString() {
return "Employee
"+super.toString()+",yearOfHiring=" + yearOfHiring + ",
annualSalary=" + annualSalary + ", vacationDays="
+ vacationDays + ", unusedVacationDays=" +
unusedVacationDays+"\n" ;
}
//Getters & setters
public int getYearOfHiring() {
return yearOfHiring;
}
public void setYearOfHiring(int yearOfHiring) {
this.yearOfHiring =
yearOfHiring;
}
public double getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(double annualSalary)
{
this.annualSalary =
annualSalary;
}
public int getVacationDays() {
return vacationDays;
}
public void setVacationDays(int vacationDays) {
this.vacationDays =
vacationDays;
}
public int getUnusedVacationDays() {
return unusedVacationDays;
}
public void setUnusedVacationDays(int
unusedVacationDays) {
this.unusedVacationDays =
unusedVacationDays;
}
}
Manager.java
package com.core.java;
import java.util.List;
//This class extends Employee class
public class Manager extends Employee{
//Instance variables
private double monthlyBonus;
private List<Employee> employeeList;
//Constructor
public Manager(String firstName, String lastname,
String cellnumber, String birthdate,int yearOfHiring, double
annualSalary, int vacationDays, int unusedVacationDays, double
monthlyBonus,
List<Employee> employeeList) {
super(firstName, lastname,
cellnumber, birthdate,yearOfHiring, annualSalary, vacationDays,
unusedVacationDays);
this.monthlyBonus =
monthlyBonus;
this.employeeList =
employeeList;
}
@Override
public String toString() {
return "Manager
"+super.toString()+" [monthlyBonus=" + monthlyBonus + ",
employeeList=" + employeeList + "]";
}
//Getters & setters
public double getMonthlyBonus() {
return monthlyBonus;
}
public void setMonthlyBonus(double monthlyBonus)
{
this.monthlyBonus =
monthlyBonus;
}
public List<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<Employee>
employeeList) {
this.employeeList =
employeeList;
}
}
Company.java
package com.core.java;
import java.util.List;
//This class holds the company details
public class Company {
//Instance variables
private String companyName;
private int startingYear;
private List<Employee> employeeList;
private List<Manager> managerList;
//Constructor
public Company(String companyName, int startingYear,
List<Employee> employeeList, List<Manager> managerList)
{
super();
this.companyName =
companyName;
this.startingYear =
startingYear;
this.employeeList =
employeeList;
this.managerList =
managerList;
}
@Override
public String toString() {
return "Company [companyName=" +
companyName + ", startingYear=" + startingYear + ",
employeeList="
+ employeeList + ", managerList=" + managerList
+ "]";
}
//Getters and setters
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName =
companyName;
}
public int getStartingYear() {
return startingYear;
}
public void setStartingYear(int startingYear) {
this.startingYear =
startingYear;
}
public List<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<Employee>
employeeList) {
this.employeeList =
employeeList;
}
public List<Manager> getManagerList() {
return managerList;
}
public void setManagerList(List<Manager>
managerList) {
this.managerList =
managerList;
}
}
MyCompany.java
package com.core.java;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//This class operates on Person,employee,manager and Company
classes
public class MyCompany {
public static void main(String[] args) {
//Create a company object
Company company=new Company("ABC
pvt ltd", 2010, new ArrayList<>(), new
ArrayList<>());
//Create employees
Employee emp1=new
Employee("Tom","Cruise","100","Nov 14",2011, 50000, 90, 30);
Employee emp2=new
Employee("John","Abr","200","Jan 5",2010, 20000, 80, 25);
Employee emp3=new
Employee("AK","Einstien","50","Dec 20",2012, 30000, 70, 40);
//Create employee list
List<Employee> empList=new
ArrayList<>();
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
//Create Managers
Manager man1=new
Manager("Lisa","Ray","100","Jul 14",2012, 75000, 100,
50,10,empList);
empList=new
ArrayList<>();
empList.add(new
Employee("Sachin","Tend","100","Feb 1",2013, 80000, 90, 30));
empList.add(new
Employee("Ajay","J","50","Oct 19",2014, 25000, 90, 30));
Manager man2=new
Manager("Alice","John","70","Aug 14",2013, 80000, 120,
60,10,empList);
//Create manager list
List<Manager> manList=new
ArrayList<>();
manList.add(man1);
manList.add(man2);
company.setEmployeeList(empList);
company.setManagerList(manList);
//Sort by employee last name
Collections.sort(empList);
System.out.println("***********Displaying sorted list of
employees***********");
System.out.println(empList);
//Sort by manager last name
Collections.sort(manList);
System.out.println("***********Displaying sorted list of
managers***********");
System.out.println(manList);
}
}
Output:
**************Please do upvote to appreciate our time.
Thank you!******************