0% found this document useful (0 votes)
20 views

jpr final

Uploaded by

phalketanu1205
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

jpr final

Uploaded by

phalketanu1205
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

A

Micro project

on

“Scientific Calculator”
Submitted By

Tanisha Phalke (07)


Munajja Dalimbkar (18)
Om Salunkhe (55)

Guided By

Ms.S.K.Kawale

Diploma Course in Computer Technology (As

per directives of I Scheme, MSBTE)

Sinhgad Institutes

Sinhgad Technical Education Society’s

SOU.VENUTAI CHAVAN POLYTECHNIC

PUNE - 411041
ACADEMIC YEAR 2023-2024
Maharashtra State Board of
Technical Education
Certificate
This is to certify that Ms. Tanisha Phalke with Roll No. 07 of Semester IV of
Diploma in Computer Technology of Institute Sou. Venutai Chavan
Polytechnic (Code: 0040) has successfully completed the MicroProject in
Java Programming (22412) for the academic year 2023-2024 as prescribed in
the curriculum.

Program code: CM Course Code: CM/4/I

Place: Pune Enrolment No: 2200400283

Date: Exam Seat No: 173001

Ms.S.K.Kawale Mrs.A.V.Kurkute Mrs(Dr).M.S.Jadhav


Course Teacher Head of Department Principle
Maharashtra State Board of
Technical Education
Certificate
This is to certify that Ms. Munajja Dalimbkar with Roll No. 18 of Semester IV
of Diploma in Computer Technology of Institute Sou. Venutai Chavan
Polytechnic (Code: 0040) has successfully completed the Micro- Project in
Java Programming (22412) for the academic year 2023-2024 as prescribed in
the curriculum.

Program code: CM Course Code: CM/4/I

Place: Pune Enrolment No: 2200400303

Date: Exam Seat No: 173019

Ms.S.K.Kawale Mrs.A.V.Kurkute Mrs(Dr).M.S.Jadhav


Course Teacher Head of Department Principle
Maharashtra State Board of
Technical Education

Certificate
This is to certify that Mr. Om Salunkhe with Roll No.55 of Semester IV of
Diploma in Computer Technology of Institute Sou. Venutai Chavan
Polytechnic (Code: 0040) has successfully completed the Micro- Project in
Java programming(22412) for the academic year 2023-2024 as prescribed in
the curriculum.

Program code: CM Course Code: CM/4/I

Place: Pune Enrollment No: 2200400442

Date: Exam Seat No: 173109

Ms.S.K.Kawale Mrs.A.V.Kurkute Mrs. (Dr).M.S.Jadhav


Subject Teacher Head of Department Principle

INDEX
SR NO CONTENTS PAGE NO

1 Aim of the Micro-Project

2 Rationale

3 Course Outcomes Achieved

4 Literature Review

5 Actual Methodology Followed

6 Actual Resources Used

7 Skills Developed

8 Applications of Micro Project

9 Source Code

10 Area of future Management

11 Conclusion
Programming in Java (22412 ) Scientific Calculator

Annexure-I
Micro-Project Proposal

1.0 Aim of the Micro-Project:


The aim of the Micro-project is to create Scientific Calculator using Java
Programming.

2.0 Intended Course Outcomes:


a) Develop Java program to solve problems using Procedure Java
Approach.
b) Develop Java Program using Inheritance.
c) Develop Java Program to create collage management system.

3.0 Proposed methodology:

1.classes and objects in Java and its uses.


2.Study the various syntaxes of Java language.
3.Study to read and write into files, by file operations.
4.Study the functionality and Hospital system.
5.Design structure of systems.
6.Implement program to create management system.
7.Prepare the final report.

Department of Computer Technology 2023-24 Page 6


Programming in Java (22412 ) Scientific Calculator

4.0 Action Plan:


Sr. Planned Planned Name of
No. Details of Activity Start Date Finish Date responsible
Team members

1 Identify the requirements of the


project. Tanisha Phalke

2
Design the structure of the project. Om Salunkhe

