0% found this document useful (1 vote)
303 views6 pages

GUI Calculator Project: Lab Report On

This lab report describes the development of a Java GUI calculator application with two Java files. Calculator.java contains the code for the graphical user interface and handles button clicks. Evaluate.java contains the code to evaluate mathematical expressions entered by the user. The application allows users to enter expressions, select mathematical operations from buttons, and see the result.

Uploaded by

Rafsun Masum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
303 views6 pages

GUI Calculator Project: Lab Report On

This lab report describes the development of a Java GUI calculator application with two Java files. Calculator.java contains the code for the graphical user interface and handles button clicks. Evaluate.java contains the code to evaluate mathematical expressions entered by the user. The application allows users to enter expressions, select mathematical operations from buttons, and see the result.

Uploaded by

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

Lab Report On

GUI Calculator Project

Course code: SE 410


Course Title: Advance Enterprise Java Lab
Assignment no: 02

Submitted To:
Md. Safaet Hossain
Associate professor & Head,
Department of CSE
City University
Submitted By:
Mehadi Hassain
172452533
Batch: 45th

Submission Date: June 15, 2021


This is a Java GUI Calculator application.
Java Files:

Calculator.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.Pattern;

public class Calculator implements ActionListener {

JFrame frame;
JTextField textField;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[8];
JButton addButton, subButton, mulButton, divButton;
JButton decButton, equButton, delButton, clrButton;

JPanel panel;

Font myFont = new Font("Nunito", Font.BOLD, 30);

Calculator() {
frame = new JFrame("Java Calculator - Mehedi Hassain");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);

textField = new JTextField();


textField.setBounds(5, 25, 410, 50);
textField.setFont(myFont);
textField.setEditable(false);
textField.setBorder(BorderFactory.createEmptyBorder());

functionButtons[0] = addButton = new JButton("+");


functionButtons[1] = subButton = new JButton("-");
functionButtons[2] = mulButton = new JButton("*");
functionButtons[3] = divButton = new JButton("/");
functionButtons[4] = decButton = new JButton(".");
functionButtons[5] = equButton = new JButton("=");
functionButtons[6] = delButton = new JButton("x");
functionButtons[7] = clrButton = new JButton("C");
delButton.setBackground(Color.ORANGE);

for (JButton functionButton : functionButtons) {


functionButton.addActionListener(this);
functionButton.setFont(myFont);
functionButton.setFocusable(false);
functionButton.setBackground(Color.WHITE);
}
for (int i = 0; i < numberButtons.length; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
numberButtons[i].setBackground(Color.WHITE);
}

delButton.setBackground(Color.lightGray);
panel = new JPanel();
panel.setBounds(0, 100, 420, 350);
panel.setLayout(new GridLayout(5, 4, 0, 0));

panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);
panel.add(clrButton);
panel.add(delButton);

frame.add(panel);
frame.add(textField);
frame.setVisible(true);
}

public static void main(String[] args) {


new Calculator();
}

@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
String fieldValue = textField.getText();

if (source == equButton) {
if(!fieldValue.isEmpty()){
double result = Evaluate.eval(fieldValue);
System.out.println(result);
textField.setText(String.valueOf(result));
}
} else if (source == clrButton) {
textField.setText("");
} else if (source == delButton) {
if (!fieldValue.isEmpty())
textField.setText(fieldValue.substring(0, fieldValue.length() - 1));
} else {
if ((fieldValue.isEmpty() || !isNumber(fieldValue.substring(fieldValue.length() - 1))) && !
isNumber(source.getText())) {
return;
}
textField.setText(fieldValue.concat(source.getText()));
}
}

public boolean isNumber(String str) {


return Pattern.compile("[0-9]+").matcher(str).matches();
}
}

Evaluate.java

public class Evaluate {


public static double eval(final String str) {
return new Object() {
int pos = -1, ch;

void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}

boolean eat(int charToEat) {


while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}

double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}

double parseExpression() {
double x = parseTerm();
for (; ; ) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}

double parseTerm() {
double x = parseFactor();
for (; ; ) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else return x;
}
}

double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus

double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt")) x = Math.sqrt(x);
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
else throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}

if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation

return x;
}
}.parse();
}
}

You might also like