milifl.blogg.se

Java console calculator
Java console calculator











  1. #JAVA CONSOLE CALCULATOR SOFTWARE#
  2. #JAVA CONSOLE CALCULATOR CODE#
  3. #JAVA CONSOLE CALCULATOR PROFESSIONAL#

#JAVA CONSOLE CALCULATOR CODE#

This makes reading your code slightly harder as other developers have to 're-parse' your variable/method names from what looks like class names. As you can tell from the syntax highlighting on your present code, PascalCase is the naming convention for type/class names. Please use camelCase instead of PascalCase for your variable and method names. Nested code blocks inside braces should be indented accordingly, instead of left-aligning everything. There's no other way of putting this, but your indentation is extremely poor. I happen to have one myself about two months ago, for reference. HasString = false //sets up the booleansĪ CLI-based calculator is often a good way to prepare one for bigger programming tasks, as there are just so many ways of doing it, catering for beginner to advanced implementations. TheAnswer = -1.0f /* Initialize to some value this should, in best practice, be a local variable. ** Compute the result of FirstNumber SecondNumber, and return the value.

#JAVA CONSOLE CALCULATOR PROFESSIONAL#

Don't let the Pascal in the title fool you it's about writing professional grade software, in any language. You're asking the right questions, and I'm glad to see you want to do better.Īn ancient book regarding this very topic was called "Professional Pascal" by Henry Ledgard. Trust me, there's a ton more I could do to this, but I'm not fixing any of the errors you might have. I'm not going to fix the variable declarations and such, only attempt to show you how other programmers write software. It will be very obvious to your teacher that you didn't write it. This is becoming long winded, so I'll try show you what I think it should look like. What happens if the user enters an operator that's not a */+-? You're depending on the global variable to be zero, and not explicitly setting the TheAnswer to any value. That's one thing you're not doing currently. This guarantees the return value from the function will always be set to SOMETHING. I also prefer that the local variable being returned is initialized to some known-good initial value.

#JAVA CONSOLE CALCULATOR SOFTWARE#

This is a big help when developing/maintaining software written by multiple people over multiple years. However, in non-trivial software, I much prefer to see a single return statement at the bottom of the routine, because it means that I can set a single breakpoint at the return, and I can see what the function is returning in the debugger.

java console calculator

I can certainly see his point his way of thinking reduces the number of lines in the method, and makes it easier to see at a glance what is being returned by the method. on one point, though, and that's the use of multiple return statements in your Answer() method. Make it easy for them to find where the blocks end. Matching up braces to find the blocks that are under an if/then/else is nobody's idea of a good time. Same rules regarding white-space, such as indentation. It makes it easier for other people to read software you've written, if you follow the convention/style that they're used to reading. Why? Because it's the convention that's used by programmers all over the planet. said above, you should camel-case method and variable names, and Pascal-case type/class names. So my advice is to make the source code as clear, easy to read, and easy to understand as you possibly can.Īs h.j.k. Source code is for HUMAN BEINGS to read and write. The reason we write in higher level languages is to make it easier for someone else (who may be you!) to understand what was INTENDED by the software. The truth is, the java compiler doesn't care about most of those things (mixed-case aside). Why does a language have support for comments?

java console calculator java console calculator

Why does a language have support for white space? Why does a language support upper and lower case? This might be a bit above your level, but think about it this way: Why do we write source code in higher level languages? The key element in writing better code, though, is to understand the goal of your source code. There are excellent suggestions in the above text, and I'd echo most of them. Public static boolean hasString2, hasInt Public static Scanner ScanInt1, ScanString Public static int FirstNumber, SecondNumber Just so you know i am a real beginner just got into java about a couple of weeks ago. I just want to know if it can be improved in any way. This is my Java 8 code for a console calculator.













Java console calculator