[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

payroll swing applet version 2

Posted on 2003-10-22
18
Medium Priority
?
624 Views
Last Modified: 2013-12-29
I need to have a payroll Swing applet that allows the user to enter two double values: hours worked and hourly rate. When the user clicks a JButton, gross pay is calculated.

In addition I need to have the federal withholding tax substracted from gross pay based on:

Income$                 Withholding%
0 to 99.99                      10
100 to 299.99      15
300 to 599.99      21
600 and up      28

I am stuck trying to have the program to substract in %.
Any help would be appreciated. Here is my code:

// Stephanie De Bernardi
// POS 406

// Modify the payroll Swing applet so that federal withholding tax is substracted from gross pay based on:
// 0 to 99.99 = 10%
// 100 to 299.99 = 15%
// 300 to 599.99 = 21%
// 600 and up = 28%


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCalculatePay2 extends JApplet implements ActionListener
{
    JLabel companyName = new
    JLabel("Accounting Inc");
    JButton calcButton = new JButton("Calculate");
    JButton okButton = new JButton("OK");
    JLabel perPersonResult = new JLabel("Payroll.");
    JLabel totalResult = new JLabel("Calculate hours worked and hourly rate");
    int totalPay;

    Font bigFont = new Font("Helvetica", Font.ITALIC, 24);

    public void init()
    {
         Container con = getContentPane();
         con.setLayout(new FlowLayout());
         companyName.setFont(bigFont);
         con.add(companyName);
         con.add(calcButton);
         con.add(okButton);
         calcButton.addActionListener(this);
         okButton.addActionListener(this);
         con.add(perPersonResult);
         con.add(totalResult);
    }

    public void start()
    {
         perPersonResult.setText("Your pay is.");
         totalResult.setText("Calculate hours worked and hourly rate");
         repaint();
    }

public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if(source == calcButton)
    {

          String response = JOptionPane.showInputDialog(null, "Enter the number of hours worked");
          int[] hoursLimit = {0, 100, 300, 600};
          int[] withholding = {10%, 15%, 21%, 28%};
          int hourlyRate = Integer.parseInt(response);

          response = JOptionPane.showInputDialog(null, "Enter the hourly rate");
          int hoursWorked = Integer.parseInt(response);


    totalPay = (hoursWorked * hourlyRate) - "%;
    totalResult.setText("$" + totalPay);
}
}

Thanks
0
Comment
Question by:stdebernardi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 3
  • 2
  • +1
18 Comments
 
LVL 15

Expert Comment

by:Javatm
ID: 9602179
To stdebernardi;

>> I am stuck trying to have the program to substract in %.

     It will be better to if you will get the %percentage 1st before you subtract it
 
>> totalPay = (hoursWorked * hourlyRate) - percentage;
     
Hope it helps . . .
JAVATM
         
0
 
LVL 3

Expert Comment

by:savalou
ID: 9602319
Something flexible would be like:

// calculate gross pay
int percentage = 0;
for (int i = hoursLimit.length; i > 0; --i) {
   if (grossPay >= hoursLimit[i]) {
      percentage = withholding[i];
      break;
   }
}
totalPay = grossPay * (1.0 - percentage);

Of course, if you are expressing percentages as integers, then divide by 100 first.

I'm a bit confused by your code, are you actually trying to use % signs in it?  % is the modulus operator.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602332
Do something like this, just a simple if & else statement;

// 0 to 99.99 = 10%
// 100 to 299.99 = 15%
// 300 to 599.99 = 21%
// 600 and up = 28%

double x;
double y;

if (x<='99') {

y = x  *  10;

}

else if (x<='299') {

y = x * 15;

}

else if (x<='599) {

y = x * 21;

else if (x>='600') {

y = x * 28;

}

totalPay = (hoursWorked * hourlyRate) - y;

Hope this helps . . .
JAVATM





 
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Expert Comment

by:jimmack
ID: 9602333
int wholding = 0;

for (int i = 0; i < 4; i++)
{
    if (hoursWorked > hoursLimit[i])
    {
        wholding = withholding[i];
    }
}

This will give you the appropriate percentage (based on your two arrays).

To start with, it assumes 0% and 0 hours.  It then steps through the hoursLimit array.  If the hoursWorked exceeds the limit, it sets the new withholding percentage (wholding).

I'll leave the rest of the calculations to you :-)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602353
Oops Sorry I'm at work this should work;

double x;
double y;

if (x<='99.99') {

y = x  *  10;

}

else if (x<='299.99') {

y = x * 15;

}

else if (x<='599.99') {

y = x * 21;

}

else if (x>='600.99') {

y = x * 28;

}

totalPay = (hoursWorked * hourlyRate) - y;

Hope it helps . . .
JAVATM
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9602367
Javatm.  There's no apostrophes around double values.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602397
Ok sorry I'm at work ! :)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602432
Ops sorry it should be like

Like : y = x * .28;

Use . ok!
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602684
// Try this :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCalculatePay2 extends JApplet implements ActionListener
{
    JLabel companyName = new
    JLabel("Accounting Inc");
    JButton calcButton = new JButton("Calculate");
    JButton okButton = new JButton("OK");
    JLabel perPersonResult = new JLabel("Payroll.");
    JLabel totalResult = new JLabel("Calculate hours worked and hourly rate");
    double totalPay;
    double x;
    double y;

    Font bigFont = new Font("Helvetica", Font.ITALIC, 24);

    public void init()
    {
         Container con = getContentPane();
         con.setLayout(new FlowLayout());
         companyName.setFont(bigFont);
         con.add(companyName);
         con.add(calcButton);
         con.add(okButton);
         calcButton.addActionListener(this);
         okButton.addActionListener(this);
         con.add(perPersonResult);
         con.add(totalResult);
    }

    public void start()
    {
         perPersonResult.setText("Your pay is.");
         totalResult.setText("Calculate hours worked and hourly rate");
         repaint();
    }

public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if(source == calcButton)
    {
    String response = JOptionPane.showInputDialog(null, "Enter the number of hours worked");
    double hourlyRate = Integer.parseInt(response);

    response = JOptionPane.showInputDialog(null, "Enter the hourly rate");
    double hoursWorked = Integer.parseInt(response);

    try {

    if (x<=99.99) {
    y = x  * .10;
    }
    else if (x<=299.99) {
    y = x * .15;
    }
    else if (x<=599.99) {
    y = x * .21;
    }
    else if (x>=600.99) {
    y = x * .28;
    }
    }
    catch(NumberFormatException ex) {
    JOptionPane.showInputDialog(null, "Invalid Value Entered","Error . . .",JOptionPane.ERROR_MESSAGE);
    }

    totalPay = (hoursWorked * hourlyRate) - y;
    totalResult.setText("$" + totalPay);
    }
}
}

Hope it helps . . .
JAVATM
0
 
LVL 3

Expert Comment

by:savalou
ID: 9602921
Javatm, dude/dudette, where does x get it's value?
0
 
LVL 15

Accepted Solution

by:
Javatm earned 1800 total points
ID: 9603040
Sorry I'm at work have mercy on me :)
Try this :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JCalculatePay2 extends JApplet implements ActionListener
{
    JLabel companyName = new
    JLabel("Accounting Inc");
    JButton calcButton = new JButton("Calculate");
    JButton okButton = new JButton("OK");
    JLabel perPersonResult = new JLabel("Payroll.");
    JLabel totalResult = new JLabel("Calculate hours worked and hourly rate");
    double totalPay;
    double x;

    Font bigFont = new Font("Helvetica", Font.ITALIC, 24);

    public void init()
    {
         Container con = getContentPane();
         con.setLayout(new FlowLayout());
         companyName.setFont(bigFont);
         con.add(companyName);
         con.add(calcButton);
         con.add(okButton);
         calcButton.addActionListener(this);
         okButton.addActionListener(this);
         con.add(perPersonResult);
         con.add(totalResult);
    }

    public void start()
    {
         perPersonResult.setText("Your pay is.");
         totalResult.setText("Calculate hours worked and hourly rate");
         repaint();
    }

public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if(source == calcButton)
    {
    String response = JOptionPane.showInputDialog(null, "Enter the number of hours worked");
    double hourlyRate = Integer.parseInt(response);

    response = JOptionPane.showInputDialog(null, "Enter the hourly rate");
    double hoursWorked = Integer.parseInt(response);

    try {

// For hourlyRate

    if (hourlyRate<=99.99) {
    x = 99.99  * .10;
    }
    else if (hourlyRate<=299.99) {
    x = 299.99 * .15;
    }
    else if (hourlyRate<=599.99) {
    x = 599.99 * .21;
    }
    else if (hourlyRate>=600.99) {
    x = 600.99 * .28;
    }

// For hoursWorked

    if (hoursWorked<=99.99) {
    x = 99.99  * .10;
    }
    else if (hoursWorked<=299.99) {
    x = 299.99 * .15;
    }
    else if (hoursWorked<=599.99) {
    x = 599.99 * .21;
    }
    else if (hoursWorked>=600.99) {
    x = 600.99 * .28;
    }

    }
    catch(NumberFormatException ex) {
    JOptionPane.showInputDialog(null, "Invalid Value Entered","Error . . .",JOptionPane.ERROR_MESSAGE);
    }

    totalPay = (hoursWorked * hourlyRate) - x;
    totalResult.setText("$" + totalPay);
    }
}
}

