Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Calculator class is below and I need this to be written in java, please and thank you public class Calculator { public enum Key

image text in transcribedThe Calculator class is below and I need this to be written in java, please and thank you

public class Calculator { public enum Key { DIGIT_0, DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4, DIGIT_5, DIGIT_6, DIGIT_7, DIGIT_8, DIGIT_9, NEGATE, DECIMAL_POINT, CLEAR, CLEAR_ENTRY, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUALS } private enum Operation { NOP, ADD, SUBTRACT, MULTIPLY, DIVIDE } public String getDisplay() { return display.toString(); } public Calculator() { display = new StringBuffer(); clear(); } public void keyPressed( Key key ) { switch( key ) { case CLEAR: clear(); break; case DIGIT_0: case DIGIT_1: case DIGIT_2: case DIGIT_3: case DIGIT_4: case DIGIT_5: case DIGIT_6: case DIGIT_7: case DIGIT_8: case DIGIT_9: case DECIMAL_POINT: if( ! errorMode ) digitKeyPressed( key ); break; case NEGATE: if( ! errorMode ) negateKeyPressed(); break; case ADD: if( ! errorMode ) addKeyPressed(); break; case SUBTRACT: if( ! errorMode ) subtractKeyPressed(); break; case MULTIPLY: if( ! errorMode ) multiplyKeyPressed(); break; case DIVIDE: if( ! errorMode ) divideKeyPressed(); break; case EQUALS: if( ! errorMode ) equalsKeyPressed(); break; } } private void clear() { display.setLength( 0 ); ac = 0.0; operand = 0.0; operation = Operation.NOP; validAC = false; validOperand = false; errorMode = false; clearDisplayUponNextDigit = true; } private void digitKeyPressed( Key key ) { if( clearDisplayUponNextDigit ) { display.setLength(0); clearDisplayUponNextDigit = false; } validOperand = false; char keyChar ='\0'; switch( key ) { case DIGIT_0: keyChar = '0'; break; case DIGIT_1: keyChar = '1'; break; case DIGIT_2: keyChar = '2'; break; case DIGIT_3: keyChar = '3'; break; case DIGIT_4: keyChar = '4'; break; case DIGIT_5: keyChar = '5'; break; case DIGIT_6: keyChar = '6'; break; case DIGIT_7: keyChar = '7'; break; case DIGIT_8: keyChar = '8'; break; case DIGIT_9: keyChar = '9'; break; case DECIMAL_POINT: keyChar = '.'; break; } display.append(keyChar); } public void addKeyPressed() { if( !validAC ) { loadAC(); if( validAC ) { operation = Operation.ADD; } } else { loadOperand(); if( validOperand ) { performSavedOperation(); operation = Operation.ADD; } } } public void subtractKeyPressed() { if( !validAC ) { loadAC(); if( validAC ) { operation = Operation.SUBTRACT; } } else { loadOperand(); if( validOperand ) { performSavedOperation(); operation = Operation.SUBTRACT; } } } public void multiplyKeyPressed() { if( !validAC ) { loadAC(); if( validAC ) { operation = Operation.MULTIPLY; } } else { loadOperand(); if( validOperand ) { performSavedOperation(); operation = Operation.MULTIPLY; } } } public void divideKeyPressed() { if( !validAC ) { loadAC(); if( validAC ) { operation = Operation.DIVIDE; } } else { loadOperand(); if( validOperand ) { performSavedOperation(); operation = Operation.DIVIDE; } } } private void equalsKeyPressed() { if( validAC ) { if( ! validOperand ) loadOperand(); if( validOperand ) performSavedOperation(); } } private void negateKeyPressed() { if( display.length() > 0 ) { if( display.charAt(0) == '-' ) { display.deleteCharAt(0); } else { display.insert( 0, '-'); } } } private void loadAC() { try { ac = Double.parseDouble(display.toString()); // display.setLength(0); // Keep the current digits in the display until new digit received. clearDisplayUponNextDigit = true; validAC = true; } catch( NumberFormatException e ) { display.setLength(0); display.append( "ERROR" ); errorMode = true; validAC = false; } } private void loadOperand() { try { operand = Double.parseDouble(display.toString()); display.setLength(0); validOperand = true; } catch( NumberFormatException e ) { display.setLength(0); display.append( "ERROR" ); errorMode = true; validOperand = false; } } private void performSavedOperation() { double result = 0.0; if( operation == Operation.ADD ) { result = ac + operand; } else if(operation == Operation.SUBTRACT ) { result = ac - operand; } else if( operation == Operation.MULTIPLY ) { result = ac * operand; } else if( operation == Operation.DIVIDE ) { result = ac / operand; } if( operation != Operation.NOP ) { display.setLength(0); display.append( result ); clearDisplayUponNextDigit = true; ac = result; } } private StringBuffer display; private double ac; private double operand; private boolean validAC; private boolean validOperand; private Operation operation; private boolean errorMode; private boolean clearDisplayUponNextDigit; } 
1. Write a calculator application. Use a grid layout to arrange buttons for the digits and for the +-x /and- operations. Use a text field to display the results. Use a Calculator class (the Calculator class source file and associated tester class is on BlackBoard) to model the calculator. Do not write vour own Calculator class. The Calculator class does not know about any graphical objects. The Calculator performs calculations, accept inputs, and provides display text. You can use the Microsoft Calcultor as a model for your Calculator. However you don't need to provide the menus and the MC. MIR, MIS, M+, sqrt, %, 1/x, +/- buttons. Calculator Edit View Help 10. Backspace CE MC MR MS sqrt 4 2 1/x

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions

Question

2. What is the industry-leading Lean-Agile methodology?

Answered: 1 week ago