
-----------------------------------
Nathan4102
Mon Sep 02, 2013 11:22 am

How can I improve this Java program?
-----------------------------------
Just the other day, I wrote this small cmd Calculator in java. Its nothing special, it runs off command line arguments, and doesn't loop or anything. Im wondering if you guys have any suggestions on how to "Improve" this program. I don't mean tell me what I can add to make it better, I mean am I using any poor java standards? Or are there more efficient/better ways to do this? Also, how would I make this using OOP? 

public class Calculator {
    public static void main (String args

Thanks a bunch guys
Nathan

Edit: Not sure why everything past my main method is indented extra. Just assume thats one indent less.

-----------------------------------
DemonWasp
Mon Sep 02, 2013 2:07 pm

RE:How can I improve this Java program?
-----------------------------------
What happens if you give more than 3 arguments?

Why not just get the integer values from argsinterface Operation that specifies public int compute ( int a, int b ) (the most general version would be public Number compute ( Number a, Number b ). Then you have class AddOperation implements Operation, etc.

You could then have a map from an operation name to its implementation:

Map operations = new HashMap();
operations.put ( "add", new AddOperation () );
operations.put ( "subtract", new SubtractOperation() );
// ...

// Then you can just look up operations by name:
Operation operation = operations.get ( args

This is just one option, there are many others, each with their own advantages and disadvantages. This one only supports binary operations (two arguments) whereas there are functions that take many arguments, one argument, or no arguments.

-----------------------------------
Nathan4102
Mon Sep 02, 2013 5:53 pm

Re: RE:How can I improve this Java program?
-----------------------------------
What happens if you give more than 3 arguments?

Why not just get the integer values from argsinterface Operation that specifies public int compute ( int a, int b ) (the most general version would be public Number compute ( Number a, Number b ). Then you have class AddOperation implements Operation, etc.

You could then have a map from an operation name to its implementation:

Map operations = new HashMap();
operations.put ( "add", new AddOperation () );
operations.put ( "subtract", new SubtractOperation() );
// ...

// Then you can just look up operations by name:
Operation operation = operations.get ( args

This is just one option, there are many others, each with their own advantages and disadvantages. This one only supports binary operations (two arguments) whereas there are functions that take many arguments, one argument, or no arguments.

Good idea, I'll implement that now. And everything after "OOP:" flew right over my head. I still have much to learn :p
