Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Breaking it down problem, too much was in main, broke it down
- // Renee Waggoner
- // June 24, 2019
- // Special Requirements: None
- package breaking_it_down;
- import java.util.Scanner;
- public class BreakingItDown {
- Scanner scan = new Scanner(System.in);
- // Top Level routine for doing a single calculation
- boolean doCalculation()
- {
- int operand1, operand2, answer;
- char operator = getOperator();
- if (operator == 'q')
- {
- return false;
- }
- operand1 = getOperand(1);
- operand2 = getOperand(2);
- answer = doArithmetic(operator, operand1, operand2);
- 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
- boolean operator_is_good = false;
- do {
- System.out.print("Enter an Operator: + - * / q for quit: ");
- String strOperator = scan.nextLine();
- strOperator = strOperator.trim();
- if (strOperator.length() == 0)
- {
- continue; // Need to try this again with no input
- }
- operator = strOperator.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 (input.length() == 0)
- {
- operand_is_bad = true;
- }
- for (int i = 0; i < input.length(); i++)
- {
- 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
- 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
- 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);
- }
- // doing this to avoid too much code in main
- public static void main(String[] args)
- {
- // the class that we need
- BreakingItDown bid = new BreakingItDown();
- // calling doCalculation
- 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
Add Comment
Please, Sign In to add comment