Scientific Calculator Java Lab Project
Scientific Calculator Java Lab Project
2024-2025
NARSIMHA REDDY ENGINEERING COLLEGE
Approved By AICTE, Permanently Affiliated to JNTUH, Accredited With NBA
and NAAC-A Grade,UGC 2(f) & 12(B), Hyderabad Sy.No.518, Maisammaguda
(V), Kompally – 500100, Telangana state
.
CERTIFICATE
This is to certify that a Micro Project entitled with: “DEVELOP SCIENTIFIC
CALCULATOR” is being Submitted By
CH.VENNELA 23X01A6627
C.HARSHITH 23X01A6628
CH.PAVAN KUMAR 23X01A6629
CH.SHIVA 23X01A6630
CH .JOSHNA 23X01A6631
In partial fulfillment of the requirement for award of the Object Oriented Programming
through JAVA of II-B.Tech I- Semester in CSE(DS) towards a record of a bonafide work
carried out under our guidance and supervision.
We hereby declare that the project work entitled entitled “Develop Scientific
Calculator”, is entirely our original work carried out under the guidance of
Mrs.P.revathy, Assistant Professor, Department of Computer Science and
Engineering, NARSIMHA REDDY ENGINEERING COLLEGE Affiliated to
JNTUH, Hyderabad, TG, India for the award of the degree of BACHELOR OF
TECHNOLOGY in COMPUTER SCIENCE AND ENGINEERING (AI/ML).
The work presented in this laboratory report/project is the result of our own
efforts and has not been submitted elsewhere for the award of any other degree
or diploma.
CH.VENNELA (23X01A6627)
C.HARSHITH (23X01A6628)
REDDY
CH.PAVAN (23X01A6629)
KUMAR
CH.SHIVA (23X01A6630)
KUMAR
CH.JOSHNA (23X01A6631)
CH.VENNELA (23X01A6627)
C.HARSHITH (23X01A6628)
REDDY
CONTENTS
1
ALGORITHM
STEP 1: Start
STEP 7: You can extend it by adding more functions, handling edge cases, and improving
user interaction if needed.
STEP 8: Stop
2
FLOWCHART
3
PROCEDURE
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.
Abstract
For the GUI of this java project, we will use the java swing toolkit
IntelliJ IDE will be suggested to use as it is faster and integrates the environment in a better
manner
Step — 1:-
Create a file named project.java inside IntelliJ IDE, Where we are going to write all the code
for the project
Step — 2:-
Import all the packages that are useful for the project
import jawa.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
Step — 3:-
Write the body matter for the project:-
Given all the commented code
4
REQUIREMENTS (HARDWARE & SOFTWARE)
Hardware Requirements
Minimum hardware requirement to run this on your Windows operating system as follows:
Software requirements
Operating System
Java SDK or JRE 1.6 or higher
Java Servlet Container (Free Servlet Container available)
Supported Database and library that supports the database connection with Java.
5
SOURCE CODE
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.*; import
java.util.*;
class Calculator {
public class Calculator {
JFrame frmCalculator;
String result="",expression="";
ArrayList<String> token=new ArrayList<String>();
Calculator() {
initialize();
}
int precedence(String x)
{
int p=10;
switch(x) {
case "+":
p=1;
case "-": break;
p=2;
case "x": break;
p=3;
case "/": break;
p=4;
case "^": break;
p=6;
case "!": break;
p=7;
} break;
return
p;
}
//operator checking
6
private boolean isoperator(String x)
{
7
if(x.equals("+") || x.equals("-") || x.equals("x") || x.equals("/") || x.equals("sqrt") || x.equals("^") ||
x.equals("!") || x.equals("sin") || x.equals("cos") || x.equals("tan") || x.equals("ln") || x.equals("log"))
return true;
else
return false;
}
s.push(i);
}else{
p=p+i+",";
}
}
while(!s.empty()) {
y=s.pop();
if(!y.equals("(") && !y.equals(")")) {
p+=y+",";
}
}
return p;
}
//factorial method
private double factorial(double y) {
double fact=1;
if(y==0 || y==1) {
fact=1;
}else {
for(int i=2; i<=y; i++) {
fact*=i;
}
}
return fact;
}
8
private double calculate(double x,double y,String c)
{
double res=0;
switch(c)
{
case "-":
res= x-y;
break;
case "+":
res= x+y;
break;
case "x":
res= x*y;
break;
case "/":
res= x/y;
break;
case "^":
res= Math.pow(x,y);
break;
default :
res= 0;
}
return res;
}
9
tokens[i].equals(" ")) {
token2.add(tokens[i]); // tokens from post fix form p actual tokens for calculation
10
}
}
Stack<Double> s=new
Stack<Double>(); double x,y;
for(String i:token2) {
if(isoperator(i)){
//if it is unary operator or function
if(i.equals("sin") ||i.equals("cos") ||i.equals("tan") ||i.equals("log") ||
i.equals("ln") || i.equals("sqrt") || i.equals("!")) {
y=s.pop();
s.push(calculate(y,i));
}else
{ //for binary operators
y=s.pop();
x=s.pop();
s.push(calculate(x,y,i));
}else{ }
if(i.equals("pi"))
s.push(Math.PI);
else if(i.equals("e"))
s.push(Math.E);
else
s.push(Double.valueOf(i));
}
}
double res=1; while(!
s.empty()) {
res*=s.pop();
}
return res; //final result
}
11
textPanel.setBounds(34, 25, 316, 80);
12
frmCalculator.getContentPane().add(textPanel);
textPanel.setLayout(null);
//clear button
JButton button1 = new JButton("C");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("0");
exprlabel.setText("");
expression ="";
token.clear();
result="";
num=false;
dot=false;
}
});
button1.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button1);
//delete button
JButton button2 = new JButton("DEL");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=textField.getText();
if(s != "0" && s.length() > 1) {
String newString = s.substring(0,s.length()-1);
textField.setText(newString);
if(expression.charAt(expression.length()-1)=='.') {
dot=false;
}
if(expression.charAt(expression.length()-1) == ',') {
expression = expression.substring(0,expression.length()-2);
}else {
expression = expression.substring(0,expression.length()-1);
}
}else {
13
textField.setText("0");
expression="";
}
}
});
button2.setFont(new Font("Calibri Light", Font.PLAIN, 14));
butttonPanel.add(button2);
num=false;
} dot=false;
});
button4.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button4);
//factorial button
JButton buttton5 = new JButton("x!");
buttton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"!");
expression+=",!";
}else {
textField.setText("0!");
expression+=",0,!";
}
num=false;
} dot=false;
});
buttton5.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(buttton5);
14
//button for sin
15
JButton button6 = new JButton("sin");
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"sin(");
}else {
textField.setText("sin(");
}
expression+=",sin,(";
num=false;
dot=false;
}
});
button6.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button6);
16
//button for squre root
JButton button10 = new
JButton("<html><body><span>√</span></body></html>");
button10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+Character.toString((char)8730));
}else
{ textField.setText(Character.toString((char)8730));
}
expression+=",sqrt";
num=false;
dot=false;
}
});
button10.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button10);
}
expression+=",cos,(";
num=false;
dot=false;
}
});
button11.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button11);
}else { expression+=",7";
} num=true;
});
button12.setBackground(new Color(220, 220, 220));
button12.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button12);
17
textField.setText(textField.getText()+"8");
}else {
textField.setText("8");
}
if(num)
{
18
expression+="8";
}else
{ expression+=",8";
} num=true;
});
button13.setBackground(new Color(220, 220, 220));
button13.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button13);
}else { expression+=",9";
} num=true;
});
button14.setBackground(new Color(220, 220, 220));
button14.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button14);
}
== '+') { if(s.charAt(s.length()-1)== '-' || s.charAt(s.length()-1)== 'x' || s.charAt(s.length()-1)
num=false;
} dot=false;
});
19
button15.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button15);
20
textField.setText(textField.getText()+"tan(");
}else
{ textField.setText("tan(");
expression+=",tan,(";
num=false;
dot=false;
}
});
button16.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button16);
}else { expression+=",4";
} num=true;
});
button17.setBackground(new Color(220, 220, 220));
button17.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button17);
}else { expression+=",5";
} num=true;
});
button18.setBackground(new Color(220, 220, 220));
button18.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button18);
21
}else {
textField.setText("6");
}
if(num)
{ expression+="6";
}else {
22
expression+=",6";
}
num=true;
}
});
button19.setBackground(new Color(220, 220, 220));
button19.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button19);
String s=textField.getText();
if(s.equals("0")) {
expression+="0";
}
== (char)(247)) { if(s.charAt(s.length()-1)== '-' || s.charAt(s.length()-1)== '+' || s.charAt(s.length()-1)
num=false;
} dot=false;
});
button20.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button20);
}
expression+=",ln,(";
num=false;
dot=false;
}
});
button21.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button21);
23
if(num)
{ textField.setText("1");
24
expression+="1";
}else
{ expression+=",1";
} num=true;
});
button22.setBackground(new Color(220, 220, 220));
button22.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button22);
}else { expression+=",2";
} num=true;
});
button23.setBackground(new Color(220, 220, 220));
button23.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button23);
}else { expression+=",3";
} num=true;
});
button24.setBackground(new Color(220, 220, 220));
button24.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button24);
25
}
if(s.charAt(s.length()-1)== '+') {
String newString = s.substring(0,s.length()-1);
newString += "-";
expression = expression.substring(0,expression.length()-1);
expression += "-";
26
textField.setText(newString);
}else if(s.charAt(s.length()-1)!= '-')
{ s += "-";
textField.setText(s);
expression += ",-";
}else {
textField.setText(s);
}
num=false;
} dot=false;
});
button25.setFont(new Font("Calibri Light", Font.BOLD, 23));
butttonPanel.add(button25);
} else {
27
expression+="0";
28
expression+=",0";
}
}
num=true;
}
});
button28.setBackground(new Color(220, 220, 220));
button28.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button28);
}
}
exprlabel.setText(s+"=");
textField.setText(result);
expression =
result; dot=true;
num=true;
token.clear();
}
});
button29.setBackground(Color.ORANGE);
button29.setFont(new Font("Calibri Light", Font.PLAIN, 22));
butttonPanel.add(button29);
}
== (char)(247)) { if(s.charAt(s.length()-1)== '-' || s.charAt(s.length()-1)== 'x' || s.charAt(s.length()-1)
29
} textField.setText(s);
30
num=false;
dot=false;
}
});
button30.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button30);
frmCalculator.setBounds(200, 100, 400, 500);
frmCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
31
RESULTS
Output:
32
CONCLUSION
The offered Java code, in conclusion, offers an effective and user-friendly scientific
calculator. Basic arithmetic, exponentiation, logarithm, and trigonometric functions are all
part of the wide variety of operations it provides. For managing erroneous options and
avoiding division by zero, the code includes exception handling. This scientific calculator
code functions as an efficient tool for carrying out numerous mathematical tasks thanks to its
menu-based user interface and precise computations.
Scientific calculators have several advantages over manual computation. Firstly, they are not
just used for obtaining numerical answers, but also play a valuable role in supporting students'
learning by aligning their functionality with the school mathematics curriculum . Secondly,
scientific calculators are handy tools for efficient computation, especially in complex design
and analysis of engineering systems . They also provide opportunities for conceptual
development and deep understanding of mathematical concepts, which is important for
student learning . Additionally, advanced scientific calculators offer features such as graphing
programs, equation solvers, matrix algebra, and advanced statistics, making them
advantageous for various applications, including simulations and graphical displays in
chemistry . Furthermore, the advent of calculators has made numerical methods just as
important as algebraic methods in the analysis of physics experiments, suggesting that
numerical methods should be given more priority in mathematics courses for scientists and
engineers .
33
REFERENCES
1. https://medium.com/@rd893918/scientific-calculator-using-java- b33dc03aeb16
2. https://javabelazy.blogspot.com/2012/03/scientific-calculator-in-
java.html#google_vignette
3. https://ide.geeksforgeeks.org/online-java-compiler
4. https://www.prepbytes.com/blog/java/calculator-program-in-java/
5. https://copyassignment.com/scientific-calculator-in- java/#google_vignettE
6. https://www.programiz.com/java-programming/examples/calculator-switch- case
34