Welcome to Dream.In.Code
Become a Java Expert!

Join 149,732 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,326 people online right now. Registration is fast and FREE... Join Now!




Calculator Applet

 
Reply to this topicStart new topic

Calculator Applet

PennyBoki
12 Feb, 2007 - 02:30 PM
Post #1

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
HI, I'm trying to write this calculator but I'm having trouble
when I compile. The compile errors are commented in the code.
I'm not asking much just show me where I go wronng with
the methods such actionPerformed.
Since I'm from a non-english speaking country I would appreciate
if you let me know if I am not comprehendable.

CODE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.lang.*;


public class Kol2_1 extends Applet implements ActionListener
{
    Button plus  = new Button("+");
    Button minus = new Button("-");
    Button times = new Button("*");
    Button div   = new Button("/");
    Button eq    = new Button("=");
    
    TextField pole = new TextField("E hej zver");
    
    double x=1.0,y=1.0, z;        
        
    
    public void init()
    {    try{
        x=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za x:"));
        y=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za y:"));
           }
        catch(StringIndexOutOfBoundsException n)
        {JOptionPane.showMessageDialog(this, "Vnesete broj.");}    

        setLayout(new GridLayout(4,2));
        add(plus);
        add(minus);
        add(times);
        add(div);
        add(eq);
        add(pole);
        
        plus.addActionPerformed(this);        //cannot find symbol method addActionPerformed(Kol2_1)
        minus.addActionPerformed(this);        //cannot find symbol method addActionPerformed(Kol2_1)
        times.addActionPerformed(this);        //cannot find symbol method addActionPerformed(Kol2_1)
        div.addActionPerformed(this);        //cannot find symbol method addActionPerformed(Kol2_1)
        eq.addActionPerformed(this);        //cannot find symbol method addActionPerformed(Kol2_1)
        
    }
    
    public void actionPerformed(ActionEvent e)
        {
            switch(e.getSource()){            //incompatible types
                case    plus:     {    z=x+y;}
                case    minus:  {    z=x-y;}
                case    times:  {    z=x*y;}
                case    div:    {    z=x/y;}
                case    eq:     {pole.setText(""+z);}
            }
    }
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Calculator Applet
12 Feb, 2007 - 02:33 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,350



Thanked: 51 times
Dream Kudos: 25
My Contributions
You do not have a method named addActionPerformed defined anywhere - only actionPerformed.
User is online!Profile CardPM
+Quote Post

PennyBoki
RE: Calculator Applet
12 Feb, 2007 - 02:39 PM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(Amadeus @ 12 Feb, 2007 - 03:33 PM) *

You do not have a method named addActionPerformed defined anywhere - only actionPerformed.


I think I have
here:
CODE
plus.addActionPerformed(this);        //cannot find symbol method addActionPerformed(Kol2_1)
        minus.addActionPerformed(this);        //cannot find symbol method addActionPerformed(Kol2_1)
        times.addActionPerformed(this);        //cannot find symbol method addActionPerformed(Kol2_1)
        div.addActionPerformed(this);        //cannot find symbol method addActionPerformed(Kol2_1)
        eq.addActionPerformed(this);


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Calculator Applet
12 Feb, 2007 - 02:45 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,350



Thanked: 51 times
Dream Kudos: 25
My Contributions
I understand that you are calling a method named addActionPerformed...what I am saying is that you do not have that method defined anywhere, unless it is not here. Can you show me where you have specified what the method addActionPerformed is supposed to do?
User is online!Profile CardPM
+Quote Post

PennyBoki
RE: Calculator Applet
12 Feb, 2007 - 02:51 PM
Post #5

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Yes, when you click the button it performes an action.
If I click the + Button it does z=x+y;
I think that this is kind of default method.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Calculator Applet
12 Feb, 2007 - 02:59 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,350



Thanked: 51 times
Dream Kudos: 25
My Contributions
OK, let's try this a different way then...wherever your code says addActionPerformed, switch it to actionPerformed.

Usually, actionPerformed is the default method.
User is online!Profile CardPM
+Quote Post

PennyBoki
RE: Calculator Applet
12 Feb, 2007 - 03:11 PM
Post #7

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
I did that still don't work. I tried this method before and it worked.
I'm prety much sure that that isn't the problem.
Here is the idea: I assign an action to a button with plus.addActionPerformed
and in the function actionPerformed I take the source of that action which in this case is the plus button which does z=x+y;
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Calculator Applet
12 Feb, 2007 - 03:54 PM
Post #8

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
And what about that incompatible types thing.
It's like the compiler says Button is not compatible with Button???
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Calculator Applet
13 Feb, 2007 - 02:44 AM
Post #9

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(Amadeus @ 12 Feb, 2007 - 03:45 PM) *

I understand that you are calling a method named addActionPerformed...what I am saying is that you do not have that method defined anywhere, unless it is not here. Can you show me where you have specified what the method addActionPerformed is supposed to do?



Stupid me, instead of addActionPerformed should stand
addActionListener.

Which means that this add stuf is for interfaces not functions.

The code is all OK except for this incompatible types stuf:

CODE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.lang.*;


public class Kol2_1 extends Applet implements ActionListener
{
    Button plus  = new Button("+");
    Button minus = new Button("-");
    Button times = new Button("*");
    Button div   = new Button("/");
    Button eq    = new Button("=");
    
