Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Lab {
- // Configuration
- public static final String TITLE = "Smart Calc";
- public static final String VERSION = "0.1";
- // Variables
- private static Scanner console;
- // Operation
- public static final int ADD = 1;
- public static final int SUBTRACT = 2;
- public static final int MULTIPLY = 3;
- public static final int DIVIDE = 4;
- public static final int REMAINDER = 5;
- public static final int POWER = 6;
- public static final int SQRT = 7;
- public static final int ABS = 8;
- public static final int QUIT = 10;
- // Initialization
- public static void main(String[] args) {
- // Welcome Message
- System.out.println(TITLE + " v." + VERSION);
- // Initialize Scanner
- console = new Scanner(System.in);
- // Grab operator from user
- int operation = getOperator();
- // Return output from operation
- System.out.println("--");
- System.out.println(operate(operation));
- }
- // Grab operation from user input
- private static int getOperator() {
- // Prompt user
- System.out.println("\nPick an operation from the list.");
- displayMenu();
- // Grab input, close scanner, and return
- System.out.print("\n?: "); // Draw attention to input
- int operator = console.nextInt();
- return operator;
- }
- // Display menu options
- private static void displayMenu() {
- System.out.println("Operations:" + "\nADD - " + ADD + "\nSUBTRACT - " + SUBTRACT + "\nMULTIPLY - " + MULTIPLY + "\nDIVIDE - " + DIVIDE + "\nREMAINDER - " + REMAINDER + "\nPOWER - " + POWER + "\nSQUARE_ROOT - " + SQRT + "\nABSOLUTE_VALUE - " + ABS + "\nQUIT - " + QUIT);
- }
- // Determine operation from input
- private static int operate(int operation) {
- // Pick operation from list
- switch (operation) {
- case ADD:
- return add();
- case SUBTRACT:
- return subtract();
- case MULTIPLY:
- return multiply();
- case DIVIDE:
- return divide();
- case REMAINDER:
- return remainder();
- case POWER:
- return power();
- case SQRT:
- return sqrt();
- case ABS:
- return abs();
- case QUIT:
- default:
- return -1; // Quit or Error
- }
- }
- // Get a number, and check if it's a good one
- private static int getIntegerInput(char c) {
- // Grab input
- System.out.print(c + ": ");
- long input = console.nextLong();
- // Grab user's input as long and check if it's in bounds
- if(input >= Integer.MIN_VALUE && input <= Integer.MAX_VALUE) {
- // Return input
- return (int) input;
- } else {
- // Otherwise, try again
- return getIntegerInput(c);
- }
- }
- private static int getIntegerInput() {
- return getIntegerInput('?');
- }
- // Operations
- private static int add() {
- System.out.println("x + y");
- return getIntegerInput('x') + getIntegerInput('y');
- }
- private static int subtract() {
- System.out.println("x - y");
- return getIntegerInput('x') - getIntegerInput('y');
- }
- private static int multiply() {
- System.out.println("x * y");
- return getIntegerInput('x') * getIntegerInput('y');
- }
- private static int divide() {
- System.out.println("x / y");
- return getIntegerInput('x') / getIntegerInput('y');
- }
- private static int remainder() {
- System.out.println("x % y");
- return getIntegerInput('x') % getIntegerInput('y');
- }
- private static int power() {
- System.out.println("x ^ y");
- return (int) Math.pow(getIntegerInput('x'), getIntegerInput('y'));
- }
- private static int sqrt() {
- System.out.println("sqrt(x)");
- return (int) Math.sqrt(getIntegerInput('x'));
- }
- private static int abs() {
- System.out.println("|x|");
- return (int) Math.abs(getIntegerInput('x'));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement