123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
-
- /**
- * Write a description of class Calculator here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
-
- import java.util.Scanner;
- public class Calculator extends BasicCalculations
- {
- // instance variables - replace the example below with your own
-
- /**
- * Constructor for objects of class Calculator
- */
- public Calculator() {
- currentNumber = 0;
- isError = false;
- }
-
- /**
- * An example of a method - replace this comment with your own
- *
- * @param y a sample parameter for a method
- * @return the sum of x and y
- */
-
- public void runCommandLoop()
- {
- Scanner input = new Scanner(System.in);
- Calculator calc = new Calculator();
- while (true) {
- System.out.print("Enter a command: ");
- String command = input.next();
-
- if (!isError) {
- if (command.equalsIgnoreCase("exit")) {
- System.out.print("Thanks for calculating with us!");
- break;
- }
-
- if (command.equalsIgnoreCase("clear")) {
- calc.clear();
- }
-
- if (command.equalsIgnoreCase("invert")) {
- calc.invertSign();
- }
-
- if (command.equalsIgnoreCase("inverse")) {
- calc.inverse(currentNumber);
- }
-
- if (command.equalsIgnoreCase("save")) {
- calc.remember();
- }
-
- if (command.equalsIgnoreCase("recall")) {
- calc.recall();
- }
-
- if (command.equalsIgnoreCase("save2")) {
- calc.addToNumInMemory();
- }
-
- if (command.equalsIgnoreCase("mode")) {
- calc.switchDisplayMode();
- }
-
- if (command.equalsIgnoreCase("units")) {
- calc.switchUnitsMode();
- }
-
- if (command.equalsIgnoreCase("add")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // read second number
- secondNumber = input.nextDouble();
- // call the add method on calc and display
- calc.add(firstNumber,secondNumber);
- } else {
- firstNumber = currentNumber;
- secondNumber = input.nextDouble();
- calc.add(firstNumber,secondNumber);
- }
- }
-
- if (command.equalsIgnoreCase("subtract")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // read second number
- secondNumber = input.nextDouble();
- // call the add method on calc and display
- calc.subtract(firstNumber,secondNumber);
- } else {
- firstNumber = currentNumber;
- secondNumber = input.nextDouble();
- calc.subtract(firstNumber,secondNumber);
- }
- }
-
- if (command.equalsIgnoreCase("multiply")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // read second number
- secondNumber = input.nextDouble();
- // call the add method on calc and display
- calc.multiply(firstNumber,secondNumber);
- } else {
- firstNumber = currentNumber;
- secondNumber = input.nextDouble();
- calc.multiply(firstNumber,secondNumber);
- }
- }
-
- if (command.equalsIgnoreCase("divide")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // read second number
- secondNumber = input.nextDouble();
- // call the add method on calc and display
- calc.divide(firstNumber,secondNumber);
- } else {
- firstNumber = currentNumber;
- secondNumber = input.nextDouble();
- calc.divide(firstNumber,secondNumber);
- }
- }
- if (command.equalsIgnoreCase("square")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.square(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.square(firstNumber);
- }
- }
-
- if (command.equalsIgnoreCase("round")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.round(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.round(firstNumber);
- }
- }
-
- if (command.equalsIgnoreCase("up")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.roundUp(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.roundUp(firstNumber);
- }
- }
-
- if (command.equalsIgnoreCase("down")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.roundDown(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.roundDown(firstNumber);
- }
- }
-
- if (command.equalsIgnoreCase("log")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.log(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.log(firstNumber);
- }
- }
-
- if (command.equalsIgnoreCase("power")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- secondNumber = input.nextDouble();
- calc.power(firstNumber, secondNumber);
- } else {
- firstNumber = currentNumber;
- secondNumber = input.nextDouble();
- calc.power(firstNumber, secondNumber);
- }
- }
-
- if (command.equalsIgnoreCase("sin")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.sin(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.sin(firstNumber);
- }
- }
-
- if (command.equalsIgnoreCase("invsin")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.asin(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.asin(firstNumber);
- }
- }
-
- if (command.equalsIgnoreCase("cos")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.cos(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.cos(firstNumber);
- }
- }
-
- if (command.equalsIgnoreCase("invcos")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.acos(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.acos(firstNumber);
- }
- }
-
- if (command.equalsIgnoreCase("tan")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.tan(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.tan(firstNumber);
- }
- // and on and on
- }
-
- if (command.equalsIgnoreCase("invtan")) {
- if (currentNumber == 0) {
- firstNumber = input.nextDouble();
- // call the add method on calc and display
- calc.atan(firstNumber);
- } else {
- firstNumber = currentNumber;
- calc.atan(firstNumber);
- }
- }
- } else {
- System.out.println("Error! Clear Error!");
- if (command.equalsIgnoreCase("clear")) {
- calc.clear();
- }
- }
- }
- }
- }
|