Calculator class

Calculator class

I am a beginner in Eclipse. I have to do a program called calculator that adds numbers. This is my code so far:

//Margaret //ICS 4U /* * Created on Apr 29, 2011 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */

/** * @author 301687257 * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */

import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class Calculator extends JFrame implements ActionListener { JTextField text = new JTextField (20); Button b, b2, b3, b4, b5, b6, b7, b8, b9, c, c2, c3, c4;

private int numClicks = 0; 
int num, num1, num2, num3 = 2; 
int counter = 0;
String numbers = ""; 
String sum = "";
double mainNum;
int operation = 1; 
int temp = 1; 

//Constructor
public Calculator (String Title){
    super (Title);
    setSize(200, 300);
    setLayout (new FlowLayout ());
    b = new Button("Clear");
    add (b);
    add (text);
    b.addActionListener(this);
    b2 = new Button("1");
    add (b2);
    add (text); 
    b2.addActionListener(this);
    b3 = new Button ("2");
    add (b3);
    add (text); 
    b3.addActionListener(this);
    b4 = new Button ("3");
    add (b4);
    add (text);
    b4.addActionListener(this);
    b5 = new Button ("4");
    add (b5);
    add (text);
    b5.addActionListener(this);
    b6 = new Button ("5");
    add (b6);
    add (text);
    b6.addActionListener(this);
    b7 = new Button ("6");
    add (b7);
    add (text);
    b7.addActionListener(this);
    b8 = new Button ("7");
    add (b8);
    add (text);
    b8.addActionListener(this);
    b9 = new Button ("8");
    add (b9);
    add (text);
    b9.addActionListener(this);
    c = new Button ("9");
    add (c);
    add (text);
    c.addActionListener(this);
    c2 = new Button ("0");
    add (c2);
    add (text);
    c2.addActionListener(this);
    c3 = new Button ("+");
    add (c3);
    add (text);
    c3.addActionListener(this);
    c4 = new Button ("=");
    add (c4);
    add (text);
    c4.addActionListener(this);

}


/* (non-Javadoc)
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    //   TODO Auto-generated method stub


    if (counter == 0)
        num1 = num;
    counter++; 
    if (counter == 1)
        num2 = num1;
    counter++; 

    if (e.getSource()==b)
        text.setText("0.");
    else if (e.getSource()==b2)
        //System.out.println ("1");
        text.setText("1");
    else if (e.getSource()==b3)
        text.setText("2");
    else if (e.getSource()==b4)
        text.setText("3");
    else if (e.getSource()==b5)
        text.setText("4");
    else if (e.getSource()==b6)
        text.setText("5");
    else if (e.getSource()==b7)
        text.setText("6");
    else if (e.getSource()==b8)
        text.setText("7");
    else if (e.getSource()==b9)
        text.setText("8");
    else if (e.getSource()==c)
        text.setText("9");
    else if (e.getSource()==c2)
        text.setText("0");
    else if (e.getSource()==c3)
        text.setText("+");
    else if (e.getSource () == c4)
        if (operation == 1)
            mainNum = mainNum + temp; 

    //text.setText(sum);

    counter++;
    //System.out.println (num); 
    num1 = num;
    num2 = num3; 

}

public static void main(String[] args) {
    Calculator myWindow = new Calculator("Calculator");
    myWindow.setVisible(true);
}

}

I am having trouble making the equal button give the answer when two buttons are pressed. Can someone help me with this? Thanks.

View Answers

May 6, 2011 at 2:09 PM

Please visit the following link:

Java Swing Calculator









Related Tutorials/Questions & Answers:
Calculator class
class Calculator extends JFrame implements ActionListener { JTextField text...Calculator class  I am a beginner in Eclipse. I have to do a program called calculator that adds numbers. This is my code so far: //Margaret //ICS
Calculator
Calculator  need a simple java program to degin a CALCULATOR without using ADVANCED JAVA....   Calculator in Java Swing
Advertisements
calculator midlet
calculator midlet  give me code calculator midlet in bluetooth application with j2me
Calculator program in Java
a class "calculator" is used. System.in takes the input from the system/user at run... program in Java package Tutorial; import java.io.*; class calculator...Calculator program in Java is used by programmer to add, subtract, multiply
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
Calculator
ModuleNotFoundError: No module named 'Calculator'
ModuleNotFoundError: No module named 'Calculator'  Hi, My Python... 'Calculator' How to remove the ModuleNotFoundError: No module named 'Calculator' error? Thanks   Hi, In your python environment you
thread program for calculator implementation
thread program for calculator implementation  Hi i'm prem i need calculator progrm in java that are implemented by Thread interface.....pls strong text
base calculator.. - Java Beginners
base calculator..  Help, i need some help about a base calculator.. i don't know how to start
class
class  Is a class a subclass of itself
calculator - Java Interview Questions
calculator  create calculator by java code  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
Program for Calculator - Swing AWT
Program for Calculator  write a program for calculator?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Hope that it will be helpful
PHP Tax Calculator - PHP
PHP Tax Calculator  In my project i required a tax calculator that can calculate the property tax
Java Calculator Program
Java Calculator Program  Hi, so I need to make a program that "works like a calculator". I need to make two versions: 1) I'm given the Expression Class and need to implement the children classes, which are Number, Product, Sum
calculator - Java Server Faces Questions
calculator  Some ideas for the project of calculator
simple calculator - Java Beginners
simple calculator  how can i create a simple calculator using java codes?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
Calculator - JSP-Servlet
Calculator  Dear Deepak Sir, Calculator program is avilable in Jsp... calculator program in jsp function checkValue(){ var msg...; } Simple calculator program in jsp /> >
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
class
is a class to represent a country in medal tally. It has an attribute of country... medals. In this class, you should also define constructors, and assessor, mutator methods. Task 2 MedalTally.java is a class to model a medal tally, containing
Scientific Calculator - Java Beginners
Scientific Calculator  Develop a scientific calculator using even-driven programming paradigm of Java.? Thanks in ADVANCE  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java
matrix calculator - Java Beginners
matrix calculator  hi..... can you help me in writing source code of matrix calculator in java... i know you are the best you can do it!!! show yourself
Class
Class, Object and Methods       Class : Whatever we can see in this world all the things... is termed as a class. All the objects are direct interacted with its class
ModuleNotFoundError: No module named 'arithmetic-calculator'
ModuleNotFoundError: No module named 'arithmetic-calculator'  Hi...: No module named 'arithmetic-calculator' How to remove the ModuleNotFoundError: No module named 'arithmetic-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'currency-calculator'
ModuleNotFoundError: No module named 'currency-calculator'  Hi, My... named 'currency-calculator' How to remove the ModuleNotFoundError: No module named 'currency-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'Dewpoint-Calculator'
ModuleNotFoundError: No module named 'Dewpoint-Calculator'  Hi, My... named 'Dewpoint-Calculator' How to remove the ModuleNotFoundError: No module named 'Dewpoint-Calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'Dewpoint-Calculator'
ModuleNotFoundError: No module named 'Dewpoint-Calculator'  Hi, My... named 'Dewpoint-Calculator' How to remove the ModuleNotFoundError: No module named 'Dewpoint-Calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'Dewpoint-Calculator'
ModuleNotFoundError: No module named 'Dewpoint-Calculator'  Hi, My... named 'Dewpoint-Calculator' How to remove the ModuleNotFoundError: No module named 'Dewpoint-Calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'Dewpoint-Calculator'
ModuleNotFoundError: No module named 'Dewpoint-Calculator'  Hi, My... named 'Dewpoint-Calculator' How to remove the ModuleNotFoundError: No module named 'Dewpoint-Calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'distances-calculator'
ModuleNotFoundError: No module named 'distances-calculator'  Hi...: No module named 'distances-calculator' How to remove the ModuleNotFoundError: No module named 'distances-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'engineering-calculator'
ModuleNotFoundError: No module named 'engineering-calculator'  Hi...: No module named 'engineering-calculator' How to remove the ModuleNotFoundError: No module named 'engineering-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'fbp_calculator'
ModuleNotFoundError: No module named 'fbp_calculator'  Hi, My... named 'fbp_calculator' How to remove the ModuleNotFoundError: No module named 'fbp_calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'jas-calculator'
ModuleNotFoundError: No module named 'jas-calculator'  Hi, My... named 'jas-calculator' How to remove the ModuleNotFoundError: No module named 'jas-calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'jas-calculator'
ModuleNotFoundError: No module named 'jas-calculator'  Hi, My... named 'jas-calculator' How to remove the ModuleNotFoundError: No module named 'jas-calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'loan-calculator'
ModuleNotFoundError: No module named 'loan-calculator'  Hi, My... named 'loan-calculator' How to remove the ModuleNotFoundError: No module named 'loan-calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'matrix-calculator'
ModuleNotFoundError: No module named 'matrix-calculator'  Hi, My... named 'matrix-calculator' How to remove the ModuleNotFoundError: No module named 'matrix-calculator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'nn_calculator'
ModuleNotFoundError: No module named 'nn_calculator'  Hi, My... 'nn_calculator' How to remove the ModuleNotFoundError: No module named 'nn_calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'opacity_calculator'
ModuleNotFoundError: No module named 'opacity_calculator'  Hi, My... named 'opacity_calculator' How to remove the ModuleNotFoundError: No module named 'opacity_calculator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'pomodoro-calculator'
ModuleNotFoundError: No module named 'pomodoro-calculator'  Hi, My... named 'pomodoro-calculator' How to remove the ModuleNotFoundError: No module named 'pomodoro-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'pomodoro-calculator'
ModuleNotFoundError: No module named 'pomodoro-calculator'  Hi, My... named 'pomodoro-calculator' How to remove the ModuleNotFoundError: No module named 'pomodoro-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'rdt_calculator'
ModuleNotFoundError: No module named 'rdt_calculator'  Hi, My... named 'rdt_calculator' How to remove the ModuleNotFoundError: No module named 'rdt_calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'rpn_calculator'
ModuleNotFoundError: No module named 'rpn_calculator'  Hi, My... named 'rpn_calculator' How to remove the ModuleNotFoundError: No module named 'rpn_calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'seguid-calculator'
ModuleNotFoundError: No module named 'seguid-calculator'  Hi, My... named 'seguid-calculator' How to remove the ModuleNotFoundError: No module named 'seguid-calculator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'sla-calculator'
ModuleNotFoundError: No module named 'sla-calculator'  Hi, My... named 'sla-calculator' How to remove the ModuleNotFoundError: No module named 'sla-calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'sla-calculator'
ModuleNotFoundError: No module named 'sla-calculator'  Hi, My... named 'sla-calculator' How to remove the ModuleNotFoundError: No module named 'sla-calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'sz-calculator'
ModuleNotFoundError: No module named 'sz-calculator'  Hi, My... 'sz-calculator' How to remove the ModuleNotFoundError: No module named 'sz-calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Temu-Calculator'
ModuleNotFoundError: No module named 'Temu-Calculator'  Hi, My... named 'Temu-Calculator' How to remove the ModuleNotFoundError: No module named 'Temu-Calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'aerospace-calculator'
ModuleNotFoundError: No module named 'aerospace-calculator'  Hi...: No module named 'aerospace-calculator' How to remove the ModuleNotFoundError: No module named 'aerospace-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'agnostic-calculator'
ModuleNotFoundError: No module named 'agnostic-calculator'  Hi, My... named 'agnostic-calculator' How to remove the ModuleNotFoundError: No module named 'agnostic-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'arithmetic-calculator'
ModuleNotFoundError: No module named 'arithmetic-calculator'  Hi...: No module named 'arithmetic-calculator' How to remove the ModuleNotFoundError: No module named 'arithmetic-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'calculator-test'
ModuleNotFoundError: No module named 'calculator-test'  Hi, My... named 'calculator-test' How to remove the ModuleNotFoundError: No module named 'calculator-test' error? Thanks   Hi, In your python

Ads