i been working on it figuring out some switch statement issues i have been having. I was wondering if this is good beginner practice? Can i implememnt power and other mathematical features such as Math.square();? if so how can i do this? Anything is well appreciated. Im hoping to add as many features into this simple calculator
2) The following code is dicey
What it does is that while you are displaying a message to enter something and lets say the user presses 2 for Subtraction, and presses the enter key according to your code it will wait for the user for another input and this causes confusion to the user who would not know what to do, so perhaps you might want to insert some sort of message statements for the user there.
Once you are done with this, perhaps you might like to keep the main method cleaner by abstracting the code into small atomic groups also known as methods.
Once you have figured out the methods part, perhaps you might want to beautify your code by re-writing it such that it incorporates inheritance or serialisation or graphical components or interfaces and so on.
Hope this helps.
Ashish Dutt wrote:Importing Math library at this stage is a waste of memory for the code ain't using any typical math functions
Unless you mean the bytes taken up by the import statement in the source file, no memory is used by importing classes, not even by classes that are being used. That's merely a convenience in the source code for the developer.
Jonathan Vazquez wrote:i been working on it figuring out some switch statement issues i have been having. I was wondering if this is good beginner practice?
Nothing wrong with learning a new statement.
Can i implememnt power and other mathematical features such as Math.square();?
Sure; but strictly speaking, the two operations are different, since "power" presumably needs two operands, and "square" only one. You could probably even base the second on the first.
if so how can i do this? Anything is well appreciated.
OK, well first I'd say you need to StopCoding (←click).
You're too preoccupied with what your code looks like, rather than working out what it is a calculator does - and I'm afraid you'll never do that properly with a Java editor in front of you.
Java is an Object-Oriented language, and what you have written is a procedure. That same code could just as easily have been written in C, or BASIC, or any other language that has no knowledge of objects. Any time all your code is in main(), chances are you're not doing things "the Java way" - even if it works.
Im hoping to add as many features into this simple calculator
And that's why you need to start thinking about objects and classes. What is a "feature"? What are the differences in the types of operation a calculator can perform (I've already given you one at the top of this post)? What are the things that are similar about them?
Say, for sake of argument, I asked you to add a "Fibonacci" function: How would you do it?
Just to get you started, I'd say that you definitely want to think about an Operation class; but how you do it is up to you. What I would advise is that you don't put a main() method in it though. Only ONE class in any Java program needs to have a main(), so I'd get used to writing them without one.