Sunday, January 25, 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.

Sunday, January 18, 2015

Headfirst Java Chapter 2

Chapter two was all about objects and classes and how they relate and interact. I found this to be a very helpful refresher on how java works and used OOP (Object Oriented Programming). I am beginning to remember how to read and write java. Because classes are like a blueprint of how an object will function, it is not hard to make one class and several objects. This allows for adding more things to a program without changing previously edited code. Objects have their own variables and methods that are either global variables and methods, inherited variables and methods from its class, or local variables and methods. A super class can be created so many classes can inherit traits from the larger class. I was able to complete the "Be the Compiler" page in Chapter 2 with relative ease.

The errors were relatively easy to spot. In the "A" section, the "t" variable was never defined. It would appear that the programmer would have wanted to declare "t" as a "new TapeDeck". To do this, they would have had to type TapeDeck t = new TapeDeck();" in the "TapeDeckTestDrive" class.

The error in the "B" section was also simple. The "playDVD" function that was called in the "DVDPlayerTestDrive" class was not defined. In order to do this the programmer would need to type "void playDVD() { //some code to run }" in the "DVDPlayer" class. this way the code in the "DVDPlayerTestDrive" class would call the "playDVD" fuction of the "new DVDPlayer" as defined by "d" from the "DVDPlayer" class.

Friday, January 9, 2015

Headfirst Java, Chapter 1

Chaper 1 of this book was interesting, and its much better than the boring textbooks like in history or math. However, it was only chapter 1, and since I already have a small background in java, I did not really learn too much from this. I did get better at what I already know, however. I was able to troubleshoot the 99 bottles of beer program and successfully make it run. My corrected code is shown below.
99 Bottles of Beer Program.

The error was one line was saying "1 bottles of beer on the wall" and that improper grammar. I simply added an else if statement so that if there was 1 bottle left, it would print "bottle" instead of "bottles". Also, this chapter served as a refresher for me since it has been a year or two since i have done any coding in java, and since then I have learned python. Python syntax is like java in many ways but is less complex. I have found myself almost writing python syntax when writing the code above. I have high hopes for this book, not only as a refresher for what I already know, but also later, to teach me more of the language.