Suppose that you are required to write an application to model interns and super
ID: 3737096 • Letter: S
Question
Suppose that you are required to write an application 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
//Staff.java
abstract class Staff{
@Override
//toString() method
public String toString() {
return "name=" + name + " address=" + address ;
}
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;
}
private String name;
private String address;
public Staff(String name,String add)
{
this.name=name;
this.address=add;
}
}
//Intern.java
import java.util.ArrayList;
/**
*
* @author pc
*/
public class Intern extends Staff implements StaffInfo {
//constructor
public Intern(String name, String add) {
super(name, add);
taskList=new ArrayList<String>();
}
private int numTasks,maxNumTasks=10, hourList[];
private ArrayList<String> taskList;
public String toString()
{
String str="Intern: ";
str=str+super.toString();//call super class constructor
str=str+" number of tasks="+numTasks+" maximum Tasks"+maxNumTasks+" ";
//add task and hours
for(int i=0;i<numTasks;i++)
{
str=str+"Task = "+taskList.get(i)+" Hour = "+hourList[i]+" ";
}
return str;
}
public void addTaskHours(String task,int hours)
{
//if empty or hours is not greater than 0 then
if(task.isEmpty() || hours<=0)
{
System.out.println("Error! data is not valid");
return;
}
if(numTasks<maxNumTasks)//cant give more tasks than maximum number of tasks
{
taskList.set(numTasks, task);
hourList[numTasks]=hours;
numTasks++;
}
else{
System.out.println("The intern has already maximum number of tasks");
}
}
public void printHours()
{//prints all Tasks taken and their hour
for(int i=0;i<numTasks;i++)
{
System.out.println("Task : "+taskList.get(i)+" Hours taken : "+hourList[i]);
}
}
public double getAverageHour()
{// computes the average hour
double avg=0.0;
int sum=0;
for(int i=0;i<numTasks;i++)
{
sum=sum+hourList[i];
}
avg=sum/numTasks;//find average
return avg;
}
@Override
public String getStaffInfo() {
String str= "Intern : Name="+ this.getName() +" Address="+ this.getAddress();
return str;
}
}
//Supervisor.java
import java.util.ArrayList;
/**
*
* @author pc
*/
public class Supervisor extends Staff implements StaffInfo {
public Supervisor(String name, String add) {
super(name, add);
taskList=new ArrayList<String>();
}
public String toString()
{
String str="Superviser: ";
str=str+super.toString();
str=str+" number of tasks="+numTasks+" maximum Tasks"+maxNumTasks+" ";
for(int i=0;i<numTasks;i++)
{
str=str+"Task = "+taskList.get(i)+" ";
}
return str;
}
@Override
public String getStaffInfo() {
// Returns (String “Supervisor” or “Intern”), name + address of the staff
String str= "Supervisor : Name="+ this.getName() +" Address="+ this.getAddress();
return str;
}
private int numTasks, maxNumTasks=5;
private ArrayList<String> taskList;
public boolean addTask(String task)
{
// adds a task to task list. Returns false if task already in the list.
for(int i=0;i<maxNumTasks;i++)
{
if(taskList.get(i).equalsIgnoreCase(task))
{
return false;
}
}
if(numTasks<maxNumTasks)
{
taskList.set(numTasks, task);
numTasks++;
return true;
}
return false;
}
public boolean removeTask (String task)
{//removes a task from task list. Returns false if task not found.
for(int i=0;i<numTasks;i++)
{
if(taskList.get(i).equalsIgnoreCase(task))
{
taskList.remove(i);
return true;
}
}
return false;
}
}
//Interface
interface StaffInfo {
// Returns (String “Supervisor” or “Intern”), name + address of the staff
String getStaffInfo();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.