Calculator class



Share on Google+Share on Google+

Calculator class
1 Answer(s)      6 years and 6 months ago
Posted in : Java Beginners

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; 

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


/* (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;
    if (counter == 1)
        num2 = num1;

    if (e.getSource()==b)
    else if (e.getSource()==b2)
        //System.out.println ("1");
    else if (e.getSource()==b3)
    else if (e.getSource()==b4)
    else if (e.getSource()==b5)
    else if (e.getSource()==b6)
    else if (e.getSource()==b7)
    else if (e.getSource()==b8)
    else if (e.getSource()==b9)
    else if (e.getSource()==c)
    else if (e.getSource()==c2)
    else if (e.getSource()==c3)
    else if (e.getSource () == c4)
        if (operation == 1)
            mainNum = mainNum + temp; 


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


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


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  need a simple java program to degin a CALCULATOR without using ADVANCED JAVA....   Calculator in Java Swing
calculator midlet
calculator midlet  give me code calculator midlet in bluetooth application with j2me
Calculator program in Java
a class "calculator" is used. takes the input from the system/user at run... package Tutorial; import*; class calculator { public static void...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
base calculator.. - Java Beginners
base calculator..  Help, i need some help about a base calculator.. i don't know how to start
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
calculator - Java Interview Questions
calculator  create calculator by java code  Hi Friend, Please visit the following link: Thanks
Program for Calculator - Swing AWT
Program for Calculator  write a program for calculator?  Hi Friend, Please visit the following link: 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
simple calculator - Java Beginners
simple calculator  how can i create a simple calculator using java codes?  Hi Friend, Please visit the following link: 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 /> >
calculator - Java Server Faces Questions
calculator  Some ideas for the project of calculator
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
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
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
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:
base calculator.. - Java Beginners
base calculator..  need some help!!! I cant install jcreator properly?? there's always an error (Error : Invalid path, "C:\Program Files\Java\jre1.6.0_01\bin\javac.exe" -classpath "C:\Program Files\Java\jdk1.6.0_01\bin" -d "C
calculator in java with stack
calculator in java with stack  i want calcultor with interface in java and in interface there is button called postfix ,,, when the user enter opertions and numbers first check if is vaild or not then convert to postfix
Java Calculator program - Java Beginners
Java Calculator program  import java.awt.*; import javax.swing.*; import java.awt.event.*; class Calculator implements ActionListener { int c,n...; Calculator() { f=new Frame("Calculator"); p=new Panel(); b1=new JButton("0
Design and Implement GPA Calculator
*; import java.awt.*; import java.awt.event.*; public class GPACalculator... Calculator"); //frame.setLayout(new BorderLayout()); frame.setLayout...); headingLabel = new JLabel("Welcome To GPA Calculator
swimming pool calculator
javax.swing.ButtonGroup; import java.awt.FlowLayout; public class VolCalc...", customers); jtabbedPane.addTab("Temp Calculator", tempCalc
Simple Java Calculator - Java Beginners
Calculator for 4 basic Math operations, Addition, Subtraction, Multiplication and Division. The calculator should simulate the look of handheld calculator containing.... Write proper code for making this calculator operational. Students
Swimming Pool Calculator - Java Beginners
Calculator is one that is very simple to use. As you can see, it is a user... java.text.DecimalFormat; public class PoolVolume { private JFrame mainFrame; private... PoolVolume() { mainFrame = new JFrame("Swimming Pool Volume Calculator
Writing Calculator Stateless Session Bean
Writing Calculator Stateless Session Bean...; } Enterprise Bean class All Bean class are defined as public and implements the javax.ejb.SessionBean. In the bean class we have implemented
Java Swing Scientific Calculator
Java Swing Scientific Calculator A Scientific Calculator is a very powerful and general purpose calculator. In addition to basic arithmetic functions... calculator using java swing. Here is the code: import java.awt.*; import
java loan calculator applet help
java loan calculator applet help  Hi, I could use some help... javax.swing.border.TitledBorder; public class LoanCalculator extends JApplet implements... adding a public class loan in the main method but I realized then that a class
class  Is a class a subclass of itself
Swimming Pool Calculator - Java Beginners
java.awt.*; import java.awt.event.*; class Login extends JFrame implements...",JOptionPane.ERROR_MESSAGE); } } } class LoginForm{ public static void main... java.text.DecimalFormat; public class PoolVolume extends JFrame{ private JFrame mainFrame
Swimming Pool Calculator - Java Beginners
; public class SwimmCalc extends JFrame implements ActionListener{ private... Calculator", tempCalc ); jtabbedPane.addTab( "Customers", customers


Advertisement null