Exercise 1: InheritanceExercise : Student worker inherited from worker # workerN
ID: 3760438 • Letter: E
Question
Exercise 1: InheritanceExercise : Student worker inherited from worker
# workerName: String
# workerId: int
+ Worker(String, String, String )
+ getName(): String
+ setName(String): void
+ getId(): int
+ setId(int): void
+ getIsWorking(): boolean
+ setIsWorking (boolean): void
# hoursWorked: int
# isWorkStudy: boolean
+ StudentWorker(name: String, id: String, isworking:String, hoursworked: String, workstudy: String, payRate: String)
+ getPayRate(): double
+ setPayRate(double): void
Class Worker will represent the superclass. It should have fields to hold the following information:
workerName – the worker’s full name;
· workerId – the worker’s assigned ID;
· isWorking – True: the worker is employed, false is not;.
constructor method Worker accepts all three parameters as Strings and converts them to their appropriate primitive types;
Setter and getter methods for WorkerName;
· Setter and getter methods for isWorking;
· toString - returns WorkerName, WorkerId and isWorking as a string.
Class StudentWorker will be derived from the Worker class. It should have fields to hold the following information:
hoursWorked - The number of hours worked in the current two-week pay period.
· isWorkStudy - True is the student having work permit; false otherwise
· payRate - The student's hourly pay rate
· constructor method accepts all six parameters as Strings and converts them to their appropriate primitive types
· Setter and getter methods for payRate.
. toString - returns workerName,workerId, isWorking, hoursWorked, isWorkStudy, and payRate, as a string
Using the following code for your testing:
//Hint: you test program could be as:
StudentWorker s = new StudentWorker(“Chris Diez”, “123456”, “true”, “25”, “false”, “11.0”);
System.out.println(s.toString()); System.out.println(s.getName()); System.out.println(s.getId()); System.out.println(s.getPayRate()); System.out.println(s.setPayRate(18.00)); System.out.println(s.toString());
Worker# workerName: String
# workerId: int
# isWorking: boolean+ Worker(String, String, String )
+ getName(): String
+ setName(String): void
+ getId(): int
+ setId(int): void
+ getIsWorking(): boolean
+ setIsWorking (boolean): void
+ toString(): StringExplanation / Answer
package mani;
public class studentWorker extends Worker {
int hoursWorked;
boolean isWorkStudy;
double payRate;
public studentWorker(String s1, String s2, String s3,String s4,String s5,String s6) {
super(s1, s2, s3);
hoursWorked=Integer.parseInt(s4);
isWorkStudy=Boolean.parseBoolean(s5);
payRate=Double.parseDouble(s6);
}
public double getPayRate(){
return payRate;
}
public void setPayRate(double payR){
payRate=payR;
}
public String toString(){
return super.toString()+" Number of Hours Worked: "+hoursWorked+". IsWorking and Studying: "+isWorkStudy+". PayRate of Worker: "+getPayRate();
}
}
package mani;
public class Worker {
String workerName;
int workerId;
boolean isWorking;
public Worker(String s1,String s2,String s3){
workerName=s1;
workerId=Integer.parseInt(s2);
isWorking=Boolean.parseBoolean(s3);
}
public String getName(){
return workerName;
}
public void setName(String name){
workerName=name;
}
public int getId(){
return workerId;
}
public void setId(int id){
workerId=id;
}
public boolean getIsWorking(){
return isWorking;
}
public void setIsWorking(boolean is){
isWorking=is;
}
public String toString(){
return "Name of Worker: "+getName()+". Id number of worker: "+getId()+". Is the Worker working still? "+getIsWorking();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.