SlideShare a Scribd company logo
Hello, I need some assistance in writing a java program THAT MUST USE STACKS (LIFO).
Create a Calculator w/ GUI
Write a program that graphically displays a working calculator for simple infix expressions that
consist of: single-digit operands, the operators: +, -, *, and /, and parentheses.
Make the following assumptions:
unary operators (e.g. -2) are illegal
all operations, including division, are integer operations (and results are integers)
the input expression contains no embedded spaces and no illegal characters
the input expression is a syntactically correct infix expression
division by zero will not occur (consider how you can remove this restriction)
Create a GUI application, the calculator has a display and a keypad of 20 keys, which are
arranged as follows:
C
<
Q
/
7
8
9
*
4
5
6
-
1
2
3
+
0
(
)
=
As the user presses keys to enter an infix expression, the corresponding characters appear in the
display. The C (Clear) key erases all input entered so far; the < (Backspace) key erases the last
character entered. When the user presses the = key, the expression is evaluated and the result
appended to the right end of the expression in the display window. The user can then press C and
enter another expression. If the user presses the Q (Quit) key, the calculator ceases operation and
is erased from the screen.
C
<
Q
/
7
8
9
*
4
5
6
-
1
2
3
+
0
(
)
=
Solution
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
JPanel[] row = new JPanel[5];
JButton[] button = new JButton[19];
String[] buttonString = {"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
".", "/", "C", "",
"+/-", "=", "0"};
int[] dimW = {300,45,100,90};
int[] dimH = {35, 40};
Dimension displayDimension = new Dimension(dimW[0], dimH[0]);
Dimension regularDimension = new Dimension(dimW[1], dimH[1]);
Dimension rColumnDimension = new Dimension(dimW[2], dimH[1]);
Dimension zeroButDimension = new Dimension(dimW[3], dimH[1]);
boolean[] function = new boolean[4];
double[] temporary = {0, 0};
JTextArea display = new JTextArea(1,20);
Font font = new Font("Times new Roman", Font.BOLD, 14);
Calculator() {
super("Calculator");
setDesign();
setSize(380, 250);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(5,5);
setLayout(grid);
for(int i = 0; i < 4; i++)
function[i] = false;
FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
FlowLayout f2 = new FlowLayout(FlowLayout.CENTER,1,1);
for(int i = 0; i < 5; i++)
row[i] = new JPanel();
row[0].setLayout(f1);
for(int i = 1; i < 5; i++)
row[i].setLayout(f2);
for(int i = 0; i < 19; i++) {
button[i] = new JButton();
button[i].setText(buttonString[i]);
button[i].setFont(font);
button[i].addActionListener(this);
}
display.setFont(font);
display.setEditable(false);
display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
display.setPreferredSize(displayDimension);
for(int i = 0; i < 14; i++)
button[i].setPreferredSize(regularDimension);
for(int i = 14; i < 18; i++)
button[i].setPreferredSize(rColumnDimension);
button[18].setPreferredSize(zeroButDimension);
row[0].add(display);
add(row[0]);
for(int i = 0; i < 4; i++)
row[1].add(button[i]);
row[1].add(button[14]);
add(row[1]);
for(int i = 4; i < 8; i++)
row[2].add(button[i]);
row[2].add(button[15]);
add(row[2]);
for(int i = 8; i < 12; i++)
row[3].add(button[i]);
row[3].add(button[16]);
add(row[3]);
row[4].add(button[18]);
for(int i = 12; i < 14; i++)
row[4].add(button[i]);
row[4].add(button[17]);
add(row[4]);
setVisible(true);
}
public void clear() {
try {
display.setText("");
for(int i = 0; i < 4; i++)
function[i] = false;
for(int i = 0; i < 2; i++)
temporary[i] = 0;
} catch(NullPointerException e) {
}
}
public void getSqrt() {
try {
double value = Math.sqrt(Double.parseDouble(display.getText()));
display.setText(Double.toString(value));
} catch(NumberFormatException e) {
}
}
public void getPosNeg() {
try {
double value = Double.parseDouble(display.getText());
if(value != 0) {
value = value * (-1);
display.setText(Double.toString(value));
}
else {
}
} catch(NumberFormatException e) {
}
}
public void getResult() {
double result = 0;
temporary[1] = Double.parseDouble(display.getText());
String temp0 = Double.toString(temporary[0]);
String temp1 = Double.toString(temporary[1]);
try {
if(temp0.contains("-")) {
String[] temp00 = temp0.split("-", 2);
temporary[0] = (Double.parseDouble(temp00[1]) * -1);
}
if(temp1.contains("-")) {
String[] temp11 = temp1.split("-", 2);
temporary[1] = (Double.parseDouble(temp11[1]) * -1);
}
} catch(ArrayIndexOutOfBoundsException e) {
}
try {
if(function[2] == true)
result = temporary[0] * temporary[1];
else if(function[3] == true)
result = temporary[0] / temporary[1];
else if(function[0] == true)
result = temporary[0] + temporary[1];
else if(function[1] == true)
result = temporary[0] - temporary[1];
display.setText(Double.toString(result));
for(int i = 0; i < 4; i++)
function[i] = false;
} catch(NumberFormatException e) {
}
}
public final void setDesign() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception e) {
}
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == button[0])
display.append("7");
if(ae.getSource() == button[1])
display.append("8");
if(ae.getSource() == button[2])
display.append("9");
if(ae.getSource() == button[3]) {
//add function[0]
temporary[0] = Double.parseDouble(display.getText());
function[0] = true;
display.setText("");
}
if(ae.getSource() == button[4])
display.append("4");
if(ae.getSource() == button[5])
display.append("5");
if(ae.getSource() == button[6])
display.append("6");
if(ae.getSource() == button[7]) {
//subtract function[1]
temporary[0] = Double.parseDouble(display.getText());
function[1] = true;
display.setText("");
}
if(ae.getSource() == button[8])
display.append("1");
if(ae.getSource() == button[9])
display.append("2");
if(ae.getSource() == button[10])
display.append("3");
if(ae.getSource() == button[11]) {
//multiply function[2]
temporary[0] = Double.parseDouble(display.getText());
function[2] = true;
display.setText("");
}
if(ae.getSource() == button[12])
display.append(".");
if(ae.getSource() == button[13]) {
//divide function[3]
temporary[0] = Double.parseDouble(display.getText());
function[3] = true;
display.setText("");
}
if(ae.getSource() == button[14])
clear();
if(ae.getSource() == button[15])
getSqrt();
if(ae.getSource() == button[16])
getPosNeg();
if(ae.getSource() == button[17])
getResult();
if(ae.getSource() == button[18])
display.append("0");
}
public static void main(String[] arguments) {
Calculator c = new Calculator();
}
}
//the gui has close button so I didn't code that and < button has not been coded since the use of C
button..... thankyou

More Related Content

PDF
Introduction to python programming
DOCX
PRACTICAL COMPUTING
PDF
Of class2
DOCX
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
PDF
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
PPTX
Python programming workshop session 3
PPT
PDF
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Introduction to python programming
PRACTICAL COMPUTING
Of class2
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Python programming workshop session 3
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers

Similar to Hello, I need some assistance in writing a java program THAT MUST US.pdf (20)

DOCX
PDF
Laziness, trampolines, monoids and other functional amenities: this is not yo...
DOCX
Operating system labs
PDF
C++ Searching & Sorting5. Sort the following list using the select.pdf
DOCX
.net progrmming part2
PDF
analysis of data structure design programs
PDF
C++ Course - Lesson 2
PDF
Simple 27 Java Program on basic java syntax
PDF
Python From Scratch (1).pdf
PDF
Question Hello, I need some assistance in writing a java pr...Hel.pdf
PPT
PDF
Developer Experience i TypeScript. Najbardziej ikoniczne duo
PPT
Algorithms with-java-1.0
PDF
PPTX
07012023.pptx
PPTX
4. functions
PDF
Write a program that obtains the execution time of selection sort, bu.pdf
PDF
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
DOCX
More instructions for the lab write-up1) You are not obli.docx
PDF
public class TrequeT extends AbstractListT { .pdf
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Operating system labs
C++ Searching & Sorting5. Sort the following list using the select.pdf
.net progrmming part2
analysis of data structure design programs
C++ Course - Lesson 2
Simple 27 Java Program on basic java syntax
Python From Scratch (1).pdf
Question Hello, I need some assistance in writing a java pr...Hel.pdf
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Algorithms with-java-1.0
07012023.pptx
4. functions
Write a program that obtains the execution time of selection sort, bu.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
More instructions for the lab write-up1) You are not obli.docx
public class TrequeT extends AbstractListT { .pdf
Ad

More from FashionColZone (20)

PDF
In order to determine the overall chromosome map for E. coli, four Hf.pdf
PDF
In a fantasy world where organisms can augment diffusion by using ma.pdf
PDF
Explain how consumer judges the quality of service (Including price .pdf
PDF
Early Americans did not practice equality according to today’s defin.pdf
PDF
Do you think that the level of prosperity of a given society influen.pdf
PDF
Design a circuit that detect the sequence 0110. Draw the Moore sta.pdf
PDF
Cyanide is a potent toxin that can kill a human in minutes. It funct.pdf
PDF
A Fullerene is a 3-regular planar graph with faces of degree 5 and 6.pdf
PDF
Assume that the paired date came from a population that is normally .pdf
PDF
Briefly describe three processing schema used in cochlear implants..pdf
PDF
A satellite moves in a circular orbit around Earth at a speed of 470.pdf
PDF
7. What do we mean by “spreading signal”in CDMA systems16. Name a.pdf
PDF
You will write a multi-interface version of the well-known concentra.pdf
PDF
You are interested in using the plasmid pGEE as a vector for incorpor.pdf
PDF
Why animals migrate I need 5 new ideas (exclude reproduction , s.pdf
PDF
Which of the following characteristics of the fetus distinguishes it.pdf
PDF
What are the major sources of cash (inflows) in a statement of cash .pdf
PDF
TYPES OF AUDIT1. Is there an internal audit function within your o.pdf
PDF
The total exergy is an extensive property of a system. True or False.pdf
PDF
1.Define culture. How can culture be conceptionalized Your resp.pdf
In order to determine the overall chromosome map for E. coli, four Hf.pdf
In a fantasy world where organisms can augment diffusion by using ma.pdf
Explain how consumer judges the quality of service (Including price .pdf
Early Americans did not practice equality according to today’s defin.pdf
Do you think that the level of prosperity of a given society influen.pdf
Design a circuit that detect the sequence 0110. Draw the Moore sta.pdf
Cyanide is a potent toxin that can kill a human in minutes. It funct.pdf
A Fullerene is a 3-regular planar graph with faces of degree 5 and 6.pdf
Assume that the paired date came from a population that is normally .pdf
Briefly describe three processing schema used in cochlear implants..pdf
A satellite moves in a circular orbit around Earth at a speed of 470.pdf
7. What do we mean by “spreading signal”in CDMA systems16. Name a.pdf
You will write a multi-interface version of the well-known concentra.pdf
You are interested in using the plasmid pGEE as a vector for incorpor.pdf
Why animals migrate I need 5 new ideas (exclude reproduction , s.pdf
Which of the following characteristics of the fetus distinguishes it.pdf
What are the major sources of cash (inflows) in a statement of cash .pdf
TYPES OF AUDIT1. Is there an internal audit function within your o.pdf
The total exergy is an extensive property of a system. True or False.pdf
1.Define culture. How can culture be conceptionalized Your resp.pdf
Ad

Recently uploaded (20)

PPTX
Presentation on HIE in infants and its manifestations
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Presentation on HIE in infants and its manifestations
GDM (1) (1).pptx small presentation for students
Microbial disease of the cardiovascular and lymphatic systems
Complications of Minimal Access Surgery at WLH
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
O7-L3 Supply Chain Operations - ICLT Program
Chinmaya Tiranga quiz Grand Finale.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Structure & Organelles in detailed.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Pharma ospi slides which help in ospi learning
2.FourierTransform-ShortQuestionswithAnswers.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
STATICS OF THE RIGID BODIES Hibbelers.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

Hello, I need some assistance in writing a java program THAT MUST US.pdf

  • 1. Hello, I need some assistance in writing a java program THAT MUST USE STACKS (LIFO). Create a Calculator w/ GUI Write a program that graphically displays a working calculator for simple infix expressions that consist of: single-digit operands, the operators: +, -, *, and /, and parentheses. Make the following assumptions: unary operators (e.g. -2) are illegal all operations, including division, are integer operations (and results are integers) the input expression contains no embedded spaces and no illegal characters the input expression is a syntactically correct infix expression division by zero will not occur (consider how you can remove this restriction) Create a GUI application, the calculator has a display and a keypad of 20 keys, which are arranged as follows: C < Q / 7 8 9 * 4 5 6 - 1 2 3 + 0 ( ) = As the user presses keys to enter an infix expression, the corresponding characters appear in the display. The C (Clear) key erases all input entered so far; the < (Backspace) key erases the last character entered. When the user presses the = key, the expression is evaluated and the result
  • 2. appended to the right end of the expression in the display window. The user can then press C and enter another expression. If the user presses the Q (Quit) key, the calculator ceases operation and is erased from the screen. C < Q / 7 8 9 * 4 5 6 - 1 2 3 + 0 ( ) = Solution import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { JPanel[] row = new JPanel[5]; JButton[] button = new JButton[19]; String[] buttonString = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*",
  • 3. ".", "/", "C", "", "+/-", "=", "0"}; int[] dimW = {300,45,100,90}; int[] dimH = {35, 40}; Dimension displayDimension = new Dimension(dimW[0], dimH[0]); Dimension regularDimension = new Dimension(dimW[1], dimH[1]); Dimension rColumnDimension = new Dimension(dimW[2], dimH[1]); Dimension zeroButDimension = new Dimension(dimW[3], dimH[1]); boolean[] function = new boolean[4]; double[] temporary = {0, 0}; JTextArea display = new JTextArea(1,20); Font font = new Font("Times new Roman", Font.BOLD, 14); Calculator() { super("Calculator"); setDesign(); setSize(380, 250); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); GridLayout grid = new GridLayout(5,5); setLayout(grid); for(int i = 0; i < 4; i++) function[i] = false; FlowLayout f1 = new FlowLayout(FlowLayout.CENTER); FlowLayout f2 = new FlowLayout(FlowLayout.CENTER,1,1); for(int i = 0; i < 5; i++) row[i] = new JPanel(); row[0].setLayout(f1); for(int i = 1; i < 5; i++) row[i].setLayout(f2); for(int i = 0; i < 19; i++) { button[i] = new JButton(); button[i].setText(buttonString[i]);
  • 4. button[i].setFont(font); button[i].addActionListener(this); } display.setFont(font); display.setEditable(false); display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); display.setPreferredSize(displayDimension); for(int i = 0; i < 14; i++) button[i].setPreferredSize(regularDimension); for(int i = 14; i < 18; i++) button[i].setPreferredSize(rColumnDimension); button[18].setPreferredSize(zeroButDimension); row[0].add(display); add(row[0]); for(int i = 0; i < 4; i++) row[1].add(button[i]); row[1].add(button[14]); add(row[1]); for(int i = 4; i < 8; i++) row[2].add(button[i]); row[2].add(button[15]); add(row[2]); for(int i = 8; i < 12; i++) row[3].add(button[i]); row[3].add(button[16]); add(row[3]); row[4].add(button[18]); for(int i = 12; i < 14; i++) row[4].add(button[i]); row[4].add(button[17]);
  • 5. add(row[4]); setVisible(true); } public void clear() { try { display.setText(""); for(int i = 0; i < 4; i++) function[i] = false; for(int i = 0; i < 2; i++) temporary[i] = 0; } catch(NullPointerException e) { } } public void getSqrt() { try { double value = Math.sqrt(Double.parseDouble(display.getText())); display.setText(Double.toString(value)); } catch(NumberFormatException e) { } } public void getPosNeg() { try { double value = Double.parseDouble(display.getText()); if(value != 0) { value = value * (-1); display.setText(Double.toString(value)); } else { } } catch(NumberFormatException e) { } }
  • 6. public void getResult() { double result = 0; temporary[1] = Double.parseDouble(display.getText()); String temp0 = Double.toString(temporary[0]); String temp1 = Double.toString(temporary[1]); try { if(temp0.contains("-")) { String[] temp00 = temp0.split("-", 2); temporary[0] = (Double.parseDouble(temp00[1]) * -1); } if(temp1.contains("-")) { String[] temp11 = temp1.split("-", 2); temporary[1] = (Double.parseDouble(temp11[1]) * -1); } } catch(ArrayIndexOutOfBoundsException e) { } try { if(function[2] == true) result = temporary[0] * temporary[1]; else if(function[3] == true) result = temporary[0] / temporary[1]; else if(function[0] == true) result = temporary[0] + temporary[1]; else if(function[1] == true) result = temporary[0] - temporary[1]; display.setText(Double.toString(result)); for(int i = 0; i < 4; i++) function[i] = false; } catch(NumberFormatException e) { } } public final void setDesign() { try { UIManager.setLookAndFeel(
  • 7. "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch(Exception e) { } } @Override public void actionPerformed(ActionEvent ae) { if(ae.getSource() == button[0]) display.append("7"); if(ae.getSource() == button[1]) display.append("8"); if(ae.getSource() == button[2]) display.append("9"); if(ae.getSource() == button[3]) { //add function[0] temporary[0] = Double.parseDouble(display.getText()); function[0] = true; display.setText(""); } if(ae.getSource() == button[4]) display.append("4"); if(ae.getSource() == button[5]) display.append("5"); if(ae.getSource() == button[6]) display.append("6"); if(ae.getSource() == button[7]) { //subtract function[1] temporary[0] = Double.parseDouble(display.getText()); function[1] = true; display.setText(""); } if(ae.getSource() == button[8]) display.append("1"); if(ae.getSource() == button[9]) display.append("2"); if(ae.getSource() == button[10])
  • 8. display.append("3"); if(ae.getSource() == button[11]) { //multiply function[2] temporary[0] = Double.parseDouble(display.getText()); function[2] = true; display.setText(""); } if(ae.getSource() == button[12]) display.append("."); if(ae.getSource() == button[13]) { //divide function[3] temporary[0] = Double.parseDouble(display.getText()); function[3] = true; display.setText(""); } if(ae.getSource() == button[14]) clear(); if(ae.getSource() == button[15]) getSqrt(); if(ae.getSource() == button[16]) getPosNeg(); if(ae.getSource() == button[17]) getResult(); if(ae.getSource() == button[18]) display.append("0"); } public static void main(String[] arguments) { Calculator c = new Calculator(); } } //the gui has close button so I didn't code that and < button has not been coded since the use of C button..... thankyou