0% found this document useful (0 votes)
4 views38 pages

Scientific Calculator Java Lab Project

The document is a laboratory report for a project titled 'Develop Scientific Calculator' submitted by students of Narsimha Reddy Engineering College in partial fulfillment of their Object Oriented Programming course. It outlines the project's objectives, requirements, implementation details, and includes source code for the calculator developed using Java. The report emphasizes adherence to OOP principles and the integration of advanced mathematical functions to create a versatile scientific calculator.

Uploaded by

harshithr283
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views38 pages

Scientific Calculator Java Lab Project

The document is a laboratory report for a project titled 'Develop Scientific Calculator' submitted by students of Narsimha Reddy Engineering College in partial fulfillment of their Object Oriented Programming course. It outlines the project's objectives, requirements, implementation details, and includes source code for the calculator developed using Java. The report emphasizes adherence to OOP principles and the integration of advanced mathematical functions to create a versatile scientific calculator.

Uploaded by

harshithr283
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Laboratory Report on

Develop Scientific Calculator


Submitted to the narshima reddy engineering college, Hyderabad in partial fulfilment
of the requirement for the award of the Laboratory of

Object Oriented Programming through JAVA


of
II-B.Tech. I-Semester
in
Computer Science and Engineering (ai/ml)
Submitted by
CH VENNELA 23X01A6627
C.HARSHITH 23X01A6628
CH PAVAN KUMAR 23X01A6629
CH.SHIVA 23X01A6630
CH JOSHNA 23X01A6631

Under the Guidance Of


Ms. REVATHY.P
(Assistant Professor, Dept of CSE(ai/ml))

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

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
.

Department of Computer Science and Engineering (ai/ml)

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.

Signature of Faculty Signature of HOD


Mrs. REVATHY.P Mr.vara prasad
(Assistant Professor) (Head Of Department)
Dept. Of CSE(ai/ml) Dept. Of CSE()
ACKNOWLEDGEMENT

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

Sl. No. Particulars Page No.


1. Introduction 01
2. Algorithm / Flowchart / Procedure 02-04
3. Requirements (Hardware and Software) 05
4. Implementation (code) 06-20
5. Results 21
6. Conclusion 22
7. References 23
INTRODUCTION

Developing a scientific calculator using Object-Oriented Programming (OOP) in Java involves


creating classes and objects to represent various functionalities. Start by defining a class for
the calculator, incorporating features like arithmetic operations, trigonometric functions, and
memory storage. Utilize encapsulation, inheritance, and polymorphism to organize and
enhance the code structure. Ensure modularity for easy maintenance and scalability.
Remember to adhere to OOP principles, such as abstraction, encapsulation, inheritance, and
polymorphism, throughout the development process.

It combining mathematical functions and programming to create a tool capable of


performing involves complex scientific calculations. The process typically includes designing
a user-friendly interface, implementing algorithms for mathematical operations, and ensuring
accuracy in calculations. Advanced features such as trigonometric functions, logarithms, and
statistical operations are integrated to cater to diverse scientific needs. The goal is to provide
a versatile and reliable tool that aids professionals, students, and researchers in performing
intricate mathematical computations with precision.

1
ALGORITHM
STEP 1: Start

STEP 2: Define the Calculator Class:


Create a class named ScientificCalculator that encapsulates the calculator functionality.
Define instance variables for storing operands and results.

STEP 3: Implement Basic Operations:


algorithm provides a basic framework for a scientific calculator. You can extend it by adding
more functions, handling edge cases, and improving user interaction if needed.

STEP 4: Extend for Scientific Functions:


Add methods for scientific functions (e.g., square root, logarithm, trigonometric functions).

STEP 5: Implement Memory Functions:


Include memory-related methods (e.g., store, recall, clear).

STEP 6: Create a Main Class:


Develop a separate class with a main method to demonstrate and test the calculator.

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

Java is a software development-oriented computer/programming language. Java has so many


features including object-oriented programming, Robust, Platform independent, and High
Performance, and also provides a very wide range of toolkits or software development kits
(SDK) for example java awt or java swing which are going to use later in this project.

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:

 Minimum Windows 95 software


 IBM-compatible 486 system
 Hard Drive and Minimum of 8 MB memory
 A CD-ROM drive
 Mouse, keyboard and sound card, if required

Software requirements

Nowadays, Java is supported by almost every operating systems. Whether it is a Windows,


Macintosh and Unix all supports the Java application development. So you can download
any of the operating system on your personal computer. Here are the minimum requirement.

 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>();

boolean num=false; boolean


dot=false;

public static void main(String[] args) {


EventQueue.invokeLater(new Runnable()
{
public void run() {
try {
Calculator window = new Calculator();
window.frmCalculator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

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

private String infixTopostfix()


{
Stack<String> s=new
Stack<String>(); String y;
int flag;
String p="";
token.add(")");
s.push("(");
for(String i: token) {
if(i.equals("(")){
s.push(i);
}else if(i.equals(")")){
y=s.pop(); while(!
y.equals("("))
{
p=p+y+",";
y=s.pop();
}
}else if(isoperator(i)){
y=s.pop();
flag=0;
if(isoperator(y) && precedence(y)>precedence(i)){
p=p+y+",";
flag=1;
}
if(flag==0)
s.push(y);

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

//for actual calculation with binary operators

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

//calculation with unary operators


private double calculate(double y,String c) {
double res=0;
switch(c) {
case "log":
res =
Math.log10(y);
break;
case "sin":
res= Math.sin(y);
break;
case "cos":
res =
Math.cos(y);
break;
case "tan":
res =Math.tan(y);
break;
case "ln":
res= Math.log(y);
break;
case "sqrt":
res= Math.sqrt(y);
break;
case "!":
res=factorial(y);
break;
}
return res;
}

private double Eval(String p)


{
String tokens[] = p.split(",");
ArrayList<String> token2=new ArrayList<String>();
for(int i=0; i<tokens.length; i++) {
if(! tokens[i].equals("") && ! tokens[i].equals(" ") && ! tokens[i].equals("\n") && !

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
}

//actual combined method for calculation


private void calculateMain() {
String tokens[]=expression.split(",");
for(int i=0; i<tokens.length; i++) {
if(! tokens[i].equals("") && ! tokens[i].equals(" ") && ! tokens[i].equals("\n") && !
tokens[i].equals(" ")) {
token.add(tokens[i]); //adding token to token array list from expression
}
}
try {
double res = Eval(infixTopostfix());
result= Double.toString(res);
}catch(Exception e) {}
}

//design of the frame with their action listner


private void initialize() {
frmCalculator = new JFrame();
frmCalculator.setResizable(false);
frmCalculator.setTitle("Calculator");
frmCalculator.getContentPane().setBackground(new Color(172, 170, 255));
frmCalculator.getContentPane().setFont(new Font("Calibri", Font.PLAIN,
15));
frmCalculator.getContentPane().setForeground(SystemColor.windowBorder);
frmCalculator.getContentPane().setLayout(null);

JPanel textPanel = new JPanel();


textPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));

11
textPanel.setBounds(34, 25, 316, 80);

12
frmCalculator.getContentPane().add(textPanel);
textPanel.setLayout(null);

JLabel exprlabel = new JLabel("");


exprlabel.setBackground(SystemColor.control);
exprlabel.setFont(new Font("Yu Gothic UI Light", Font.PLAIN, 20));
exprlabel.setHorizontalAlignment(SwingConstants.RIGHT);
exprlabel.setForeground(UIManager.getColor("Button.disabledForeground"));
exprlabel.setBounds(2, 2, 312, 27);
textPanel.add(exprlabel);

JTextField textField = new JTextField();


exprlabel.setLabelFor(textField);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBackground(SystemColor.control);
textField.setEditable(false);
textField.setText("0");
textField.setBorder(null);
textField.setFont(new Font("Yu Gothic UI Light", textField.getFont().getStyle(), 32));
textField.setBounds(2, 30, 312, 49);
textPanel.add(textField);
textField.setColumns(10);

JPanel butttonPanel = new JPanel();


butttonPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
butttonPanel.setBackground(SystemColor.inactiveCaptionBorder);
butttonPanel.setBounds(34, 120, 316, 322);
frmCalculator.getContentPane().add(butttonPanel);
butttonPanel.setLayout(new GridLayout(0, 5, 0, 0));

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

//button for constant pi


JButton button3 = new
JButton("<html><body><span>π</span></body></html>"); button3.setFont(new
Font("Calibri Light", Font.PLAIN, 17)); button3.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+Character.toString((char)960));
}else {
textField.setText(Character.toString((char)960));
}
expression += ",pi";
num=false;
dot=false;
}
});
butttonPanel.add(button3);

//button for x^Y


JButton button4 = new
JButton("<html><body><span>X<sup>y</sup></span></body></html>");
button4.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;
});
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);

JButton button7 = new JButton("(");


button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"(");
}else {
textField.setText("(");
}
expression+=",(";
num=false;
dot=false;
}
});
button7.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button7);

JButton button8 = new JButton(")");


button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+")");
}else {
textField.setText(")");
}
expression+=",)";
num=false;
dot=false;
}
});
button8.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button8);

JButton button9 = new JButton("e");


button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"e");
}else {
textField.setText("e");
}
expression+=",e";
num=false;
dot=false;
}
});
button9.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button9);

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

JButton button11 = new JButton("cos");


button11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"cos(");
}else
{ textField.setText("cos(");

}
expression+=",cos,(";
num=false;
dot=false;
}
});
button11.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button11);

JButton button12 = new JButton("7");


button12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"7");
}else {
textField.setText("7");
}
if(num)
{ expression+="7";

}else { expression+=",7";

} num=true;
});
button12.setBackground(new Color(220, 220, 220));
button12.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button12);

