public
anonymous / Input.java
Created

  • Download Gist
Input.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
* 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;
}
}
Math.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
* 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);
}
}
Menu.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*
* 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);
}
}
}
MenuCalc.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
* 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());
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.