One really key method to writing code it to first write a prep-code, then you should test the code, then do the real code. The prep-code is just what you want to happen and not how to do it, then test code is where you start to test certain parts of the code without implementing methods, then the real code is the full blown code where everything will work.
A few new things were explained in this chapter. The Integer.parseInt() method was explained. the "Integer" is a class that ships with Java. "parseInt()" converts whatever is in the parenthesis into an integer if, and only if, it is a digit. For example, a string "3" would be changed into an integer 3. For loops were explained. They are formatted like so; for (variable : array). They mean for every variable in the array repeat. The post-increment operator, or ++ after a variable, was explained and it just adds 1 to whatever variable it is in front of. There is also a post-decrement operator which is a variable with -- after it. it subtracts 1 rather than adds 1. "Pre" versions of both exist and mean add or subtract 1 then use the new value of the variable, if they are in an expression of some kind. Break statements were explained. They just immediately end loops by saying "break". How to make a random number was explained. "int randomNum" declares an integer that is called randomNum. Then "= (int) (Math.random() * 5) follows it. The "(int)" forces the random number to be an integer. "Math" is a class that comes with Java, and "random()" is a method within that class. it generates a random number from zero to just less than 1 (so 0.999999...). The "* 5" increases the range of the random number from anywhere between 0 and just below 5 (or 4.9999999...). Since the variable must be an integer, any of those decimal nines will be cut off, leaving a number between 0 and 4. How to get user input was also explained. "String guess = helper.getUserInput("enter a number");" is how to do so. "String guess" declares the string variable guess. "helper" is an instance of a class made to help specifically with the battleship game. "getUserInput" is a method of the "helper" class that takes what the user has typed when the user pressed return, and gives it back as a string. "("enter a number")" is what is going to be displayed right before the method starts looking for user input. There is also another kind of for loop. "for(int i = 0; i < 100; i++) {}". "int i = 0" declares a new integer variable. "i < 100" is the boolean test. this specific for loop will run 100 times. "i++" adds 1 to "i" every time the loop is run. When the amount of times a loop is wanted to run, a for loop should be used rather than a while loop.
This is the JVM section of the textbook. It was easy to complete because all that was needed was to run through the program and add variables, the writing on the page explains what I did to figure out the output.
Game.class This is the main class
GameHelper.class
Simpledotcom.class
This is the result when a game is played. The player won without missing.
No comments:
Post a Comment