Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Breaking It Down: this program ask for operator (+,-,*,/) then ask for two inputs and does the math
- // Renee Waggoner
- // June 24, 2019
- // Special Requirements: None
- package breaking_it_down;
- import java.util.Scanner;
- public class BreakingItDown {
- // scanner object
- Scanner scan = new Scanner(System.in);
- // Top Level routine for doing a single calculation
- // doCalculation() is method, which returns the boolean value @return if operator is equal to 'q' return false, else true
- boolean doCalculation()
- {
- /**
- * doCalculation() is method, which returns the boolean value
- * @return if operator is equal to 'q' return false, else true
- *
- */
- int operand1, operand2, answer;
- // operator is a variable of char type
- char operator = getOperator();
- // initialize q to quit
- if (operator == 'q')
- {
- return false;
- }
- /**
- * get the value for operand1 if we pass the
- * 1 as argument in getOperand() method
- * if 2 as an argument then getOperand() returns
- * value for operand2
- */
- operand1 = getOperand(1);
- operand2 = getOperand(2);
- // get the final calculated answer after calling doArithmetic() function, which takes three arguments, operator, operand1, operand2
- answer = doArithmetic(operator, operand1, operand2);
- /**
- * Format the result
- * and print on console
- */
- format(operator, operand1, operand2, answer);
- return true;
- } // end of top level routine
- // Get the Operator reliably
- char getOperator()
- {
- char operator = 'q';
- // This next block will get the user desired operator
- // getOperator should contain the following logic
- // condition for loop, if true, loop continues
- boolean operator_is_good = true;
- do {
- System.out.print("Enter an Operator: + - * / q for quit: ");
- String getOperator = scan.nextLine();
- getOperator = getOperator.trim();
- if (getOperator.length() == 0)
- {
- continue; // Need to try this again with no input
- }
- operator = getOperator.charAt(0);
- operator_is_good = false;
- // instructions for what to do with user responses
- switch (operator)
- {
- case 'q':
- case '+':
- case '-':
- case '*':
- case '/':
- operator_is_good = true;
- break;
- default:
- System.out.println("Your operator is bad ... try again:");
- break;
- } // end of switch
- } while (!operator_is_good);
- return operator;
- } // end of char getOperator method
- // get one operand reliably, getOperand method will get the next hunk of logic
- int getOperand(int which)
- {
- which = 2;
- System.out.println("Enter operand " + which);
- // collect the users operand into the "input" variable
- String input;
- boolean operand_is_bad;
- do {
- operand_is_bad = false;
- input = scan.nextLine();
- input = input.trim();
- // if user doesn't enter a value
- if (input.length() == 0)
- {
- operand_is_bad = true;
- }
- for (int i = 0; i < input.length(); i++)
- { // validate number from string input
- char c = input.charAt(i);
- if (c < '0' || c > '9')
- {
- // Oops, bad digit
- operand_is_bad = true;
- System.out.println("Your last input was bad, try again");
- scan.close();
- }// end of if statement
- } // end for statement
- } while (operand_is_bad);
- // The following statement will be covered later in the course.
- // For now, just know that it converts a String to an integer
- // Note that Integer.parseInt is picky and blows up with any bad characters
- return Integer.parseInt(input);
- } // end of int getOperand
- // Note: using char because of single character 'q'
- int doArithmetic(char operator, int operand1, int operand2) {
- int answer;
- // Now we have operator, operand1, and operand2.
- // Time to calculate the answer
- // doArithmetic will take the following logic, according to operator
- switch (operator)
- {
- case '+':
- answer = operand1 + operand2;
- break;
- case '-':
- answer = operand1 - operand2;
- break;
- case '*':
- answer = operand1 * operand2;
- break;
- case '/':
- answer = operand1 / operand2;
- break;
- default:
- System.out.println("We shouldn't get here in doArithmentic!!!!");
- answer = -1;
- break;
- } // end of switch
- return answer;
- } // end of int doArithmetic
- // void means no return and then using formatted string using specified arguments
- void format(char operator, int operand1, int operand2, int answer)
- {
- System.out.println(operand1 + " " + operator + " " + operand2 + " = " + answer);
- }
- // We like to keep minimal code in main
- public static void main(String[] args)
- {
- // Construct our class, create BreakingItDown object
- BreakingItDown bid = new BreakingItDown();
- // Call doCalculation for each calculation we are doing, loop until doCalculation() returns true.
- while (bid.doCalculation())
- {
- System.out.println("=======================");
- } // end of while (continue_calculating)
- // prints when user enters 'q'
- System.out.println("Finished Calculations");
- } // end of main
- } // end of class BreakItDown
Advertisement
Add Comment
Please, Sign In to add comment