-
-
Save jflaherty/3682476 to your computer and use it in GitHub Desktop.
AutoCalc by joshellis625
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ********************************************** | |
// 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment