Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: You will create two classes. If identifiers/names are specified, you must

ID: 3733916 • Letter: J

Question

Java:

You will create two classes. If identifiers/names are specified, you must use them, and you must use the standard order of declaration as shown on the right. All error messages must go to System.err as usual . Responsibilities for code are divided up between the two classes, to earn maximum credit follow the instructions closely so not to mix these up. All methods have to be documented. Don’t assume a method prints something unless says so. For each, carefully identify return type, and number/types of possible parameters.

Worker class:

Constants

MAX_HOURS – maximum hours a worker can work – set to 100

Data Fields – highlight the validity rules for each - you will need them below.

fName – first name of the Worker

hPay – hourly pay rate, must be greater than zero, can have decimal values (default 0.0)

hours – the number of hours the worker has worked so far this week. Whole numbers only. Valid values are zero or greater, can’t be more than MAX_HOURS. (default 0)

wType – work type, an integer (1 = full-time, 0 = part-time, -1 = unknown) (default -1)

retired – whether or not the Worker is retired, can only be true or false (default false)

Constructors

For all constructors, verify the data is in the valid range, otherwise ignore it and leave the default values.

Constructor with only the name as a parameter

Constructor with the first name, pay and hours as parameters – assume the pay and hours are valid

Constructor with all fields as parameters – you must check that the hours, pay and type are valid, if not, set default values (if not already set).

Getters – each method returns the value stored in the particular field.

getfName

getPay

getHours

getType – returns the appropriate integer

isRetired – returns the value of the retired instance variable.

Setters

setName

setPay – verify the parameter is valid, if not ignore, leave current value unchanged.

setHours Must check that the number passed as a parameter is valid – a worker cannot have more than MAX_HOURS hours or less than zero. If the number is not valid, leave the current number of hours unchanged and print a specific informative error message mentioning the name of the person and the bad value. E.g., Invalid type for Jasmin: 5 (remember, you have access to all of the instance variables from inside the class, so you can just refer to fName :)

setType Must check that the number passed as a parameter is valid – a worker can only be full- or parttime, do not accept unknown or any other type. If the number is not valid, leave the current type unchanged and print a specific informative error message. Same as above, mention the name and invalid value.

setRetired – set to true or false depending if retired or not

Utility Functions

computePay() Returns the pay due to the worker based on her hourly pay and the hours she has worked. There is no printing in this method, you are simply computing and returning a value.

printRev() Print the name of the Worker in reverse. E.g., “Jasmin” -> “nimsaJ”. Requirement: You must “loop” through the String, character by character from the end to the start. All your output has to be on the same line, followed by a newline.

toString() Returns a string used to print all of the data fields of the class, one per line, but no final newline. Each data item has a label identifying it. To convincingly demonstrate your ability to format strings your output format must match mine. You must “spell out” the work type, i.e., instead of using a number to identify full-time vs part-time or unknown, use one of these descriptive strings instead: “full-time”, “part-time”, “unknown”.

typeToStr() (optional – but highly recommended) – returns the value of the wType instance variable as one of the descriptive strings: “full-time”, “part-time”, “unknown”. Useful for toString()

Explanation / Answer

Worker class code:

public class Worker

{

//Constants

public static final int MAX_HOURS = 25;

//Data Fields

String fName;

double hPay;

int hours,wType;

boolean retired;

//Constructors

//default constructor

public Worker() {}

//Constructor with only the name as a parameter

public Worker(String name)

{

fName=name;

}

/*Constructor with the first name, pay and hours as parameters

assume the pay and hours are valid*/

public Worker(String fname,double pay,int hours)

{

this.fName=fname;

hPay=pay;

this.hours=hours;

}

//Constructor with all fields as parameters

public Worker(String fName, double hPay, int hours, int wType, boolean retired)

{

super();

this.fName = fName;

//validation on pay

if(hPay>0)

{

this.hPay=hPay;

}else {this.hPay=0.0;}

//validation on hours

if(hours>=0 && hours<=MAX_HOURS)

{

this.hours=hours;

}else

{

this.hours=0;

}

//validation on type

if(wType>1)

{

this.wType=-1;

}else

{

this.wType=wType;

}

this.retired = retired;

}

//Getters

public String getfName()

{

return fName;

}

public double gethPay()

{

return hPay;

}

public int getHours()

{

return hours;

}

public int getwType()

{

return wType;

}

public boolean isRetired()

{

return retired;

}

//Setters

public void setName(String fName)

{

this.fName = fName;

}

public void setPay(double hPay)

{

//validation

if(hPay>0)

{

this.hPay=hPay;

}else

{

hPay=hPay;

}

}

public void setHours(int hours)

{

//validation on hours

if(hours>=0 && hours<=MAX_HOURS)

{

this.hours=hours;

}else

{

hours=hours;

System.err.println("Invalid type for "+fName+":"+hours);

}

this.hours = hours;

}

public void setType(int wType)

{

//validation on type

if(wType==1 || wType==0)

{

this.wType=wType;

}else if(wType>1 || wType <0)

{

wType=wType;

System.err.println("Invalid type for "+fName+":"+wType);

}

}

public void setRetired(boolean retired)

{

if(retired==true)

{

this.retired=true;

}else if(retired==false)

{

this.retired=false;

}

}

//Utility Functions

public double computePay()

{

return (hours*hPay);

}

public void printRev()

{

// a newline.

System.out.print(" ");

for(int i=fName.length()-1;i>=0;i--)

System.out.print(fName.charAt(i));

}

//toString()

public String toString()

{

return "Worker [First Name:"+fName+" "+

"Hourly Pay:"+hPay+" "+

"Hours Worked:"+hours+" "+

"Work Type:"+this.typeToStr()+" "+

"Retired:"+retired+" ]";

}

//(optional – but highly recommended)

public String typeToStr()

{

String workType=null;

if(wType==1)

{

workType="full-time";

}else if(wType==0)

{

workType="part-time";

}else if(wType==-1)

{

workType="unknown";

}

return workType;

}

}

Test code:

public class WokerTest {

public static void main(String[] args)

{

//creating object of worker class

Worker worker = new Worker();

//setting details using setter methods

worker.setName("Jasmine");

worker.setPay(75);

worker.setHours(5);

worker.setType(1);

worker.setRetired(false);

//displaying details

System.out.println(worker.toString());

System.out.println("Payment for the worker "+worker.getfName()+":"+worker.computePay());

System.out.print("Reverse Name of the worker "+worker.getfName()+":");

worker.printRev();

}

}

Output:

Worker [First Name:Jasmine
Hourly Pay:75.0
Hours Worked:5
Work Type:full-time
Retired:false ]
Payment for the worker Jasmine:375.0
Reverse Name of the worker Jasmine:
enimsaJ

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote