Instantly share code, notes, and snippets.

# anonymous/Input.java Created Sep 10, 2012

What would you like to do?
 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package menucalc; import java.util.*; /** * * @author Josh */ public class Input { public int menuChoice; public int choice1; public int choice2; public int choices[] = {0, 0}; Scanner input = new Scanner(System.in); public int menuInput() throws InputMismatchException { System.out.println("Which operation would you like to do?"); menuChoice = input.nextInt(); return menuChoice; } public int[] chooseNumbers() throws InputMismatchException { System.out.print("What is your first number? "); choice1 = input.nextInt(); System.out.print("What is your second number? "); choice2 = input.nextInt(); choices[0] = choice1; choices[1] = choice2; return choices; } }
 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package menucalc; /** * * @author Josh */ public class Math { static int answer; static Input Input = new Input(); public static int doAddition(int a, int b) { return (a + b); } public static int doSubtraction(int a, int b) { if (a > b) { answer = a - b; } else if (a < b) { answer = b - a; } return answer; } public static int doMultiplication(int a, int b) { return (a * b); } public static int doDivision(int a, int b) { return (a / b); } }
 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package menucalc; /** * * @author Josh */ public class Menu { Input Input = new Input(); Math Math = new Math(); public void listMenu() { System.out.println("1: Addition"); System.out.println("2: Subtraction"); System.out.println("3: Multiplication"); System.out.println("4: Division"); System.out.println(); } public void chooseMenu (int a) { if (a == 1) { Input.chooseNumbers(); System.out.print(Input.choices[0] + " plus " + Input.choices[1] + " equals "); System.out.println(Math.doAddition(Input.choices[0], Input.choices[1])); System.exit(0); } else if (a == 2) { Input.chooseNumbers(); System.out.print("The difference between " + Input.choices[0] + " and " + Input.choices[1] + " is "); System.out.println(Math.doSubtraction(Input.choices[0], Input.choices[1])); System.exit(0); } else if (a == 3) { Input.chooseNumbers(); System.out.print(Input.choices[0] + " times " + Input.choices[1] + " equals "); System.out.println(Math.doMultiplication(Input.choices[0], Input.choices[1])); System.exit(0); } else if (a == 4) { Input.chooseNumbers(); System.out.print(Input.choices[0] + " divided by " + Input.choices[1] + " equals "); System.out.println(Math.doDivision(Input.choices[0], Input.choices[1])); System.exit(0); } } }