Wednesday, January 28, 2015

Headfirst Java Chapter 3

  In this chapter I learned about primitive and reference variables. Primitive variables values are the bits representing the value. While a reference variable value is the bits representing a way to get to an object on the heap. A reference variable is null when not assigned an object. Also I learned about creating an array of variables and how it is always an object.

Dog Code:
class Dog {
    String name;
    public static void main (String[] args) {
        //make a Dog object and access it
        Dog dog1 = new Dog();
        dog1.bark();
        dog1.name = "Bart";

        //now make a Dog array
        Dog[] myDogs = new Dog[3];
        //and put some dogs in it
        myDogs[0] = new Dog();
        myDogs[1] = new Dog();
        myDogs[2] = dog1;

        //now access the Dogs using the array references
        myDogs[0].name = "Fred";
        myDogs[1].name = "Marge";

        //Hmmm... what is myDog[2] name?
        System.out.print("last dog's name is ");
        System.out.println("myDogs[2].name);

        //new loop through the array, and tell all dogs to bark
        int x = 0;
        while(x < myDogs.length) {
            myDogs[x].bark();
            x = x + 1;
        }
    }

    public void bark() {
        System.out.println(name + " says Ruff!");
    }

    public void cat();
    public void chaseCat();

Tuesday, January 20, 2015

Headfirst Java Chapter 2

Some things that i learned/have to say about chapter 2:
  • This chapter helped me understand the basics of Object Oriented Programming (OOP).
  • I learned that with OOP I don't have to back and edit old code.
  • A class defines all Java code.
  • A class can be related to a blueprint in terms of what it does.
  • Things that an object knows are called instance variables.
  • Methods are what an object does.
  • Classes can take instance variables from a super class and use them. This is defined as inheriting.

Tuesday, January 13, 2015

Headfirst Java Chapter 1

99 Bottles of Beer Code

At first the code printed everything right until the end. At the end it printed "1 bottles of beer on the wall", which is gramatically incorrect. I fixed it so it prints "1 bottles of beer on the wall". I added an if statement that makes "bottles" into "bottle" if beernum is equal to 1. This was put inbetween the "System.out" code and the "No more bottles of beer on the wall" code so it worked.