Wednesday, January 21, 2015

Aaron's Java Blag 4: Where making useful programs is strictly prohibited.



Another day, another not very useful program. Maybe tomorrow I'll make something useful.

Today I learned how to do loops, so I decided it would be fun to make a pseudo-calculator. All it can really do is add and subtract, but I think that’s pretty good. I also went back a bit and got a refresher on another data type I needed- booleans.

If you read my second post, you might remember what a boolean is. According to the book, it’s basically a value that is either true or false (Bryant Data Types). Since I needed something that could keep my loop running until I told it to stop, a true/false data type was great for my program.

The next thing I learned for this program was how to do a while loop. According to the book, “while loops continue to loop as long as something is true. A while loop has only one argument: an expression that can be evaluated as true or false” (Bryant Looping). Basically, that means that a while loop repeats itself over and over until something (in this case a boolean) changes from true to false. This allows you to repeat the same task over and over with no limit, so it’s great for getting multiple inputs of the same type, as long as you do something with the input before the loop restarts itself.

Because pulling out a calculator is too easy.

Here’s my program. It’s pretty simple- it just takes the number you input, adds it to the total, and then loops back to asking for input again until you type “end” as the input. You’ll notice I applied other things that I’ve learned from the book for earlier programs, such as scanner and if statements. Here’s what it ends up looking like when you run it and give it some numbers:

 
Pretty annoying and long to make compared to a normal calculator, right? Actually, that ties into today’s Amazing and Thought Provoking QuestionTM. Do you enjoy taking time to accomplish a task in the more challenging way, or do you prefer to take the easiest path to success? For me, it depends on what I’m doing. If it’s a hobby I’m doing on my free time, I’ll go for the more challenging option, as I feel more accomplished that way (making this certainly felt like more of an accomplishment than pulling out a calculator would). However, if it’s something mandatory, such as a school assignment, I typically go for the easiest way to still get full credit.

Bryant, Jay. Java 7 for Absolute Beginners. Berkeley, CA: Apress, 2012. Books 24/7. Web. 21 Jan. 2015.


No comments:

Post a Comment