Instantly share code, notes, and snippets.

# jflaherty/AutoCalc.java forked from anonymous/AutoCalc.java Created Sep 9, 2012

AutoCalc by joshellis625
 // ********************************************** // Created by: Joshua Ellis (joshellis625) // // Solve multiple simple math problems with two // user inputted numbers // ********************************************** public class AutoCalc { private int x = 0 private int y = 0; AutoCalc(int x, int y) { this.x = x; this.y = y; } public static int getSum() { return(x + y); } public static int getDiff() { return(x - y); } public static float getDivide() { return((float)x / (float)y); } public static int getMultiply() { return(x * y); } }
 import java.util.Scanner; public class AutoCalcRunner { public static void main(String[] args) { int x = 0; int y = 0; AutoCalc ac; ac = new AutoCalcRunner().getInput(); System.out.println(); System.out.println("Let's do some math using the numbers " + ac.x + " and " + ac.y + "!"); System.out.println(); System.out.println("The sum of " + ac.x + " and " + ac.y + " is " + ac.getSum()); System.out.println("The difference between " + ac.x + " and " + ac.y + " is " + ac.getDiff()); System.out.println(ac.x + " divided by " + ac.y + " is " + ac.getDivide()); System.out.println(ac.x + " times " + ac.y + " is " + ac.getMultiply()); } //create new Scanner input and convert String to Int public AutoCalc getInput() { while(true) { try { Scanner input = new Scanner(System.in); //Apply String input to variables xString and yString System.out.print("Input first number: "); String firstNumber = input.next(); System.out.print("Input second number: "); String secondNumber = input.next(); //Convert Strings to Integers x = Integer.parseInt(firstNumber); y = Integer.parseInt(secondNumber); return(new AutoCalc(x, y)); } catch (NumberFormatException nfe) { System.err.println("I said enter numbers..."); } } } }