Avatar of Member_2_4942450
Member_2_4942450

asked on 

Program that calculates witholdings in a paychec?

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?
JavaProgramming

Avatar of undefined
Last Comment
for_yan
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You're on the right track. What's the difficulty?
Avatar of Member_2_4942450
Member_2_4942450

ASKER

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Open in new window

Avatar of Member_2_4942450

ASKER

Is that it?
I don't see the part where the user inputs the values?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Well i thought it was that method that you needed help on.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You just need to put it together with what you're already doing
Avatar of Member_2_4942450

ASKER

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

Avatar of Member_2_4942450

ASKER

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

Avatar of for_yan
for_yan
Flag of United States of America image


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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

Avatar of for_yan
for_yan
Flag of United States of America image

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.
Avatar of Member_2_4942450

ASKER

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

Avatar of for_yan
for_yan
Flag of United States of America image

You should put my code in the file called Employee.java
Avatar of for_yan
for_yan
Flag of United States of America image

Java file has to be named the same way as the name of the class, otherwise it would not compile
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

Avatar of for_yan
for_yan
Flag of United States of America image

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> 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 {
Avatar of for_yan
for_yan
Flag of United States of America image

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
Avatar of Member_2_4942450

ASKER

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.

Avatar of for_yan
for_yan
Flag of United States of America image

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

I fixed it in the code I posted, is it not working?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

Avatar of Member_2_4942450

ASKER

Good except I need it displied in a dialog box

For example


Withholding: 8.775
net pay 49.725


Avatar of for_yan
for_yan
Flag of United States of America image

This is the result for 200, 5.85, 15:

Withholding: 175.5
net pay 994.5

Open in new window

Avatar of Member_2_4942450

ASKER

Good except I need it displayed in a dialog box
Avatar of for_yan
for_yan
Flag of United States of America image

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

> 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 :)
Avatar of for_yan
for_yan
Flag of United States of America image

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

Avatar of for_yan
for_yan
Flag of United States of America image

You are running the right code.
Avatar of Member_2_4942450

ASKER

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

Avatar of for_yan
for_yan
Flag of United States of America image

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

> 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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> 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

Avatar of Member_2_4942450

ASKER

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

Avatar of for_yan
for_yan
Flag of United States of America image


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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

Let me know if you have any questions about the code
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> with overlaoded metyhods

thats pretty much just repeating what I have already posted. Please stop doing that you're just confusing sobeservices2
Avatar of for_yan
for_yan
Flag of United States of America image

Don't be coinfused - you were running correct code - now I added
overloaded methods
Avatar of for_yan
for_yan
Flag of United States of America image

Objects, I posted the code first - you are just confsing the dialog
Avatar of for_yan
for_yan
Flag of United States of America image

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

Avatar of Member_2_4942450

ASKER

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

> IS THE BELOW CORRECT

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

ASKER

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

question talks about three method but just describes two, not sure if thats a typo or not.
Avatar of Member_2_4942450

ASKER

Yes I do need three methods.

You are correct "objects"
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

they are *not* overloaded methods btw
Avatar of for_yan
for_yan
Flag of United States of America image

No this just has them, but is not yet using them
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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?
Avatar of Member_2_4942450

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of for_yan
for_yan
Flag of United States of America image

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

Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo