jpr final
jpr final
Micro project
on
“Scientific Calculator”
Submitted By
Guided By
Ms.S.K.Kawale
Sinhgad Institutes
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.
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.
INDEX
SR NO CONTENTS PAGE NO
2 Rationale
4 Literature Review
7 Skills Developed
9 Source Code
11 Conclusion
Programming in Java (22412 ) Scientific Calculator
Annexure-I
Micro-Project Proposal
2
Design the structure of the project. Om Salunkhe
5
Test the project. Om Salunkhe
6
Prepare the final report. Tanisha Phalke
2 18 Munajja Dalimbkar
3 15 Om Salunkhe
Annexure - II
Micro-Project Report
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.
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.
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
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;
Calculator() {
initialize();
}
int precedence(String x)
{
int p=10;
switch(x) {
case "+":
p=1;
break;
case "-":
p=2;
break;
case "x":
p=3;
break;
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;
}
}
}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;
}
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;
}
case "sqrt":
res= Math.sqrt(y);
break;
case "!":
res=factorial(y);
break;
}
return res;
}
else
s.push(Double.valueOf(i));
}
}
double res=1;
while(!s.empty()) {
res*=s.pop();
}
return res; //final result
}
frmCalculator.getContentPane().setForeground(SystemColor.windowBorder);
frmCalculator.getContentPane().setLayout(null);
exprlabel.setForeground(UIManager.getColor("Button.disabledForeground"));
exprlabel.setBounds(2, 2, 312, 27);
textPanel.add(exprlabel);
//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 {
textField.setText("0");
expression="";
}
}
});
button2.setFont(new Font("Calibri Light", Font.PLAIN, 14));
butttonPanel.add(button2);
//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);
}
expression+=",sqrt";
num=false;
dot=false;
}
});
button10.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button10);
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);
}
});
button16.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button16);
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);
expression+=",x";
}else {
textField.setText(s);
}
num=false;
dot=false;
}
});
button20.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button20);
});
button22.setBackground(new Color(220, 220, 220));
button22.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button22);
num=false;
dot=false;
}
});
butttonPanel.add(button26);
}
});
button28.setBackground(new Color(220, 220, 220));
button28.setFont(new Font("Calibri Light", Font.PLAIN, 17));
butttonPanel.add(button28);
expression = result;
dot=true;
num=true;
token.clear();
}
});
button29.setBackground(Color.ORANGE);
button29.setFont(new Font("Calibri Light", Font.PLAIN, 22));
butttonPanel.add(button29);
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);
}
}
OUTPUT:-
Department of Computer
PageTechnology
33 2023-24
Programming in Java (22412 ) Scientific Calculator
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