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. 

Wednesday, December 17, 2014

1.3.8

1.3.8 While Loops
Number Guesser

Conclusion:

1. If you change between 1 and 20 from the previous program to between 1 and 6000, how many guesses will you need to guarantee that you have the right answer? Explain.

6000 because there are 6000 numbers one can guess so someone could guess every number except the right one, then the last guess would be the right one, which would add up to 6000 guesses.

2. Describe the difference between a while loop and a for loop.

A for loop repeats a block of code for every time an event occurs. A while loop repeats a block of code so long as a condition remains true.

Sunday, December 14, 2014

1.3.7

1.3.7 For Loops

Mastermind


Lottery Ticket



Questions:
1. Sometimes code using an iterative loop can be written without a loop, simply repeating the iterated code over and over as separate lines in the program. Explain the disadvantages of developing a program this way.


If a program is developed this way, it will take up much more space, be more time consuming, and take longer to run than if the code were just created with a loop.

2. Name a large collection across which you might iterate.

A collection of music, perhaps to make sure two directories have the same exact songs and to fill the ones that are missing from each other. my collection has about 900 songs, and the are people with much bigger ones out there.



3.What is the relationship between iteration and the analysis of a large set of data?
One can generate a program to iterate a block of code that preforms an action on each datum in the set. For example, a program could examine a set of numbers and for each number that is over 10, increase a variable by 1.

Monday, December 8, 2014

1.3.6 Tuples and Lists

1.3.6 Tuples and Lists




Conclusion

1. Consider a string, tuple, and list of characters.

In []: a = 'acbde'

In []: b = ('a', 'b', 'c', 'd', 'e')

In []: c = ['a', 'b', 'c', 'd', 'e']

The values of a[3], b[3], and c[3] are all the same. In what ways are a, b, and c different?

a is a string, b is a tuple, and c is a list.

2. Why do computer programming languages almost always have a variety of variable types?
Not everything can be answered with one variable type. For example, some situations require a true

or false statement whereas others require a number. These varibales would have to be two different types.

3. Why can't everything be represented with an integer?

Not all situations require an integer. Some require a true or false. Others require a float in that the decimal places need to be accounted for.

Thursday, December 4, 2014

Tweet
1.       How many characters are in this sentence? Does it matter whether Python is storing the string as one byte per character or four bytes per character?

41 characters. Yes becuase unicode uses four bytes per character, and ASCII uses one byte per character. unicode offers a wider range of characters.

2.      This question asks you about something you have not learned. In fact, the question is asking about details that go beyond what you will learn in this course. However, wondering what is going on at a lower level of abstraction – and talking about it – can be a useful strategy when learning about computing.

Describe what you think occurs in memory when the following code is executed.

In []: a = 'one string'
In []: b = 'another'
In []: c = a[:3] + ' and ' + b
In []: print(c[6:10])
a is stored, and b is stored. c is stored as 'one' from a, a new part; ' and ' and b. then the print command calls c, but only characters 6 through 10; "d an"