User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,955 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,143 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1556 | Replies: 3 | Solved
Reply
Join Date: Feb 2007
Posts: 3
Reputation: PennyBoki is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
PennyBoki PennyBoki is offline Offline
Newbie Poster

Applet Calculator

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

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.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:    {    try{z=x/y;}
                                    catch(NumberFormatException m)
                                    {
                                        if(y==0) JOptionPane.showMessageDialog(this,"Division by zero.");
                                    }
                                }
                case    eq:     {pole.setText(""+z);}
            }
    }
}
Last edited by PennyBoki : Feb 12th, 2007 at 6:42 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2005
Posts: 72
Reputation: Cudmore is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Cudmore's Avatar
Cudmore Cudmore is offline Offline
Junior Poster in Training

Re: Applet Calculator

  #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  
Join Date: Feb 2007
Posts: 3
Reputation: muthulakshmil is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
muthulakshmil muthulakshmil is offline Offline
Newbie Poster

Re: Applet Calculator

  #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???

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.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:    {    try{z=x/y;}
                                    catch(NumberFormatException m)
                                    {
                                        if(y==0) JOptionPane.showMessageDialog(this,"Division by zero.");
                                    }
                                }
                case    eq:     {pole.setText(""+z);}
            }
    }
}
Reply With Quote  
Join Date: Feb 2007
Posts: 3
Reputation: PennyBoki is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
PennyBoki PennyBoki is offline Offline
Newbie Poster

Re: Applet Calculator

  #4  
Feb 13th, 2007
thanks alot
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 6:22 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC