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.