Skip to content

Instantly share code, notes, and snippets.

@pdpi
Forked from anonymous/AutoCalc.java
Created September 9, 2012 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdpi/3687939 to your computer and use it in GitHub Desktop.
Save pdpi/3687939 to your computer and use it in GitHub Desktop.
AutoCalc by joshellis625
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment