Q:

Java Calculator Program Using AWT & Applet

0

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

Java AWT Calculator Operations and Task

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

  • Addition
  • Subtraction
  • Multiplication
  • division

The list of operation of the task will be performed by the total 16 buttons and one text view area or result view window. These sixteens buttons are ten(0-9) number buttons, four operator buttons, one dot buttons 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 a changes in two more places one is the applet code and the second one is the class name in java program. If you do not make any changes as suggested in the below you will get an error "applet is not initialized". Both are shown in the below example.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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);
 }
}

Output:

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now