creating calculator using java applet


what is java applet? actually if you use java in programming then there are 2 ways in which you can create your program, namely using standalone application or an applet that is webbased. for this though I use EditPlus to compile my program using appletviewer.
and here is the source code

//<APPLET code=calculator.class height=318 name=calculator width=201 VIEWASTEXT><PARAM //NAME="foreground" VALUE="FFFFFF"><PARAM NAME="background" VALUE="008080"><PARAM NAME="label" //VALUE="This string was passed from the HTML host."></APPLET>

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

public class calculator extends java.applet.Applet implements ActionListener {
	TextField txtTotal = new TextField("");
    Button button[] = new Button[10];
	Button divide = new Button("/");
	Button mult = new Button("*");
	Button plus = new Button ("+");
	Button minus = new Button("-");
	Button isequalto = new Button("=");
	Button clear = new Button("CA");
	double num ,numtemp ;
	int counter;
	String strnum = "",strnumtemp = "" ;
	String op = "";

	public void operation() {
	 counter ++;
		 if (counter == 1) {
		  numtemp = num;
		  strnum = "";
		  num = 0;
		 }else{
		  if (op == "+") numtemp += num;
		  else if (op == "-") numtemp -= num;
		  else if (op == "*") numtemp = numtemp * num;
		  else if (op == "/") numtemp = numtemp / num;
		  strnumtemp = Double.toString(numtemp);
		  txtTotal.setText(strnumtemp);
		  strnum = "";
		  num = 0;
		 }
	}

	public void init() {
	 setLayout(null);

	 plus.setBackground(Color.blue);
	 plus.setForeground(Color.white);
     minus.setBackground(Color.blue);
	 minus.setForeground(Color.white);
     divide.setBackground(Color.blue);
	 divide.setForeground(Color.white);
	 isequalto.setBackground(Color.blue);
	 isequalto.setForeground(Color.white);
	 mult.setBackground(Color.blue);
	 mult.setForeground(Color.white);
	 clear.setBackground(Color.blue);
	 clear.setForeground(Color.red);

	 for(int i = 0;i <= 9; i ++) {
		 button[i] = new Button(String.valueOf(i));
		 button[i].setBackground(Color.orange);
		 button[i].setForeground(Color.blue);
	 }

	 button[1].setBounds(0,53,67,53);
	 button[2].setBounds(67,53,67,53);
	 button[3].setBounds(134,53,67,53);
	 button[4].setBounds(0,106,67,53);
	 button[5].setBounds(67,106,67,53);
	 button[6].setBounds(134,106,67,53);
	 button[7].setBounds(0,159,67,53);
	 button[8].setBounds(67,159,67,53);
	 button[9].setBounds(134,159,67,53);

	 for (int i = 1;i <= 9; i ++) {
		 add(button[i]);
	 }

	 txtTotal.setBounds(0,0,200,53);
	 add(txtTotal);
	 plus.setBounds(0,212,67,53);
	 add(plus);
	 button[0].setBounds(67,212,67,53);
	 add(button[0]);
	 minus.setBounds(134,212,67,53);
	 add(minus);
	 divide.setBounds(134,264,67,53);
	 add(divide);
	 isequalto.setBounds(67,264,67,53);
	 add(isequalto);
	 mult.setBounds(0,264,67,53);
	 add(mult);
	 add(clear);
	}

	public void start() {
	 for(int i = 0;i <= 9; i ++) {
		 button[i].addActionListener(this);
	 }
	 plus.addActionListener(this);
	 minus.addActionListener(this);
	 divide.addActionListener(this);
	 mult.addActionListener(this);
	 isequalto.addActionListener(this);
	 clear.addActionListener(this);
	}

	public void stop() {
	 for(int i = 0;i <= 9; i ++) {
		 button[i].addActionListener(null);
	 }
	 plus.addActionListener(null);
	 minus.addActionListener(null);
	 divide.addActionListener(null);
	 mult.addActionListener(null);
	 isequalto.addActionListener(null);
	 clear.addActionListener(null);
	}

	public void actionPerformed(ActionEvent e) {
		for(int i = 0;i <= 9; i++) {
			if (e.getSource() == button[i]) {
			   play(getCodeBase(),i + ".au");
			   strnum += Integer.toString(i);
			   txtTotal.setText(strnum);
			   num = Double.valueOf(strnum).doubleValue();
			}
		}

        if (e.getSource() == plus) {
		 operation();
		 op = "+";
		}

		if (e.getSource() == minus) {
		 operation();
		 op = "-";
		}

		if (e.getSource() == divide) {
		 operation();
		 op = "/";
		}

		if (e.getSource() == mult) {
		 operation();
		 op = "*";
		}

		if (e.getSource() == isequalto) {
		 if (op == "+") numtemp += num;
		 else if (op == "-") numtemp -= num;
		 else if (op == "*") numtemp = numtemp * num;
		 else if (op == "/") numtemp = numtemp / num;

		 strnumtemp = Double.toString(numtemp);
		 txtTotal.setText(strnumtemp);
		 strnumtemp = "";
		 numtemp = 0;
		 strnum = "";
		 num = 0;
		 counter = 0;
		}

		if (e.getSource() == clear) {
		 txtTotal.setText("0");
		 strnumtemp = "";
		 numtemp = 0;
		 strnum = "";
		 num = 0;
		 counter = 0;
		}
	}
}

and here is the screenshot.
Photobucket

when you create an applet always remember to use the HTML tag just like above this code using the <applet> </applet> tag. this is because applet is run in the browser and you need to specify that it an applet. But there may be some limitations that happened when you run your program in the browser due to some default security settings taking place in a browser. either way coding with java is always fun then. 🙂

Advertisements

11 comments on “creating calculator using java applet

  1. N.Kathiravan says:

    it works pretty good….thanks..

  2. mae says:

    can you expalin this line please,…..
    play(getCodeBase(),i + “.au”);

  3. karwan says:

    thanks for you
    but where is a frame and main?

  4. karwan says:

    thanks for you
    but where is a main and frame?

  5. kumar says:

    Very nice. Thanks for u.

  6. prince says:

    applets do not have main function

  7. Abhishek Saini says:

    thanks its working gud bt there is one problem i.e. there is no opotion of clear…………….
    bt its gud

  8. kamran says:

    pretty easy

  9. doki says:

    If u want clean button – do this:

    Add this before add(clear);
    clear.setBounds(0, 317, 201, 53);

    and in function init() place this too:
    setSize(203, 372);

  10. lokesh says:

    can i get the flow chat ?

  11. Sonam says:

    how do we exit the application..?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s