[Go to site: main page, start]

0% found this document useful (0 votes)
9 views15 pages

Advanced Java Scientific Calculator

The document describes a Java-based scientific calculator that offers a wide range of mathematical functionalities, including trigonometric functions, logarithmic computations, and factorial calculations. It features a user-friendly interface and is designed to assist students, professionals, and enthusiasts in performing complex calculations efficiently. The code includes a graphical user interface and various mathematical operations, making it a versatile tool for mathematical endeavors.

Uploaded by

yabsira Assefa
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)
9 views15 pages

Advanced Java Scientific Calculator

The document describes a Java-based scientific calculator that offers a wide range of mathematical functionalities, including trigonometric functions, logarithmic computations, and factorial calculations. It features a user-friendly interface and is designed to assist students, professionals, and enthusiasts in performing complex calculations efficiently. The code includes a graphical user interface and various mathematical operations, making it a versatile tool for mathematical endeavors.

Uploaded by

yabsira Assefa
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

Department of Computer Science

Course Title:-Advanced Java Programming


No Name ID No
1 Kaleab Yemane 0486
2 Yabsira Asefa 0054
3 Samuel Hailu
4 Nathaniyan Abebe

Submitted to instructuor Debritu


Submisson date on 28/05/2016 e.c
Introduction

The Java code presented here represents a sophisticated and


versatile scientific calculator that offers an extensive range of
mathematical functionalities to cater to the diverse needs of
users. This feature-rich calculator encompasses a wide array of
operations, including modulo calculation, trigonometric
functions such as sine (sin), cosine (cos), and tangent (tan), as
well as inverse trigonometric functions like arcsine (sin inverse),
arccosine (cos inverse), and arctangent (tan inverse).
Additionally, it provides logarithmic computation, access to the
mathematical constant Pi, square root and cubic root
calculations, exponentiation for raising numbers to a power,
evaluation of the floor function, factorial calculation, and basic
arithmetic operations. By integrating these advanced
mathematical capabilities into a user-friendly interface, this
scientific calculator empowers users to perform complex
calculations with precision and efficiency, making it an
indispensable tool for students, professionals, and enthusiasts
in the fields of mathematics and science.
package scientific_calculator;

import [Link].*;

import [Link].*;

import [Link];

import [Link];

import [Link];

