Question
In this assignment, you will write a manager class for a list of objects of your subclass and a test program to show that the
In this assignment, you will write a manager class for a list of objects of your subclass and a test program to show that the manager class operates as designed.
Your manager class will use one of the data structures we studied in CS 212 (array, array list, linked list) to hold a group of objects of your subclass. The constructor of the manager class will have no input parameter. It will set up the data structure to hold objects but at this time it will not know how many objects will be stored.
- Note: If you have decided to use an array as your data structure, you can set some original default size for the array. However, this may need to be changed later depending upon how many objects the user later wishes to store.
- In other words, you may need to replace the current array with a larger array.
The manager class will also have the following:
- A method that allows the user to enter a group of objects into the list:
- This method will have one input parameter the number of objects to add to the list.
- It will contain a loop; inside the loop it will call the subclass .enterObject() method to set up a subclass object that will then be added to the list.
- If you chose an array as your data structure, you may need to set up a new array; the original one set up by the constructor may not be large enough to hold all the objects.
- A print method that will print the objects currently in the list, numbering them beginning with 1.
- .get() and .set() methods so a user can retrieve an object or replace an object in the list.
- A search method:
- This method will receive an object as an input parameter and will return true/false to indicate whether the object is/is not in the list.
- A method that returns the largest object in the list.
- This method will have no input parameters.
- It will use the subclass .compareTo() method to determine the sizing of objects in the list.
Along with your manager class, you will write a test program that demonstrates that your manager class works as written. The test program will perform the following tasks:
- Set up a manager class object:
- The manager class constructor will set up the data structure (an instance variable of the manager class) to hold the subclass objects.
- Since no number of objects has been specified, if you chose to use an array, the constructor will set up an array of some fixed size; this may need to be changed later.
- Display a menu with the following options:
- Enter a group of objects into the list.
- Print the current contents of the list.
- Search the list to determine if the users target object is in the list or not.
- Print the largest object currently in the list.
- Exit the menu.
Prior code:
Person.java
import java.util.Comparator; import java.util.Scanner; public class Person implements Comparable{ private String name; private int age; public Person() { name = ""; age = 0; } public Person(String name, int age) { super(); this.name = name; if (age < 0) { age = 20; } else { this.age = age; } } public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [Name=" + name + ", Age=" + age + "]"; } @Override public int compareTo(Person p) { return Comparator.comparing(Person::getName).thenComparingInt(Person::getAge).compare(this, p); } public static Person enterObject() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age of person"); String name = sc.nextLine(); int age = sc.nextInt(); try { if (age < 0) { throw new IllegalArgumentException(); } } catch (Exception e) { age = 20; // setting up default age variable if age is -ve value System.out.println("You enter wrong value, so set age to default value 20"); } return new Person(name, age); } }
Employee.java
import java.util.Comparator; import java.util.Scanner; public class Employee extends Person { private String company; private double salary; public Employee() { super(); company = ""; salary = 10000; } public Employee(String name, int age, String company, double salary) { super(name, age); this.company = company; this.salary = salary; } public String getCompany() { return company; } public double getSalary() { return salary; } public void setCompany(String company) { this.company = company; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Employee [Name=" + getName() + ", Age=" + getAge() + ", Company=" + company + ", Salary=" + salary + "]"; } public int compareTo(Employee e) { return Comparator.comparing(Employee::getName).thenComparingInt(Employee::getAge) .thenComparing(Employee::getCompany).thenComparingDouble(Employee::getSalary).compare(this, e); } public static Employee enterObject() { Scanner sc = new Scanner(System.in); System.out.println("Enter name,age, Company and salary of Employee"); String name = sc.nextLine(); int age = sc.nextInt(); sc.nextLine(); String company = sc.nextLine(); double sal = sc.nextDouble(); return new Employee(name, age, company, sal); } }
TestDriver.java
public class TestDriver { public static void main(String[] args){ Person p1 = new Person("Anusha",24); Employee e1 = new Employee("Sireesha",25,"XXX",45000); Employee e2 = new Employee("Jyothi",23,"YYY",35000); System.out.println(p1.toString()); System.out.println(e1.toString()); System.out.println(e1.compareTo(e2)); System.out.println(e2.toString()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started