Suppose that you are required to write an application in JAVA to model interns a
ID: 3737097 • 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
Please find the code below with detailed inline comments.
CODE
===================
Staff.java
********************************
public abstract class Staff {
protected String name, address;
public Staff(String name, String address) {
super();
this.name = name;
this.address = address;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
}
Intern.java
********************************
import java.util.Arrays;
public class Intern extends Staff implements StaffInfo{
private int numTasks, hourList[];
private String taskList[];
private static final int MaxNumTasks = 10;
public Intern(String name, String address) {
super(name, address);
numTasks = 0;
taskList = new String[MaxNumTasks];
hourList = new int[MaxNumTasks];
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Intern : [numTasks = " + numTasks + ", hourList = " + Arrays.toString(hourList) + ", taskList = "
+ Arrays.toString(taskList) + "]";
}
public void addTaskHours(String task, int h) {
if(numTasks >= MaxNumTasks) {
System.out.println("Sorry, cannot have more than " + MaxNumTasks + " tasks...");
return;
}
if(task == null || h <= 0) {
System.out.println("Invalid task details provided...");
return;
}
taskList[numTasks ++] = task;
hourList[numTasks ++] = h;
System.out.println("Task successfully added!");
}
public void printHours() {
System.out.println("The task details are: ");
for(int i=0; i<numTasks; i++) {
System.out.println("Task: " + taskList[i] + ", Hours: " + hourList[i]);
}
}
public double getAverageHour() {
double avg = 0;
for(int i=0; i<numTasks; i++) {
avg += hourList[i];
}
avg /= numTasks;
return avg;
}
@Override
public String getStaffInfo() {
return "Intern [Name = " + name + ", Address = " + address + ", Number of Tasks = "
+ numTasks + ", List of Hours = " + Arrays.toString(hourList) + ", List of Tasks = "
+ Arrays.toString(taskList) + "]";
}
}
Supervisor,java
********************************
import java.util.Arrays;
public class Supervisor extends Staff implements StaffInfo{
private int numTasks;
private String taskList[];
private static final int MaxNumTasks = 5;
public Supervisor(String name, String address) {
super(name, address);
numTasks = 0;
taskList = new String[MaxNumTasks];
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Supervisor : [numTasks=" + numTasks + ", taskList=" + Arrays.toString(taskList) + "]";
}
public boolean findTask(String task) {
for(int i=0; i<numTasks; i++) {
if(taskList[i].equals(task))
return true;
}
return false;
}
public boolean addTaskHours(String task) {
if(numTasks >= MaxNumTasks) {
System.out.println("Sorry, cannot have more than " + MaxNumTasks + " tasks...");
return false;
}
if(task == null) {
System.out.println("Invalid task details provided...");
return false;
}
if(findTask(task)) {
System.out.println("Cannot add this task as it is a duplicate task....");
return false;
}
taskList[numTasks ++] = task;
System.out.println("Task successfully added!");
return true;
}
public boolean removeTask(String task) {
if(task == null) {
System.out.println("Invalid task details provided...");
return false;
}
if(!findTask(task)) {
System.out.println("Cannot find the given task....");
return false;
}
String[] newTaskList = new String[MaxNumTasks];
int k=0;
for(int i=0; i<numTasks; i++) {
if(!taskList[i].equals(task))
newTaskList[k ++] = taskList[i];
}
taskList = newTaskList;
System.out.println("Task successfully removed!");
return true;
}
@Override
public String getStaffInfo() {
return "Supervisor : [Name = " + name + ", Address = " + address + ", Number of Tasks = " +
+ numTasks + ", List of Tasks = " + Arrays.toString(taskList) + "]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.