08/02/2023

Java Program Using Applet to Implement a Arithmetic Calculator

Write a Java Program Using Applet to Implement a Simple Arithmetic Calculator. We are going to write a java calculator program using AWT(Abstract Window Toolkit) and applet. As we know that calculator programs should perform some basics operation such as Addition, Subtraction, Multiplication, and division using the Plus, Minus, Multiply and Division operators present on our keyboard. The calculator program in java using AWT and applet is based on GUI(Graphical User Interface). Simple Calculator program in java using the applet.

Java Program Using Applet to Implement a Simple Arithmetic Calculator

Note: The name is case-sensitive so before renaming make sure all the changes are the same.

AWT Full Form in Java

AWT's full form is Abstract Window Toolkit.

Java Program Using Applet to Implement a Simple Arithmetic Calculator



import
java.awt.*; import java.awt.event.*; /*java header files required to run this program*/ import java.applet.*; /*<applet code = "calculator.class" width = 260 height = 310></applet>*/ public class calculator extends Applet implements ActionListener { TextField t1; Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0; /*All the buttons required in a Calculator Program in Java Using AWT and Applet*/ Button add, sub, mul, div, eql, dot; /*Operations we are going to perform*/ String msg = "", tmp; int a, b; public void init() { setLayout(null); t1 = new TextField(20); button1 = new Button("1"); button2 = new Button("2"); button3 = new Button("3"); button4 = new Button("4"); button5 = new Button("5"); button6 = new Button("6"); button7 = new Button("7"); button8 = new Button("8"); button9 = new Button("9"); button0 = new Button("0"); /*Initilizing a 0 to 9 number in buttons */ add = new Button("+"); sub = new Button("-"); div = new Button("/"); mul = new Button("*"); dot = new Button("."); eql = new Button("="); /*Initilizing all the operator to perform all the calculations in java AWT calculator*/ add(t1); /*result view window */ add(button7); add(button8); add(button9); add(div); /*First show in a calculator*/ add(button4); add(button5); add(button6); add(mul); /*Second show in a calculator*/ add(button1); add(button2); add(button3); add(sub); /*Third show in a calculator*/ add(dot); add(button0); add(eql); add(add); /*Forth show in a calculator*/ t1.setBounds(30, 30, 200, 40); button7.setBounds(30, 80, 44, 44); button8.setBounds(82, 80, 44, 44); button9.setBounds(134, 80, 44, 44); button4.setBounds(30, 132, 44, 44); button5.setBounds(82, 132, 44, 44); button6.setBounds(134, 132, 44, 44); button1.setBounds(30, 184, 44, 44); button2.setBounds(82, 184, 44, 44); button3.setBounds(134, 184, 44, 44); dot.setBounds(30, 236, 44, 44); button0.setBounds(82, 236, 44, 44); eql.setBounds(134, 236, 44, 44); add.setBounds(186, 236, 44, 44); sub.setBounds(186, 184, 44, 44); mul.setBounds(186, 132, 44, 44); div.setBounds(186, 80, 44, 44); button0.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); button5.addActionListener(this); button6.addActionListener(this); button7.addActionListener(this); button8.addActionListener(this); button9.addActionListener(this); //button0.addActionListener(this); //button0.addActionListener(this); div.addActionListener(this); mul.addActionListener(this); add.addActionListener(this); sub.addActionListener(this); eql.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")) { String str1 = t1.getText(); tmp = str; a = Integer.parseInt(str1); msg = ""; } else if (str.equals("=")) { String str2 = t1.getText(); b = Integer.parseInt(str2); int sum = 0; if (tmp == "+") sum = a + b; else if (tmp == "-") sum = a - b; else if (tmp == "*") sum = a * b; else if (tmp == "/") sum = a / b; String str1 = String.valueOf(sum); t1.setText("" + str1); msg = ""; } else { //String ae.getActionCommand(); //str += ae.getActionCommand(); msg += str; t1.setText("" + msg); } } public void paint(Graphics g) { g.setColor(Color.cyan); g.fillRect(20, 20, 220, 270); } }

Java AWT Calculator Operations and Task


There are four operations needed to perform a GUI(Graphical User Interface) based calculator, operations or tasks are followed.

  • Addition
  • Subtraction
  • Multiplication
  • Division

The list of operations of the task will be performed by the total 16 buttons and one text view area or result in the view window. These sixteens buttons are ten(0-9) number buttons, four operator buttons, one dot button and one equal button.

How to Run Java Applet Program


First, you need to download or copy-paste the program to any text editor you have, after that you need to save the program as "calculator.java". If you wish to change the program name you need to make changes in two more places one is the applet code.

Please read How to Run Java Applet Program in CMD Using Appletviewer for more information. I have explained all the things in detail with pictures.

The second one is the class name in the java program. If you do not make any changes as suggested below you will get an error "applet is not initialized". Both are shown in the below example.

How to Run Java Applet Program

The Applet is Not Initialized


If you are facing this issue make sure that your program class name and applet code are in the program with a comment and the program saves with the same name. Below is an example.

I have saved the below code as "calculator.java" and my Class name in the Java program is "calculator" and the applet code is also saved with the "calculator.class".

After following all the steps we can see that our calculator is working 100% without any problem. I also attached the source code with the output so you can see that our program is working fine without single errors.

Simple Calculator Program in Java Using Applet Output


Calculator Program in Java Using AWT and Applet

Similar to Calculator Using Applet


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: