Suppose that you are required to write an application in JAVA to model interns a
ID: 3737099 • Letter: S
Question
Suppose that you are required to write an application in JAVA to model interns and supervisors in a company.
1- Define an abstract superclass called Staff to store two properties as:
String name;
String address;
[4 points] Provide (only one) constructor which sets the name and address properties.
[6 points] Provide getters & setters and toString() methods for the class Staff.
2- Define:
[2 points] Two subclasses of Staff, one named Intern and one Supervisor.
[3 points] Both classes, Intern and Supervisor must implement the interface StaffInfo, provided below.
3- Requirements for the class Intern:
should maintain a list of tasks taken and the respective hours for each task.
an intern cannot have more than 10 tasks
should also provide a method to add a task with hours
print all tasks taken with task description and hours
calculate the average hours of the tasks assigned
[5 points] Intern(…) constructor calling the parent constructor
[5 points] private instance variables: numTasks, taskList[], hourList[], MaxNumTasks=10
[5 points] toString() method to describe itself. Add the string “Intern: ” to the beginning.
[5 points] addTaskHours(task, hours) adds a task and its hours. Validate that task description is not null and hours are greater than zero
[5 points] // printHours() prints all Tasks taken and their hour
[5 points] // getAverageHour() computes the average hour
4- Requirements for the class Supervisor:
should maintain a list of tasks they currently supervise
be able to add or remove a task supervised
assume that a supervisor cannot supervise more than 5 tasks
[5 points] private instance variables: numTasks, taskList[], MaxNumTasks=5
[5 points] Supervisor (…) constructor calling the parent constructor
[5 points] toString() method to describe itself. Add the string “Superviser: ” to the beginning.
[5 points] addTask(task) adds a task to task list. Returns false if task already in the list.
[5 points] removeTask (task) removes a task from task list. Returns false if task not found.
Note: Your program solution may only be uploaded in .jar format. No other format will be graded.
Interface to be implemented by the Intern & the Supervisor class as described in item 2 above.
/**************************************************
* StaffInfo interface declaration.
**************************************************/
public interface StaffInfo {
// Returns (String “Supervisor” or “Intern”), name + address of the staff
String getStaffInfo();
}
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements.
// Staff.java
public abstract class Staff {
//attributes
private String name;
private String address;
//2 argument constructor
public Staff(String name, String address) {
this.name = name;
this.address = address;
}
//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//returns a string containing staff details
@Override
public String toString() {
return "Name: " + name + " Address: " + address;
}
}
// StaffInfo.java
public interface StaffInfo {
/* Returns (String “Supervisor” or “Intern”), name + address of the staff */
String getStaffInfo();
}
// Intern.java
public class Intern extends Staff implements StaffInfo {
// attributes
private int numTasks;
private String taskList[];
private int hourList[];
private static final int MaxNumTasks = 10;
// 2 argument constructor
public Intern(String name, String address) {
super(name, address);/* passing values to super class */
numTasks = 0;
// initializing arrays
taskList = new String[MaxNumTasks];
hourList = new int[MaxNumTasks];
}
@Override
public String getStaffInfo() {
/**
* in the method description, it said that this method will return
* (String “Supervisor” or “Intern”), name + address of the staff which
* is exactly toString() method does, so returning it
*/
return toString();
}
@Override
public String toString() {
String data = "Intern: " + super.toString();
return data;
}
/**
* method to add a task and hours
* @param task - task to be added
* @param hours - number of hours
*/
public void addTaskHours(String task, int hours) {
if (task == null || hours <= 0) {
//invalid
System.out.println("Invalid task");
} else {
if (numTasks == MaxNumTasks) {
//arrays are full
System.out.println("Maximum tasks are there already!");
} else {
//adding
taskList[numTasks] = task;
hourList[numTasks] = hours;
numTasks++;
}
}
}
/**
* method to print all tasks and hours
*/
public void printHours() {
System.out.println("Tasks: ");
for (int i = 0; i < numTasks; i++) {
System.out.println(taskList[i] + " (" + hourList[i] + ") hours ");
}
}
/**
* @returns the average hours of all tasks
*/
public double getAverageHour() {
if (numTasks == 0) {
return 0;
}
double total = 0;
for (int i = 0; i < numTasks; i++) {
total += hourList[i];
}
double average = (double) total / numTasks;
return average;
}
}
// Supervisor.java
public class Supervisor extends Staff implements StaffInfo {
// attributes
private int numTasks;
private String taskList[];
private static final int MaxNumTasks = 5;
// 2 argument constructor
public Supervisor(String name, String address) {
super(name, address);/* passing values to super class */
numTasks = 0;
// initializing array
taskList = new String[MaxNumTasks];
}
@Override
public String getStaffInfo() {
return toString();
}
@Override
public String toString() {
return "Supervisor: " + super.toString();
}
/**
* method to add a task
* @param task - task to be added
* @return true if added, else false
*/
public boolean addTask(String task) {
for (int i = 0; i < numTasks; i++) {
if (taskList[i].equalsIgnoreCase(task)) {
//task exists
return false;
}
}
if (numTasks == MaxNumTasks) {
// maximum tasks reached
return false;
}
//adding
taskList[numTasks] = task;
numTasks++;
return true;
}
/**
* method to remove a task
* @param task - task to be removed
* @return true if removed, else false
*/
public boolean removeTask(String task) {
for (int i = 0; i < numTasks; i++) {
if (taskList[i].equalsIgnoreCase(task)) {
/**
* task found, shifting the remaining elements to occupy the
* current space
*/
for (int j = i; j < numTasks - 1; j++) {
taskList[j] = taskList[j + 1];
}
numTasks--;
return true;
}
}
return false;//not found
}
}
// Test.java
public class Test {
public static void main(String[] args) {
/**
* Testing both Intern and Supervisor classes
*/
Intern intern = new Intern("Alice", "Wonderland, SA Street 22NB");
intern.addTaskHours("Work1", 5);
intern.addTaskHours("Work2", 7);
intern.addTaskHours("Work3", 3);
intern.addTaskHours("Work4", 4);
intern.addTaskHours("Work5", 9);
System.out.println(intern.getStaffInfo());
intern.printHours();
System.out.println("Average hours: "+intern.getAverageHour());
Supervisor supervisor=new Supervisor("John", "Westeros");
System.out.println(supervisor.getStaffInfo());
if(supervisor.addTask("Work1")){
System.out.println("Work1 task is added");
}else{
System.out.println("Work1 task is not added");
}
if(supervisor.removeTask("Work1")){
System.out.println("Work1 task is removed");
}else{
System.out.println("Work1 task is not removed");
}
}
}
/*OUTPUT*/
Intern:
Name: Alice
Address: Wonderland, SA Street 22NB
Tasks:
Work1 (5) hours
Work2 (7) hours
Work3 (3) hours
Work4 (4) hours
Work5 (9) hours
Average hours: 5.6
Supervisor:
Name: John
Address: Westeros
Work1 task is added
Work1 task is removed
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.