Thursday, February 26, 2015

Feb. 26th Warm-Up

Today we had to make a relatively simple program that takes a user input of a full name (first and last) and splits it into first and last name. Here's my code:


Wednesday, February 11, 2015

Head-First Java - Chapter 4 - What I Learned

There was a lot of information in Chapter 4, from parameters and arguments, to return values, to encapsulation, to instance and local variables. Here are a couple things I learned in particular:

  • Using the vocabulary "call" and "pass" refers to using a method within a particular reference object, and inputing a value where applicable ("passing" the value). For example:
    Dog d = new Dog();            //Makes new dog ref obj
    d.bark(3);                    //Tells it to bark 3 times

    void bark(int numOfBarks) {   //Defines bark function
        while (numOfBarks > 0) {  //3 is inserted into numOfBarks
            System.out.println("ruff");
            numOfBarks = numOfBarks - 1; //Bark will repeat
        }
    }
    • In the above example, a new reference object for Dog is created, and the value of 3 is passed in. Now, 3 replaces the "numOfBarks" int, causing the program to run and print out "ruff ruff ruff".
  • Whenever we use the void return type, it causes the particular method we're referring to to not return any values. We can set these methods to return anything, but we have to be sure to declare a specific return value and eventually return the correct value type.
  • Encapsulation is basically just a format used to prevent instant variables from being declared something ridiculous, like a cat's height being zero (theCat.height = 0;). In order to prevent something like this from happening, we just have to "encapsulate" the instance variables with setter methods. "Setters" are just methods that set instance variable values, and "getters" receive them. 
  • So, instead of theCat.height = 0 being possible, we have to create code like this:
    public void setHeight(int ht) {    //Height is set by "ht"
        if (ht > 9) {                  //Code only works if you set the height greater than 9
            height = ht;
        }
    }  
  • By doing this, it forces the input to be larger than 9, so we don't get a case of the Pancake Cat.
  • Lastly, this chapter hit upon the difference between instance and local variables.
    • In short, instance variables are declared within a class but NOT within a method, whereas local variables are specifically declared within a method itself.
    • Instance variables CAN be used without "initializing" them, or giving them a value. Depending on the type, this could output a 0, 0.0, false, or null.  
    • HOWEVER, local variables (declared in a method) can NOT function like this. It will output an error.
    • Local variables NEED values, whereas instance variables DON'T. For example, this code would run perfectly fine....
class compSciGrade {
    private int quarterTwoGrade;
    private int getGrade() {
        return quarterTwoGrade;
    }
}  
public class compSciGradeTestDrive {
    public static void main (String[] args) {
        compSciGrade q2 = new compSciGrade();
        System.out.println("Your Quarter 2 grade in CompSci is " + q2.getGrade() + ".");
    }
}
  • ...because the variable quarterTwoGrade is declared within a class but not a method, making it an instance variable. Since it is not given a value, the program will automatically assign it the value of 0, and the output would read "Your Quarter 2 grade in CompSci is 0." That's not passing, but at least it's not an error!
    • If the instance variable was moved into the method getGrade(), it wouldn't run properly. I guess you would be passing something - an error!