In: Computer Science
Okay, can someone please tell me what I am doing wrong??
I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance.
* * * * * HourlyTest Class * * * * *
import java.util.Scanner;
public class HourlyTest {
public static void main(String[] args)
{
String firstName;
String lastName;
double
hoursWorked;
double payRate;
Scanner scan = new
Scanner(System.in);
// Ask user to enter
values
System.out.print("Enter
first name: ");
firstName =
scan.nextLine();
System.out.print("Enter
last name: ");
lastName =
scan.nextLine();
System.out.print("Enter
the number of hours worked: ");
hoursWorked =
Double.parseDouble(scan.nextLine());
System.out.print("Enter
hourly rate: $");
payRate =
Double.parseDouble(scan.nextLine());
// Create Hourly class
object
Hourly hourly = new
Hourly();
hourly.setFirstName(firstName);
hourly.setLastName(lastName);
hourly.setHoursWorked(hoursWorked);
hourly.setPayRate(payRate);
// Call method to get
weeklyPay
double weeklyPay =
hourly.getPaycheck();
// if enter uses invalid
data
while (weeklyPay == 0.0)
{
System.out.print("Enter the number of hours worked: ");
hoursWorked = scan.nextInt();
System.out.print("Enter hourly rate: $");
payRate = scan.nextDouble();
scan.nextLine();
hourly.setHoursWorked(hoursWorked);
hourly.setPayRate(payRate);
weeklyPay = hourly.getPayRate();
}
// Print weekly
weeklyPay value
System.out.println("\n"
+ firstName + " " + lastName +
"'s weekly paycheck will be: $" + String.format("%.2f",
weeklyPay));
scan.close();
} // end of main method
} // end of class HourlyTest
* * * * * Hourly Class * * * * *
public class Hourly {
// Setting private Instance variables
private String firstName;
private String lastName;
private double hoursWorked;
private double payRate;
private double weeklyPay;
private static final int MAXREGULAR = 40;
// constructor
public Hourly() {
// set data field to
zero
this.firstName =
"";
this.lastName =
"";
this.hoursWorked =
0.0;
this.payRate =
0.0;
} // end of constructor
// Set and get methods
public String getFirstName() {
return firstName;
} // end of getter
public void setFirstName(String firstName)
{
this.firstName =
firstName;
} // end of setter
public String getLastName() {
return lastName;
} // end of getter
public void setLastName(String lastName)
{
this.lastName =
lastName;
} // end of setter
public double getHoursWorked() {
return
hoursWorked;
} // end of getter
public void setHoursWorked(double
hoursWorked) {
if (hoursWorked >= 1
&& hoursWorked <= 80) {
this.hoursWorked = hoursWorked;
} else {
System.out.println("\nYou must enter a number between 1 and 80.
"
+ "Please try again!\n");
this.hoursWorked = 0.0;
}
} // end of setter
public double getPayRate() {
return payRate;
} // end of getter
public void setPayRate(double payRate)
{
if (payRate >= 15
&& payRate <= 30) {
this.payRate = payRate;
} else {
System.out.println("\nYou must enter an hourly rate between "
+ "$15 - $30. Please try again!\n");
this.payRate = 0.0;
}
} // end of setter
// Method to return weeklyPay
public double getPaycheck() {
this.weeklyPay =
this.hoursWorked * this.payRate;
if (hoursWorked >
MAXREGULAR) { // if here was overtime. .
.
double otHours = hoursWorked - MAXREGULAR; // calculate overtime
hours
double otPay = otHours * (payRate * 0.5); // pay "half" the rate
for overtime hours worked
weeklyPay = weeklyPay + otPay; // add overtime pay to regular
pay
}
return weeklyPay;
} // end of method getPaycheck
} //end of class Hourly
* * * * * What needs corrected based on the following comment by instructor * * * * *
For this assignment, you were supposed to create a constructor that that accepts those instance variables and performs some input validation and then prints an error message. Then, the HourlyTest class checks the weeklyPaycheck amount and, if it's 0, prompts the user for input again.
Please help!
import java.util.Scanner; public class HourlyTest { static class Hourly { // Setting private Instance variables private String firstName; private String lastName; private double hoursWorked; private double payRate; private double weeklyPay; private static final int MAXREGULAR = 40; Scanner scan = new Scanner(System.in); // constructor public Hourly() { // set data field to zero this.firstName = ""; this.lastName = ""; this.hoursWorked = 0.0; this.payRate = 0.0; } // end of constructor // Set and get methods public String getFirstName() { return firstName; } // end of getter public void setFirstName(String firstName) { this.firstName = firstName; } // end of setter public String getLastName() { return lastName; } // end of getter public void setLastName(String lastName) { this.lastName = lastName; } // end of setter public double getHoursWorked() { return hoursWorked; } // end of getter public void setHoursWorked(double hoursWorked) { while(hoursWorked < 1 || hoursWorked > 80) { System.out.println("\nYou must enter a number between 1 and 80. " + "Please try again!\n"); System.out.print("Enter the number of hours worked: "); hoursWorked = Double.parseDouble(scan.nextLine()); } this.hoursWorked = hoursWorked; } // end of setter public double getPayRate() { return payRate; } // end of getter public void setPayRate(double payRate) { while(payRate < 15 || payRate > 30) { System.out.println("\nYou must enter an hourly rate between " + "$15 - $30. Please try again!\n"); System.out.print("Enter hourly rate: $"); payRate = Double.parseDouble(scan.nextLine()); } this.payRate = payRate; } // end of setter // Method to return weeklyPay public double getPaycheck() { this.weeklyPay = this.hoursWorked * this.payRate; if (hoursWorked > MAXREGULAR) { // if here was overtime. . . double otHours = hoursWorked - MAXREGULAR; // calculate overtime hours double otPay = otHours * (payRate * 0.5); // pay "half" the rate for overtime hours worked weeklyPay = weeklyPay + otPay; // add overtime pay to regular pay } return weeklyPay; } // end of method getPaycheck } //end of class Hourly public static void main(String[] args) { String firstName; String lastName; double hoursWorked; double payRate; Scanner scan = new Scanner(System.in); // Create Hourly class object Hourly hourly = new Hourly(); // Ask user to enter values System.out.print("Enter first name: "); firstName = scan.nextLine(); hourly.setFirstName(firstName); System.out.print("Enter last name: "); lastName = scan.nextLine(); hourly.setLastName(lastName); System.out.print("Enter the number of hours worked: "); hoursWorked = Double.parseDouble(scan.nextLine()); hourly.setHoursWorked(hoursWorked); System.out.print("Enter hourly rate: $"); payRate = Double.parseDouble(scan.nextLine()); hourly.setPayRate(payRate); // Call method to get weeklyPay double weeklyPay = hourly.getPaycheck(); // if enter uses invalid data while (weeklyPay == 0.0) { System.out.print("Enter the number of hours worked: "); hoursWorked = scan.nextInt(); System.out.print("Enter hourly rate: $"); payRate = scan.nextDouble(); scan.nextLine(); hourly.setHoursWorked(hoursWorked); hourly.setPayRate(payRate); weeklyPay = hourly.getPaycheck(); } // Print weekly weeklyPay value System.out.println( "\n" + firstName + " " + lastName + "'s weekly paycheck will be: $" + String.format("%.2f", weeklyPay)); scan.close(); } // end of main method } // end of class HourlyTest
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.