import java.util.Scanner; // Program uses class Scanner to accept user input
import java.text.NumberFormat; // Program Uses Class NumberFormat to format number output
import java.util.Locale; // Program Needs to Use U.S. Currency
public class PayrollProgram
{
// main method begins execution of Java application
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input;
// Set Currency to U.S. Dollars
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
// Variable Specifications
String employeeName; // Placeholder for employee name
float hoursWorked; // Number of hours worked in a week
float hourlyRate; // Employee's hourly rate
float totalPay; // total hours worked plus the hourly rate
float overtimePay; // overtime pay
// Show Program Welcome Message
System.out.println( "Employee Weekly Payroll Program\n\n"); // Tells program to show Welcome Message
// while(true) // will loop until STOP is entered no particular letter case expected
{
input = new Scanner (System.in);
// Show an Empty Line
System.out.println( );
// On screen prompt for Employee Name
System.out.print( "Enter your name: " );
employeeName = input.nextLine(); // read the employee's name from memory
do
{
// Prompt for Employee's Hourly Rate
System.out.print( "Enter hourly rate: " ); // prompt message
hourlyRate = input.nextFloat(); // reads employee's hourly rate
if (hourlyRate < 0)
{
System.out.println( "ERROR: The hourly rate must be a positive number." ); // show error message
}
} while ( hourlyRate < 0 ); // end do while statement
do
{
System.out.print( "Enter total hours worked this week: " ); // prompts the employee for hours worked for the week
hoursWorked = input.nextFloat(); // reads number of hours entered and stores into memory
if (hoursWorked < 0)
{
System.out.println( "ERROR: The number of hours worked must be a positive number." ); // shows error message
}
} while ( hoursWorked < 0 ); // end do while statement
// Calculate Employee's Pay
totalPay = hourlyRate * hoursWorked;
overtimePay = totalPay + overtimePay;
// Calculate OVERTIME HOURS
if (hoursWorked > 40)
{
overtimePay = (hoursWorked - 40) * (hourlyRate * 1.5);
}
else
{ totalPay = hoursWorked * hourlyRate;
overtimePay = 0;
}
// Output program results to the display
System.out.println( "\n\nWEEKLY PAYROLL RESULTS"); // Show Exit Message
System.out.println( "\n\nEmployee Name: " + employeeName);
System.out.println( "Gross Pay: " + currency.format(totalPay) );
System.out.println( "Overtime Hours: " + overtimePay); //OVERTIME PAY
// Show an Empty Line
System.out.println( );
} // end while statement
} // end method main
} //end class Payroll
Calculating Overtime Pay In Java
Page 1 of 15 Replies - 5756 Views - Last Post: 07 January 2011 - 11:33 PM
#1
Calculating Overtime Pay In Java
Posted 07 January 2011 - 10:11 PM
I'm having trouble calculating overtime pay in my program. I can't seem to get the right errors fixed to save my life. Please see:
Replies To: Calculating Overtime Pay In Java
#2
Re: Calculating Overtime Pay In Java
Posted 07 January 2011 - 10:20 PM
Well, what are the errors? In order to fix them, we have to know them...
#3
Re: Calculating Overtime Pay In Java
Posted 07 January 2011 - 10:29 PM
The error I'm getting is
found : double
required: float
overtimePay = (hoursWorked - 40) * (hourlyRate * 1.5);
^
1 error with exit code 1
LokaRosa, on 07 January 2011 - 09:11 PM, said:
I'm having trouble calculating overtime pay in my program. I can't seem to get the right errors fixed to save my life. Please see:
import java.util.Scanner; // Program uses class Scanner to accept user input
import java.text.NumberFormat; // Program Uses Class NumberFormat to format number output
import java.util.Locale; // Program Needs to Use U.S. Currency
public class PayrollProgram
{
// main method begins execution of Java application
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input;
// Set Currency to U.S. Dollars
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
// Variable Specifications
String employeeName; // Placeholder for employee name
float hoursWorked; // Number of hours worked in a week
float hourlyRate; // Employee's hourly rate
float totalPay; // total hours worked plus the hourly rate
float overtimePay; // overtime pay
// Show Program Welcome Message
System.out.println( "Employee Weekly Payroll Program\n\n"); // Tells program to show Welcome Message
// while(true) // will loop until STOP is entered no particular letter case expected
{
input = new Scanner (System.in);
// Show an Empty Line
System.out.println( );
// On screen prompt for Employee Name
System.out.print( "Enter your name: " );
employeeName = input.nextLine(); // read the employee's name from memory
do
{
// Prompt for Employee's Hourly Rate
System.out.print( "Enter hourly rate: " ); // prompt message
hourlyRate = input.nextFloat(); // reads employee's hourly rate
if (hourlyRate < 0)
{
System.out.println( "ERROR: The hourly rate must be a positive number." ); // show error message
}
} while ( hourlyRate < 0 ); // end do while statement
do
{
System.out.print( "Enter total hours worked this week: " ); // prompts the employee for hours worked for the week
hoursWorked = input.nextFloat(); // reads number of hours entered and stores into memory
if (hoursWorked < 0)
{
System.out.println( "ERROR: The number of hours worked must be a positive number." ); // shows error message
}
} while ( hoursWorked < 0 ); // end do while statement
// Calculate Employee's Pay
totalPay = hourlyRate * hoursWorked;
overtimePay = totalPay + overtimePay;
// Calculate OVERTIME HOURS
if (hoursWorked > 40)
{
overtimePay = (hoursWorked - 40) * (hourlyRate * 1.5);
}
else
{ totalPay = hoursWorked * hourlyRate;
overtimePay = 0;
}
// Output program results to the display
System.out.println( "\n\nWEEKLY PAYROLL RESULTS"); // Show Exit Message
System.out.println( "\n\nEmployee Name: " + employeeName);
System.out.println( "Gross Pay: " + currency.format(totalPay) );
System.out.println( "Overtime Hours: " + overtimePay); //OVERTIME PAY
// Show an Empty Line
System.out.println( );
} // end while statement
} // end method main
} //end class Payroll
#4
Re: Calculating Overtime Pay In Java
Posted 07 January 2011 - 10:32 PM
Since you are using floats, you have to convert the number to float.
overtimePay = (hoursWorked - 40) * (hourlyRate * 1.5);
By default the 1.5 is a double and thus evaluates incorrectly. You need to explicitly make it a float:
overtimePay = (hoursWorked - 40) * (hourlyRate * 1.5f);
However, in general, I suggest just using doubles. You will get more precision.
overtimePay = (hoursWorked - 40) * (hourlyRate * 1.5);
By default the 1.5 is a double and thus evaluates incorrectly. You need to explicitly make it a float:
overtimePay = (hoursWorked - 40) * (hourlyRate * 1.5f);
However, in general, I suggest just using doubles. You will get more precision.
#5
Re: Calculating Overtime Pay In Java
Posted 07 January 2011 - 11:21 PM
Thank you so much! I changed it to doubles as you recommended and fixed a variable error and it worked!
#6
Re: Calculating Overtime Pay In Java
Posted 07 January 2011 - 11:33 PM
You're very welcome. Glad I could help.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|