public
Created — forked from /AutoCalc.java

AutoCalc by joshellis625

  • Download Gist
AutoCalc.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
// **********************************************
// 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);
}
}
AutoCalcRunner.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
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...");
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.