letsdoitjava

Breaking It Down

Jun 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. // Breaking it down problem, too much was in main, broke it down
  2. // Renee Waggoner
  3. // June 24, 2019
  4. // Special Requirements: None
  5. package breaking_it_down;
  6. import java.util.Scanner;
  7.  
  8. public class BreakingItDown {  
  9.     Scanner scan = new Scanner(System.in);
  10.  
  11.     // Top Level routine for doing a single calculation
  12.     boolean doCalculation()
  13.     {
  14.         int operand1, operand2, answer;
  15.         char operator = getOperator();
  16.         if (operator == 'q')
  17.         {
  18.             return false;
  19.         }
  20.         operand1 = getOperand(1);
  21.         operand2 = getOperand(2);
  22.         answer = doArithmetic(operator, operand1, operand2);
  23.         format(operator, operand1, operand2, answer);
  24.         return true;
  25.     } // end of top level routine
  26.  
  27.     // Get the Operator reliably
  28.     char getOperator()
  29.     {
  30.         char operator = 'q';
  31.         // This next block will get the user desired operator
  32.         // getOperator should contain the following logic
  33.         boolean operator_is_good = false;
  34.         do {
  35.             System.out.print("Enter an Operator: + - * / q for quit: ");
  36.             String strOperator = scan.nextLine();
  37.             strOperator = strOperator.trim();
  38.             if (strOperator.length() == 0)
  39.             {
  40.                 continue; // Need to try this again with no input
  41.             }
  42.             operator = strOperator.charAt(0);
  43.             operator_is_good = false;
  44.             // instructions for what to do with user responses
  45.             switch (operator)
  46.             {
  47.             case 'q':
  48.             case '+':
  49.             case '-':
  50.             case '*':
  51.             case '/':
  52.                 operator_is_good = true;
  53.                 break;
  54.             default:
  55.                 System.out.println("Your operator is bad ... try again:");
  56.                 break;
  57.             } // end of switch
  58.  
  59.         } while (!operator_is_good);
  60.         return operator;
  61.     } // end of char getOperator method
  62.  
  63.     // get one operand reliably, getOperand method will get the next hunk of logic
  64.     int getOperand(int which)
  65.     {
  66.         which = 2;
  67.         System.out.println("Enter operand " + which);
  68.         // collect the users operand into the "input" variable
  69.         String input;
  70.         boolean operand_is_bad;
  71.         do {
  72.             operand_is_bad = false;
  73.  
  74.             input = scan.nextLine();
  75.             input = input.trim();
  76.             if (input.length() == 0)
  77.             {
  78.                 operand_is_bad = true;
  79.             }          
  80.             for (int i = 0; i < input.length(); i++)
  81.             {
  82.                 char c = input.charAt(i);
  83.                 if (c < '0' || c > '9')
  84.                 {
  85.                     // Oops, bad digit
  86.                     operand_is_bad = true;
  87.                     System.out.println("Your last input was bad, try again");
  88.                     scan.close();
  89.                 }// end of if statement
  90.  
  91.             } // end for statement
  92.         } while (operand_is_bad);
  93.  
  94.         // The following statement will be covered later in the course.
  95.         // For now, just know that it converts a String to an integer
  96.         // Note that Integer.parseInt is picky and blows up with any bad characters
  97.         return Integer.parseInt(input);
  98.        
  99.     } // end of int getOperand
  100.  
  101.     int doArithmetic(char operator, int operand1, int operand2) {
  102.         int answer;
  103.         // Now we have operator, operand1, and operand2.
  104.         // Time to calculate the answer
  105.         // doArithmetic will take the following logic
  106.         switch (operator)
  107.         {
  108.         case '+':
  109.             answer = operand1 + operand2;
  110.             break;
  111.         case '-':
  112.             answer = operand1 - operand2;
  113.             break;
  114.         case '*':
  115.             answer = operand1 * operand2;
  116.             break;
  117.         case '/':
  118.             answer = operand1 / operand2;
  119.             break;
  120.         default:
  121.             System.out.println("We shouldn't get here in doArithmentic!!!!");
  122.             answer = -1;
  123.             break;
  124.         } // end of switch
  125.         return answer;
  126.     } // end of int doArithmetic
  127.  
  128.     // void means no return and then using formatted string using specified arguments
  129.     void format(char operator, int operand1, int operand2, int answer)
  130.     {
  131.         System.out.println(operand1 + " " + operator + " " + operand2 + " = " + answer);
  132.     }
  133.     // doing this to avoid too much code in main
  134.     public static void main(String[] args)
  135.     {
  136.         // the class that we need
  137.         BreakingItDown bid = new BreakingItDown();
  138.  
  139.         // calling doCalculation
  140.         while (bid.doCalculation())
  141.         {
  142.             System.out.println("=======================");
  143.         } // end of while (continue_calculating)
  144.  
  145.         // prints when user enters 'q'
  146.         System.out.println("Finished Calculations");
  147.     } // end of main
  148. } // end of class BreakItDown
Add Comment
Please, Sign In to add comment