JButton button13 = new JButton("8");


button13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {

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

JButton button14 = new JButton("9");


button14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"9");
}else {
textField.setText("9");
}
if(num)
{ expression+="9";

}else { expression+=",9";

} num=true;
});
button14.setBackground(new Color(220, 220, 220));
button14.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button14);

//button for division operator


JButton button15 = new
JButton("<html><body><span>÷</span></body></html>");
button15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=textField.getText();
if(s.equals("0")) {
expression+="0";

}
== '+') { if(s.charAt(s.length()-1)== '-' || s.charAt(s.length()-1)== 'x' || s.charAt(s.length()-1)

String newString = s.substring(0,s.length()-1);


textField.setText(newString+Character.toString((char)247));
expression = expression.substring(0,expression.length()-1);
expression += "/";
}else if(s.charAt(s.length()-1)!= (char)247) {
textField.setText(s+Character.toString((char)247));
expression+=",/";
}else
{ textField.setText(s);

num=false;
} dot=false;
});

19
button15.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button15);

JButton button16 = new JButton("tan");


button16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {

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

JButton button17 = new JButton("4");


button17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"4");
}else {
textField.setText("4");
}
if(num)
{ expression+="4";

}else { expression+=",4";

} num=true;
});
button17.setBackground(new Color(220, 220, 220));
button17.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button17);

JButton button18 = new JButton("5");


button18.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"5");
}else {
textField.setText("5");
}
if(num)
{ expression+="5";

}else { expression+=",5";

} num=true;
});
button18.setBackground(new Color(220, 220, 220));
button18.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button18);

JButton button19 = new JButton("6");


button19.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"6");

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

JButton button20 = new JButton("x");


button20.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

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)

String newString = s.substring(0,s.length()-1);


newString += "x";
textField.setText(newString);
expression = expression.substring(0,expression.length()-1);
expression += "x";
}else if(s.charAt(s.length()-1)!= 'x') {
s += "x";
textField.setText(s);
expression+=",x";
}else
{ textField.setText(s);

num=false;
} dot=false;
});
button20.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button20);

JButton button21 = new JButton("ln");


button21.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"ln(");
}else
{ textField.setText("ln(");

}
expression+=",ln,(";
num=false;
dot=false;
}
});
button21.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button21);

JButton button22 = new JButton("1");


button22.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"1");
}else { }

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

JButton button23 = new JButton("2");


button23.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"2");
}else {
textField.setText("2");
}
if(num)
{ expression+="2";

}else { expression+=",2";

} num=true;
});
button23.setBackground(new Color(220, 220, 220));
button23.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button23);

JButton button24 = new JButton("3");


button24.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"3");
}else {
textField.setText("3");
}
if(num)
{ expression+="3";

}else { expression+=",3";

} num=true;
});
button24.setBackground(new Color(220, 220, 220));
button24.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button24);

JButton button25 = new JButton("-");


button25.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=textField.getText();
if(s.equals("0")) {
expression+="0";

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

JButton button26 = new


JButton("<html><body><span>log<sub>10</sub></span></body></html>"); button26.setFont(new
Font("Calibri Light", Font.PLAIN, 17));
button26.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! "0".equals(textField.getText())) {
textField.setText(textField.getText()+"log(");
}else {
textField.setText("log(");
}
expression+=",log,(";
num=false;
dot=false;
}
});
butttonPanel.add(button26);

JButton button27 = new JButton(".");


button27.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{ String s=textField.getText();
if(s.charAt(s.length()-1)!= '.') {
if(num && dot==false) {
expression+=".";
s += ".";
}else if(num==false && dot ==false){
expression+=",.";
s += ".";
}
}
num=true;
dot=true;
textField.setText(s);
}
});
button27.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button27);

JButton button28 = new JButton("0");


button28.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if("0".equals(textField.getText())) {
textField.setText("0");
}else {
textField.setText(textField.getText()+"0");
if(num) {

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

//actual functioning on clicking = button


JButton button29 = new JButton("=");
button29.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculateMain();
String s="";
token.remove(token.size()-1);
for(String i: token) {
if(i.equals("/")) {
s+=Character.toString((char)247);
}else if(i.equals("sqrt")) {
s+=Character.toString((char)8730);
}else if(i.equals("pi")) {
s+=Character.toString((char)960);
}else
{ s+=i;

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

JButton button30 = new JButton("+");


button30.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=textField.getText();
if(s.equals("0")) {
expression+="0";

}
== (char)(247)) { if(s.charAt(s.length()-1)== '-' || s.charAt(s.length()-1)== 'x' || s.charAt(s.length()-1)

String newString = s.substring(0,s.length()-1);


newString += "+";
textField.setText(newString);
expression = expression.substring(0,expression.length()-1);
expression += "+";
}else if(s.charAt(s.length()-1)!= '+') {
s += "+";
textField.setText(s);
expression+=",+";
}else {

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

You might also like