Advertisement
joxaren

calc

Apr 11th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2.  
  3. public class Calc {
  4.  
  5.     void divide(int x, int y) {
  6.         double z = (double) x / y;
  7.         System.out.println("You divided " + x + " by " + y + ". Result is " + new DecimalFormat("##.##").format(z));
  8.     }
  9.  
  10.     void multiply(int x, int y) {
  11.         int z = x * y;
  12.         System.out.println("You multiplied " + x + " by " + y + ". Result is " + z);
  13.     }
  14.  
  15.     void increment(int x, int y) {
  16.         int z = x + y;
  17.         System.out.println("You incremented " + x + " by " + y + ". Result is " + z);
  18.     }
  19.  
  20.     void decrement(int x, int y) {
  21.         int z = x - y;
  22.         System.out.println("You decremented " + x + " by " + y + ". Result is " + z);
  23.     }
  24.  
  25.     public static void main(String[] args) {
  26.         Calc t = new Calc();
  27.         InputParser parser = new InputParser();
  28.         InputGetter h = new InputGetter();
  29.         String userInput = h.getUserInput("What would you like to do? \nYou can divide, multiply, increment or decrement two numbers.");
  30.         if (userInput.equals("*") || userInput.equals("/") || userInput.equals("divide") ||userInput.equals("+") || userInput.equals("-")){
  31.             int firstDigit = parser.getFirstDIgitValue();
  32.             int secondDigit = parser.getSecondDigitValue();
  33.             if (userInput.equals("/")|| userInput.equals("divide")) {
  34.                 t.divide(firstDigit, secondDigit);
  35.             }
  36.             if (userInput.equals("*")) {
  37.                 t.multiply(firstDigit, secondDigit);
  38.             }
  39.             if (userInput.equals("+")){
  40.                 t.increment(firstDigit, secondDigit);
  41.             }
  42.             if (userInput.equals("-")) {
  43.                 t.decrement(firstDigit, secondDigit);
  44.             }
  45.         } else {
  46.             System.out.print("Sorry, you will have to try again.");
  47.         }
  48.     }
  49. }
  50.  
  51. _____________________________________
  52.  
  53. import java.io.*;
  54.  
  55. public class InputGetter {
  56.  
  57.     public String getUserInput(String prompt) {
  58.         String inputLine = null;
  59.         System.out.print(prompt + " ");
  60.         try {
  61.             BufferedReader is = new BufferedReader(
  62.                     new InputStreamReader(System.in));
  63.             inputLine = is.readLine();
  64.             if (inputLine.length() == 0 ) return null;
  65.         } catch (IOException e) {
  66.             System.out.println("IOException: " + e);
  67.         }
  68.         return inputLine;
  69.     }
  70. }
  71.  
  72. ___________________________________
  73.  
  74. public class InputParser {
  75.  
  76.     InputGetter h = new InputGetter();
  77.  
  78.     int getFirstDIgitValue() {
  79.         int firstDigit = Integer.parseInt(h.getUserInput("Please enter the first digit."));
  80.         return firstDigit;
  81.     }
  82.  
  83.     int getSecondDigitValue() {
  84.         int secondDigit = Integer.parseInt(h.getUserInput("Please enter the second digit."));
  85.         return secondDigit;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement