Skip to content

Instantly share code, notes, and snippets.

Created September 10, 2012 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3693501 to your computer and use it in GitHub Desktop.
Save anonymous/3693501 to your computer and use it in GitHub Desktop.
/*
* 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);
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package menucalc;
/**
*
* @author Josh Ellis
*/
public class MenuCalc {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//create objects
Menu Menu = new Menu();
Input Input = new Input();
Math Math = new Math();
//show menu choices
Menu.listMenu();
//take menu choice and send choice to appropriate operation (addition, subtraction, etc.)
Menu.chooseMenu(Input.menuInput());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment