SlideShare a Scribd company logo
Question: Hello, I need some assistance in writing a java pr...
Hello, I need some assistance in writing a java program using 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 java.awt.event.*;
public class MyCalculator extends Frame
{
public boolean setClear=true;
double number, memValue;
char op;
String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-",
"." };
String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" };
String memoryButtonText[] = {"MC", "MR", "MS", "M+" };
String specialButtonText[] = {"Backspc", "C", "CE" };
String buttonsText[]={"C","<","Q","/"};
String buttonText1[]={"7","8","9","*"};
String ButtonsText2[]={"4","5","6","-"};
String ButtonText3[]={"1","2","3","+"};
String ButtonText4[]={"0","(",")","="};
MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];
MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];
MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];
MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];
Label displayLabel=new Label("0",Label.RIGHT);
Label memLabel=new Label(" ",Label.RIGHT);
final int FRAME_WIDTH=325,FRAME_HEIGHT=325;
final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;
final int TOPX=30, TOPY=50;
///////////////////////////
MyCalculator(String frameText)//constructor
{
super(frameText);
int tempX=TOPX, y=TOPY;
displayLabel.setBounds(tempX,y,240,HEIGHT);
displayLabel.setBackground(Color.BLUE);
displayLabel.setForeground(Color.WHITE);
add(displayLabel);
memLabel.setBounds(TOPX, TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT);
add(memLabel);
// set Co-ordinates for Memory Buttons
tempX=TOPX;
y=TOPY+2*(HEIGHT+V_SPACE);
for(int i=0; i0)
resText=resText.substring(0,resText.length()-2);
return resText;
}
////////////////////////////////////////
public static void main(String []args)
{
new MyCalculator("Calculator -Ravi");
}
}
/*******************************************/
class MyDigitButton extends Button implements ActionListener
{
MyCalculator cl;
//////////////////////////////////////////
MyDigitButton(int x,int y, int width,int height,String cap, MyCalculator clc)
{
super(cap);
setBounds(x,y,width,height);
this.cl=clc;
this.cl.add(this);
addActionListener(this);
}
////////////////////////////////////////////////
static boolean isInString(String s, char ch)
{
for(int i=0; i

More Related Content

PPT
ma project
DOC
5: Every Java application is required to have
DOC
15: Which method call converts the value in variable stringVariable to an int...
DOC
Comp 328 final guide (devry)
DOC
7: Assume the following class declaration.
DOC
18: Which of the following does not generate an event?
DOC
202: When the user clicks a JCheckBox, a(n) occurs.
DOC
17: provides the basic attributes and behaviors of a window—a title bar at th...
ma project
5: Every Java application is required to have
15: Which method call converts the value in variable stringVariable to an int...
Comp 328 final guide (devry)
7: Assume the following class declaration.
18: Which of the following does not generate an event?
202: When the user clicks a JCheckBox, a(n) occurs.
17: provides the basic attributes and behaviors of a window—a title bar at th...

Similar to Question Hello, I need some assistance in writing a java pr...Hel.pdf (20)

DOC
8: Which statement below could be used to randomly select a state from an arr...
DOC
11: Which is a correct way to invoke the static method sqrt of the Math class?
DOC
1: The .class extension on a file means that the file
DOC
13: What do the following statements do?
DOC
12: When an object is concatenated with a String
DOC
22: The logical relationship between radio buttons is maintained by objects o...
DOC
Comp 328 final guide (devry)
DOC
14: Consider the class below:
DOC
4: Which of the following is a Scanner method?
DOC
3: A(n) ________ enables a program to read data from the user.
DOC
23: Which layout manager is the default for JPanel?
DOC
6: Which of the following statements about creating arrays and initializing t...
DOC
10: In the Java graphics system, coordinate units are measured in ________.
DOC
19: When the user presses Enter in a JTextField, the GUI component generates ...
DOC
Comp 328 final guide (devry)
DOC
21: Which method determines if a JRadioButton is selected?
DOC
16: Which of the following is the method used to display a dialog box to gath...
PDF
17 Jo P May 08
PDF
Hello, I need some assistance in writing a java program THAT MUST US.pdf
DOCX
Mmc manual
8: Which statement below could be used to randomly select a state from an arr...
11: Which is a correct way to invoke the static method sqrt of the Math class?
1: The .class extension on a file means that the file
13: What do the following statements do?
12: When an object is concatenated with a String
22: The logical relationship between radio buttons is maintained by objects o...
Comp 328 final guide (devry)
14: Consider the class below:
4: Which of the following is a Scanner method?
3: A(n) ________ enables a program to read data from the user.
23: Which layout manager is the default for JPanel?
6: Which of the following statements about creating arrays and initializing t...
10: In the Java graphics system, coordinate units are measured in ________.
19: When the user presses Enter in a JTextField, the GUI component generates ...
Comp 328 final guide (devry)
21: Which method determines if a JRadioButton is selected?
16: Which of the following is the method used to display a dialog box to gath...
17 Jo P May 08
Hello, I need some assistance in writing a java program THAT MUST US.pdf
Mmc manual

More from hainesburchett26321 (20)

PDF
Write an identity in terms of sines and cosines to simplify it. The .pdf
PDF
What is a sampling distribution Describe the similarities between t.pdf
PDF
What is the easiest to memorize the order of all the Eras and their .pdf
PDF
What is stock split How is it equivalent to paying a dividendS.pdf
PDF
Understanding Risk Terms and DefinitionsSolutionWhat is Risk.pdf
PDF
Thomas Townsend has an embarrassing criminal past. In 1985, he was c.pdf
PDF
The lymphatic system is composed of all of the following except lymph.pdf
PDF
Surgeons have sometimes cut the corpus callosum as a treatment for w.pdf
PDF
5 of 12 Incorrect Sapling Learning assify these items according to wh.pdf
PDF
Recall that E. coli can grow in glucose-salts medium, which contains .pdf
PDF
Random samples of 200 men, all retired were classified according to .pdf
PDF
Please help I think the answer is B, but Im not too sure.A. Amni.pdf
PDF
Need help writing the code for a basic java tic tac toe game Tic.pdf
PDF
Inventory is recorded at what value on the balance Sheet Inven.pdf
PDF
If you are monitoring a 1 V source on an oscilloscope and the scope m.pdf
PDF
If Emery has $1,700 to invest at 7 per year compounded monthly, how .pdf
PDF
Identify and discuss features of organizations managers need to know.pdf
PDF
he first photosynthetic organisms to oxygenate the Earth’s atmospher.pdf
PDF
following concepts from Johnsons book in Ch. 1, Ch. 2 or Ch. 3-- w.pdf
PDF
Explain how the structure and function are linked together for the f.pdf
Write an identity in terms of sines and cosines to simplify it. The .pdf
What is a sampling distribution Describe the similarities between t.pdf
What is the easiest to memorize the order of all the Eras and their .pdf
What is stock split How is it equivalent to paying a dividendS.pdf
Understanding Risk Terms and DefinitionsSolutionWhat is Risk.pdf
Thomas Townsend has an embarrassing criminal past. In 1985, he was c.pdf
The lymphatic system is composed of all of the following except lymph.pdf
Surgeons have sometimes cut the corpus callosum as a treatment for w.pdf
5 of 12 Incorrect Sapling Learning assify these items according to wh.pdf
Recall that E. coli can grow in glucose-salts medium, which contains .pdf
Random samples of 200 men, all retired were classified according to .pdf
Please help I think the answer is B, but Im not too sure.A. Amni.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdf
Inventory is recorded at what value on the balance Sheet Inven.pdf
If you are monitoring a 1 V source on an oscilloscope and the scope m.pdf
If Emery has $1,700 to invest at 7 per year compounded monthly, how .pdf
Identify and discuss features of organizations managers need to know.pdf
he first photosynthetic organisms to oxygenate the Earth’s atmospher.pdf
following concepts from Johnsons book in Ch. 1, Ch. 2 or Ch. 3-- w.pdf
Explain how the structure and function are linked together for the f.pdf

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Presentation on HIE in infants and its manifestations
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Institutional Correction lecture only . . .
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
RMMM.pdf make it easy to upload and study
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
master seminar digital applications in india
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Cell Types and Its function , kingdom of life
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Presentation on HIE in infants and its manifestations
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Institutional Correction lecture only . . .
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
RMMM.pdf make it easy to upload and study
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial disease of the cardiovascular and lymphatic systems
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
master seminar digital applications in india
Supply Chain Operations Speaking Notes -ICLT Program
A systematic review of self-coping strategies used by university students to ...
Cell Types and Its function , kingdom of life
STATICS OF THE RIGID BODIES Hibbelers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Question Hello, I need some assistance in writing a java pr...Hel.pdf

  • 1. Question: Hello, I need some assistance in writing a java pr... Hello, I need some assistance in writing a java program using 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
  • 2. 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 java.awt.event.*; public class MyCalculator extends Frame { public boolean setClear=true; double number, memValue; char op;
  • 3. String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." }; String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" }; String memoryButtonText[] = {"MC", "MR", "MS", "M+" }; String specialButtonText[] = {"Backspc", "C", "CE" }; String buttonsText[]={"C","<","Q","/"}; String buttonText1[]={"7","8","9","*"}; String ButtonsText2[]={"4","5","6","-"}; String ButtonText3[]={"1","2","3","+"}; String ButtonText4[]={"0","(",")","="}; MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length]; MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length]; MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length]; MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length]; Label displayLabel=new Label("0",Label.RIGHT); Label memLabel=new Label(" ",Label.RIGHT); final int FRAME_WIDTH=325,FRAME_HEIGHT=325; final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10; final int TOPX=30, TOPY=50; /////////////////////////// MyCalculator(String frameText)//constructor { super(frameText); int tempX=TOPX, y=TOPY; displayLabel.setBounds(tempX,y,240,HEIGHT); displayLabel.setBackground(Color.BLUE); displayLabel.setForeground(Color.WHITE); add(displayLabel); memLabel.setBounds(TOPX, TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT); add(memLabel); // set Co-ordinates for Memory Buttons tempX=TOPX; y=TOPY+2*(HEIGHT+V_SPACE); for(int i=0; i0) resText=resText.substring(0,resText.length()-2); return resText;
  • 4. } //////////////////////////////////////// public static void main(String []args) { new MyCalculator("Calculator -Ravi"); } } /*******************************************/ class MyDigitButton extends Button implements ActionListener { MyCalculator cl; ////////////////////////////////////////// MyDigitButton(int x,int y, int width,int height,String cap, MyCalculator clc) { super(cap); setBounds(x,y,width,height); this.cl=clc; this.cl.add(this); addActionListener(this); } //////////////////////////////////////////////// static boolean isInString(String s, char ch) { for(int i=0; i