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

I\'m supposed to create a JAVA code to create this: Sample Run 1 Number of hours

ID: 3622896 • Letter: I

Question

I'm supposed to create a JAVA code to create this:
Sample Run 1
Number of hours worked this week: 30
Pay rate per hour: 10
This individual did not work overtime.
The gross pay is $300.0

Sample Run 2
Number of hours worked this week: 50
Pay rate per hour: 20
This individual worked overtime.
The gross pay is $1100.0

My code:
import java.util.Scanner; // Needed for the Scanner class

public class Lab3_1
{
public static void main(String[] args)
{
final double REG_HRS = 40.0; // Hours at regular pay before overtime starts
final double OT_RATE = 1.5; // Overtime payrate
double hoursWorked;
double ratePerHour;
double grossPay;


// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);

System.out.print("Number of hours worked this week: ");
hoursWorked = keyboard.nextDouble();

System.out.print("Pay rate per hour: ");
ratePerHour = keyboard.nextDouble();

if (hoursWorked > REG_HRS)
{
System.out.println("This individual worked overtime.");
grossPay = ratePerHour * REG_HRS +
ratePerHour * OT_RATE * (hoursWorked - REG_HRS);
System.out.println("The gross pay is $" + grossPay);
}
else
System.out.println("This individual did not work overtime.");
grossPay = ratePerHour * hoursWorked;
System.out.println("The gross pay is $" + grossPay);

if (grossPay > 5000);
System.out.println("You earned more than $5,000 this week!!!");

}
}

When I run the code this is what I get:
Sample Run 1:
Number of hours worked this week: 30
Pay rate per hour: 10
This individual did not work overtime.
The gross pay is $300.0
You earned more than $5,000 this week!!!
Sample Run 2:
Number of hours worked this week: 50
Pay rate per hour: 20
This individual worked overtime.
The gross pay is $1100.0
The gross pay is $1000.0
You earned more than $5,000 this week!!!

What am I doing wrong? How can I fix it?

Explanation / Answer

Thank you for posting a problem that you'd actually tried and got stuck on! That's what these forums are for, but many people just try to get their work done for them... anyway... First, the reason you're getting the "You earned more than $5,000 this week!!!" statement is kind of subtle. It's valid syntax to have an if statement with no block of code to execute, and when you put a semicolon after it, it ends the statement. if (grossPay > 5000); // the semicolon means this statement is through System.out.println("You earned more than $5,000 this week!!!"); // this is executed no matter what What you want is to put the print statement in a block: if (grossPay > 5000) { System.out.println("You earned more than $5,000 this week!!!"); } Now technically you don't have to... for one-liners it's legal not to use the braces, like: if (grossPay > 5000) System.out.println("You earned more than $5,000 this week!!!") or if (grossPay > 5000) System.out.println("You earned more than $5,000 this week!!!") but this shorthand is just begging for errors; if you add another line it'll be outside of the for statement. In fact, this is exactly what's biting you in the other print statement. Everything after the 'else' in the gross pay calculation should be in brackets... right now, only the first statement is viewed as being part of the 'else', which is why "This individual did not work overtime" is not printed in the second case, but the second gross pay calculation is. Hope that gets you going-- let me know if you need more help.