2
\$\begingroup\$

After much trial and error, I finally came up with this code to call on other classes to perform their function. This code works and does what I want it to do, so I am looking for some comments on the code efficiency.

What I want to happen is have the user input which calculator they would like to use and have that calculator come up presenting its own variables and calculations. The code for the calculators works just fine, so I don't need help with that. Just the efficiency of my calling from other classes.

My first iteration of this was to have the other calculators inside methods within the class. When I tried to call them up, it was nothing but problems, but this way seems to work. (I only mention this because my last question got knocked out because the code didn't work, so I found a way that worked, albeit not how I intended.)

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter 1 for Quadratic Calculator");
    System.out.println("Enter 2 for Compound Interest Calculator");
    int ans = sc.nextInt();

    if (ans == 1) {
        Quadratic.CalcQuad();
    } else if (ans == 2){
        CompInt.CalcInt();
    } else {
        System.out.println("That was not an option.");
    }

    sc.close();
}
\$\endgroup\$
  • \$\begingroup\$ It makes little sense to worry about the microseconds it takes to execute these method calls, when the human takes much much longer to make a choice. \$\endgroup\$ – 200_success Jan 27 '15 at 7:09
  • \$\begingroup\$ Not worried so much about how long it takes, but rather how clean/proper my code is to do what I want it to do. Probably my fault in the wording. Apologies. \$\endgroup\$ – PALADIN 458S Jan 27 '15 at 8:47
  • \$\begingroup\$ Not only am I still barely getting into programming...I'm still learning HOW to ask questions about programming the right way. \$\endgroup\$ – PALADIN 458S Jan 27 '15 at 8:47
  • \$\begingroup\$ I see. "Efficiency" probably isn't the right word then — the connotation is that it's about runtime performance. You're probably more concerned with whether your code is compact, idiomatic, or expressive. \$\endgroup\$ – 200_success Jan 27 '15 at 8:56
2
\$\begingroup\$

Simplify multiple conditions using a switch

Whenever multiple conditions depend only on the value of a single variable, consider using a switch instead to simplify:

switch (ans) {
    case 1:
        Quadratic.CalcQuad();
        break;
    case 2:
        CompInt.CalcInt();
        break;
    default:
        System.out.println("That was not an option.");
}

Naming

The common convention is to use camelCase for function names. The method names CalcQuad and CalcInt violate that.

It also seems that the class names could be better. If you spell out their purpose, the names won't be much longer but a lot more intuitive and unambiguous, for example:

  • Quadratic -> QuadraticCalculator
  • CompInt -> CompoundInterestCalculator

And then the methods that do the calculation can be simply calculate in both classes, without including the type of calculation, which is already included in the class name.

Alternatively, you could use a single Calculator class with calculateQuadratic and calculateCompoundInterest methods.

\$\endgroup\$
1
\$\begingroup\$

Thought you meant efficiency as in runtime performance. Woops.

Disclaimer: I am no expert by any stretch. My answer is based on a very basic understanding of Java and the JVM. Use my (possibly incorrect) answer at your own peril.

Since you're simply calling a function, the efficiency of that call (I would think) would rely on the JVM.

I'm willing to bet (but I'm no expert, so I may very well be wrong) that the execution time of, say, Quadratic.CalcQuad(); would be very similar, if not identical to the time it would take if you computed whatever is in that function, in your main class.

You can check the execution time by getting the current time right before the function call, as well as immediately after, and then subtract the two. For example:

/*
    The beginning of your program here
*/
if (ans == 1) {
        int startTime = System.currentTimeMillis();
        Quadratic.CalcQuad();
        int endTime = System.currentTimeMillis();
        System.out.print("It took ");
        System.out.print(endTime - startTime);
        System.out.print(" milliseconds to calculate a quadratic using a function call.");

        //This will output how long it takes to run the function (In milliseconds).

        startTime = System.currentTimeMillis();

    /**Paste the contents of "Quadratic.CalcQuad();" here, to perform the same
    calculation without calling the function.**/

        endTime = System.currentTimeMillis();
        System.out.print("It took ");
        System.out.println(endTime - startTime);
        System.out.print(" milliseconds to calculate the same quadratic WITHOUT the function call.");

        //This will output how long it takes to run the same function, 
        //only without the call (also in milliseconds).
    } else if (ans == 2){
/*
    The rest of your program here
*/
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.