Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

INSTITUTE OF ENGINEERING

& MANAGEMENT
Department of Computer Science & Engineering

Name : Adrineel Saha ; Ayush Kumar


Jha Class Roll : 191 ; 197
Enrollment No. :
12019002003114 ;
12019002007020
Subject Name : OOP Theory Mini
Project Date : 08/12/2021
Scientific Calculator

Design a scientific calculator using the Java Swing framework. Your


design should contains following
operations,
1. Basic operators : +, -, *, /, ^(for exponential),%(mod) 2. Basic math
function: sin, cos, tan, log, sin-1, cos-1, tan-1(inverse)
3. Basic errors: parenthesis checking, operand checking (e.g.
1..2,1.2.3,12A etc. are invalid operands),
4. Basic conversions from one unit to another unit.
5. Basic mathematical conversion like decimal to binary and vice
versa.
Input pattern:
I. Your input expression may contain operands with multiple digits.
(For example, 23+8*9 is valid).
II. It can evaluate any combination like 2+cos(x)+log20
III. It can also work perfectly for those operators associativity
working from right to left.
Basic Design characteristics:
You can add multiple panels in a single layout. Each frame consists of
multiple buttons of similar type of operations. For example all the
buttons of mathematical functions like sin,cos etc must be added into
a single panel.
You have to design in a concise manner. For example you can add
multiple layouts to cover up different functionalities. Suppose in a
single layout you are covering only the point 4.
Source Code:-
package scientific_calculator;

