Java applet program for calculator

Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the + – * % operations. Add a text field to display the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
/* 
<applet code="Cal" width=300 height=300>
</applet>
*/
 
public class Cal extends Applet
implements ActionListener
{
	String msg=" ";
	int v1,v2,result;
	TextField t1;
	Button b[]=new Button[10];
	Button add,sub,mul,div,clear,mod,EQ;
	char OP;
	public void init()
	{
		Color k=new Color(120,89,90);
		setBackground(k);
		t1=new TextField(10);
		GridLayout gl=new GridLayout(4,5);
		setLayout(gl);
		for(int i=0;i<10;i++)
		{
			b[i]=new Button(""+i);
		}
		add=new Button("add");
		sub=new Button("sub");
		mul=new Button("mul");
		div=new Button("div");
		mod=new Button("mod");
		clear=new Button("clear");
		EQ=new Button("EQ");
		t1.addActionListener(this);
		add(t1);
		for(int i=0;i<10;i++)
		{
			add(b[i]);
		}
		add(add);
		add(sub);
		add(mul);
		add(div);
		add(mod);
		add(clear);
		add(EQ);
		for(int i=0;i<10;i++)
		{
			b[i].addActionListener(this);
		}
		add.addActionListener(this);
		sub.addActionListener(this);
		mul.addActionListener(this);
		div.addActionListener(this);
		mod.addActionListener(this);
		clear.addActionListener(this);
		EQ.addActionListener(this);
	}
 
	public void actionPerformed(ActionEvent ae)
	{
		String str=ae.getActionCommand();
		char ch=str.charAt(0);
		if ( Character.isDigit(ch))
		t1.setText(t1.getText()+str);
		else
		if(str.equals("add"))
		{
			v1=Integer.parseInt(t1.getText());
			OP='+';
			t1.setText("");
		}
		else if(str.equals("sub"))
		{
			v1=Integer.parseInt(t1.getText());
			OP='-';
			t1.setText("");
		}
		else if(str.equals("mul"))
		{
			v1=Integer.parseInt(t1.getText());
			OP='*';
			t1.setText("");
		}
		else if(str.equals("div"))
		{
			v1=Integer.parseInt(t1.getText());
			OP='/';
			t1.setText("");
		}
		else if(str.equals("mod"))
		{
			v1=Integer.parseInt(t1.getText());
			OP='%';
			t1.setText("");
		}
		if(str.equals("EQ"))
		{
			v2=Integer.parseInt(t1.getText());
			if(OP=='+')
				result=v1+v2;
			else if(OP=='-')
				result=v1-v2;
			else if(OP=='*')
				result=v1*v2;
			else if(OP=='/')
				result=v1/v2;
			else if(OP=='%')
				result=v1%v2;
			t1.setText(""+result);
		}	
		if(str.equals("clear"))
		{
			t1.setText("");
		}
	}
}

Output:

Java program base calculator output
Java program base calculator output

78 Responses to “Java applet program for calculator”

    • Markusmccullan

      Java applet program does’nt have main method.we just over write the predefined method

      Reply
    • applet never has a main method… its got its own set of functions and methods

      Reply
  1. Abhinav

    i got an error in public class Cal
    “Cal is not abstract and doesn’t overrude abstract method actionPerformed (ActionEvent) in ActionListener”

    Reply
    • you need to override this method in your class since it is abstract. methods of abstract classes needs to be overidden.

      Reply
    • you need to override this method in your class since it is abstract. methods of abstract classes needs to be overridden.

      Reply
  2. Dipankar Nalui

    How to solve this Error?

    dipankar@dipankar-Lenovo ~/Desktop $ javac Cal.java
    dipankar@dipankar-Lenovo ~/Desktop $ java Cal
    Error: Main method not found in class Cal, please define the main method as:
    public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application
    dipankar@dipankar-Lenovo ~/Desktop $

    Reply

Leave a Reply