    TextField pole = new TextField("E hej zver");
    
    double x=0.0,y=0.0, z;        
        
    
    public void init()
    {    
        try{
        x=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za x:"));
        y=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za y:"));
           }
        catch(StringIndexOutOfBoundsException n)
        {JOptionPane.showMessageDialog(this, "Vnesete broj.");}    

        setLayout(new GridLayout(4,2));
        add(plus);
        add(minus);
        add(times);
        add(div);
        add(eq);
        add(pole);
        
        plus.addActionListener(this);        
        minus.addActionListener(this);        
        times.addActionListener(this);        
        div.addActionListener(this);        
        eq.addActionListener(this);        
        
        
        
        
    }
    
    public void actionPerformed(ActionEvent e)
        {
            switch(e.getSource()){            //incompatible types
                case    plus:     {    z=x+y;}
                case    minus:  {    z=x-y;}
                case    times:  {    z=x*y;}
                case    div:    {    try{z=x/y;}
                                    catch(NumberFormatException m)
                                    {
                                        if(y==0) JOptionPane.showMessageDialog(this,"Division by zero.");
                                    }
                                }
                case    eq:     {pole.setText(""+z);}
            }
    }
}

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Calculator Applet
13 Feb, 2007 - 04:35 AM
Post #10

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
IT WORKS

yeah cool.gif ...
Amadeus thanks for the help.

the code is:
CODE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.lang.*;


public class Kol2_1 extends Applet implements ActionListener
{
    Button plus  = new Button("+");
    Button minus = new Button("-");
    Button times = new Button("*");
    Button div   = new Button("/");
    Button eq    = new Button("=");
    
    Button b;
    
    TextField pole = new TextField("E hej zver");
    
    double x=0.0,y=0.0, z;        
        
    
    public void init()
    {    
        try {
        x=Double.parseDouble(JOptionPane.showInputDialog("Enter value for x:"));
        y=Double.parseDouble(JOptionPane.showInputDialog("Enter value for y:"));
            }            
        catch(NumberFormatException n)
        {JOptionPane.showMessageDialog(this, "You havent entered a number.");}    

        setLayout(new GridLayout(3,2));
        add(plus);
        add(minus);
        add(times);
        add(div);
        add(eq);
        add(pole);
        
        plus.addActionListener(this);    
        minus.addActionListener(this);        
        times.addActionListener(this);    
        div.addActionListener(this);    
        eq.addActionListener(this);        
        
        
        
        
    }
    
    public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==plus)        z=x+y;
            if(e.getSource()==minus)    z=x-y;
            if(e.getSource()==times)    z=x*y;
            if(e.getSource()==div)    {    try{z=x/y;}
                                    catch(ArithmeticException m)
                                        {
                                             JOptionPane.showMessageDialog(this,"Division by zero.");
                                        }
                                    }
            if(e.getSource()==eq) {pole.setText(""+z);}
            
            
            
            
            }
}


the html code is:

CODE
<html>
    <head>
    </head>
    <body bgcolor="000000">
        <center>
            <applet
                code    = "Kol2_1.class"
                width    = "100"
                height    = "150"
                >
            </applet>
        </center>
    </body>
</html>

biggrin.gif
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Calculator Applet
13 Feb, 2007 - 05:16 AM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,350



Thanked: 51 times
Dream Kudos: 25
My Contributions
Congratulations! glad it works!
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 05:17AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month