calculator java source code
#1

Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code = "calc" width = 750 height = 600>
</applet>
*/

public class calc extends Applet implements ActionListener{
    String msg = "",flag = "";
    int i=0,j=0;
    Button one,two,three,four,five,six,seven,eight,nine,zero;
    Button plus,minus,mul,div,equal,ce;
    TextField text;
    Button blist[] = new Button[10];

    public void init(){
        setLayout(null);
        one = new Button("1");
        two = new Button("2");
        three = new Button("3");
        four = new Button("4");
        five = new Button("5");
        six = new Button("6");
        seven = new Button("7");
        eight = new Button("8");
        nine = new Button("9");
        zero = new Button("0");

        plus = new Button("+");
        minus = new Button("-");
        mul = new Button("*");
        div = new Button("/");
        equal = new Button("=");
        ce = new Button("c");

        one.setBounds(200,200,20,20);
        two.setBounds(230,200,20,20);
        three.setBounds(260,200,20,20);
        four.setBounds(200,230,20,20);
        five.setBounds(230,230,20,20);
        six.setBounds(260,230,20,20);
        seven.setBounds(200,260,20,20);
        eight.setBounds(230,260,20,20);
        nine.setBounds(260,260,20,20);
        zero.setBounds(200,290,20,20);

        plus.setBounds(290,200,20,20);
        minus.setBounds(290,230,20,20);
        mul.setBounds(290,260,20,20);
        div.setBounds(290,290,20,20);
        equal.setBounds(260,290,20,20);
        ce.setBounds(230,290,20,20);

        text = new TextField(10);
        text.setBounds(200,170,110,20);
        blist[0] = (Button) add(zero);
        blist[1] = (Button) add(one);
        blist[2] = (Button) add(two);
        blist[3] = (Button) add(three);
        blist[4] = (Button) add(four);
        blist[5] = (Button) add(five);
        blist[6] = (Button) add(six);
        blist[7] = (Button) add(seven);
        blist[8] = (Button) add(eight);
        blist[9] = (Button) add(nine);
        add(plus);
        add(minus);
        add(mul);
        add(div);
        add(equal);
        add(ce);
        add(text);
        for(int i=0;i<10;i++)
            blist[i].addActionListener(this);
        plus.addActionListener(this);
        minus.addActionListener(this);
        mul.addActionListener(this);
        div.addActionListener(this);
        equal.addActionListener(this);
        ce.addActionListener(this);
        text.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae){
        String str = ae.getActionCommand();
        
        if(str.equals("1"))
            i = i*10 + 1;
        else if(str.equals("2"))
            i = i*10 + 2;
        else if(str.equals("3"))
            i = i*10 + 3;
        else if(str.equals("4"))
            i = i*10 + 4;
        else if(str.equals("5"))
            i = i*10 + 5;
        else if(str.equals("6"))
            i = i*10 + 6;
        else if(str.equals("7"))
            i = i*10 + 7;
        else if(str.equals("8"))
            i = i*10 + 8;
        else if(str.equals("9"))
            i = i*10 + 9;
        else if(str.equals("0"))
            i = i*10;
        
        if (str.equals("=")){
            if (flag.equals("+"))
                i += j;
            if (flag.equals("-"))
                i = j-i;
            if (flag.equals("*"))
                i *= j;
            if (flag.equals("/"))
                i = j/i;
        }
            
        if (str.equals("+")){
            if (flag.equals("+"))
                {i += j;j=i;}
            else
                {j = i;i=0;}
            flag = "+";
        }
        if (str.equals("-")){
            if (flag.equals("-"))
            {    i = j-i;j=i;}
            else
                {j = i;i=0;}
            flag = "-";
        }
        if (str.equals("*")){
            if (flag.equals("*"))
            {    i *= j;j=i;}
            else
                {j = i;i=0;}
            flag = "*";
        }
        if (str.equals("/")){
            if (flag.equals("/"))
            {    i = j/i;j=i;}
            else
                {j = i;i=0;}
            flag = "/";
        }
        if (str.equals("c"))
        {    i = 0;j=0;flag = "";}

        
        text.setText(i+"");
        if (str.equals("=")) i=0;
        repaint();
    }

    public void paint(Graphics g){
        g.drawString(msg,100,100);
    }
}
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page
Popular Searches: hvof cost calculator, hoffman code calculator, calculator for blind in vb, sourse code engineer calculator in java, routing cabinet doors calculator, pneumatic press tonnage calculator, e85 calculator,

[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  projects in core java project topics 6 16,170 16-01-2018, 11:15 AM
Last Post: dhanabhagya
  Steganography implemented in Java science projects buddy 14 12,240 24-05-2016, 10:15 AM
Last Post: dhanabhagya
  STUDENT INFORMATION SYSTEM IN JAVA project topics 14 10,657 19-08-2015, 11:28 PM
Last Post: Guest
  mini projects in java project topics 7 18,813 01-05-2015, 04:18 PM
Last Post: seminar report asees
  Visa Processing System full report and asp source code project topics 2 5,563 19-06-2014, 06:54 PM
Last Post: seminar report asees
  distributed cache updating for the dynamic source routing protocol project report tiger 4 3,000 05-03-2013, 02:22 PM
Last Post: Guest
  application projects in java and vb.net (titles and topics) project topics 1 5,506 28-11-2012, 01:11 PM
Last Post: seminar details
  Energy-Efficient Routing in Mobile Ad Hoc Networks: Mobility-Assisted Case (Java) project topics 1 1,771 12-11-2012, 12:42 PM
Last Post: seminar details
  Net Auction java based project report project topics 2 3,718 01-11-2012, 12:59 PM
Last Post: seminar details
  distributed applications using RMI-Java model. seminar class 1 1,489 31-05-2012, 12:29 AM
Last Post: Guest

Forum Jump: