0% found this document useful (0 votes)
56 views7 pages

Java Applet Calculator Program

The document describes a Java program that implements a basic calculator applet. The program imports necessary libraries and defines buttons for numbers, operators, and functions. It initializes the buttons and text field, adds action listeners, and contains logic for number input, calculations, and output display. When run, the applet code is embedded in an HTML page and the user can perform basic arithmetic operations that are displayed in the text field.

Uploaded by

Karunakar Ella
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views7 pages

Java Applet Calculator Program

The document describes a Java program that implements a basic calculator applet. The program imports necessary libraries and defines buttons for numbers, operators, and functions. It initializes the buttons and text field, adds action listeners, and contains logic for number input, calculations, and output display. When run, the applet code is embedded in an HTML page and the user can perform basic arithmetic operations that are displayed in the text field.

Uploaded by

Karunakar Ella
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

CALCULATOR PROGRAM

PROGRAM:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.Math.*;
import java.io.*;
public class calc1 extends Applet implements ActionListener
{
String msg = "",flag = "";
float i=0,j=0;
Button one,two,three,four,five,six,seven,eight,nine,zero,sq,pw;
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");
sq = new Button("sq root");

pw= new Button("x^2");


one.setBounds(200,200,25,25);
two.setBounds(230,200,25,25);
three.setBounds(260,200,25,25);
four.setBounds(200,230,25,25);
five.setBounds(230,230,25,25);
six.setBounds(260,230,25,25);
seven.setBounds(200,260,25,25);
eight.setBounds(230,260,25,25);
nine.setBounds(260,260,25,25);
zero.setBounds(200,290,25,25);
plus.setBounds(290,200,25,25);
minus.setBounds(290,230,25,25);
mul.setBounds(290,260,25,25);
div.setBounds(290,290,25,25);
equal.setBounds(260,290,25,25);
ce.setBounds(230,290,25,25);
sq.setBounds(200,320,55,25);
pw.setBounds(260,320,55,25);
text = new TextField(40);
text.setBounds(200,170,120,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);
add(sq);
add(pw);
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);
sq.addActionListener(this);
pw.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(flag.equals("sq root"))
i=(float)(Math.sqrt(i));
if(flag.equals("x^2"))
i=(float)(Math.pow(i,2));
}
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 = "";
}
if(str.equals("sq root"))
{
flag="sq root";
}
if(str.equals("x^2"))
{
i=i; flag="x^2";
}
text.setText(i+"");
if (str.equals("=")) i=0;
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,100,100);
}
}
HTML CODE:

<applet code = "calc1.class" width = 700 height = 600>


</applet>
OUTPUT:

ADDITION:
2+3=5
SUBTRACTION:
10-5=5
MULTIPLICATION:
2*7=14
DIVISION:
14/2=7

You might also like