Applet Calculator

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2007
Posts: 3
Reputation: PennyBoki is an unknown quantity at this point 
Solved Threads: 0
PennyBoki PennyBoki is offline Offline
Newbie Poster

Applet Calculator

 
-1
  #1
Feb 12th, 2007
the errors are commented in the code:
help???

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4. import javax.swing.*;
  5. import java.lang.*;
  6.  
  7.  
  8. public class Kol2_1 extends Applet implements ActionListener
  9. {
  10. Button plus = new Button("+");
  11. Button minus = new Button("-");
  12. Button times = new Button("*");
  13. Button div = new Button("/");
  14. Button eq = new Button("=");
  15.  
  16. TextField pole = new TextField("E hej zver");
  17.  
  18. double x=0.0,y=0.0, z;
  19.  
  20.  
  21. public void init()
  22. {
  23. try{
  24. x=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za x:"));
  25. y=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za y:"));
  26. }
  27. catch(StringIndexOutOfBoundsException n)
  28. {JOptionPane.showMessageDialog(this, "Vnesete broj.");}
  29.  
  30. setLayout(new GridLayout(4,2));
  31. add(plus);
  32. add(minus);
  33. add(times);
  34. add(div);
  35. add(eq);
  36. add(pole);
  37.  
  38. plus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  39. minus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  40. times.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  41. div.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  42. eq.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  43.  
  44.  
  45.  
  46.  
  47. }
  48.  
  49. public void actionPerformed(ActionEvent e)
  50. {
  51. switch(e.getSource()){ //incompatible types
  52. case plus: { z=x+y;}
  53. case minus: { z=x-y;}
  54. case times: { z=x*y;}
  55. case div: { try{z=x/y;}
  56. catch(NumberFormatException m)
  57. {
  58. if(y==0) JOptionPane.showMessageDialog(this,"Division by zero.");
  59. }
  60. }
  61. case eq: {pole.setText(""+z);}
  62. }
  63. }
  64. }
Last edited by PennyBoki; Feb 12th, 2007 at 7:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 74
Reputation: Cudmore is an unknown quantity at this point 
Solved Threads: 5
Cudmore's Avatar
Cudmore Cudmore is offline Offline
Junior Poster in Training

Re: Applet Calculator

 
0
  #2
Feb 12th, 2007
*squints at code*

.....

Haha, silly me. I missed this at first glance.

1) instead of *.addActionPerformed(this); use *.addActionListener(this);

2) e.getSource() returns an Object, and switch statements work with int values. Might wanna reconsider, with casting and/or an if-else statement.
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: muthulakshmil is an unknown quantity at this point 
Solved Threads: 1
muthulakshmil muthulakshmil is offline Offline
Newbie Poster

Re: Applet Calculator

 
0
  #3
Feb 12th, 2007
you are giving plus.addactionPerformed(this);. you should not give like that. please give plus.addActionListener(this);. and then after finishing the constructor write the method

public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("+"))
{....}
}

here e.getActionCommand() method return the caption(that is the string that you written on the button). and then check whether it it equal to +,-,*,/ and then perform the relevant action.







Originally Posted by PennyBoki View Post
the errors are commented in the code:
help???

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4. import javax.swing.*;
  5. import java.lang.*;
  6.  
  7.  
  8. public class Kol2_1 extends Applet implements ActionListener
  9. {
  10. Button plus = new Button("+");
  11. Button minus = new Button("-");
  12. Button times = new Button("*");
  13. Button div = new Button("/");
  14. Button eq = new Button("=");
  15.  
  16. TextField pole = new TextField("E hej zver");
  17.  
  18. double x=0.0,y=0.0, z;
  19.  
  20.  
  21. public void init()
  22. {
  23. try{
  24. x=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za x:"));
  25. y=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za y:"));
  26. }
  27. catch(StringIndexOutOfBoundsException n)
  28. {JOptionPane.showMessageDialog(this, "Vnesete broj.");}
  29.  
  30. setLayout(new GridLayout(4,2));
  31. add(plus);
  32. add(minus);
  33. add(times);
  34. add(div);
  35. add(eq);
  36. add(pole);
  37.  
  38. plus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  39. minus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  40. times.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  41. div.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  42. eq.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/
  43.  
  44.  
  45.  
  46.  
  47. }
  48.  
  49. public void actionPerformed(ActionEvent e)
  50. {
  51. switch(e.getSource()){ //incompatible types
  52. case plus: { z=x+y;}
  53. case minus: { z=x-y;}
  54. case times: { z=x*y;}
  55. case div: { try{z=x/y;}
  56. catch(NumberFormatException m)
  57. {
  58. if(y==0) JOptionPane.showMessageDialog(this,"Division by zero.");
  59. }
  60. }
  61. case eq: {pole.setText(""+z);}
  62. }
  63. }
  64. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: PennyBoki is an unknown quantity at this point 
Solved Threads: 0
PennyBoki PennyBoki is offline Offline
Newbie Poster

Re: Applet Calculator

 
0
  #4
Feb 13th, 2007
thanks alot
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC