Friday, February 13, 2015

Chapter 5

For a while, I've been working on a little project called "Infinite Mario Bros". Looks pretty good so far.
Some of Chapter 5's content was actually surprisingly informative and helpful.
Consider this bit of code-
I made this before I discovered that there were two formats for for loops;
for (int i=0; i < Main.mainPanel.Enemies.size(); i++) { Main.mainPanel.enemies.get(i) }
for (Enemy test : Main.mainPanel.enemies) { test }

Using the new for loop, my code is easier to modify and looks much cleaner overall, because instead of typing "Main.mainPanel.enemies.get(i)" each time, i can just type "test".







And if you're wondering what this code does, it goes through a list of enemies, checks if they're overlapping with Mario, and if he's falling, change their state. In other words...
Goomba goes squish.

Tuesday, February 3, 2015

Chapter 4

In Chapter 4 of Headfirst Java, I learned about Instance and Local variables, and how they act. Instance variables are declared as part of the class and have default values, but Local variables are defined within a method and have no initial value. For example...

public class Dog{
    public int size;
    public void printSize() {
        System.out.print(size);
    }
}

Calling the printSize() method on this Dog class will return 0, since the default value of ints are 0. However, you can't do this...

public class Dog{
    public void printSize() {
        int size;
        System.out.print(size);
    }
}

This code won't compile because the size variabe is Local and has no default value.

I also learned that there are two different ways to compare variables, as I discovered in the cat simulator program. The == operator  compares variables on a binary level, meaning the variables have to be exactly the same values and be in the same "heap", whereas you can use .equals() to compare similar objects from different heaps (such as strings with the same characters).