By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,470 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,470 IT Pros & Developers. It's quick & easy.

Need help with payroll program.

P: 3
I am trying to add the following information to a payroll program I am working on for assignment. Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Her's what I have.

// Calculate.java
// Calculate weekly pay.

import java.util.Scanner;


public class calculate
{
// main method begins execution of java application
public static void main( String args[] )
{
Scanner input = new Scanner ( System.in );
double hourlyrate;
double hours;
double pay;

//boolean stop = false; // Exit the loop below

//loop until user enters "stop" as the employee name:

// while (!stop)

// create scanner to obtain data from user


System.out.print( "Please enter employee name or stop to exit program: " );
String name = input.nextLine();

while (name != "stop"){

System.out.println("Please enter hourly rate: $" );}

hourlyrate = input.nextDouble();

if (hourlyrate >= 0){

System.out.println("hourly rate is: " + hourlyrate);}

if (hourlyrate <= 0){

System.out.println("Hourly rate must be a positive value. " + "Please enter the hourly rate again: $");}

hourlyrate = input.nextDouble();

System.out.println("Please enter hours worked: ");

hours = input.nextDouble();

if (hours >=0){

System.out.println("hours is: " + hours);}

if (hours <=0){

System.out.println("Hours worked must be a positive value. " + "Please enter the hours worked again: ");}

hours = input.nextDouble();

pay = hourlyrate * hours;

input.nextLine();

System.out.printf("%s pay is $ %.2f\n",name, pay);




// Display employee information


} // end method main

} // end class Calculate
Jan 30 '07 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
I am trying to add the following information to a payroll program I am working on for assignment. Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Her's what I have.

// Calculate.java
// Calculate weekly pay.

import java.util.Scanner;


public class calculate
{
// main method begins execution of java application
public static void main( String args[] )
{
Scanner input = new Scanner ( System.in );
double hourlyrate;
double hours;
double pay;

//boolean stop = false; // Exit the loop below

//loop until user enters "stop" as the employee name:

// while (!stop)

// create scanner to obtain data from user


System.out.print( "Please enter employee name or stop to exit program: " );
String name = input.nextLine();

while (name != "stop"){

System.out.println("Please enter hourly rate: $" );}

hourlyrate = input.nextDouble();

if (hourlyrate >= 0){

System.out.println("hourly rate is: " + hourlyrate);}

if (hourlyrate <= 0){

System.out.println("Hourly rate must be a positive value. " + "Please enter the hourly rate again: $");}

hourlyrate = input.nextDouble();

System.out.println("Please enter hours worked: ");

hours = input.nextDouble();

if (hours >=0){

System.out.println("hours is: " + hours);}

if (hours <=0){

System.out.println("Hours worked must be a positive value. " + "Please enter the hours worked again: ");}

hours = input.nextDouble();

pay = hourlyrate * hours;

input.nextLine();

System.out.printf("%s pay is $ %.2f\n",name, pay);




// Display employee information


} // end method main

} // end class Calculate
Hey, the question says you should write the Employee class. Write code for that and post it and we will be able to help where you are wrong.
Jan 30 '07 #2

P: 3
Hey, the question says you should write the Employee class. Write code for that and post it and we will be able to help where you are wrong.
public class Employee
{

private String employeeName; // name for wages
private double hourlyrate;
private double hours;

// method to set the name
public void setEmployeeName( String name )
{
employeeName = name; // store name
} // end method setEmployeeName

// method to retrieve the employee name
public String getEmployeeName()
{
return employeeName;
} // end method getEmployeeName

// display employee name
public void displayMessage()
{
// this statement calls getEmployeeName
System.out.printf( "Employee's name is: %s\n",
getEmployeeName() );
} // end method displayMessage
public void setHourlyrate( double hourlyrate )
{
hourlyrate = hourlyrate;
}

public double getHourlyrate(){
return hourlyrate;
}
public void setHours( double hours )
{
hours = hours;
}

public double getHours(){
return hours;
}
public double getPay(){
return hourlyrate * hours;
}

} // end class Employee

// Calculate.java
// Calculate weekly pay.

import java.util.Scanner;


public class calculate
{
// main method begins execution of java application
public static void main( String args[] )
{
Scanner input = new Scanner ( System.in );

double hourlyrate;
double hours;
double pay;

//boolean stop = false; // Exit the loop below

//loop until user enters "stop" as the employee name:

// while (!stop)

// create scanner to obtain data from user

Employee aEmployee = new Employee();

System.out.println( "Please enter employee name or stop to exit program: " );
String name = input.nextLine();
aEmployee.setEmployeeName( name );
System.out.println();
aEmployee.displayMessage();

while (name != "stop"){

System.out.println( "Program Ended" );

System.out.println("Please enter hourly rate: $" );}

hourlyrate = input.nextDouble();

if (hourlyrate >= 0){

System.out.println("hourly rate is: " + hourlyrate);}

if (hourlyrate <= 0){

System.out.println("Hourly rate must be a positive value. " + "Please enter the hourly rate again: $");}

hourlyrate = input.nextDouble();

System.out.println("Please enter hours worked: ");

hours = input.nextDouble();

if (hours >=0){

System.out.println("hours is: " + hours);}

if (hours <=0){

System.out.println("Hours worked must be a positive value. " + "Please enter the hours worked again: ");}

hours = input.nextDouble();

pay = hourlyrate * hours;

input.nextLine();

System.out.printf("%s pay is $ %.2f\n",name, pay);




// Display employee information


} // end method main

} // end class Calculate
Feb 1 '07 #3

10K+
P: 13,264
public class Employee
{

private String employeeName; // name for wages
private double hourlyrate;
private double hours;

// method to set the name
public void setEmployeeName( String name )
{
employeeName = name; // store name
} // end method setEmployeeName

// method to retrieve the employee name
public String getEmployeeName()
{
return employeeName;
} // end method getEmployeeName

// display employee name
public void displayMessage()
{
// this statement calls getEmployeeName
System.out.printf( "Employee's name is: %s\n",
getEmployeeName() );
} // end method displayMessage
public void setHourlyrate( double hourlyrate )
{
hourlyrate = hourlyrate;
}

public double getHourlyrate(){
return hourlyrate;
}
public void setHours( double hours )
{
hours = hours;
}

public double getHours(){
return hours;
}
public double getPay(){
return hourlyrate * hours;
}

} // end class Employee

// Calculate.java
// Calculate weekly pay.

import java.util.Scanner;


public class calculate
{
// main method begins execution of java application
public static void main( String args[] )
{
Scanner input = new Scanner ( System.in );

double hourlyrate;
double hours;
double pay;

//boolean stop = false; // Exit the loop below

//loop until user enters "stop" as the employee name:

// while (!stop)

// create scanner to obtain data from user

Employee aEmployee = new Employee();

System.out.println( "Please enter employee name or stop to exit program: " );
String name = input.nextLine();
aEmployee.setEmployeeName( name );
System.out.println();
aEmployee.displayMessage();

while (name != "stop"){

System.out.println( "Program Ended" );

System.out.println("Please enter hourly rate: $" );}

hourlyrate = input.nextDouble();

if (hourlyrate >= 0){

System.out.println("hourly rate is: " + hourlyrate);}

if (hourlyrate <= 0){

System.out.println("Hourly rate must be a positive value. " + "Please enter the hourly rate again: $");}

hourlyrate = input.nextDouble();

System.out.println("Please enter hours worked: ");

hours = input.nextDouble();

if (hours >=0){

System.out.println("hours is: " + hours);}

if (hours <=0){

System.out.println("Hours worked must be a positive value. " + "Please enter the hours worked again: ");}

hours = input.nextDouble();

pay = hourlyrate * hours;

input.nextLine();

System.out.printf("%s pay is $ %.2f\n",name, pay);




// Display employee information


} // end method main

} // end class Calculate
Going in the right direction but I'm still not interested yet. How do we create the Epmloyees if you haven't added a construcor for that.

Add two constructors, indent the code and post it using code tags.
Feb 1 '07 #4

P: 3
Going in the right direction but I'm still not interested yet. How do we create the Epmloyees if you haven't added a construcor for that.

Add two constructors, indent the code and post it using code tags.
I think I figured it out. I have another project involving an Inventory program. I can see from previous post that it's a popular topic.
Feb 1 '07 #5

10K+
P: 13,264
I think I figured it out. I have another project involving an Inventory program. I can see from previous post that it's a popular topic.
Good. I always like to see people find the solutions themselves. The inventory thing seems to be the thing of the moment.
Feb 2 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.