Share
Explore BrainMass

Question about A java program to read values and calculate payroll

Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should
prompt the user to enter a positive amount.

The teacher posted that this is what the program should have looked like:
import java.util.Scanner;

public class Payroll
{
public static void main (String args[])
{

String name;
double payRate;
double hours;
double weeklyPay;

Scanner input = new Scanner(System.in);

System.out.print("Enter name: ");
name = input.next();

System.out.print("Enter payrate: "); // prompt
payRate = input.nextDouble();

System.out.print("Enter hours: "); // prompt
hours = input.nextDouble();

weeklyPay = payRate * hours;

System.out.print(name + " earned ");
System.out.printf("$ %.2fn", weeklyPay);

} //end of main method

} // end of Payroll class

Can you do the modifications and post the solution in the correct format, or tell me how I get it to the .java file extension?

Solution Preview

We have two main things we have to add to the program provided in the question. First, we need to add a loop in the main function to continue asking for data until the user requests a stop. Second, we have to make sure that the values entered are positive.

In order to check that the values are positive we should have a routine that reads in the numbers. That way the details of doing that are not in the main function. The method to do this is:

// This method is a generic method to read in a positive number. The ...

Solution Summary

This solution demonstrates how to write a Java program to read in valid integer values and calculate payroll information from them. There is some discussion in the solution to explain how the program works.

$2.19