Hope this helps . . .
JAVATM
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9603073
If you want to you can customize it like :

//  For hourlyRate
x = hourlyRate * .28;

and

//  For hoursWorked
x = hoursWorked * .28;


0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 200 total points
ID: 9603189
Here is the replacement for your actionPerformed method.  Just put this into your original code.

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source == calcButton)
        {
   
             String response = JOptionPane.showInputDialog(null, "Enter the number of hours worked");
             double[] payLimit = {0.0, 100.0, 300.0, 600.0};
             double[] withholding = {0.10, 0.15, 0.21, 0.28};
             try
             {
                 double hourlyRate = Double.parseDouble(response);
       
                 response = JOptionPane.showInputDialog(null, "Enter the hourly rate");
                 double hoursWorked = Double.parseDouble(response);
       
                 double totalPay = (hoursWorked * hourlyRate);
   
                 double wholding = 0.0;
                 for (int i = 0; i < 4; i++)
                 {
                     if (totalPay > payLimit[i])
                     {
                         wholding = withholding[i];
                     }
                 }
   
                 totalPay = totalPay - (totalPay * wholding);
                 
                 DecimalFormat fmt = new DecimalFormat("#.00");
                 totalResult.setText("$" + fmt.format(totalPay));
             }
             catch (NumberFormatException nfe)
             {
                 totalResult.setText("Only enter decimal numbers");
             }
        }
    }
0
 

Author Comment

by:stdebernardi
ID: 9603744
Jimmack,

I am getting the following error for the latest version you proposed.

C:\Documents and Settings\Stephanie\Desktop\Program\WEEK 5\JCalculatePay\JCalculatePay2.java:77: cannot resolve symbol
symbol  : class DecimalFormat
location: class JCalculatePay2
                 DecimalFormat fmt = new DecimalFormat("#.00");
                 ^
C:\Documents and Settings\Stephanie\Desktop\Program\WEEK 5\JCalculatePay\JCalculatePay2.java:77: cannot resolve symbol
symbol  : class DecimalFormat
location: class JCalculatePay2
                 DecimalFormat fmt = new DecimalFormat("#.00");
                                         ^

I will take a closer look at the several solutions presented by all of you, but until now, none of them compile correctly even when I try to change or debug. But thanks anyway. Hopefully we will find a solution.
0
 

Author Comment

by:stdebernardi
ID: 9603887
After looking at every solutions I found one that works.
It seems always so easy when you actually see the errors you did or what you missed.
Thanks to all of you for your help, I understood my errors.
Thanks again.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9603916
If your receiving that error try to add this to your import statement.

import java.text.DecimalFormat;

or

import java.text.*;

hope this helps . . .
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9603955
Thanks dude, I dont actually have a compiler or a jdk on my machine.
I just tried to compile it in the web because I'm at work. Anyways thanks again.
I just want to correct something.

JOptionPane.showInputDialog(null, "Invalid Value Entered","Error . . .",JOptionPane.ERROR_MESSAGE);

Change it to this :

JOptionPane.showMessageDialog(null, "Invalid Value Entered","Error . . .",JOptionPane.ERROR_MESSAGE);

because your declaring a message.
JAVATM
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9604463
Sorry I think your a girl :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question