Program that calculates witholdings in a paychec?

Member_2_4942450
Member_2_4942450 used Ask the Experts™
on
I am looking to create a program in java that calculates pay.

Hours worked?
Rate of pay per hour?
Witholding rate?
Gross pay?
Net Pay?

I was thinking about creating 3 methods something like computeNetPay() method.
The computeNetPay receives the values for hours, pay rate and withholding rate.
It would then compute the gross pay and reduces it by the appropriate withholding amount to produce net pay.

To calculate gross is putting hours worked times pay per hour)

I want to use computerNetPay() to receive two parameters, they represent the hours and pay rate, and the withholding rate.
I can assume for this program that it is 15%

Hourly rate is assumed to be 5.85

Any help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
You're on the right track. What's the difficulty?

Author

Commented:
Not sure where to begin or even how to start.

I came with this program before but trying to see if I can use things before and modify them to this scenerio.


import javax.swing.JOptionPane;
import java.util.Scanner;
public class Eggs
{
        public static void main(String[] args)
        {

        String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of Eggs" , "You worked" );
        String mString = eggsString;
        System.out.println(mString);
        eggs = Integer.parseInt(mString);
        JOptionPane.showMessageDialog
                  (null, "You have eggs " + eggs/12 + " dozen " + eggs%12 + " eggs remaining.");

        }
}

Open in new window

Top Expert 2016

Commented:
e.g.
public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
	return hours * payRate * withholdingRatePercent / 100.0;
}

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Is that it?
I don't see the part where the user inputs the values?
Top Expert 2016

Commented:
Well i thought it was that method that you needed help on.
Top Expert 2016

Commented:
You just need to put it together with what you're already doing

Author

Commented:
Confused still how like this
public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * withholdingRatePercent / 100.0;
}

import javax.swing.JOptionPane;
import java.util.Scanner;
public class Eggs
{
        public static void main(String[] args)
        {

        String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );
        String mString = eggsString;
        System.out.println(mString);
        eggs = Integer.parseInt(mString);
        JOptionPane.showMessageDialog
                  (null, "You have eggs " + eggs/12 + " dozen " + eggs%12 + " eggs remaining.");

        }
}

Open in new window

Author

Commented:
How about something like this.

How can I put all this together or parts of this together to solve my problem I state above.

public class Employee
{
   private int empNum;
   private double empPayRate;
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:

Look at something like that and modify it apprpriately

import javax.swing.*;

public class Employee
{
   private int empNum;
   private double empPayRate;
   public Employee(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;

       System.out.println("Result: " + result);



   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
	return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
you're getting close, you can combine what you have to collectinput from user and then use it to call your method to calculate the net pay
finally display the result to the user.

let me know if you have any questions

import java.text.NumberFormat;

import javax.swing.JOptionPane;

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

		// Used for formatting currency values
		
		NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

		//  Collect input from user
		
		String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked", "You worked");
		double hoursWorked = Double.parseDouble(hoursWorkedString);
		String payrateString = JOptionPane.showInputDialog(null, "Enter Pay Rate", "Pay Rate");
		double payrate = Double.parseDouble(payrateString);
		String withholdingString = JOptionPane.showInputDialog(null, "Enter withholding percent rate", "Withholding");
		double withholding = Double.parseDouble(withholdingString);
		
		// Calculate net pay
		
		double netpay = computeNetPay(hoursWorked, payrate, withholding);
		JOptionPane.showMessageDialog(null, "Net Pay: "+currencyFormat.format(netpay));
		
	}
	
	public static double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * withholdingRatePercent / 100.0;
	}

}

Open in new window

Awarded 2011
Awarded 2011

Commented:
In the above code I combines all pieces above to show you how it
should more or less be cobined. Look at it and make sure
that you calculate correctly all necessary numbers - not just one
and name it appropritely - what is atually net pay, waht ios withholding, etc,
I was not paying attention to  these details.

Author

Commented:
Ok it returned this

Pay.java:7: invalid method declaration; return type required
   public Employee(){
          ^
Pay.java:52: class, interface, or enum expected
Toggle HighlightingOpen in New WindowSelect All
^
Pay.java:75: illegal character: \169
Copyright © 1996 - 2011 Experts Exchange, LLC. All rights reserved.
          ^
3 errors

Open in new window

Awarded 2011
Awarded 2011

Commented:
You should put my code in the file called Employee.java
Awarded 2011
Awarded 2011

Commented:
Java file has to be named the same way as the name of the class, otherwise it would not compile
Mick BarryJava Developer
Top Expert 2010

Commented:
if you don't need to input the rate and tax you can just hardcode them as in the following example

Your calculation in net pay aslo doesn't look right, it appears to be calculating tax instead of net pay.
import java.text.NumberFormat;

import javax.swing.JOptionPane;

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

		// Used for formatting currency values
		
		NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

		//  Collect input from user
		
		String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked", "You worked");
		double hoursWorked = Double.parseDouble(hoursWorkedString);
		double payrate = 5.85;
		double withholding = 15.0;
		
		// Calculate net pay
		
		double netpay = computeNetPay(hoursWorked, payrate, withholding);
		JOptionPane.showMessageDialog(null, "Net Pay: "+currencyFormat.format(netpay));
		
	}
	
	public static double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * (100.0 - withholdingRatePercent) / 100.0;
	}

}

Open in new window

Awarded 2011
Awarded 2011

Commented:
My code compiles and
if you enter 200 in the first widow,
5.85 in the second and
15 in the third it gives:

Result: 175.5
Mick BarryJava Developer
Top Expert 2010

Commented:
> Ok it returned this

open the code i posted in a new window and copy it from there
and save it to Eggs.java

If you want to rename it to Pay.java then change

