Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with screen output in applet

 
Larry Brink
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, I am new to java, I have been working on an applet, which so far compiles and mostly works okay. The problem is the screen output after the calculation in the ActionPerformed Method, there is none showing. I am hoping some one will help point me in the right direction so I can figure out why. At this point, I have been working on this applet long enough that I think I am brain Dead. My code is below.
import javax.swing.*; //Must import both of these
import java.awt.*; //for every java applet
import java.awt.event.*; // for the event class,(ActionListener)
import java.applet.*; //Must import for extends Applet
import java.text.*;
/**
* Program for the CalcPay class.
*
* @author Larry Brink
* @version 11/18/2003
*/
public class CalcPay extends JApplet implements ActionListener
{
//need in every applet, the container contains eveything in the applet
Container con = getContentPane();
//creating label for enter hours
JLabel enterHours = new JLabel("Enter Hours Worked");
//creating label for enter rate
JLabel enterRate = new JLabel("Enter Hourly Rate");
Label seeStart = new Label("");
//creating label for gross pay
JLabel gross = new JLabel("");
//creating label for net pay
JLabel net = new JLabel("");
//creating label for withholding
JLabel holding = new JLabel("");
//creating Button for calculate pay
JButton calculatePay = new JButton("Calculate Pay");
//creating text field for entering hours worked
JTextField hoursWorked = new JTextField(2);
//creating text field for entering hourly rate
JTextField hourlyRate = new JTextField(5);
//Font myNewFont changes the Font, the "+" needs to be used to use both Italic and Bold
Font myFont = new Font ("Arial", Font.BOLD +Font.ITALIC, 14);

/**
* Description of the Method
*/
public void init() // initialize the Method
{
//setting the Container Layout, different way then used above
con.setLayout (new FlowLayout());
//setting the font style for the JLabels
enterHours.setFont (myFont);
enterRate.setFont (myFont);
calculatePay.setFont (myFont);
con.add (enterHours); //adds the JLabel enterHours
con.add (hoursWorked); //adds the JTextField hoursWorked
con.add (enterRate); //adds the JLabel enterRate
con.add (hourlyRate); //adds the JTextField hourlyRate
calculatePay.setForeground(Color.red);
calculatePay.setBackground(Color.blue);
con.add (calculatePay);//adds the JButton calculatePay
con.add(gross); //adds the JLabel gross pay
con.add(net); //adds the JLabel net pay
con.add(holding); //adds the JLabel withholding
calculatePay.addActionListener(this);
hoursWorked.requestFocus();
}

/**
* Description of the method
* param thisCalc Description of the parameter
*/

public void actionPerformed(ActionEvent thisCalc)
{
int[] income = {0, 100, 300, 600};
int[] withHolding = {10, 15, 21, 28};
int hours = Integer.parseInt(hoursWorked.getText ());
double rate = Double.parseDouble(hourlyRate.getText ());
int withHoldingAmount = 0;
double netPay = 0;
double grossPay = 0;
int x = income.length;

for (x = income.length; x >= 0; --x)
{
if (grossPay >= income [x])
{
withHoldingAmount = withHolding[x];
grossPay = hours * rate;
netPay = grossPay - withHoldingAmount;
x = 0;

}
}
gross.setText("Your gross pay is $ " + grossPay);
net.setText("Your net pay is $ " + netPay);
holding.setText("Your withholding is $ " + withHoldingAmount);
}
}
Please take it easy on me, I am new to this. Thanks in advanced for any and all help.
Larry
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Larry ...
We've all been there before, staring at the same block of code until our eyes start to cross. But you're almost there. Look at this line:
for (x = income.length; x >= 0; --x)
Recall that the largest index into an array is one less than the length. Try starting the "for" loop at x = income.length - 1 and see if it doesn't work a little better.
Good luck with Java!
[ November 24, 2003: Message edited by: Wayne L Johnson ]
 
Larry Brink
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your prompt reply Wayne, I did finnaly get the output to show. But, isn't there always a but, any way now the output shows, the calculation is correct for gross pay, and for net pay, the problem now is, or I am thinking something with the array. If, for example a persons income is 0 - 99.99, then the withholding would be 10. For 100 - 299.99, the withholding should be 15, and so on. Now that is where the problem comes in. For some reason the withholding is stuck on, or only shows 10, instead of coresponding to the income array.
I did set the withHoldingAmount = withHolding [x];
Now that should make the withHolding correspond to the income, correct.
once again here is my code.

Again, I would appreciate either a point in the right direction, or if need be, a swift kick, hopefully the first.
Thanks for any and all pointers.
Larry
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larry,
You are close. The for loop is the problem not the array. Remove grossPay and netPay calcs from the loop because one is constant and the other is only dependent on the final withHoldingAmount.

Then you figures come out.
Regards,
Manfred.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic