By the end of this module, you will be able to:
  
 
 
 Objects:
 Where does this leave classes?
Objectives
 
        
   
 
        
Objects and Memory Layout
 
        
  
Take the example of the Facebook user class:
public class FBUser {
    private long userId;
    private String firstName, lastName;
    private FBUser[] friends;
    
    public void setId(long id) {
        userId = id;
    }
    public long getId() {
        return userId;
    }
}
The class gives the blueprint for each object:
In-Class Exercise 1: Why do we need
  objects?
   What function do objects provide compared to variables,
  arrays, etc?  How do they relate to static variables stored in
    classes?
In-Class Exercise 2: Given the following
  code that you can add to the FBUser
  class:
 We're trying to invoke the getId method just like we've called
  static methods in the past.  What do you think will happen when you
  compile your code?  When you run it?  Try!
    public static void main(String[] args) {
        long id = FBUser.getId();
        System.out.println(id);
    }
Multiple objects exist, each with possibly different sets of data. Objects are allocated with new.
    public static void main(String[] args) {
        FBUser user1 = new FBUser();
        FBUser user2 = new FBUser();
        
        user1.setId(5);
        System.out.println(user1.getId() + " " + user2.getId());
    }
In-Class Exercise 3: What does the above print out? Why?
Methods in a class can operate on an object using the dot notation. If var is a variable referring to an object,
      var.methodName();
invokes the methodName method on var's object.
Read this as "we're invoking the methodName method of the var object"
When an object is allocated with new, e.g.:
    A var = new A();
A reference to the new object's memory is returned.
The variable (var here) is set to this reference.
This should feel familiar -- its the same as with arrays.
The type of the variable is the name of the class:
    A var = new A();
The variable itself:
    A var = new A();
A slightly more complicated example:
    FBUser var1 = new FBUser();
    FBUser var2 = var1;
    var2.setId(10);
    System.out.println(var1.getId());
 
In-Class Exercise 4: First, write a new class to use your FBUser class.
Second, fill in the FBUser class with other appropriate methods, and use them from your other class. Create a few users, and populate their friend lists.
public class FBUser {
    private long userId;
    private String firstName, lastName;
    private FBUser[] friends;
    
    public void setId(long id) {
        userId = id;
    }
    public long getId() {
        return userId;
    }
}
An even more complicated example:
public class ObjExample {
    private int data;
    public void modify(int v) {
        data = v;
    }
    public int access() {
        return data;
    }
    public void addData(ObjExample o) {
        data += o.access();
    }
    public static void main(String[] args) {
        ObjExample v1 = new ObjExample();
        ObjExample v2 = v1;
        v2.modify(10);
        System.out.println(v1.access());
        ObjExample v3 = new ObjExample();
        v3.modify(3);
        v3.addData(v2);
        System.out.println(v3.access());
        v3.addData(v3);  // what's happening here?
        v1.addData(v2);  // ...and here?
    }
}
In-Class Exercise 5: What does the above
  print out?  Why?
   What are the last two lines doing?  Describe them
We can now better understand the static keyword.
static data is global, and is shared between all objects of a class.
static methods are not associated with any specific object, thus none of the non-static data can be directly accessed within one. These methods can access and modify comparably static data in the class.
Non-static data has a separate copy for each object.
Non-static methods can access the global non-static data in the class. They access and modify versions of that data specific to the current object.
public class StaticExample {
    private static int staticVar;
    private int objectVar;
    public void objSetVars() {
        objectVar = 10;
        staticSetVars();
    }
    public int[] objGetVars() {
        return new int[]{staticvar, objectVar}; 
    }
    public static void staticSetVars() {
        staticVar = 10;    
        objSetVars();
    }
    public static int[] staticGetVars() {
        return new int[]{staticvar, objectVar}; 
    }
    public static void main(String[] args) {
        staticSetVars();
        objSetVars();
        StaticExample v = new StaticExample();
        v.staticSetVars();
        v.objSetVars();
    }
}
 
 null is a valid value for a variable of
  any non-primitive type.  This includes: objects arrays Strings null is simply a reference to nothing 
In-Class Exercise 7: Why
  does null exist?
 
 null is often used to denote "there is
	no valid object". It is sometimes returned when an object is expected to denote
	an error. Example: What if you try and lookup a facebook user that
	doesn't exist -- FBUser findUser(String first, String
	last)?
Null Objects
 
        
  
 
         
        
    
public class NullTest {
    private int n;
    public int getN() {
        return n;
    }
    public static void main(String[] args) {
        NullTest nt = null;
        System.out.println(nt.getN());
    }
}
In-Class Exercise 8: Execute this program. What happens when this program is executed? Why?
In-Class Exercise 9: What is the default
  value for variables of a class's type?  Write a small program to
  print out the default value of the variable:
   FBUser u;
 
 If a class gives the blueprint to create
  objects, constructors lay the initial foundation for each
  object. Constructors are methods that are invoked when a new object is
  created that are used to initialize the data of the new object. FBUser u = new FBUser();
Constructors
 
        
The constructor can be overloaded just like any other method:
FBUser u = new FBUser("chester", "thecat");
What does a constructor look like? An example:
public class FBUser {
    private long userId;
    private String firstName, lastName;
    private FBUser[] friends;
    public FBUser() {
        // initialize object's data here
    }
    public FBUser(String first, String last) {
        firstName = first;
        lastName = last;
    }
    public void setId(long id) {
        userId = id;
    }
    public long getId() {
        return userId;
    }
}
In-Class Exercise 10: Add a print
  statement to the constructor to see when it is invoked.  
   Initialize the data in the constructor, and then use a method to
  access the data, and verify it was initialized correctly.
 
 Where is the problem in the following code?  
 
Classy Scope
 
        
public class FBUser {
    private long userId;
    private String firstName, lastName;
    private FBUser[] friends;
    public FBUser(String firstName, String lastName) {
        // uh oh...
    }
    public void setId(long id) {
        userId = id;
    }
    public long getId() {
        return userId;
    }
}
  
 
        
public class FBUser {
    private long userId;
    private String firstName, lastName;
    private FBUser[] friends;
    public FBUser(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public void setId(long id) {
        userId = id;
    }
    public long getId() {
        return userId;
    }
}
  
this is, generically, a variable that references the "current" object.
If a method takes a FBUser argument, or a variable holds a FBUser, this can be used.
public class FBUser {
    private long userId;
    private String firstName, lastName;
    private FBUser[] friends;
    public FBUser() {
        friends    = new FBUser[10];
        friends[0] = this;
    }
}
In-Class Exercise 11: Add a method
  to your FBUser class:
   void addFriend(FBUser friend) However, add a check that will prevent a FBUser from adding
    itself to its own friend list.  In the main, this undesirable
    action might be done with: 
	FBUser user = new FBUser();
	user.addFriend(user);
In-Class Exercise 12: Add a method
  to your FBUser class:
   void addAsFriend(FBUser otherUser) This method adds the current FBUser as a friend in
  the otherUser.
 
 Zombies... Eat brains of humans. Turn bit humans into zombies. Can be killed by humans. Humans... Shoot zombies in the brain (so much with the brains!). Fight other humans, and might kill them. (Why so mean?) Are turned into zombies when bit. Have names. 
In-Class Exercise 13: 
  
 
 
 Why does Object-Oriented Programming (OOP) exist? Why is it useful?  Why use OOP instead of making all code and
      data static? Come up with two examples where it has utility. 
In-Class Exercise 14: Imagine you wanted
  to write a card game platform to support playing many different card
  games.
   Design a set of classes and objects to describe cards for the
  card game "War".  Write "prototype" classes that include the
  classes, data in the classes, and methods, but do not provide an
  implementation of the methods.
     
A fundamental goal of object oriented design is reuse.
 Code written for one situation should be, within reason,
      reusable in other situations Writing fairly generic classes enables this. It is important to design your classes before writing
      your code.  I've been doing this for you in homeworks. 
In-Class Exercise 15:  Take your design
  from the previous Exercise.   Redesign your objects so that many of them can be reused between
  games Other factors to consider:
    
Zombies.  Objectified Zombies.  Yeeeees.
 
        
  
  
 
        
  
Your job is to define a set of methods and data for the Person class
that will tell a story when each of the humans and zombies take
actions.  All of your data in the Person class must be private.  Be
creative!!!  A possible story line follows:
Person joe  = new Person("Joe");
Person bob  = new Person("Bob");
Person jane = new Person("Jane");
Person z    = new Person();
System.out.println(Person.numHumans() + " humans vs. " + Person.numZombies() + " zombies.");
z.bite(bob); // poor bob
bob.bite(z); // confused zombie
Person derpbie = bob;
derpbie.bite(derpbie); // this is getting ridiculous
System.out.println(Person.numHumans() + " humans vs. " + Person.numZombies() + " zombies.");
jane.shoot(z);    // humans FTW!
jane.shoot(joe);  // wait...what?
jane.shoot(jane); // oh...oh no.
System.out.println(Person.numHumans() + " humans vs. " + Person.numZombies() + " zombies.");
while (true) {
    derpbie.bite(derpbie); // bored zombie is bored
}
Joe, ready for action!
Bob, ready for action!
Jane, ready for action!
...mmmm, brains.  Oh noes, a zombie!
3 humans vs. 1 zombies.
Bob is gnawed on by a zombie.
The zombie formerly known as Bob bites a zombie, like an idiot.
The zombie formerly known as Bob is hungry...and remembers he has an arm.  Nomnomnomnom.
...etc...
 
        
OO Design