In this unique piece of code, the code picks a random integer between 1 and 100 and sets it to the numberToGuess. Our code also assigns three other variables, win, guess and numberOfTries. Every time you make a guess, the integer you select is set to the guess value, then it is checked of it is equal to the numberToGuess. If it is not the numberToGuess, then win remains false and two new methods are run. The first method checks if the guess is greater than the numberToGuess. If true, then it will tell you that your guess is too high. Else, if the guess is less than the numberToGuess the code will tell you your guess is too low. Every time a guess is made, one is added to the numberOfTries. When the guess is equal to the randomly assigned numberToGuess, the boolean value win is set to true and the build ends after telling you the numberToGuess and the numberOfTries.
Friday, January 30, 2015
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.
HeadFirst Java Chapter 2
Chapter two is 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 learn 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.
Tuesday, January 13, 2015
HeadFirst Java - Chapter 1
Chapter 1 of this book call Headfirst Java was very intriguing, reading this book was much better than any other book or textbook that i have read in school. However, it was only chapter 1 and it was kinda just an introduction to Java and since I already have a small background in Java I really did not learn much as it was more of refresher. But on the other hand I was able to troubleshoot the 99 bottles of beer song program and troubleshoot the random phrase generator and make them both successfully run. The code for both are posted below.
99 Bottles of beer program
The error in this program was that there was a line say " 1 bottles of beer on the wall" and that is very improper grammar. So all I did to fix it was instead of having beerNum > 1, I made it > 0 and it ended up solving the grammatical error. So again this served as a refresher for me for my Java skills, but since then I have learned Python. Python syntax is like Java in many ways but is less complex. I have found that I am almost writing Python syntax when writing the code above. I have high hopes for this Java book to teach me more about Java and take me more in depth about it.
Random Phrase Generator
Subscribe to:
Comments (Atom)