import javax.swing.*;
import java.awt.event.*;
class Scientific_Calculator implements ActionListener{
JFrame JF;
public JTextField t;
JButton
b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,d_2_b,b_2_d,bmod,sin,cos,tan,bsum,bsub,b
m ul,bdiv,dot,l_par,r_par,

delete,clear,equal,arcsin,arccos,arctan,log,kilo,hecto,deca,desi,senti,mili,ex
p o,ln;
public Scientific_Calculator(){
JF = new JFrame("Scientific Calculator");
t = new JTextField();
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
bsum = new
JButton("+"); bsub = new
JButton("-"); bmul = new
JButton("*");

bdiv = new JButton("/"); d_2_b


= new JButton("DTB"); b_2_d =
new JButton("BTD"); bmod =
new JButton("%"); sin = new
JButton("sin"); cos = new
JButton("cos"); tan = new
JButton("tan"); arcsin = new
JButton("sin-1"); arccos = new
JButton("cos-1"); arctan = new
JButton("tan-1"); log = new
JButton("log"); dot = new
JButton(".");

l_par = new JButton("(");


r_par = new JButton(")");
delete = new JButton("DEL");
clear = new JButton("CLR");
equal = new JButton("="); kilo
= new JButton("KIlo"); hecto =
new JButton("Hecto"); deca =
new JButton("Deca"); desi =
new JButton("Desi"); senti =
new JButton("Senti"); mili =
new JButton("Mili");
expo = new JButton("^");
ln = new JButton("ln");

JF.add(ln);
JF.add(t);
JF.add(b0);
JF.add(b1);
JF.add(b2);
JF.add(b3);
JF.add(b4);
JF.add(b5);
JF.add(b6);
JF.add(b7);
JF.add(b8);
JF.add(b9);
JF.add(bsum);
JF.add(bsub);
JF.add(bmul);
JF.add(bdiv);
JF.add(bmod);
JF.add(sin);
JF.add(cos);
JF.add(tan);
JF.add(b_2_d);
JF.add(d_2_b);
JF.add(arcsin);
JF.add(arccos);
JF.add(arctan);
JF.add(delete);
JF.add(dot);
JF.add(log);
JF.add(l_par);
JF.add(r_par);
JF.add(clear);
JF.add(kilo);
JF.add(hecto);
JF.add(deca);
JF.add(desi);
JF.add(senti);
JF.add(mili);
JF.add(equal);
JF.add(expo);
JF.setLayout(null);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);

JF.setSize(400,400);

t.setBounds(40,40,280,40);

b0.setBounds(40,110,70,40);
b1.setBounds(110,110,70,40);
b2.setBounds(180,110,70,40);
bsum.setBounds(250,110,70,40)
;

b3.setBounds(40,150,70,40);
b4.setBounds(110,150,70,40);
b5.setBounds(180,150,70,40);
bsub.setBounds(250,150,70,40)
;

b6.setBounds(40,190,70,40);
b7.setBounds(110,190,70,40);
b8.setBounds(180,190,70,40);
bmul.setBounds(250,190,70,40)
;

b9.setBounds(40,230,70,40);
log.setBounds(110,230,70,40);
ln.setBounds(180,230,70,40);
bdiv.setBounds(250,230,70,40)
;

sin.setBounds(40,270,70,40);
cos.setBounds(110,270,70,40);
tan.setBounds(180,270,70,40);
bmod.setBounds(250,270,70,40);

arcsin.setBounds(40,310,70,40);
arccos.setBounds(110,310,70,40)
;
arctan.setBounds(180,310,70,40)
;
expo.setBounds(250,310,70,40);

kilo.setBounds(40,350,70,40);
hecto.setBounds(110,350,70,40)
;
deca.setBounds(180,350,70,40);
dot.setBounds(250,350,70,40);

desi.setBounds(40,390,70,40);
senti.setBounds(110,390,70,40);
mili.setBounds(180,390,70,40);
l_par.setBounds(250,390,70,40)
;

b_2_d.setBounds(40,430,70,40);
d_2_b.setBounds(110,430,70,40)
;
r_par.setBounds(180,430,70,40);
equal.setBounds(250,430,70,40);

delete.setBounds(110,470,70,40)
; clear.setBounds(180,470,70,40);

b0.addActionListener(this)
;
b1.addActionListener(this)
;
b2.addActionListener(this)
;
b3.addActionListener(this)
;
b4.addActionListener(this)
;
b5.addActionListener(this)
;
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bsum.addActionListener(this)
;

bsub.addActionListener(this);
bmul.addActionListener(this)
;
bdiv.addActionListener(this);

d_2_b.addActionListener(this);
b_2_d.addActionListener(this);
bmod.addActionListener(this);
sin.addActionListener(this);

cos.addActionListener(this);
tan.addActionListener(this);
arcsin.addActionListener(this);
arccos.addActionListener(this);
arctan.addActionListener(this);
log.addActionListener(this);
dot.addActionListener(this);
l_par.addActionListener(this);
r_par.addActionListener(this);
delete.addActionListener(this);
clear.addActionListener(this);
equal.addActionListener(this);
kilo.addActionListener(this);
hecto.addActionListener(this);
deca.addActionListener(this);
desi.addActionListener(this);
senti.addActionListener(this);
mili.addActionListener(this);
expo.addActionListener(this);
ln.addActionListener(this);
}
public void actionPerformed(ActionEvent AE)
{ if(AE.getSource()==b0)
t.setText(t.getText().concat("0"));
if(AE.getSource()==b1)

t.setText(t.getText().concat("1"));
if(AE.getSource()==b2)

t.setText(t.getText().concat("2"));
if(AE.getSource()==b3)

t.setText(t.getText().concat("3"));
if(AE.getSource()==b4)

t.setText(t.getText().concat("4"));
if(AE.getSource()==b5)

t.setText(t.getText().concat("5"));
if(AE.getSource()==b6)

t.setText(t.getText().concat("6"));
if(AE.getSource()==b7)

t.setText(t.getText().concat("7"));
if(AE.getSource()==b8)

t.setText(t.getText().concat("8"));
if(AE.getSource()==b9)
t.setText(t.getText().concat("9"));
if(AE.getSource()==dot)

t.setText(t.getText().concat("."));
if(AE.getSource()==l_par)

t.setText(t.getText().concat("("));
if(AE.getSource()==r_par)

t.setText(t.getText().concat(")"));
if(AE.getSource()==sin)

t.setText(t.getText().concat("sin"));
if(AE.getSource()==cos)
t.setText(t.getText().concat("cos"));
if(AE.getSource()==tan)

t.setText(t.getText().concat("tan"));
if(AE.getSource()==arcsin)

t.setText(t.getText().concat("sin-1"));
if(AE.getSource()==arccos)

t.setText(t.getText().concat("cos-1"));
if(AE.getSource()==arctan)

t.setText(t.getText().concat("tan-1"));
if(AE.getSource()==log)

t.setText(t.getText().concat("log"));
if(AE.getSource()==ln)

t.setText(t.getText().concat("ln"));
if(AE.getSource()==b_2_d)

t.setText(t.getText().concat("BTD"));
if(AE.getSource()==d_2_b)
t.setText(t.getText().concat("DTB"));
if(AE.getSource()==bsum)

t.setText(t.getText().concat("+"));
if(AE.getSource()==bsub)

t.setText(t.getText().concat("-"));
if(AE.getSource()==bmul)

t.setText(t.getText().concat("*"));
if(AE.getSource()==bdiv)

t.setText(t.getText().concat("/"));
if(AE.getSource()==bmod)

t.setText(t.getText().concat("%"));
if(AE.getSource()==expo)
t.setText(t.getText().concat("^"));
if(AE.getSource()==kilo)

t.setText(t.getText().concat("Kilo"));
if(AE.getSource()==hecto)

t.setText(t.getText().concat("Hecto"));
if(AE.getSource()==deca)

t.setText(t.getText().concat("Deca"));
if(AE.getSource()==desi)

t.setText(t.getText().concat("Desi"));
if(AE.getSource()==senti)

t.setText(t.getText().concat("Senti"));
if(AE.getSource()==mili)

t.setText(t.getText().concat("Mili"));
if(AE.getSource()==clear)
t.setText("");
if(AE.getSource()==delete)
{
String s1;
String s = t.getText();
s1 = s.substring(0,s.length()-1);
t.setText(s1);

}
if(AE.getSource()==equal){
String eqn = t.getText();
String s3;

int a = 0,y = 0,c = 0;


for(int i = 0;i<eqn.length();i++)
{ if(eqn.charAt(i) == '(') a++;
if(eqn.charAt(i) == ')') a--;

for(int i = 0;i<eqn.length();i++)
{ if(eqn.charAt(i) == '.'){ y++;

if(y>1)
break;
}
if(eqn.charAt(i) == '+' || eqn.charAt(i) == '-' || eqn.charAt(i) == '*' ||
eqn.charAt(i) == '/' || eqn.charAt(i) == '%' || eqn.charAt(i) == '^')
y = 0;
}

for(int i = 0;i<eqn.length()-1;i++){
if((eqn.charAt(i) == '.' ||
(Character.getNumericValue(eqn.charAt(i)) >= 0 &&
Character.getNumericValue(eqn.charAt(i)) <= 9)) &&
((eqn.charAt(i+1) >= 'a' && eqn.charAt(i+1) <='z') ||
(eqn.charAt(i+1) >= 'a' && eqn.charAt(i+1) <='z'))){
c++;
break;
}
}
if(a != 0 || y>1 || c != 0)
t.setText("Error");
else{
int i = 0,k = 0,p = 0,j = 0;
String t = "";
String s = eqn+"+0";
double[] arr = new double[20];
double[] b = new double[20];
while(i != s.length()-1){
if(s.charAt(i) >= '0' && s.charAt(i) <= '9'){
while((s.charAt(i) >= '0' && s.charAt(i) <= '9') || s.charAt(i)=='.'){
t = t+s.charAt(i); i++;

}
arr[k] = Double.parseDouble(t); k++;

t = "";
}
if(s.charAt(i) == '+'){
b[p] = 1;
p++;
i++;
}
if(s.charAt(i) == '-'){
b[p] = 2;
p++;
i++;
}
if(s.charAt(i) == '*'){
b[p] = 3;
p++;
i++;
}
if(s.charAt(i) == '/'){
b[p] = 4;
p++;
i++;
}
if(s.charAt(i) == '%'){
b[p] = 5;
p++;
i++;
}
if(s.charAt(i) == '^'){
b[p] = 6;
p++;
i++;
}
if(s.charAt(i) == ')' || s.charAt(i) == '('){
i++;
}

if(s.charAt(i) == 'l' && s.charAt(i+1) == 'o'){


i = i+4;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = Math.log(Double.parseDouble(t))/Math.log(10.0); k++;

t = "";
}
if(s.charAt(i) == 'l' && s.charAt(i+1) == 'n'){
i = i+3;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = Math.log(Double.parseDouble(t)); k++;

t = "";
}
if(s.charAt(i) == 's' && s.charAt(i+3) != '-'){
i = i+4;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = Math.sin(Double.parseDouble(t)*Math.PI/180); k++;

t = "";
}
if(s.charAt(i) == 'c' && s.charAt(i+3) != '-'){
i = i+4;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = Math.cos(Double.parseDouble(t)*Math.PI/180);
k++;
t = "";
}
if(s.charAt(i) == 't' && s.charAt(i+3) != '-'){
i = i+4;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = Math.tan(Double.parseDouble(t)*Math.PI/180); k++;

t = "";
}
if(s.charAt(i) == 's' && s.charAt(i+3) == '-'){
i = i+6;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = Math.asin(Double.parseDouble(t)); k++;

t = "";
}
if(s.charAt(i) == 'c' && s.charAt(i+3) == '-'){
i = i+6;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = Math.acos(Double.parseDouble(t)); k++;

t = "";
}

if(s.charAt(i) == 't' && s.charAt(i+3) == '-'){ i = i+6;

while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = Math.atan(Double.parseDouble(t)); k++;

t = "";
}
if(s.charAt(i) == 'K'){
i = i+5;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = (Double.parseDouble(t))/1000.0; k++;
t = "";
}
if(s.charAt(i) == 'H'){
i = i+6;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = (Double.parseDouble(t))/100.0; k++;

t = "";
}
if(s.charAt(i) == 'D' && s.charAt(i+2) == 'c'){ i = i+5;

while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = (Double.parseDouble(t))/10.0; k++;
t = "";
}
if(s.charAt(i) == 'D' && s.charAt(i+2) == 's'){ i = i+5;

while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = (Double.parseDouble(t))*10.0; k++;

t = "";
}
if(s.charAt(i) == 'S' && s.charAt(i+1) == 'e'){ i = i+6;

while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = (Double.parseDouble(t))*100.0; k++;

t = "";
}
if(s.charAt(i) == 'M'){
i = i+5;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
arr[k] = (Double.parseDouble(t))*1000.0; k++;

t = "";
}
if(s.charAt(i) == 'B'){
i = i+4;
while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
double m = Double.parseDouble(t); double n;

double sum = 0;
double r;
double q = 0;
while(m != 0){
n = m % 10;
r = n*Math.pow(2,q);
q++;
sum = sum + r;
m = Math.floor(m/10); }

arr[k] = sum;
k++;
t = "";
}
if(s.charAt(i) == 'D' && s.charAt(i+1) == 'T'){ i = i+4;

while(s.charAt(i) != ')'){
t = t+s.charAt(i);
i++;
}
double z = Double.parseDouble(t); double f;

double g ;
double h = 0;
double num = 0;
while(z != 0){
f = z%2;
g = f*Math.pow(10,h); h++;

num = num+g;
z = Math.floor(z/2);
}
arr[k] = num;
k++;
t = "";
}
}

int v;
for(j=0;j<b.length;j++){
if(b[j] == 6){
arr[j] = Math.pow(arr[j],arr[j+1]);
for(v=j;v<b.length-1;v++){ b[v] = b[v+1];
}
for(v=j+1;v<arr.length-1;v++){ arr[v] =
arr[v+1]; }

}
}
for(int w=0;w<b.length;w++)
{ for(j=0;j<b.length;j++){

if(b[j] == 3 || b[j] == 4 || b[j] == 5){ if(b[j] == 3){

arr[j] = arr[j]*arr[j+1]; }

else if(b[j] == 4){


arr[j] = arr[j]/arr[j+1]; }

else{
arr[j] = arr[j]%arr[j+1]; }

for(v=j;v<b.length-1;v++){ b[v] = b[v+1];

}
for(v=j+1;v<arr.length-1;v++){ arr[v] =
arr[v+1]; }

j=0;
break;
}
}
}

for(j=0;j<b.length;j++){
if(b[j] == 1){
arr[j+1] = arr[j+1]+arr[j];
}
if(b[j] == 2){
arr[j+1] = arr[j] - arr[j+1];
}
if(b[j] == 0){
break;
}
}
eqn = String.valueOf(arr[j-1]);
}
t.setText(eqn);
}
}
public static void main(String[] args){
Scientific_Calculator SC = new Scientific_Calculator();
}

}
Output:-

You might also like