If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Help Needed w/ simple Calculator Program!

For some reason, I'm getting an error message when I try to compile the following program. Its a simple calculator program that take three arguments from the command line (two integers and an operator) to preform math problems.

Ok, this is doing exactly what your code does - in less lines. However, it has the disadvantage of only working with operator(-name)s consisting of exactly one character and there is still considerable duplication of code.

So, what about having a class for the operator? You could then simply write in main() something like:

which calculates the number of sets with k elements, that are a subset of an n-element-set; as it is mathematically defined (extended to integers rather than only for non-negative integers). Eg. your chances at sweepstakes where you choose 6 numbers out of 49 is 1 : (6 outof 49).

This is definitely not the most efficient way to compute that number, but
- it illustrates how to keep code that is not clear at first glance separate from code that does different things and most important: how to make explicit what it does (only in second place: how)
- it is not a trivial translation into the corresponding java expression (because there is none in this case )
- it is one of the few (binary) operators I could think of for which it makes sense to choose a symbol that is not only one character (another would be the greatest common divisor gcd, but that you'd rather like to write gcd(a,b). If interested, just tell)

Now, how are all those marvellous operators organized so that we can simply call Operator.get(symbol)? Well, only one outof many possibilities:

I know, all that abstract, static, ... might be confusing. But if you later on want a more sophisticated calculator, you have to organize it in a way similar to this. There are still enough (unsolved) problems, for example what about operators that aren't binary and infix? Eg. -42 (unary, prefix) or 7! (factorial; unary, postfix). What about braces like in (1+2)*3 ? This is not easy and you're better off if you have the what-and-how-to-do-when-symbol-is-x separated from that and can concentrate on one thing at a time.

--
p.s.: to get this example working, you must somewhere once instantiate your XxxxOperator classes, eg. in main(): new CombinationsOperator(); Thats enough, you need not assign this newly created CombinationsOperator to some variable.