Tuesday, January 27, 2015

HeadFirst Java Chapter 3

Chapter 3 was all about variables and all the uses they have and how to utilize them to the fullest extent. There are two types of variables, primitive and reference. primitive variables are variables such as boolean, integer, or float variables that hold bits representing a value. Reference variables reference an object and use it in a method  by utilizing the dot operator.If a reference variable has no object to reference, it will return "null". I was able to complete the "dog" code.'


This is the "dog" code, however there is an error. This can be seeen becuase of the "null" in the output. a reference variable is not referencing an object. I fixed it by moving "dog1.name = "Bart";" above "dog1.bark();" because when the method "dog1.bark();" was ran, there was no name associated with dog1 so then dog1 was supposed to bark, it returned "null says Ruff!" instead of "Bart says Ruff!"


Here is the corrected version of the code.
I was also able to complete the "Be the Compiler" section of this chapter pretty easily.



With A, "myBooks[0]","myBooks[1]", and "myBooks[2]" were not declared as the object "new Books();". threee lines of code, one fore each "myBooks" needed to be included to make the array of "myBooks" into objects. B was simple. Arrays begin at zero so z should equal -1 to begin rather than 0, and the while loops should continue until z is greater than or equal to 3. The 2 in the image should be a 3.




No comments:

Post a Comment