Home Answers Viewqa Java-Beginners Java Calculator program

 
 


Avishek Bhattacharyya
Java Calculator program
4 Answer(s)      3 years and 4 months ago
Posted in : Java Beginners

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Calculator implements ActionListener
{
int c,n;
String s1,s2,s3,s4,s5;
Frame f;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
Panel p;
TextField tf1;
GridLayout gl;
Calculator()
{
f=new Frame("Calculator");
p=new Panel();
b1=new JButton("0");
b1.addActionListener(this);
b2=new JButton("1");
b2.addActionListener(this);
b3=new JButton("2");
b3.addActionListener(this);
b4=new JButton("3");
b4.addActionListener(this);
b5=new JButton("4");
b5.addActionListener(this);
b6=new JButton("5");
b6.addActionListener(this);
b7=new JButton("6");
b7.addActionListener(this);
b8=new JButton("7");
b8.addActionListener(this);
b9=new JButton("8");
b9.addActionListener(this);
b10=new JButton("9");
b10.addActionListener(this);
b11=new JButton("+");
b11.addActionListener(this);
b12=new JButton("-");
b12.addActionListener(this);
b13=new JButton("x");
b13.addActionListener(this);
b14=new JButton("/");
b14.addActionListener(this);
b15=new JButton("=");
b15.addActionListener(this);
b16=new JButton("A/C");
b16.addActionListener(this);
tf1=new TextField(20);
p.add(tf1);
tf1.setBackground(Color.yellow);
gl=new GridLayout(4,4,10,20);
p.setLayout(gl);
p.add(b1);p.add(b2);p.add(b3);p.add(b4);p.add(b5);p.add(b6);p.add(b7);p.add(b8);p.add(b9);p.add(b10);p.add(b11);p.add(b12);p.add(b13);p.add(b14);p.add(b15);p.add(b16);
f.add(p);
f.setSize(100,200);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
s3=tf1.getText();
s4="0";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b2)
{
s3=tf1.getText();
s4="1";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b3)
{
s3=tf1.getText();
s4="2";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b4)
{
s3=tf1.getText();
s4="3";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b5)
{
s3=tf1.getText();
s4="4";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b6)
{
s3=tf1.getText();
s4="5";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b7)
{
s3=tf1.getText();
s4="6";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b8)
{
s3=tf1.getText();
s4="7";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b9)
{
s3=tf1.getText();
s4="8";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b10)
{
s3=tf1.getText();
s4="9";
s5=s3+s4;
tf1.setText(s5);
}
if(e.getSource()==b11)
{
s1=tf1.getText();
tf1.setText("");
c=1;
}
if(e.getSource()==b12)
{
s1=tf1.getText();
tf1.setText("");
c=2;
}
if(e.getSource()==b13)
{
s1=tf1.getText();
tf1.setText("");
c=3;
}
if(e.getSource()==b14)
{
s1=tf1.getText();
tf1.setText("");
c=4;
}
if(e.getSource()==b15)
{
s2=tf1.getText();
if(c==1)
{
n=Integer.parseInt(s1)+Integer.parseInt(s2);
tf1.setText(String.valueOf(n));
}
else
if(c==2)
{
n=Integer.parseInt(s1)-Integer.parseInt(s2);
tf1.setText(String.valueOf(n));
}
else
if(c==3)
{
n=Integer.parseInt(s1)*Integer.parseInt(s2);
tf1.setText(String.valueOf(n));
}
else
if(c==4)
{
n=Integer.parseInt(s1)/Integer.parseInt(s2);
tf1.setText(String.valueOf(n));
}
}
if(e.getSource()==b16)
{
tf1.setText("");
}
}
public static void main(String args[])
{
Calculator c=new Calculator();
}
}










In the above program,the intermediate results are not generated.eg,if 14+3-2,then the above program gives 15 but I would like to see 17 and the subtract 2 from it to get to get 15.It 'd be helpful if u could pls suggest the possible correction.
View Answers

January 14, 2010 at 10:51 AM


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 for you.
Thanks

June 26, 2012 at 9:58 PM


Hello friends For multidigit calculator code u can visit the follwowing link


July 2, 2012 at 12:37 AM


Java Calculator Code and methods


February 22, 2013 at 11:49 AM


import java.awt.*; import java.awt.event.*;

