Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Beginner 1

Status
Not open for further replies.

tbarnes2788

Programmer
Feb 26, 2004
7
0
0
US
I need help!! I am writing a problem the in interactive and will calculate a companys payroll. My problem is I can not get the program to execute all after first method. That is it will allow me to enter the requested information but will stop after info entered and displayed. Also having a problem with my "if" statement. Here's the code, please tell me what I'm doing wrong? Thanks

import javax.swing.*;
public class Pay
{
public static void main(String[] args) throws Exception{
int hoursWorked = 0;
System.out.println("Enter hours worked");
System.out.println(" 40 hours");
System.out.println(" 45 hours");
System.out.println(" 50 hours");
String hrsWork = JOptionPane.showInputDialog("Enter Hours Worked");
hoursWorked = Integer.parseInt(hrsWork);
System.out.println(hoursWorked);
double payRate=0;
System.out.println("Enter Pay Rate");
System.out.println("7.00");
System.out.println("10.00");
System.out.println("12.00");
String rate = JOptionPane.showInputDialog("Enter Pay Rate");
payRate= Integer.parseInt(rate);
System.out.println(payRate); }
public double calculatePay(double payRate, double regularPay){
double pRate = payRate;
regularPay = pRate * 40;
System.out.println( "Regular Pay is " + regularPay);
return regularPay;}
public double overTimePay(double hoursWorked, double payRate){
double overTimePay;
double hrsWk = hoursWorked;
overTimePay = (hoursWorked - 40) * 2 * payRate;
System.out.println("OverTimePay is " + (hoursWorked - 40) * 2 * payRate);
return overTimePay;}
if(hoursWorked > 40){
regularPay = 40 * payRate;
overTimePay = (hoursWorked - 40) * 2 * payRate;}
else{
regularPay= hoursWorked * payRate;
overTimePay = 0.0;}
System.out.println("Regular Pay for 40 hours is " + regularPay);
System.out.println("Overtime Pay is " + overTimePay);
}
 
Well first off you created methods to calculate Pay and overtime pay but never call them. Another thing is that you’re if statement is outside of the main block. I modified your code a little to get it to run.

Code:
import javax.swing.*;
public class Pay
{
	public static void main(String[] args) throws Exception
	{
		Pay companyPayroll = new Pay();
		companyPayroll.doPayRoll();
	}
	public Pay() {}
	public static void doPayRoll()
	{

		int 	hoursWorked = 0;
		double 	payRate		= 0;
		String 	rate 		= "";
		double 	rPay 		= 0;
		double 	oTimePay 	= 0;
		int 	answer		= 0;

		System.out.println("Enter hours worked");
		System.out.println(" 40 hours");
		System.out.println(" 45 hours");
		System.out.println(" 50 hours");

		String hrsWork = JOptionPane.showInputDialog("Enter Hours Worked");

		hoursWorked = Integer.parseInt(hrsWork);
		System.out.println(hoursWorked);

		System.out.println("Enter Pay Rate");
		System.out.println("7.00");
		System.out.println("10.00");
		System.out.println("12.00");

		rate = JOptionPane.showInputDialog("Enter Pay Rate");
		payRate= Integer.parseInt(rate);
		System.out.println(payRate);

		if(hoursWorked > 40)
		{
			rPay = calculatePay(payRate);
			oTimePay = overTimePay(hoursWorked, payRate);
		}
		else
		{
			rPay= hoursWorked * payRate;
			oTimePay = 0.00;
		}

		System.out.println("Regular Pay for 40 hours is " + rPay);
		System.out.println("Overtime Pay is " + oTimePay);

		answer  = JOptionPane.showConfirmDialog(null,"Are you done?", "Please Choose One",
												JOptionPane.YES_NO_OPTION);
		if (answer != JOptionPane.YES_OPTION)
		{
			doPayRoll();
		}
		else
		{
			System.exit(2);
		}
	}
    public static double calculatePay(double payRate)
    {
		double pRate = payRate;
		double regularPay;
		regularPay = pRate * 40;
		System.out.println( "Regular Pay is " + regularPay);
		return regularPay;
	}
	public static double overTimePay(double hoursWorked, double payRate)
	{
		double overTimePay;
		double hrsWk = hoursWorked;
		overTimePay = (hoursWorked - 40) * 2 * payRate;
		System.out.println("OverTimePay is " + (hoursWorked - 40) * 2 * payRate);
		return overTimePay;
	}

}
 
Thanks a million, I have really been stressing about this code. My text book is very confusing and I 'm taking this course on line. Do you have any suggestion on good reference books that will help me understand what I'm doing?
 
the best book ever is head first java by kathy sierra and bert bates. I know there are a lot of free stuff out there but this book is really great. Kathy also hosts the site javaranch.com

~za~
You can't bring back a dead thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top