During the last few weeks, we have been learning about arrays in Java. Following Mr. Palmer's latest and greatest dreams about this class, it was decided that we would complete various activities in order to achieve a merit badge with an oh so clever pun, with one of the activities being a blogpost portraying our knowledge of arrays. Therefore, prepare to be amazed by the ensuing bullet list of information which I have acquired via multiple sources.
* An array is a variable that has multiple values
* The declaration of an array first states its data type, using one of
the data type keywords, followed by " [] "to express that it will be an array variable
* An array can be initialized in its declaration by assigning values
of the appropriate data type, enclosed
within curly brackets
* Stored values are indexed starting at zero
* The size of an array is
determined by its declaration and cannot be changed later.
* The index number of the last element in an array will always be
one less than the array length
BryttonBurnsideAPComputerScience
Wednesday, April 8, 2015
Friday, February 27, 2015
Battleship
This is the Game Helper code.
This shows that there were separate programs inside the Battleship package.
This is the basic code that establishes the rules.
This is the code for the game portion.
This is just the test code.
These are the results of the game.
This code is Palmer Approved
Friday, February 6, 2015
Java Chapter Four
As these are the bullet points of Chapter Four, they highlight some of the more significant features and aspects of Java which are explained within the chapter. Since I have never done anything Java-related before APCompSci, everything I read in the chapter was basically new to me, thus teaching me some important topics which will no doubt help me later down the road in my use of Java. Some things I learned during this chapter that I think are important are encapsulation, the difference between local and instance variables, and how to use the equals sign to compare two entities.
The first thing I learned from Chapter Four was the term Encapsulation, which is used to protect instance variables from being wrongfully changed. To protect the variable, the "private" and "public" access modifiers are used in order to differentiate between code which may be altered by anyone, and code which can only be altered by the creator. With encapsulation, it is best if the instance variable is made "private" while the getter and setter methods are made "public" in order to ensure code security while maintaining efficiency. Encapsulation is important to understand and utilize because it helps protect the programmer's code from other people altering it in unwanted fashion, which would have the potential of destroying the entire program. Thus, it is in the programmers best interest to use encapsulation.
Another thing I learned was the difference between instance and local variables, and how they are treated differently by the compiler. Instance variables are declared inside a class but outside a method, whereas local variables are located within methods. Also, instance variables have default values in the case that the variable is not explicitly assigned a specific value. For example, if the line were "private int size;" there obviously is not a specific value assigned to "size", thus the compiler will assign it with a zero. In the case of an unassigned string, the compiler will assign the variable with "null", while the variable will be "false" if it is a boolean. As for local variables, if they are not initially assigned a value, the compiler returns with an error because they do not have default values. This is important because whereas if the programmer forgot to give an instance variable a value, the compiler would assign it a default value, if the programmer forgot a value for a local variable, the compiler would return with errors. Thus, knowing the difference between the two types of variables can help to alleviate certain problems, save time, and avoid unnecessary errors.
As is seen in the above image, double equal sign is used to compare two primitives or two reference variables, while the equals() method is used to compare to different objects. For example, if you wanted to compare variables "a" and "b" to see if they are indeed equal to each other, you would use two equal signs like so: "a == b". If you were to check and see if two different objects "a" and "3", you would use the equals() method. For example, it may look like this: "if(test1.equals(test2))". This is important to understand as it helps the programmer more easily compare variables and objects, thus saving time and stress.
This is the dog code which appears at the beginning of the chapter. I compiled the code and then added comments to aid in the explanation of what the program is actually doing. It was really the only substantial code in the chapter which did not already have annotations with it, so that is why I included it.
This is the online compiler I used for the dog code, as I wasn't using the school computers.
Thursday, January 29, 2015
Our Lord and Savior, JDR
This code ensures that everybody knows that John D. Randolph is our true hero, as He guides us to the light. He acts as the perfect role model, thus having the life which is the ultimate goal of every follower of the Randolph faith.
Wednesday, January 21, 2015
Java Chapter Three
I learned what this piece of code does, thanks to chapter three. The first half establishes a reference variable named dog1 that is of the Dog type. The second half creates a new dog object and adds it to the "heap". To link the object to the reference, thus assigning the new Dog to the reference variable dog1, the equal sign is used.
dog1.bark();
After the previous line of code, this line ensues, as it give dog1 an action to perform. the first half of the code is the reference variable, while the second half is the action. Basically, the dot operator is used to explain that you use the thing before the dot to do the thing stated after the dot. Thus, this line of code means to use dog1 to invoke the bark method.
With Primitive Variables, the value of the variable is the value, such as 5, -27.9, or 'a'.
With Reference Variables, the value of the variable is the bits representing a way to get to a specific object.
This is a dog.
Friday, January 16, 2015
Java Chapter Two
On thing I learned was that instance variables are things an object knows about itself; they represent an object's state, and can have unique values for each object of that type.
Another thing I learned was that methods are things an object can do.
This code creates 3 objects which each have the title, genre, and rating of 3 separate movies. In this code, the dot operator is used to set instance variables to a specific value. As is seen below, this is what the output of the code would be.
Monday, January 12, 2015
Java Chapter One
This is the code to the Beer Song program, which has the job of reciting the entire "Beer Song" from 99 bottles to zero bottles. We were given the code, however there was one error which caused the song to say "1 bottles of beer on the wall" instead of the grammatically correct "1 bottle...". Thus, my task was to fix the code in order to make it grammatically sound. In order to achieve such a goal, I placed the "if (beerNum == 1)" chunk of code under the prints because now the code will notice that the number of beers left is 1, making it say bottle instead of bottles.
This is the PhraseOMatic program, which is meant to choose one random word from each of the three lists in order to say a phrase such as, "All we need is a John D Randolph". This is my favorite possible phrase because John D Randolph is my hero. In order to create the phrases, the program determines the number of words in each list and then chooses a random integer which represents the place value of the word in the list. Thus, it would choose the third, first, and fifth words in the first, second, and third list respectively.
Subscribe to:
Comments (Atom)