class AA implements ActionListener { Frame f; TextField tf;

Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,plush,minus,mul,div,equal,clear;

String s="";

int n,c;

AA()
{
    f=new Frame("jamshjed");
    tf=new TextField();
    tf.setBounds(50,50,300,50);

    b1=new Button("1");
    b1.setBounds(50,150,50,50);
    b1.addActionListener(this);

    b2=new Button("2");
    b2.setBounds(105,150,50,50);
    b2.addActionListener(this);

    b3=new Button("3");
    b3.setBounds(160,150,50,50);b3.addActionListener(this);

    b4=new Button("4");
    b4.setBounds(215,150,50,50);b4.addActionListener(this);

    b5=new Button("5");
    b5.setBounds(270,150,50,50);b5.addActionListener(this);

    b6=new Button("6");
    b6.setBounds(50,230,50,50);b6.addActionListener(this);

    b7=new Button("7");
    b7.setBounds(105,230,50,50);b7.addActionListener(this);

    b8=new Button("8");
    b8.setBounds(160,230,50,50);b8.addActionListener(this);

    b9=new Button("9");
    b9.setBounds(215,230,50,50);b9.addActionListener(this);

    b0=new Button("0");
    b0.setBounds(270,230,50,50);b0.addActionListener(this);

    plush=new Button("+");
    plush.setBounds(50,310,160,50);plush.addActionListener(this);

    minus=new Button("-");
    minus.setBounds(215,310,105,50);minus.addActionListener(this);

    mul=new Button("*");
    mul.setBounds(50,390,50,50);mul.addActionListener(this);

    equal=new Button("=");
    equal.setBounds(105,390,50,50);equal.addActionListener(this);

    div=new Button("/");
    div.setBounds(160,390,50,50);div.addActionListener(this);

    clear=new Button("c");
    clear.setBounds(215,390,105,50);clear.addActionListener(this);

    f.add(b1);
    f.add(b2);
    f.add(b3);
    f.add(b4);
    f.add(b5);
    f.add(b6);
    f.add(b7);
    f.add(b8);
    f.add(b9);
    f.add(b0);
    f.add(plush);
    f.add(minus);
    f.add(mul);
    f.add(equal);
    f.add(div);
    f.add(clear);

    f.add(tf);
    f.setSize(500,500);
    f.setLayout(null);
    f.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
    if(e.getActionCommand().equals("1"))
    {

        s=s+"1";
        tf.setText(s);
    }

    if(e.getActionCommand().equals("2"))
    {
        s=s+"2";
        tf.setText(s);
    }

    if(e.getActionCommand().equals("3"))
    {
        s=s+"3";
        tf.setText(s);
    }

    if(e.getActionCommand().equals("4"))
    {
        s=s+"4";
        tf.setText(s);
    }

    if(e.getActionCommand().equals("5"))
    {
        s=s+"5";
        tf.setText(s);
    }

    if(e.getActionCommand().equals("6"))
    {
        s=s+"6";
        tf.setText(s);
    }
    if(e.getActionCommand().equals("7"))
    {
        s=s+"7";
        tf.setText(s);
    }
    if(e.getActionCommand().equals("8"))
    {
        s=s+"8";
        tf.setText(s);
    }

    if(e.getActionCommand().equals("9"))
    {
        s=s+"9";
        tf.setText(s);
    }

    if(e.getActionCommand().equals("0"))
    {
        s=s+"0";
        tf.setText(s);
    }

    if(e.getActionCommand().equals("+"))
    {
        n=Integer.parseInt(tf.getText());
        tf.setText("");
        s="";
        c=1;


    }

    if(e.getActionCommand().equals("-"))
    {
        n=Integer.parseInt(tf.getText());
        tf.setText("");
        s="";
        c=2;
    }
    if(e.getActionCommand().equals("/"))
    {
        n=Integer.parseInt(tf.getText());
        tf.setText("");
        s="";
        c=3;

    }
    if(e.getActionCommand().equals("*"))
    {
        n=Integer.parseInt(tf.getText());
        tf.setText("");
        s="";
        c=4;

    }

    if(e.getActionCommand().equals("c"))
    {

        tf.setText("");
        s="";


    }
    if(e.getActionCommand().equals("="))
    {

        if(c==1)
        {
            int j=Integer.parseInt(tf.getText());
            int k=n+j;
            String ss=String.valueOf(k);
            tf.setText(ss);

        }
        if(c==2)
        {
            int j=Integer.parseInt(tf.getText());
            int k=n-j;
            String ss=String.valueOf(k);
            tf.setText(ss);

        }

        if(c==3)
        {
            int j=Integer.parseInt(tf.getText());
            int k=n/j;
            String ss=String.valueOf(k);
            tf.setText(ss);

        }
        if(c==4)
        {
            int j=Integer.parseInt(tf.getText());
            int k=n*j;
            String ss=String.valueOf(k);
            tf.setText(ss);

        }

    }



}

public static void main(String... aa)
{
    AA o=new AA();
}

}









Related Pages:
Calculator
Calculator  need a simple java program to degin a CALCULATOR without using ADVANCED JAVA....   Calculator in Java Swing
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
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
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
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...://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Hope
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... for this generated file go to * Window - Preferences - Java - Code Style - Code Templates
base calculator.. - Java Beginners
?? 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... to be set. In first path: change as C:\Program Files\Java\jdk1.6.0_01\bin
Calculator program in Java
Calculator program in Java is used by programmer to add, subtract, multiply... other wise it will be a fraction value) Example of Calculator program in Java... a class "calculator" is used. System.in takes the input from the system/user at run
Simple Java Calculator - Java Beginners
Simple Java Calculator  Write a Java program to create simple Calculator for 4 basic Math operations, Addition, Subtraction, Multiplication and Division. The calculator should simulate the look of handheld calculator containing
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
Example - Simple Calculator
Java: Example - Simple Calculator Here is the source for the simple calculator shown at the left. It's divided into three source files. Main (Calc.java) - A simple main program. User interface (CalcGUI.java
Java: Example - Simple Calculator
Java: Example - Simple Calculator Here is the source for the simple calculator shown at the left. It's divided into three source files. Main (Calc.java) - A simple main program. User interface (CalcGUI.java
Swimming Pool Calculator - Java Beginners
Swimming Pool Calculator  I have to write a program to calculate... Calculator is one that is very simple to use. As you can see, it is a user... depth of a pool and the program will calculate the volume of that pool based
Writing Calculator Program in Swing
Writing Calculator Program in Swing   ... shot. For developing a small calculator program in swing we need two different.... Calculator Code in Java Swing Please save the code as SwingCalculator.java
java loan calculator applet help
java loan calculator applet help  Hi, I could use some help correcting a code here. I need to write a Java applet program (together with its html...); that java cannot find symbol and it points to where Loan is after new
Swimming Pool Calculator - Java Beginners
Swimming Pool Calculator  When I run the program the login window doesn't appear...Please help 1)LoginForm.java import javax.swing.*; import...()); mainFrame = new JFrame("Swimming Pool Volume Calculator"); calcButton = new
Swimming Pool Calculator - Java Beginners
Swimming Pool Calculator  Okay, so I tried making the program with this coding: import java.awt.*; import javax.swing.*; import java.awt.event.... Calculator", tempCalc ); jtabbedPane.addTab( "Customers", customers
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
Simple Calculator Application In Java Script
Simple Calculator Application In Java Script  ... the basics of JavaScript and create your first JavaScript program. What is simple Calculator The objective of this project is  learn how to write a simple
base calculator.. - Java Beginners
base calculator..  Help, i need some help about a base calculator.. i don't know how to start
Graphical calculator using AWT - Java Beginners
calculator program."); class CalcGUI extends JFrame { private final Font...Graphical calculator using AWT  Hi Sir, Thanks for the reply...); this.pack(); this.setTitle("Simple Calculator"); this.setResizable(false
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
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
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
calculator - Java Server Faces Questions
calculator  Some ideas for the project of calculator
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
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 /> >
swimming pool calculator
swimming pool calculator  i'm writing a program to calculate the measurements of a swimming pool & a hot tub. and then the user can enter...", customers); jtabbedPane.addTab("Temp Calculator", tempCalc
Example - Calc Extensions
. The calculator example program (See Example - Calc Main) uses integers. Extend the calculator to use double floating-point numbers... Java: Example - Calc Extensions Example - Calc Main, Example - Calc GUI
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
Writing Calculator Stateless Session Bean
Writing Calculator Stateless Session Bean...;String    COMP_NAME="java:comp/env/ejb/CalculatorBean";  ... for our Calculator Session Bean:   /* * 
Graphical calculator using AWT - Java Beginners
Graphical calculator using AWT  hi Sir, I need a source code for the following prgm...pls help me.. Implement a simple graphical calculator using AWT.The calculator shd perform simple operation like addition, subtraction
Java program - Applet
Java program  Can you please provide me a code for scientific calculator in java I need that in urgent.Please post me the answers  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java
Write a program in java...
Write a program in java...  Hi, friends Please, can you help me? Q1: Write a program in java to simulate a calculator. Your program should take two... to enter an integer number. Write a program in java to find the factorial
calculator midlet
calculator midlet  give me code calculator midlet in bluetooth application with j2me
How can i implement the calculator programe in jsp code
;calculator program in jsp</title> <script> function checkValue(){ var...; } } %> <center> <h2>Simple calculator program in jsp</h2> <...How can i implement the calculator programe in jsp code  Please send
program
program  write a program different between to dates in terms of days in java
program
program  WAP a java program to form 1/2+3/4+5/6+7/8 series
program
program  explanation of program on extending thread class   Hi Friend, Please go through the following link: Java Threads Thanks
program
program  write a java program to input a no. and print wheather the no. is a special no or not. (special no. are those no whose factorial of the sum of digit of the no is same as the original
program
is working in Java department and salary is 10000". Instantiate the Employee class
java program for
java program for   java program for printing documents,images and cards
PHP Tax Calculator - PHP
PHP Tax Calculator  In my project i required a tax calculator that can calculate the property tax
Java Program
Java Program  A Java Program that print the data on the printer but buttons not to be printed
a Java program
a Java program    Write a Java program to print even numbers from 2 to 1024? Write a Java program to print ? My Name is Mirza? 100 times? Write a Java program to print Fibonacci Series? Write a Java program to reverse a number
rogram - Java Beginners
Java program for calculator  I need the calculator program in Java

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.