public class ScientificCalculatorGUI extends JFrame {

private JTextField display;

public ScientificCalculatorGUI() {

super("Scientific Calculator");

initComponents();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(450, 300);

setLocationRelativeTo(null); // Center the frame on the screen

setResizable(false);

setVisible(true);

private void initComponents() {

display = new JTextField();

[Link](false);

[Link]([Link]);

[Link](new Dimension(300,80));

[Link]([Link]);
[Link](new Font("Arial", [Link], 30));

JPanel buttonPanel = new JPanel(new GridLayout(5, 5, 7, 5));

[Link](Color.LIGHT_GRAY);

String[] buttonLabels = {

"C", "AC","π","%","Log","(",")",

"7", "8", "9","/","sin","cos","tan",

"4", "5", "6", "*","sin-","cos-","tan-",

"1", "2", "3", "-","\u221A","x\u00B2","x\u00B3",

"0", ".", "=", "+","n!","floor","^",

};

for (String label : buttonLabels) {

JButton button = new JButton(label);

[Link]([Link]);

[Link]([Link]);

[Link]([Link]([Link]));

[Link](new Font("Arial", [Link], 10));

[Link](new ButtonClickListener());

[Link](button);

setLayout(new BorderLayout());

add(display, [Link]);

add(buttonPanel, [Link]);

}
private class ButtonClickListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

JButton source = (JButton) [Link]();

String buttonText = [Link]();

switch (buttonText) {

case "=":

evaluateExpression();

break;

case "C":

clearLastEntry();

break;

case "AC":

clearAll();

break;

case "\u221A":

calculateSquareRoot();

break;

case "x\u00B2":

calculatePower();

break;

case "n!":

factorial();

break;

case "π":

pi();
break;

case "x\u00B3":

backward();

break;

case "floor":

floor();

break;

case "Log":

log();

break;

default:

appendToDisplay(buttonText);

break;

private void evaluateExpression() {

try {

String expression = [Link]();

double result = [Link](expression);

[Link]([Link](result));

} catch (Exception ex) {

[Link]("Error");

}
private void clearLastEntry() {

String currentText = [Link]();

if (![Link]()) {

[Link]([Link](0, [Link]() - 1));

private void clearAll() {

[Link]("");

private void appendToDisplay(String text) {

[Link]([Link]() + text);

private void calculateSquareRoot() {

try {

double operand = [Link]([Link]());

double result = [Link](operand);

[Link]([Link](result));

} catch (NumberFormatException ex) {

[Link]("Error");

private void calculatePower() {

try{double number = [Link]([Link]([Link]()));


number = [Link](number, 2);

[Link]([Link](number));

}catch(Exception ex){

[Link]("Error");

private void factorial(){

try {

double number = [Link]([Link]([Link]()));

double sum = 1;

while (number != 0) {

sum = sum * number;

number--;

[Link]([Link](sum));

}catch (Exception ex){

[Link]("Error");

private void pi(){

double number = [Link];

[Link]([Link](number));

private void backward(){

try{double number = [Link]([Link]([Link]()));


number = [Link](number, 3);

[Link]([Link](number));

}catch(Exception ex){

[Link]("Error");

public void floor(){

try {

double number = [Link]([Link]([Link]()));

number = [Link](number);

[Link]([Link](number));

} catch (Exception ex) {

[Link]("Error");

public void log(){

try {

double number = [Link]([Link]([Link]()));

number = [Link](number);

[Link]([Link](number));

} catch (Exception ex) {

[Link]("Error");

public static void main(String[] args) {


[Link](() -> new ScientificCalculatorGUI());

class Calculator {

static double evaluate(String expression) {

return new Object() {

int pos = -1, ch;

void nextChar() {

ch = (++pos < [Link]()) ? [Link](pos) : -1;

boolean isDigit() {

return [Link](ch);

double parse() {

nextChar();

double x = parseExpression();

if (pos < [Link]()) throw new RuntimeException("Unexpected: " + (char) ch);

return x;

double parseExpression() {

double x = parseTerm();

for (; ; ) {

if (eat('+')) x += parseTerm();
else if (eat('-')) x -= parseTerm();

else return x;

double parseTerm() {

double x = parseFactor();

for (; ; ) {

if (eat('*')) x *= parseFactor();

else if (eat('/')) x /= parseFactor();

else if (eat('%')) x =x% parseFactor();

else if (eat('^')) x = [Link](x, parseFactor());

else return x;

double parseFactor() {

if (eat('+')) return parseFactor();

if (eat('-')) return -parseFactor();

double x;

int startPos = [Link];

if (eat('(')) {

x = parseExpression();

eat(')');

} else if (isDigit() || ch == '.') {

while (isDigit() || ch == '.') nextChar();

x = [Link]([Link](startPos, [Link]));
} else if (ch >= 'a' && ch <= 'z') {

while (ch >= 'a' && ch <= 'z') nextChar();

String func = [Link](startPos, [Link]);

x = parseFactor();

switch (func) {

case "\u221A":

x = [Link](x);

break;

case "sin":

x = [Link](x);

break;

case "cos":

x = [Link](x);

break;

case "tan":

x = [Link](x);

break;

case "sin-":

x = [Link](x);

break;

case "cos-":

x = [Link](x);

break;

case "tan-":

x = [Link](x);
break;

case "n!":

double sum=1;

for(x=x;x>0;x--){

sum = sum*x;

x = sum;

break;

default:

throw new RuntimeException("Unknown function: " + func);

} else {

throw new RuntimeException("Unexpected: " + (char) ch);

if (eat('^')) x = [Link](x, parseFactor());

return x;

boolean eat(int charToEat) {

while ([Link](ch)) nextChar();

if (ch == charToEat) {

nextChar();

return true;
}

return false;

}.parse();

}
In conclusion, the Java code for this sophisticated scientific
calculator embodies a comprehensive suite of mathematical
functionalities that cater to a diverse range of user needs. From
basic arithmetic operations to advanced trigonometric
calculations, logarithmic functions, and factorial computations,
this calculator offers a wealth of capabilities to support users in
their mathematical endeavors. With features such as modulo
calculation, sine, cosine, tangent, inverse trigonometric
functions, logarithms, access to the mathematical constant Pi,
square root and cubic root calculations, exponentiation, floor
function evaluation, and factorial computation, this calculator
stands out as a versatile and powerful tool for students,
professionals, and enthusiasts in the fields of mathematics and
science. By providing a user-friendly interface and robust
mathematical capabilities, this scientific calculator empowers
users to perform complex calculations with precision and
efficiency, making it an indispensable companion for anyone
seeking reliable and comprehensive mathematical support.

You might also like