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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 
import java.util.Scanner;
 
public class AutoCalc {
 
/* It's conventional that field names are in camelCase, so I changed that.
*
* The private keyword means only this class can look at these values. Here,
* declaring the field as 'static' means that it's a class field, rather than
* an instance field. The two x and y values are specific to one particular
* instance of the calculator, rather than to the Calculator class a a whole,
* and so are not supposed to be static.
*
* I also deleted SUM, DIFF, etc. Think of these fields you define here not
* as variables, but as properties that describe the state of your object.
* The sum, diff, etc are not instrinsic parts of the object state, they're
* calculated from x and y, which, in your case, are the actual 'state'
* fields.
*/
private int x, y;
 
public static void main(String[] args) {
/* We're going to create a calculator object, rather than having static
* everything.
*/
AutoCalc calc = new AutoCalc();
calc.run();
 
}
 
public void run() {
/* I could've left all of this in main() and just called calc.getSum(), etc,
* but I prefer it when tasks are divided a bit more finely. This way,
* main() just worries about setting up the calc object and executing it
* (allowing you to then make that setup more complex if it needs to be)
* and the actual work is separated from the setup.
*/
getInput();
System.out.println();
System.out.println("Let's do some math using the numbers " + x + " and " + y + "!");
System.out.println();
System.out.println("The sum of " + x + " and " + y + " is " + getSum());
System.out.println("The difference between " + x + " and " + y + " is " + getDiff());
System.out.println(x + " divided by " + y + " is " + getDivide());
System.out.println(x + " times " + y + " is " + getMultiply());
}
 
public void getInput() {
/* Not much to say here, this works well enough. */
while (true) {
try {
 
Scanner input = new Scanner(System.in);
 
//Apply String input to variables xString and yString
System.out.print("Input first number: ");
String X_STRING = input.next();
System.out.print("Input second number: ");
String Y_STRING = input.next();
 
//Convert Strings to Integers
x = Integer.parseInt(X_STRING);
y = Integer.parseInt(Y_STRING);
 
break;
 
} catch (NumberFormatException nfe) {
System.err.println("I said enter numbers...");
}
}
}
 
public int getSum() {
/* The SUM, DIFF, etc fields you'd declared all the way up top weren't
* really intrinsic parts of the object state, they were just variables
* you needed as a way to get partial calculations done. What you could
* have done is declare them locally to where you wanted to use them,
* like this:
*/
int sum = x + y;
return sum;
 
}
 
public int getDiff() {
/* But even then, those local variables aren't REALLY necessary, since
* you're not doing anything too elaborate in these functions:
*/
return x - y;
 
}
 
public float getDivide() {
/* Though you might want to reintroduce them if things get more complex */
float xf = (float) x,
yf = (float) y;
return xf / yf;
 
}
 
public int getMultiply() {
return x * y;
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.