What's Here?
Members: 244,264
Replies: 693,081
Topics: 113,150
Snippets: 3,863
Tutorials: 935
Total Online: 1,251
Members: 75
Guests: 1,176
Loading. Please Wait...
java applet program
zedenaka
26 Jul, 2007 - 05:23 AM
New D.I.C Head
Joined: 4 Jul, 2007
Posts: 32
My Contributions
guys i need help on my program. i cant seem to find whats wrong. pls i need your help.
here is the .java code:
CODE
import java.awt.*; import java.applet.*; import java.awt.event.*; public class mp1f extends Applet implements ActionListener { Label lnLbl=new Label("Last Name: "); TextField lnTxt=new TextField(20); Label fnLbl=new Label("First Name: "); TextField fnTxt=new TextField(20); Label miLbl=new Label("Middle Initial: "); TextField miTxt=new TextField(20); Label posLbl=new Label("Position: "); TextField posTxt=new TextField(20); Label hrsLbl=new Label("No. of Working Hours: "); TextField hrsTxt=new TextField(20); Button compButton=new Button("COMPUTE"); Button rstButton=new Button("RESET"); Label scLbl=new Label("Salary Computation:"); Label rateLbl=new Label("Rate: "); TextField rateTxt=new TextField(20); Label gpLbl=new Label("Gross Pay: "); TextField gpTxt=new TextField(20); Label dedLbl=new Label("Deductions:"); Label sssLbl=new Label("SSS: "); TextField sssTxt=new TextField(20); Label taxLbl=new Label("Tax: "); TextField taxTxt=new TextField(20); Label medLbl=new Label("Medicare: "); TextField medTxt=new TextField(20); Label tdLbl=new Label("Total Deductions: "); TextField tdTxt=new TextField(20); Label bonLbl=new Label("Bonus: "); TextField bonTxt=new TextField(20); Label npLbl=new Label("Net Pay of: "); public void init() { add(lnLbl); add(lnTxt); add(fnLbl); add(fnTxt); add(miLbl); add(miTxt); add(posLbl); add(posTxt); add(hrsLbl); add(hrsTxt); add(compButton); compButton.addActionListener(this); add(rstButton); rstButton.addActionListener(this); add(scLbl); add(rateLbl); add(rateTxt); add(gpLbl); add(gpTxt); add(dedLbl); add(sssLbl); add(sssTxt); add(taxLbl); add(taxTxt); add(medLbl); add(medTxt); add(tdLbl); add(tdTxt); add(bonLbl); add(bonTxt); add(npLbl); } public void actionPerformed(ActionEvent e) { String arg,ln,fn,mi,pos; int rate; double gp,sss,tax,med,td,bon,np,hrs; ln=lnTxt.getText(); fn=fnTxt.getText(); mi=miTxt.getText(); pos=posTxt.getText(); hrs=Double.parseDouble(hrsTxt.getText()); gp=Double.parseDouble(gpTxt.getText()); sss=Double.parseDouble(sssTxt.getText()); tax=Double.parseDouble(taxTxt.getText()); med=Double.parseDouble(medTxt.getText()); td=Double.parseDouble(tdTxt.getText()); bon=Double.parseDouble(bonTxt.getText()); np=Double.parseDouble(npLbl.getText()); rate=Integer.parseInt(rateTxt.getText()); arg=e.getActionCommand(); if(pos=="manager") { rate=800; rateTxt.setText("+rate"); } else if(pos=="supervisor") { rate=500; rateTxt.setText("+rate"); } else if(pos=="employee") { rate=300; rateTxt.setText("+rate"); } else { rateTxt.setText("Wrong Input"); } arg=e.getActionCommand(); if(arg=="COMPUTE") { gp=rate*hrs; gpTxt.setText("+gp"); sss=(gp)*(0.5); sssTxt.setText("+sss"); tax=(gp)*(0.10); taxTxt.setText("+tax"); med=100; medTxt.setText("+med"); td=(sss+tax)+med; tdTxt.setText("+td"); if(bon>=20000) { bon=(gp)*(0.5); bonTxt.setText("+bon"); } else if(bon<20000) { bon=(gp)*(0.3); bonTxt.setText("+bon"); } np=(gp-td)+bon; npLbl.setText("Net Pay of" +fn+ "is" +np); } if(arg=="RESET") { lnTxt.setText(" "); fnTxt.setText(" "); miTxt.setText(" "); posTxt.setText(" "); hrsTxt.setText(" "); rateTxt.setText(" "); gpTxt.setText(" "); sssTxt.setText(" "); taxTxt.setText(" "); medTxt.setText(" "); tdTxt.setText(" "); bonTxt.setText(" "); npLbl.setText("Net Pay of:"); } } }
and here is for the html file
CODE
<html> <applet code="mp1f.class" height=600 width=200> </applet> </html>
zedenaka
RE: Java Applet Program 26 Jul, 2007 - 05:40 AM
New D.I.C Head
Joined: 4 Jul, 2007
Posts: 32
My Contributions
well you see when i run the program i can input the last name,first name, middle initial,position and no of hours worked. but when i click the compute button it wont create an output on the boxes below which should have the output. even when i click the clear button(which should clear all data) nothing happens. i think it is in this part which have the problem, but i cant seem to figure it out. pls help.
CODE
public void actionPerformed(ActionEvent e) { String arg,ln,fn,mi,pos; int rate; double gp,sss,tax,med,td,bon,np,hrs; ln=lnTxt.getText(); fn=fnTxt.getText(); mi=miTxt.getText(); pos=posTxt.getText(); hrs=Double.parseDouble(hrsTxt.getText()); gp=Double.parseDouble(gpTxt.getText()); sss=Double.parseDouble(sssTxt.getText()); tax=Double.parseDouble(taxTxt.getText()); med=Double.parseDouble(medTxt.getText()); td=Double.parseDouble(tdTxt.getText()); bon=Double.parseDouble(bonTxt.getText()); np=Double.parseDouble(npLbl.getText()); rate=Integer.parseInt(rateTxt.getText()); arg=e.getActionCommand(); if(pos=="manager") { rate=800; rateTxt.setText("+rate"); } else if(pos=="supervisor") { rate=500; rateTxt.setText("+rate"); } else if(pos=="employee") { rate=300; rateTxt.setText("+rate"); } else { rateTxt.setText("Wrong Input"); } arg=e.getActionCommand(); if(arg=="COMPUTE") { gp=rate*hrs; gpTxt.setText("+gp"); sss=(gp)*(0.5); sssTxt.setText("+sss"); tax=(gp)*(0.10); taxTxt.setText("+tax"); med=100; medTxt.setText("+med"); td=(sss+tax)+med; tdTxt.setText("+td"); if(bon>=20000) { bon=(gp)*(0.5); bonTxt.setText("+bon"); } else if(bon<20000) { bon=(gp)*(0.3); bonTxt.setText("+bon"); } np=(gp-td)+bon; npLbl.setText("Net Pay of" +fn+ "is" +np); } if(arg=="RESET") { lnTxt.setText(" "); fnTxt.setText(" "); miTxt.setText(" "); posTxt.setText(" "); hrsTxt.setText(" "); rateTxt.setText(" "); gpTxt.setText(" "); sssTxt.setText(" "); taxTxt.setText(" "); medTxt.setText(" "); tdTxt.setText(" "); bonTxt.setText(" "); npLbl.setText("Net Pay of:"); } } }
PennyBoki
RE: Java Applet Program 26 Jul, 2007 - 06:14 AM
system("revolution");
Joined: 11 Dec, 2006
Posts: 2,075
Thanked: 12 times
Dream Kudos: 500
Expert In: Java,C++,C
My Contributions
instead of
QUOTE
if(arg=="RESET") { lnTxt.setText(" "); fnTxt.setText(" "); miTxt.setText(" "); posTxt.setText(" "); hrsTxt.setText(" "); rateTxt.setText(" "); gpTxt.setText(" "); sssTxt.setText(" "); taxTxt.setText(" "); medTxt.setText(" "); tdTxt.setText(" "); bonTxt.setText(" "); npLbl.setText("Net Pay of:"); } }
do
CODE
if(e.getSource()==rstButton) //the name of the button { lnTxt.setText(" "); fnTxt.setText(" "); miTxt.setText(" "); posTxt.setText(" "); hrsTxt.setText(" "); rateTxt.setText(" "); gpTxt.setText(" "); sssTxt.setText(" "); taxTxt.setText(" "); medTxt.setText(" "); tdTxt.setText(" "); bonTxt.setText(" "); npLbl.setText("Net Pay of:"); } }
Now do the same for the other button and now I ask you:
On what event you think these(below) would happen(take action)?
QUOTE
ln=lnTxt.getText(); fn=fnTxt.getText(); mi=miTxt.getText(); pos=posTxt.getText(); hrs=Double.parseDouble(hrsTxt.getText()); gp=Double.parseDouble(gpTxt.getText()); sss=Double.parseDouble(sssTxt.getText()); tax=Double.parseDouble(taxTxt.getText()); med=Double.parseDouble(medTxt.getText()); td=Double.parseDouble(tdTxt.getText()); bon=Double.parseDouble(bonTxt.getText()); np=Double.parseDouble(npLbl.getText()); rate=Integer.parseInt(rateTxt.getText());
jmanuelh
RE: Java Applet Program 26 Jul, 2007 - 08:47 AM
New D.I.C Head
Joined: 18 Nov, 2006
Posts: 23
Thanked: 2 times
My Contributions
Hi!
As penny said, to compare strings you need to use equals(), because string isn't a primitive variable like integer of char, so you have to use something like this:
CODE
if(pos.equals("manager"))
to compare 2 strings
To convert string to int you need to parse like this:
CODE
int converted = Integer.parseInt(ln); //where ln is, put the string variable you want to convert
J. Manuel
This post has been edited by jmanuelh : 26 Jul, 2007 - 08:54 AM
zedenaka
RE: Java Applet Program 4 Aug, 2007 - 08:16 AM
New D.I.C Head
Joined: 4 Jul, 2007
Posts: 32
My Contributions
thanks i already got this one
CODE
import java.awt.*; import java.applet.*; import java.awt.event.*; public class mp1f extends Applet implements ActionListener,ItemListener { Label lnLbl=new Label("Last Name: "); TextField lnTxt=new TextField(20); Label fnLbl=new Label("First Name: "); TextField fnTxt=new TextField(20); Label miLbl=new Label("Middle Initial: "); TextField miTxt=new TextField(20); Label posLbl=new Label("Position: "); TextField posTxt=new TextField(20); Label hrsLbl=new Label("No. of Working Hours: "); TextField hrsTxt=new TextField(20); Button compButton=new Button("COMPUTE"); Button rstButton=new Button("RESET"); Label scLbl=new Label("Salary Computation:"); Label rateLbl=new Label("Rate: "); TextField rateTxt=new TextField(20); Label gpLbl=new Label("Gross Pay: "); TextField gpTxt=new TextField(20); Label dedLbl=new Label("Deductions:"); Label sssLbl=new Label("SSS: "); TextField sssTxt=new TextField(20); Label taxLbl=new Label("Tax: "); TextField taxTxt=new TextField(20); Label medLbl=new Label("Medicare: "); TextField medTxt=new TextField(20); Label tdLbl=new Label("Total Deductions: "); TextField tdTxt=new TextField(20); Label bonLbl=new Label("Bonus: "); TextField bonTxt=new TextField(20); Label npLbl=new Label("Net Pay of: "); public void init() { add(lnLbl); add(lnTxt); add(fnLbl); add(fnTxt); add(miLbl); add(miTxt); add(posLbl); add(posTxt); add(hrsLbl); add(hrsTxt); add(compButton); compButton.addActionListener(this); add(rstButton); rstButton.addActionListener(this); add(scLbl); add(rateLbl); add(rateTxt); add(gpLbl); add(gpTxt); add(dedLbl); add(sssLbl); add(sssTxt); add(taxLbl); add(taxTxt); add(medLbl); add(medTxt); add(tdLbl); add(tdTxt); add(bonLbl); add(bonTxt); add(npLbl); } public void actionPerformed(ActionEvent e) { String arg,ln,fn,mi,pos; int rate=0,med=100; double gp,sss,tax,td,bon=0,np,hrs; hrs=Double.parseDouble(hrsTxt.getText()); fn=fnTxt.getText(); ln=lnTxt.getText(); mi=miTxt.getText(); pos=posTxt.getText(); arg=e.getActionCommand(); if(pos.equals("manager")) { rate=800; rateTxt.setText(""+rate); } if(pos.equals("supervisor")) { rate=500; rateTxt.setText(""+rate); } if(pos.equals("employee")) { rate=300; rateTxt.setText(""+rate); } if(arg=="COMPUTE") { gp=rate*hrs; gpTxt.setText(""+gp); sss=(gp)*(0.5); sssTxt.setText(""+sss); tax=(gp)*(0.10); taxTxt.setText(""+tax); medTxt.setText(""+med); td=(sss+tax)+med; tdTxt.setText(""+td); if(bon>=20000) { bon=(gp)*(0.5); bonTxt.setText(""+bon); } if(bon<20000) { bon=(gp)*(0.3); bonTxt.setText(""+bon); } np=(gp-td)+bon; npLbl.setText("Net Pay of " +fn+ " " +ln+ " is " +np); } if(arg=="RESET") { lnTxt.setText(" "); fnTxt.setText(" "); miTxt.setText(" "); posTxt.setText(" "); hrsTxt.setText(" "); rateTxt.setText(" "); gpTxt.setText(" "); sssTxt.setText(" "); taxTxt.setText(" "); medTxt.setText(" "); tdTxt.setText(" "); bonTxt.setText(" "); npLbl.setText("Net Pay of:"); } } }
thanks for your help!!!
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month