Thursday, February 26, 2015

Headfirst Java Chapter 5

This chapter was about bigger, stronger methods, and the beginnings of creating a game of battleship. This chapter focused on creating a simpler version than the 2D grid with three ships. Instead I made a one row board with one ship that will be expanded into the full game in Chapter 6.

One method to writing code is to first write prep-code, then test code, then the real code. The prep-code is just what you want to happen and not how to do it. then the test code is where you start to test certain parts of the code without implementing methods. then the real code id 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 be the JVM secion 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.
I completed the simpledotcom game with the code shown below.
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.

Tuesday, February 10, 2015

Headfirst Java Chapter 4

As described in previous sections, a class is a blueprint for an object. All objects within a class will have the same methods, however, they may behave differently depending on instance variables. Arguments are passed on to methods as parameters.  Arguments are generated within a method, and sent somewhere. Parameters are arguments from one method that were sent to another method. values can be returned from methods using the return command. When a method is declared to return a value, the method must be declared using the same, or a compatible variable type as the returned value. methods can have multiple parameters, but they will be arranged in the order they were sent in as arguments, regardless of what the parameters are named.

Getters and setters, or accessors and mutators, set and retrieve data. For example, a setter method will set a variable, then the getter method is called to retrieve the value that was originally set. However we have a problem because all of our data thus far has been exposed, meaning any reference variable can change anything with a dot operator. This can be prevented by making all instance variables start with "private" and all getter and setter methods "public" so they can be accesses anywhere.

Even if Instance variables are not assigned values, they have a default value. integers is 0, floats are 0.0, booleans are false, and references are null. Instance variables are declared within a class but not within a methoid and local variables are declared within a method. Local variables, unlike Instance variables, need a value assigned to them before use. They do not have a default value. to see if any primitive variables are equal, one can use the == operator. The same goes to see if two reference variables refer to the same object. However the == operator cannot be used when seeing if two objects are the same. for that one must use the equals() method.

This is the "Be the Compiler" Section of Chapter 4. The annotations on the page explain what needs to be done. A will run as it stands, and it will output '42 84'. In B the getter "getTime()" needs to be declared with the same variable type that it is returning, which is a string. the output of the corrected code would be 'time: 1245".


Monday, February 2, 2015

First Program

Number Guesser:

I created a number guesser, in which, one must enter a number in an attempt to guess a random number of one through twenty five. If the guess is too high, the program will tell the player than their guess was too high, and it will say too low if the player guesses too low. The goal is to try and guess the number the program is thinking of in the least amount of guesses.
This is the code for the number guesser.