3 Develop a program using Java


language. Munajja Dalimbkar

4 Debug code and eliminate errors


occurred while compilation. Munajja Dalimbkar

5
Test the project. Om Salunkhe

6
Prepare the final report. Tanisha Phalke

5.0 Resources Required:

S. No. Resources required Specifications


1 Computer system Intel(R) Pentium CPU, RAM 4 GB
2 Operating System Windows 10, 64 Bit Operating System
3 Software’s Jdk 1.8.0, notepad, command prompt.

6.0 Team members:

S. No. Roll. Number Name of Student


1 07 Tanisha Phalke

2 18 Munajja Dalimbkar

3 15 Om Salunkhe

Annexure - II

Department of Computer Technology 2023-24 Page 7


Programming in Java (22412 ) Scientific Calculator

Micro-Project Report

1.0 Aim of the Micro-Project:


The aim of the Micro-project is to create scientific calculator using Java.

2.0 Rationale:

This program is a Java implementation of a basic scientific calculator that allows the user to
perform basic mathematical operations such as addition, subtraction, multiplication, division and
exponentiation. The program prompts the user to input two numbers and a desired operation, and
then uses a switch statement to perform the appropriate calculation and return the result. The
program abstracts the complexity of mathematical operations and provides a simple and user-
friendly interface for performing common calculations. This program can be a good starting point
for building more advanced calculators with additional features and functionality.

3.0 Course Outcomes Achieved:


a) Develop Java program solve problems using Procedure Java Approach.
b) Develop Java program using classes and objects.

4.0 Literature Review:


The programs demonstrate the use of the following functions, which are widely used in
Java:
a) Classes and objects in Java.
b) Use of case in different way.

c) Decision making statements in Java.

5.0 Actual Methodology Followed:


o The aim of the Micro-project is to create collage Management System using Java.
 Study classes and objects in Java and its uses.
 Study the various syntaxes of Java language.
 Study to get and display entries in system.
 Study the report properly .
 Design structure of systems.
 Implement program to create management system.

Department of Computer Technology 2023-24 Page 8


Programming in Java (22412 ) Scientific Calculator

 Prepare the final report.

6.0 Actual Resources Used:

S. No. Resources required Specifications


1 Computer system Intel(R) Pentium CPU, RAM 4 GB
2 Operating System Windows 10, 64 Bit Operating System
3 Software’s Jdk 1.8.0, Notepad, Command prompt.

7.0 Skills Developed:


During the course of this micro-project, we learned to create Collage management
system by using Java.
a) We learnt various new syntaxes of Java language.
b) We also learned Inheritance in java.
c) We also learnt classes and objects in Java.

8.0 Applications of this Micro-project:

This micro-project finds its application in:


a) Learning Tool: Students can use it to practice solving equations, verifying results, and
understanding mathematical principles.

b) Accounting: Accountants can use it for basic calculations and more complex financial
analysis.

c) Banking and Finance: Professionals in banking and finance can use a Java calculator to
perform financial calculations such as interest rates, loan payments, investment returns, and
amortization schedules.

9.0 Source Code:-

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;

Department of Computer Technology 2023-24 Page 9


Programming in Java (22412 ) Scientific Calculator

import java.awt.event.*;
import java.util.*;

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;
break;
case "-":
p=2;
break;
case "x":
p=3;
break;

Department of Computer Technology 2023-24 Page 10


Programming in Java (22412 ) Scientific Calculator

case "/":
p=4;
break;
case "^":
p=6;
break;
case "!":
p=7;
break;
}

return p;
}

//operator checking
private boolean isoperator(String x)
{
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();

Department of Computer Technology 2023-24 Page 11


Programming in Java (22412 ) Scientific Calculator

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


private double calculate(double x,double y,String c)
{
double res=0;

Department of Computer Technology 2023-24 Page 12


Programming in Java (22412 ) Scientific Calculator

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;

Department of Computer Technology 2023-24 Page 13


Programming in Java (22412 ) Scientific Calculator

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") && ! tokens[i].equals(" ")) {
token2.add(tokens[i]); // tokens from post fix form p actual
tokens for calculation
}
}

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

Department of Computer Technology 2023-24 Page 14


Programming in Java (22412 ) Scientific Calculator

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

Department of Computer Technology 2023-24 Page 15


Programming in Java (22412 ) Scientific Calculator

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


null, null, null));
textPanel.setBounds(34, 25, 316, 80);
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 ="";

Department of Computer Technology 2023-24 Page 16


Programming in Java (22412 ) Scientific Calculator

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 {
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() {

Department of Computer Technology 2023-24 Page 17


Programming in Java (22412 ) Scientific Calculator

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

Department of Computer Technology 2023-24 Page 18


Programming in Java (22412 ) Scientific Calculator

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

//button for sin


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

Department of Computer Technology 2023-24 Page 19


Programming in Java (22412 ) Scientific Calculator

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

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

Department of Computer Technology 2023-24 Page 20


Programming in Java (22412 ) Scientific Calculator

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

Department of Computer Technology 2023-24 Page 21


Programming in Java (22412 ) Scientific Calculator

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())) {
textField.setText(textField.getText()+"8");
}else {
textField.setText("8");
}
if(num) {
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);

Department of Computer Technology 2023-24 Page 22


Programming in Java (22412 ) Scientific Calculator

//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;
}
});
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())) {
textField.setText(textField.getText()+"tan(");
}else {
textField.setText("tan(");
}
expression+=",tan,(";
num=false;
dot=false;

Department of Computer Technology 2023-24 Page 23


Programming in Java (22412 ) Scientific Calculator

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

Department of Computer Technology 2023-24 Page 24


Programming in Java (22412 ) Scientific Calculator

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");
}else {
textField.setText("6");
}
if(num) {
expression+="6";
}else {
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";
}
if(s.charAt(s.length()-1)== '-' || s.charAt(s.length()-1)== '+'
|| s.charAt(s.length()-1) == (char)(247)) {
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);

Department of Computer Technology 2023-24 Page 25


Programming in Java (22412 ) Scientific Calculator

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 {
textField.setText("1");
}
if(num) {
expression+="1";
}else {
expression+=",1";
}
num=true;
}

Department of Computer Technology 2023-24 Page 26


Programming in Java (22412 ) Scientific Calculator

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

Department of Computer Technology 2023-24 Page 27


Programming in Java (22412 ) Scientific Calculator

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";
}
if(s.charAt(s.length()-1)== '+') {
String newString = s.substring(0,s.length()-1);
newString += "-";
expression =
expression.substring(0,expression.length()-1);
expression += "-";
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,(";

Department of Computer Technology 2023-24 Page 28


Programming in Java (22412 ) Scientific Calculator

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) {
expression+="0";
}
else {
expression+=",0";
}
}
num=true;

Department of Computer Technology 2023-24 Page 29


Programming in Java (22412 ) Scientific Calculator

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

Department of Computer Technology 2023-24 Page 30


Programming in Java (22412 ) Scientific Calculator

expression+="0";
}
if(s.charAt(s.length()-1)== '-' || s.charAt(s.length()-1)== 'x' ||
s.charAt(s.length()-1) == (char)(247)) {
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 {
textField.setText(s);
}
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);
}
}

Department of Computer Technology 2023-24 Page 31


Programming in Java (22412 ) Scientific Calculator

OUTPUT:-

Department of Computer
PageTechnology
33 2023-24
Programming in Java (22412 ) Scientific Calculator

10.0 Area of future Improvement:

Future improvements for a calculator built using Java could include enhancing the
user interface, adding advanced mathematical functions, optimizing performance,
integrating with external systems, customizing features, ensuring accessibility,
ensuring cross-platform compatibility, enhancing security, collecting user feedback,
and providing comprehensive documentation.

11.0 Conclusion:
We learn to design calculator using Java; also we understood basic fundamentals
of calculator systems of creation as well as other applications.

Department of Computer
PageTechnology
33 2023-24

You might also like