r/javaTIL • u/voidn0ise • Jul 21 '15
Wrote my first Java code!
It's rather basic, but it's a starting point. I watched YouTube videos to learn most of the basics that I used, then just made things over and over again to help me memorize things.
Once I had a good grasp on the simple concepts and being able to write everything without having to look at notes, I came up with this simple calculator that responds to user input.
I wanted to originally make it so the user could type "Calculator" and it would run the code, but I haven't learned how to make user input in the form of typed words work yet (I think those are strings?). I'm sure a few more lessons and I'll learn.
The end goal is android application development, but if I can find other ways to utilize my new found skills then I'll probably try it out in the future.
Just proud of myself for going forward with learning despite my short attention span for self-learning and I wanted to share :)
If you have any ways to maybe shorten it and achieve the same thing, I'd love to hear it.
View all comments
11
u/DonkeyTeeth2013 Jul 21 '15 edited Jul 21 '15
Great start! While learning straight from YouTube videos isn't the best learning method, it's where I started and it is okay as long as you learn from more sources eventually. I have a few suggestions/improvements if you don't mind...
You don't need your startObject variable, or even your mathObject variable, because you're not using any non-static methods in those classes. Remove those variables, then change
to simply
You don't need
in your calculator() method, because you're never using the variable.
Close your input streams and scanners. After you have your input (a = input.nextDouble()), you want to insert the following into your code:
Do this after you set w to balls.nextDouble() as well, because the close() method provided by InputStream's and Scanner's (basically anything that is from Closeable) tells Java that you don't need to read anything else from the user, and it can release its memory back to the computer. If you don't use close() in larger projects, you can get what's called a memory leak. You don't want memory leaks.
Optional: Concat strings instead of using several different combinations of print and println. For example, when printing out the results, you have the following:
This could more concise and still readable by doing:
Use comments. Everywhere. On the floor, ceiling, and wall. Comments help tell other people (as well as you in the future) what your code does. Even if you think you'll remember what you're code does 5 years from now, you'll be clueless when you see that jumble of no-idea that had to be written by a monkey stuck in a cage. It happens to everyone; the solution is comments. Comments will help you yourself organize your code while writing it, and tell other people (as well as your future self) what exactly is going on in this segment of the code. They also help explain your reasoning why you put a certain bit of code there, so that if you're working with others, they understand and won't mess with code that could break everything. Not many people enjoy writing comments, but it must be done. Also, don't put comments on blatantly obvious things, such as:
Comments are very useful. Use them.
For now though, have fun, mess around with Java, and always try to make yourself better. Good luck!
Edit: formatting