letsdoitjava

Breaking It Down

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