> public class Eggs {

to:

public class Pay {
Awarded 2011
Awarded 2011

Commented:
When you enter numbers in the windows it is important that
they should replace initial text in the line - no piece of the text should be left
in the line

Author

Commented:
Ok looks good so far.

I need net pay where do I fix the calculation

When computeNetPay ( - receives one parameter, it represents the number of hours worked, the witholding rate is  assumed to be 15%, and the hourly rate is assumed to be 5.85.

Awarded 2011
Awarded 2011

Commented:
This will calculate net pay
hours*rate - withholding

import javax.swing.*;

public class Employee
{
   private int empNum;
   private double empPayRate;
   public Employee(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;



       System.out.println("Withholding: " + result);

       double ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       System.out.println("net pay " + ntepay);



   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
	return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
I fixed it in the code I posted, is it not working?
Mick BarryJava Developer
Top Expert 2010

Commented:
are you using this code?
import java.text.NumberFormat;

import javax.swing.JOptionPane;

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

		// Used for formatting currency values
		
		NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

		//  Collect input from user
		
		String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked", "You worked");
		double hoursWorked = Double.parseDouble(hoursWorkedString);
		double payrate = 5.85;
		double withholding = 15.0;
		
		// Calculate net pay
		
		double netpay = computeNetPay(hoursWorked, payrate, withholding);
		JOptionPane.showMessageDialog(null, "Net Pay: "+currencyFormat.format(netpay));
		
	}
	
	public static double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * (100.0 - withholdingRatePercent) / 100.0;
	}

}

Open in new window

Author

Commented:
Good except I need it displied in a dialog box

For example


Withholding: 8.775
net pay 49.725


Awarded 2011
Awarded 2011

Commented:
This is the result for 200, 5.85, 15:

Withholding: 175.5
net pay 994.5

Open in new window

Author

Commented:
Good except I need it displayed in a dialog box
Awarded 2011
Awarded 2011

Commented:
shows in a window

import javax.swing.*;

public class Employee
{
   private int empNum;
   private double empPayRate;
   public Employee(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;



       System.out.println("Withholding: " + result);

       double ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       System.out.println("net pay " + ntepay);
   JOptionPane.showMessageDialog(null,"net pay " + ntepay );


   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
	return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
to then expand it to calculate gross pay is then a simple matter of repeating what you did for gross pay
import java.text.NumberFormat;

import javax.swing.JOptionPane;

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

		// Used for formatting currency values
		
		NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

		//  Collect input from user
		
		String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked", "You worked");
		double hoursWorked = Double.parseDouble(hoursWorkedString);
		double payrate = 5.85;
		double withholding = 15.0;
		
		// Calculate net pay
		
		double netpay = computeNetPay(hoursWorked, payrate, withholding);
		JOptionPane.showMessageDialog(null, "Net Pay: "+currencyFormat.format(netpay));

		// Calculate gross pay
		
		double grosspay = computeGrossPay(hoursWorked, payrate);
		JOptionPane.showMessageDialog(null, "Gross Pay: "+currencyFormat.format(grosspay));

	}
	
	public static double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * (100.0 - withholdingRatePercent) / 100.0;
	}

	public static double computeGrossPay(double hours, double payRate) {
		// calulcate gross pay here
        return 0.0;
	}
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
> Good except I need it displayed in a dialog box

the code I posted already displays it in a dialog box. Sounds like you're running the wrong code :)
Awarded 2011
Awarded 2011

Commented:
A little bit more beautiful

import javax.swing.*;

public class Employee
{
   private int empNum;
   private double empPayRate;
   public Employee(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;



       System.out.println("Withholding: " + result);

       double ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       System.out.println("net pay " + ntepay);
   JOptionPane.showMessageDialog(null,"Net pay: " + ntepay  + " withholdong: " + result);


   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
	return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:
You are running the right code.

Author

Commented:
Here is the code I am running all just to make sure I am not getting a few of these post confused.

Below is the desired results that I need

Create a class named Pay that includes five double variables that hold hours worked, rate of pay per hour, witholding rate, gross pay , and net pay.
 
(Create three overloaded computeNetPay() methods. WHen computeNetPay() receives values for hours, pay rate, and witholding rate, computes the gross pay and reduces it by the appropriate witholding amount to produce the net pay. (Gross pay is computed as hours worked multiplied by pay per hour)
 
When computeNetPay() receives two parameters, represent the hours and pay rate and the withholding rate is assumed to be 15%.
 
When computeNetPay() receives one parameter, represent the number of hours worked, the witholding rate of 15% and the hourly rate of 5.85.
 
Then write a main() method that test all three overloaded method
import javax.swing.*;

public class Pay
{
   private int empNum;
   private double empPayRate;
   public Pay(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;



       System.out.println("Withholding: " + result);

       double ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       System.out.println("net pay " + ntepay);
   JOptionPane.showMessageDialog(null,"net pay " + ntepay );


   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:
little better still

import javax.swing.*;

public class Employee
{
   private int empNum;
   private double empPayRate;
   public Employee(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;



       System.out.println("Withholding: " + result);

       double grossPay =     Double.parseDouble(eggsString)*    Double.parseDouble(payRateString);
       double ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       System.out.println("net pay " + ntepay);
   JOptionPane.showMessageDialog(null,"Gross pay: " + grossPay + "   Net pay: " + ntepay  + "   Withholdong: " + result);


   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
	return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
> A little bit more beautiful

that code doesn't even call your computeNetPay() method
see the code I posted for how to correctly call your method, and display the results in a dialog
Mick BarryJava Developer
Top Expert 2010

Commented:
> Here is the code I am running all just to make sure I am not getting a few of these post confused.


that code is wrong and doesn't do what is asked, see the attached instead

import java.text.NumberFormat;

import javax.swing.JOptionPane;

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

		// Used for formatting currency values
		
		NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

		//  Collect input from user
		
		String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked", "You worked");
		double hoursWorked = Double.parseDouble(hoursWorkedString);
		double payrate = 5.85;
		double withholding = 15.0;
		
		// Calculate net pay
		
		double netpay = computeNetPay(hoursWorked, payrate, withholding);
		JOptionPane.showMessageDialog(null, "Net Pay: "+currencyFormat.format(netpay));

		// Calculate gross pay
		
		double grosspay = computeGrossPay(hoursWorked, payrate);
		JOptionPane.showMessageDialog(null, "Gross Pay: "+currencyFormat.format(grosspay));

	}
	
	public static double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * (100.0 - withholdingRatePercent) / 100.0;
	}

	public static double computeGrossPay(double hours, double payRate) {
		// calulcate gross pay here
        return 0.0;
	}
}

Open in new window

Author

Commented:
Objects !

IS THE BELOW CORRECT

Below is the desired results that I need

Create a class named Pay that includes five double variables that hold hours worked, rate of pay per hour, witholding rate, gross pay , and net pay.
 
(Create three overloaded computeNetPay() methods. WHen computeNetPay() receives values for hours, pay rate, and witholding rate, computes the gross pay and reduces it by the appropriate witholding amount to produce the net pay. (Gross pay is computed as hours worked multiplied by pay per hour)
 
When computeNetPay() receives two parameters, represent the hours and pay rate and the withholding rate is assumed to be 15%.
 
When computeNetPay() receives one parameter, represent the number of hours worked, the witholding rate of 15% and the hourly rate of 5.85.
 
Then write a main() method that test all three overloaded method
import java.text.NumberFormat;

import javax.swing.JOptionPane;

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

                // Used for formatting currency values
                
                NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

                //  Collect input from user
                
                String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked", "You worked");
                double hoursWorked = Double.parseDouble(hoursWorkedString);
                double payrate = 5.85;
                double withholding = 15.0;
                
                // Calculate net pay
                
                double netpay = computeNetPay(hoursWorked, payrate, withholding);
                JOptionPane.showMessageDialog(null, "Net Pay: "+currencyFormat.format(netpay));

                // Calculate gross pay
                
                double grosspay = computeGrossPay(hoursWorked, payrate);
                JOptionPane.showMessageDialog(null, "Gross Pay: "+currencyFormat.format(grosspay));

        }
        
        public static double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * (100.0 - withholdingRatePercent) / 100.0;
        }

        public static double computeGrossPay(double hours, double payRate) {
                // calulcate gross pay here
        return 0.0;
        }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:

with overlaoded metyhods
import javax.swing.*;

public class Employee
{
   private int empNum;
   private double empPayRate;
   public Employee(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;



       System.out.println("Withholding: " + result);

       double grossPay =     Double.parseDouble(eggsString)*    Double.parseDouble(payRateString);
       double ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       System.out.println("net pay " + ntepay);
   JOptionPane.showMessageDialog(null,"Gross pay: " + grossPay + "   Net pay: " + ntepay  + "   Withholdong: " + result);


   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
	return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

        public double computeNetPay(double hours, double payRate) {
	return hours * payRate * 15.0 / 100.0;
}

           public double computeNetPay(double hours) {
	return hours * 5.85 * 15.0 / 100.0;
}
    


    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
Let me know if you have any questions about the code
Mick BarryJava Developer
Top Expert 2010

Commented:
> with overlaoded metyhods

thats pretty much just repeating what I have already posted. Please stop doing that you're just confusing sobeservices2
Awarded 2011
Awarded 2011

Commented:
Don't be coinfused - you were running correct code - now I added
overloaded methods
Awarded 2011
Awarded 2011

Commented:
Objects, I posted the code first - you are just confsing the dialog
Awarded 2011
Awarded 2011

Commented:
Just add these overloadded methods to your code,
or use my latest code (rename it to Pay,java as you diod befiore
if you want)

        public double computeNetPay(double hours, double payRate) {
	return hours * payRate * 15.0 / 100.0;
}

           public double computeNetPay(double hours) {
	return hours * 5.85 * 15.0 / 100.0;
}
    

Open in new window

Author

Commented:
Ok you two

Relax

Below is the code object posted which right now I am running correctly and have no issues with

import java.text.NumberFormat;

import javax.swing.JOptionPane;

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

                // Used for formatting currency values
                
                NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

                //  Collect input from user
                
                String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked", "You worked");
                double hoursWorked = Double.parseDouble(hoursWorkedString);
                double payrate = 5.85;
                double withholding = 15.0;
                
                // Calculate net pay
                
                double netpay = computeNetPay(hoursWorked, payrate, withholding);
                JOptionPane.showMessageDialog(null, "Net Pay: "+currencyFormat.format(netpay));

                // Calculate gross pay
                
                double grosspay = computeGrossPay(hoursWorked, payrate);
                JOptionPane.showMessageDialog(null, "Gross Pay: "+currencyFormat.format(grosspay));

        }
        
        public static double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * (100.0 - withholdingRatePercent) / 100.0;
        }

        public static double computeGrossPay(double hours, double payRate) {
                // calulcate gross pay here
        return 0.0;
        }
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
> IS THE BELOW CORRECT

yes thats looking pretty close.
I left the computeGrossPay() calculation for you to complete

Author

Commented:
Here is - for ya
code

Using overloaded method which I think is working haven't check yet
import javax.swing.*;

public class Employee
{
   private int empNum;
   private double empPayRate;
   public Employee(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;



       System.out.println("Withholding: " + result);

       double grossPay =     Double.parseDouble(eggsString)*    Double.parseDouble(payRateString);
       double ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       System.out.println("net pay " + ntepay);
   JOptionPane.showMessageDialog(null,"Gross pay: " + grossPay + "   Net pay: " + ntepay  + "   Withholdong: " + result);


   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
        return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

        public double computeNetPay(double hours, double payRate) {
        return hours * payRate * 15.0 / 100.0;
}

           public double computeNetPay(double hours) {
        return hours * 5.85 * 15.0 / 100.0;
}
    


    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
question talks about three method but just describes two, not sure if thats a typo or not.

Author

Commented:
Yes I do need three methods.

You are correct "objects"
Mick BarryJava Developer
Top Expert 2010

Commented:
> Using overloaded method which I think is working haven't check yet

they are *not* overloaded methods btw
Awarded 2011
Awarded 2011

Commented:
No this just has them, but is not yet using them
Mick BarryJava Developer
Top Expert 2010

Commented:
to add the third method you would just repeat what has been done for the other two
so whats the 3rd method need to do?

Author

Commented:
Ok guys I think you both helped me out a good bit. Let me take a few mins to absorb these post and see what all went all. IT all came very fast very good

THough
Java Developer
Top Expert 2010
Commented:
The following better matches the details of the question
have left the last method for you to implement

import java.text.NumberFormat;

import javax.swing.JOptionPane;

public class Pay {
	
	private static double payrate = 5.85;
	private static double withholding = 15.0;
	
	public static void main(String[] args) {

		// Used for formatting currency values
		
		NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

		//  Collect input from user
		
		String hoursWorkedString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked", "You worked");
		double hoursWorked = Double.parseDouble(hoursWorkedString);
		
		// Calculate net pay
		
		double netpay = computeNetPay(hoursWorked, payrate);
		JOptionPane.showMessageDialog(null, "Net Pay: "+currencyFormat.format(netpay));

		// Calculate gross pay
		
		double grosspay = computeNetPay(hoursWorked);
		JOptionPane.showMessageDialog(null, "Gross Pay: "+currencyFormat.format(grosspay));

	}
	
	public static double computeNetPay(double hours, double payRate) {
        return hours * payRate * (100.0 - withholding) / 100.0;
	}

	public static double computeNetPay(double hours) {
		// calulcate gross pay here
        return 0.0;
	}
}

Open in new window

Awarded 2011
Awarded 2011
Commented:
This will use overloaded methods but press OK
if you don't eneter bvalue

import javax.swing.*;

public class Employee
{
   private int empNum;
   private double empPayRate;
   public Employee(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = -1;
       double grossPay = -1.0;
       double ntepay = -1.0;

       if(!withholdString.startsWith("With")  && !payRateString.startsWith("Pay")) {

       result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;
             grossPay =     Double.parseDouble(eggsString)*    Double.parseDouble(payRateString);
            ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       }        else if (!payRateString.startsWith("Pay")) {
              result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),15.0) ;
            grossPay =     Double.parseDouble(eggsString)*    Double.parseDouble(payRateString);
            ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       }         else       {
             result = computeNetPay(Double.parseDouble(eggsString), 5.85,15.0) ;
                 grossPay =     Double.parseDouble(eggsString)*   5.85;
            ntepay = Double.parseDouble(eggsString)*    5.85 - result;
       }



       System.out.println("Withholding: " + result);

      //grossPay =     Double.parseDouble(eggsString)*    Double.parseDouble(payRateString);
       // ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       System.out.println("net pay " + ntepay);
   JOptionPane.showMessageDialog(null,"Gross pay: " + grossPay + "   Net pay: " + ntepay  + "   Withholdong: " + result);


   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
	return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

        public double computeNetPay(double hours, double payRate) {
	return hours * payRate * 15.0 / 100.0;
}

           public double computeNetPay(double hours) {
	return hours * 5.85 * 15.0 / 100.0;
}
    


    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:
Here you can even press Cancel
where you don't need to enter data

import javax.swing.*;

public class Employee
{
   private int empNum;
   private double empPayRate;
   public Employee(){
                String eggsString = null;
        int eggs;
        eggsString = JOptionPane.showInputDialog(null, "Enter the Number of hours worked" , "You worked" );

        double payRate;
        String payRateString = JOptionPane.showInputDialog(null, "Enter Pay rate" , "Pay  rate:" );

       double withhold;

       String withholdString = JOptionPane.showInputDialog(null, "Enter withohlding %" , "Withholding %" );

       double result = -1.0;
       double grossPay = -1.0;
       double ntepay = -1.0;

       if((withholdString != null  && (!withholdString.startsWith("With"))  && (payRateString != null && !payRateString.startsWith("Pay")))) {

       result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),Double.parseDouble(withholdString)) ;
             grossPay =     Double.parseDouble(eggsString)*    Double.parseDouble(payRateString);
            ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       }        else if (payRateString != null && !payRateString.startsWith("Pay") ) {
              result = computeNetPay(Double.parseDouble(eggsString), Double.parseDouble(payRateString),15.0) ;
            grossPay =     Double.parseDouble(eggsString)*    Double.parseDouble(payRateString);
            ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       }         else       {
             result = computeNetPay(Double.parseDouble(eggsString), 5.85,15.0) ;
                 grossPay =     Double.parseDouble(eggsString)*   5.85;
            ntepay = Double.parseDouble(eggsString)*    5.85 - result;
       }



       System.out.println("Withholding: " + result);

      //grossPay =     Double.parseDouble(eggsString)*    Double.parseDouble(payRateString);
       // ntepay = Double.parseDouble(eggsString)*    Double.parseDouble(payRateString) - result;
       System.out.println("net pay " + ntepay);
   JOptionPane.showMessageDialog(null,"Gross pay: " + grossPay + "   Net pay: " + ntepay  + "   Withholdong: " + result);


   }
    public double computeNetPay(double hours, double payRate, double withholdingRatePercent) {
	return hours * payRate * withholdingRatePercent / 100.0;
}
   public void setValues()
   {
      empNum = 111;
      empPayRate = 22.22;
   }
   public void methodThatUsesInstanceAttributes()
   {
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }
   public void methodThatUsesLocalVariables()
   {
      int empNum = 33333;
      double empPayRate = 555.55;
      System.out.println("Employee number is " + empNum);
      System.out.println("Pay rate is " + empPayRate);
   }

        public double computeNetPay(double hours, double payRate) {
	return hours * payRate * 15.0 / 100.0;
}

           public double computeNetPay(double hours) {
	return hours * 5.85 * 15.0 / 100.0;
}
    


    public static void main(String [] args){
        new Employee();
    }
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial