Learning Java: Day 5

Writing a Program
Yesterday was filled with errands, a doctor appointment, and testing breadfruit recipes for an upcoming recipe book (not mine; I'm just a tester).  I ended up with only two hours to work on learning java before my brain turned off and I had to go to bed.   Today I'm trying to catch up...

Chapter 5 is where I get to create a real game program.  In this case, it's a very simple version of Battleship although instead of ships we're killing Dot Coms. Supposedly, that makes the book a business expense... The book hasn't covered how to create user interfaces yet so everything is still command line driven.  Somewhere in chapter 14 I'll get to learn the really cool stuff but right now I'm still figuratively learning to tie my shoelaces.

What we are learning in this chapter:

Loop de loop
Loops are used to allow the program to cycle through a bit of code over and over again until a certain condition happens. There are three types of loops: while, for, and enhanced for.

While is used when you don't know how many times you need to repeat the loop; you just want it to continue looping until some condition is met. A game where you keep going until you get "killed" would be an example.

The regular for loop is used when you know how many times you want to cycle through the loop. You normally set the counter to zero to start and then specify how many cycles to do. You might use this type of loop when printing out the lyrics to "100 Bottles of Beer (On the Wall)".

The enhanced for is used for traveling through the members of an array (and other "collections" that I haven't learned about yet) You can think of this type of loop as the "for each" loop because it reads "FOR EACH thing in the collection do this...." You might use this type of loop in a children's game, "for each cupcake in the tin, frost it".

We also learn how to make big numbers fit into small numbers (like a long to an int) by chopping them down to fit the space using the cast operator. The result is like chopping off all the numbers after the decimal point and just leaving the whole number.

And we learn to change a string into an int. Don't ask.

The final task was to create the Simple Dot Com game. After working and working at fixing errors in my game code, it finally works. That's HOURS and HOURS of fiddling.  But The Game Works. Yea! Now I can go to bed.


0 comments:

Post a Comment