Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- public class Calc {
- void divide(int x, int y) {
- double z = (double) x / y;
- System.out.println("You divided " + x + " by " + y + ". Result is " + new DecimalFormat("##.##").format(z));
- }
- void multiply(int x, int y) {
- int z = x * y;
- System.out.println("You multiplied " + x + " by " + y + ". Result is " + z);
- }
- void increment(int x, int y) {
- int z = x + y;
- System.out.println("You incremented " + x + " by " + y + ". Result is " + z);
- }
- void decrement(int x, int y) {
- int z = x - y;
- System.out.println("You decremented " + x + " by " + y + ". Result is " + z);
- }
- public static void main(String[] args) {
- Calc t = new Calc();
- InputParser parser = new InputParser();
- GettingInput h = new GettingInput();
- String userInput = h.getUserInput("What would you like to do? \nYou can divide, multiply, increment or decrement two numbers.");
- if (userInput.equals("*") || userInput.equals("/") || userInput.equals("divide") ||userInput.equals("+") || userInput.equals("-")){
- int firstDigit = parser.getFirstDIgitValue();
- int secondDigit = parser.getSecondDigitValue();
- if (userInput.equals("/")|| userInput.equals("divide")) {
- t.divide(firstDigit, secondDigit);
- }
- if (userInput.equals("*")) {
- t.multiply(firstDigit, secondDigit);
- }
- if (userInput.equals("+")){
- t.increment(firstDigit, secondDigit);
- }
- if (userInput.equals("-")) {
- t.decrement(firstDigit, secondDigit);
- }
- } else {
- System.out.print("Sorry, you will have to try again.");
- }
- }
- }
- ________________________________________________________--
- import java.io.*;
- public class GettingInput {
- public String getUserInput(String prompt) {
- String inputLine = null;
- System.out.print(prompt + " ");
- try {
- BufferedReader is = new BufferedReader(
- new InputStreamReader(System.in));
- inputLine = is.readLine();
- if (inputLine.length() == 0 ) return null;
- } catch (IOException e) {
- System.out.println("IOException: " + e);
- }
- return inputLine;
- }
- }
- _________________________________________________________
- public class InputParser {
- GettingInput h = new GettingInput();
- int getFirstDIgitValue() {
- int firstDigit = Integer.parseInt(h.getUserInput("Please enter the first digit."));
- return firstDigit;
- }
- int getSecondDigitValue() {
- int secondDigit = Integer.parseInt(h.getUserInput("Please enter the second digit."));
- return secondDigit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement