Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this program, you will write a command line calculator that can evaluate simp

ID: 3553260 • Letter: I

Question

In this program, you will write a command line calculator that can evaluate simple mathematical

expressions typed in by the user in floating point. In addition, your calculator will have a

memory that allows it to store or modify named variables. When the program starts, it should

print your name and state that it is a command line calculator. Then it should go into "command

mode", showing a prompt (such as %), and responding to inputs typed by the user. Example

usage is given as follows.

java MemCalc

Command line calculator with memory by <your name>

% a = 3 + 4

7.0

% bee = a * 3

21.0

% a + bee

28.0

% bee + 3.1

24.1

% a = 4.3

4.3

% 57

57.0

% c

c not found

% var

a: 4.0

bee: 21.0

% quit

The calculator must accept the following 5 input types of input from the user:

1. A command (quit, clear, var)

Result: The program executes the command

quit

Explanation / Answer

//============================================================ //Program: Calculator.java //============================================================ import java.applet.Applet; import java.awt.*; public class Calculator extends Applet { final static int max_digits = 12; // max number of digits allowed //============================================== // Instantiation of buttons and variables. //============================================== private boolean decimal_set = false; // flag for decimal point private boolean op_set = false; // flag to know an operation was done private boolean dec_tag = false; private Op_Type operation = new Op_Type(); // defines operation for calculating private factorial r_factorial = new factorial(); private int num_digits = 0; // check for digits entered private float total; // total of calculation private float memory; // calculator memory private StringBuffer num_entered; // holds entered number until // it is converted to a double private GridBagLayout key_pad; // Area for keys private GridBagConstraints key_bounds; // bounds of GridBag private TextField display; // number area private Button num_keys[] = new Button[10]; // digits private Button op_keys[] = new Button[4]; // +, - , *, and / private Button mem_keys[] = new Button[4]; // memory keys and clear private Button decimal_key = new Button("."); private Button sign_key = new Button("(+|-)"); // negation key private Button equal_key = new Button("="); // Iterative factorial key private Button factorial_i_key = new Button("! iteratively"); // Recursive factorial key private Button factorial_r_key = new Button("! recursively"); private void init_buttons() { int i; for(i=0; i < 10; i++) { num_keys[i] = new Button(Integer.toString(i)); num_keys[i].setBackground(Color.white); }// set number keys i = 0; op_keys[i] = new Button("+"); // Plus Key op_keys[i].setBackground(Color.white); mem_keys[i] = new Button("MS"); // Store Memory mem_keys[i].setBackground(Color.white); i++; op_keys[i] = new Button("-"); // Minus Key op_keys[i].setBackground(Color.white); mem_keys[i] = new Button("M+"); // Add to Memory mem_keys[i].setBackground(Color.white); i++; op_keys[i] = new Button("X"); // Multiplication Key op_keys[i].setBackground(Color.white); mem_keys[i] = new Button("MR"); // Recall Memory mem_keys[i].setBackground(Color.white); i++; op_keys[i] = new Button("/"); // Divide Key op_keys[i].setBackground(Color.white); mem_keys[i] = new Button("Clear"); // Clear Memory mem_keys[i].setBackground(Color.white); i++; } private void addComponent(Component comp, GridBagLayout gbl, GridBagConstraints gbc, int row, int column, int width, int height) { // set gridx and gridy gbc.gridx = column; gbc.gridy = row; // set gridwidth and gridheight gbc.gridwidth = width; gbc.gridheight = height; // gbc.insets(3,3,3,3); // set internal padding to 3 x 3 gbl.setConstraints(comp, gbc); add(comp); } public void init () { int i; key_pad = new GridBagLayout(); // key_pad.setBackground(Color.blue); setLayout(key_pad); // set to applet setBackground(Color.black); key_bounds = new GridBagConstraints(); display = new TextField("0", 30); // set display to 30 chars display.setEditable(false); // don't allow people to alter display display.setBackground(Color.black); display.setForeground(Color.green); addComponent(display, key_pad, key_bounds, 0, 0, 4, 1); num_entered = new StringBuffer(""); init_buttons(); key_bounds.fill = GridBagConstraints.BOTH; for(i = 0; i < 4; i++)// Display memory keys { addComponent(mem_keys[i], key_pad, key_bounds, 1, i, 1, 1); } for(i = 7; i < 10; i++)// Display keys 7 to 9 { addComponent(num_keys[i], key_pad, key_bounds, 3, i-7, 1, 1); } for(i = 4; i < 7; i++)// Display keys 4 to 6 { addComponent(num_keys[i], key_pad, key_bounds, 4, i-4, 1, 1); } for(i = 1; i < 4; i++)// Display keys 1 to 3 { addComponent(num_keys[i], key_pad, key_bounds, 5, i-1, 1, 1); } int j = 6; for(i = 0; i < 4; i++)// Display operation keys { addComponent(op_keys[i], key_pad, key_bounds, j--, 3, 1, 1); } // Display 0 key addComponent(num_keys[0], key_pad, key_bounds, 6, 0, 1, 1); decimal_key.setBackground(Color.white); // Display decimal key addComponent(decimal_key, key_pad, key_bounds, 6, 1, 1, 1); sign_key.setBackground(Color.white); // Display +/- key addComponent(sign_key, key_pad, key_bounds, 6, 2, 1, 1); // Display factorial keys factorial_i_key.setBackground(Color.red); factorial_r_key.setBackground(Color.red); addComponent(factorial_i_key, key_pad, key_bounds, 7, 0, 3, 1); addComponent(factorial_r_key, key_pad, key_bounds, 7, 3, 1, 1); equal_key.setBackground(Color.green); // Display = key addComponent(equal_key, key_pad, key_bounds, 8, 0, 4, 1); } public boolean action(Event evt, Object obj) { int i; Float temp_float; if(evt.target instanceof Button) { // Check if not past digit limit if ( (num_digits 0; i--) { total = total * i; } num_entered = new StringBuffer(Float.toString(total)); display.setText(num_entered.toString()); num_digits = 0; dec_tag = false; decimal_set = false; total = 0; op_set = true; } if (evt.target == factorial_r_key) { temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); total = r_factorial.factorial(total); num_entered = new StringBuffer(Float.toString(total)); display.setText(num_entered.toString()); total = 0; op_set = true; num_digits = 0; dec_tag = false; decimal_set = false; } if (evt.target == equal_key) // equal key operations { switch(operation.Which_Op()) { case 0: // add temp_float = new Float(num_entered.toString()); total += temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); break; case 1: // subtract temp_float = new Float(num_entered.toString()); total -= temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); break; case 2: // multiply temp_float = new Float(num_entered.toString()); total *= temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); break; case 3: // divide temp_float = new Float(num_entered.toString()); if(temp_float.floatValue() != 0) { total /= temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); } else { display.setText("Error. Division by 0"); num_entered = new StringBuffer(""); num_digits = 0; total = 0; dec_tag = false; return true; } break; default: temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); num_entered = new StringBuffer(Float.toString(total)); break; } display.setText(num_entered.toString()); op_set = true; num_digits = 0; dec_tag = false; decimal_set = false; return true; }// End of equal key operations if. for (i = 0; i < 4; i++) // calculator operations { if (evt.target == op_keys[i]) { switch (i) { case 0: // add operation.ADD_OP(); temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); break; case 1: // subtract operation.SUB_OP(); temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); break; case 2: // multiply operation.MULT_OP(); temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); break; case 3: // divide operation.DIV_OP(); temp_float = new Float(num_entered.toString()); total = temp_float.floatValue(); break; default: } num_entered = new StringBuffer(Float.toString(total)); display.setText(String.valueOf(num_entered)); decimal_set = false; op_set = true; num_digits = 0; dec_tag = false; return true; } if (evt.target == mem_keys[i]) { switch (i) { case 0: // Store Memory num_digits = max_digits+1; temp_float = new Float(num_entered.toString()); memory = temp_float.floatValue(); break; case 1: // Memory + num_digits = max_digits+1; temp_float = new Float(num_entered.toString()); memory += temp_float.floatValue(); break; case 2: // Memory Recall num_digits = max_digits+1; num_entered = new StringBuffer(String.valueOf(memory)); display.setText(Float.toString(memory)); break; case 3: // Clear num_digits = 0; total = 0; num_entered = new StringBuffer("0"); // clear display display.setText(num_entered.toString()); num_entered = new StringBuffer(""); // clear string break; default: break; } decimal_set = false; op_set = true; num_digits = 0; dec_tag = false; return true; } } // End of calculator operations for loop } // End of largest if: if(evt.target instanceof Button) return false; } } // End of Calculator Applet Class, END OF PROGRAM
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote