Question
Please help with correcting my coding/calculations for under 40 hours in my java program. My program keeps outputting overtime pay as a negative value when
Please help with correcting my coding/calculations for under 40 hours in my java program. My program keeps outputting overtime pay as a negative value when I input any value under 40 as hours in it when I would prefer it result in $0 instead.
This is the original question that was posted:
Your code will ask the user to input hours worked and base pay (regular pay rate). Determine whether hours worked is greater than 40 hours: If hours worked is greater than 40, then regular pay will be calculated at 40 hours (Full Week Hours) * pay rate. Overtime Pay will be calculated at (hours worked - Full Week Hours) * OT rate * pay rate. If hours worked is not greater than 40, then regular pay will be calculated as hours worked * pay rate. There will be no Overtime Pay Display two output statements: Output statement #1: Regular pay is $ and overtime pay is $ Output statement #2: Gross pay, including overtime pay is $
Here is my code:
import java.util.*; public class PayCalculator{ public static void main(String []args){ //Declare variables int hours; float basePay,regularPay=0,overtimePay=0,grossPay=0;
//Declare constants final int FULL_WEEK = 40; final int OVERTIME_RATE = (int)1.5;
//Create scanner Scanner sc = new Scanner(System.in);
//Take user input System.out.print("Enter number of hours worked: "); hours = sc.nextInt();
System.out.print("Enter base pay: "); basePay = sc.nextFloat();
//Calculations regularPay = hours * basePay; overtimePay = (hours - FULL_WEEK) * OVERTIME_RATE * basePay; grossPay = regularPay + overtimePay;
//Display results
if(hours>40) { regularPay = FULL_WEEK * basePay; overtimePay = (hours-40) * basePay * OVERTIME_RATE; } else { regularPay = hours * basePay; grossPay = regularPay + overtimePay; } System.out.println("Regular pay is $ "+regularPay+" and overtime pay is $ "+overtimePay);
System.out.println("Gross pay, including overtime pay is $ "+grossPay); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started