Skip to content

@pdpi / forked from anonymous/

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
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();;
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.
System.out.println("Let's do some math using the numbers " + x + " and " + y + "!");
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(;
//Apply String input to variables xString and yString
System.out.print("Input first number: ");
String X_STRING =;
System.out.print("Input second number: ");
String Y_STRING =;
//Convert Strings to Integers
x = Integer.parseInt(X_STRING);
y = Integer.parseInt(Y_STRING);
} 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
Something went wrong with that request. Please try again.