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

Create a Java program that does the following: 1. Enter the hours worked 2. Ente

ID: 3803705 • Letter: C

Question

Create a Java program that does the following:

1. Enter the hours worked

2. Enter the rate of pay

3. If hours worked is over 40, they earn the rate of pay and a half

4. Display the output to the screen and format to a currency format

5. Do the calculation in #3 two different ways. One using an If and Else statement, also another with using two IF statements. (One of the two ways of doing it should be a comment so it doesn't ruin the program.)

6. Send the Output to a txt file displaying only the gross pay

Example of what it should look like:

Inside the txt file:

Enter the total hours worked: 47 Enter the rate of pay 17.50 The gross pay is: $883.75

Explanation / Answer


//import for Scanner and other utility classes
import java.util.*;


class TestClass {
public static void main(String args[] ) throws Exception {


//Scanner
Scanner s = new Scanner(System.in);
  
System.out.println("Enter the total hours worked: ");
int N = s.nextInt();
  
System.out.println("Enter the rate of pay: ");
double rate = s.nextDouble();
  
double gross;
  
//calculated using single if and else condition
if(N>40) {
gross = N*rate + (N-40)*(rate/2);
} else {
gross = N*rate;
}
  
  
/*
  
gross = N*rate;
if(N>40) {
gross += (N-40)*(rate/2);
  
}
  
  
*/
System.out.println("The gross pay is: $"+gross);

//setting result in output
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
System.out.println("The gross pay is: $"+gross);
}
}

Sample Output :