leo_kor

Calculator Helper

Apr 9th, 2020
432
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class CalcHelper {
  6.  
  7.     private void appStart() {
  8.         System.out.println("Press any key to continue...");
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         try {
  12.             reader.readLine();
  13.         }
  14.  
  15.         catch (IOException e) {
  16.             e.getStackTrace();
  17.         }
  18.     }
  19.  
  20.     public void printInstructions() {
  21.         System.out.println("Welcome to Simple Java Calculator.");
  22.  
  23.         try {
  24.             Thread.sleep(1000);
  25.         }
  26.         // Exception handling
  27.         catch (InterruptedException e) {
  28.             e.getStackTrace();
  29.         }
  30.  
  31.         appStart();
  32.  
  33.         System.out.println("Enter two numbers and the desired mathematical operation.");
  34.     }
  35.  
  36.     // Reading doubles
  37.     public double readDouble() {
  38.         double input = 0;
  39.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  40.  
  41.         // Read the input
  42.         try{
  43.         input = Double.parseDouble(reader.readLine());
  44.         }
  45.  
  46.         // Handle exceptions
  47.         catch (IOException e) {
  48.             e.getStackTrace();
  49.         }
  50.  
  51.         catch(NumberFormatException e) {
  52.             System.out.println("Oops, something went wrong. Try again, please.");
  53.             readDouble();   // Restart if things go wrong
  54.         }
  55.         return input;
  56.     }
  57.  
  58.     // Reading strings
  59.     public String readString() {
  60.         String s = ""; // default operator is plus
  61.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  62.  
  63.         try {
  64.             s = reader.readLine();
  65.         }
  66.  
  67.         // Handle exception
  68.         catch (IOException e) {
  69.             e.getStackTrace();
  70.         }
  71.  
  72.         return s;
  73.     }
  74.  
  75.  
  76.     // Selecting operation
  77.     public void operationSelect(String operator, double a, double b) {
  78.         switch(operator) {
  79.  
  80.             // Summation
  81.             case "+":
  82.             {
  83.                 double result = a + b;
  84.                 System.out.println(a + " + " + b + " equals " + result);
  85.                 break;
  86.             }
  87.  
  88.             // Subtraction
  89.             case "-":
  90.             {
  91.                 double result = a - b;
  92.                 System.out.println(a + " - " + b + " equals " + result);
  93.                 break;
  94.             }
  95.  
  96.             // Division
  97.             case "/":
  98.  
  99.             case ":": {
  100.                 double result = 0;
  101.  
  102.                 try {
  103.                     result = a / b;
  104.                 }
  105.  
  106.                 catch (ArithmeticException e) {
  107.                     e.getStackTrace();
  108.                 }
  109.  
  110.                 System.out.println(a + " / " + b + " equals " + result);
  111.                 break;
  112.             }
  113.  
  114.  
  115.             // Multiplication
  116.             case "*": {
  117.                 double result = a * b;
  118.                 System.out.println(a + " * " + b + " equals " + result);
  119.                 break;
  120.  
  121.             }
  122.  
  123.             // In case of the error
  124.             default:
  125.                 System.out.println("Unknown operation");
  126.                 break;
  127.         }
  128.     }
  129. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×