Professional Documents
Culture Documents
Micro Project
On
Calculator
1
CERTIFICATE
“Calculator”
GROUP MEMBERS
66.Anand Chauhan
67.Sanjana Pawar
68.Rasika Shivdikar
69.Sagar Ansurkar
70.Sakshi Phadtare
2
Sr. No Name of the topic Page no
PART –A PLAN
1 Brief Introduction 1
3 Action Plan 3
4 Resources Used 4
PART –B OUTCOMES
2 Aim of Micro-Project 7
3
Calculator AJP,22517,SemV
PART-A PLAN
1.0 Brief Introduction
____________________________________________________
It create calculator using advance java language The calculator is like a part of
every daily life, it is a very powerful and general-purpose tool. We as human beings
tend to forget many things hence calculation is also a part of it. And what makes
this project more interesting is that we are building a Scientific Calculator. Unlike
basic calculators that can only handle smaller values, a scientific calculator can
handle numbers on a much vaster scale, which can be useful when it comes to
collecting data or working as a physicist or chemist.
PART –B OUTCOMES
1.0 Brief Description
____________________________________________________
AWT(Abstract Window Toolkit) is an API that helps in building
GUI (Graphical User Interface) based java applications. GUI helps in user
interactions using some graphics. It primarily consists of a set of classes and
methods that are required for creatin and managing the GUI in a simplified
manner such as buttons,windows,frame,textfield,RadioButton etc
I have uses Action listener interface for EventHandling.
Numeric button
when any of the numeric button pressed whatever value in label l1 will be
What ever value in label l1 will be stored in a variable zt and then concatenated
with the corresponding number and and then displayed in the label l1
for NEGATIVE and DECIMAL PTS Button we did it similarly
Arithmetic button
NOW here we store the value of label l1 into a variable num1 after converting
into double type which will be technically 1st number
and then and set label l1 to null we will just use a check variable for getting
that this particular airthmetic button(here +) was clicked so we can do this
operation in our = button
Equal button
NOW again store the value of l1 into num2 variable which will be
techincally 2nd number
and then check the value of variable check and then do corresponding
operation and after that display result in label l1
Clear button
here updated all the variable we use to its Default value 0
and set label l1 to null so that we can start our new calculation afterward
what I did is just handled one execption in EQUAL and
all AIRTHMETIC Buttons and printed a desired message according to
situtation
when we was converting the value into the double value, BUT lets say that,
label l1 has null value (i.e. label is empty) and we still pressed these button
then it will generate NumberFormatException execption,
So handled that and printed desired message
For eg:
If I click1 then + and then i click - instead of some other numeric button, hence
this an invalid format, and when -was clicked at that time label was null hence
execption generated so just handled it and printed invalid format inlabel
SIMILARLY, when label is null,and = was clicked in that situation ENTER
NUMBER FIRST will be displayed inside label
Code of Calculator.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame frame;
JTextField textfield;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[9];
JButton addButton,subButton,mulButton,divButton;
JButton decButton, equButton, delButton, clrButton, negButton;
JPanel panel;
double num1=0,num2=0,result=0;
char operator;
Calculator(){
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;
functionButtons[8] = negButton;
for(int i =0;i<9;i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
}
for(int i =0;i<10;i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
}
negButton.setBounds(50,430,100,50);
delButton.setBounds(150,430,100,50);
clrButton.setBounds(250,430,100,50);
panel.setLayout(new GridLayout(4,4,10,10));
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);
frame.add(panel);
frame.add(negButton);
frame.add(delButton);
frame.add(clrButton);
frame.add(textfield);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<10;i++) {
if(e.getSource() == numberButtons[i]) {
textfield.setText(textfield.getText().concat(String.valueOf(i)));
}
}
if(e.getSource()==decButton) {
textfield.setText(textfield.getText().concat("."));
}
if(e.getSource()==addButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='+';
textfield.setText("");
}
if(e.getSource()==subButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='-';
textfield.setText("");
}
if(e.getSource()==mulButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='*';
textfield.setText("");
}
if(e.getSource()==divButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='/';
textfield.setText("");
}
if(e.getSource()==equButton) {
num2=Double.parseDouble(textfield.getText());
switch(operator) {
case'+':
result=num1+num2;
break;
case'-':
result=num1-num2;
break;
case'*':
result=num1*num2;
break;
case'/':
result=num1/num2;
break;
}
textfield.setText(String.valueOf(result));
num1=result;
}
if(e.getSource()==clrButton) {
textfield.setText("");
}
if(e.getSource()==delButton) {
String string = textfield.getText();
textfield.setText("");
for(int i=0;i<string.length()-1;i++) {
textfield.setText(textfield.getText()+string.charAt(i));
}
}
if(e.getSource()==negButton) {
double temp = Double.parseDouble(textfield.getText());
temp*=-1;
textfield.setText(String.valueOf(temp));
}
}
}
Calculator:
1)Addition of 2+1:
2)Substraction of 3-1:
3)Multiply of 5*2:
4)Division of 6/2: