GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 20: Working with Classes and Object-Oriented Design


Objectives

By the end of this module, you will be able to:




Before Starting

If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:




Constructors

Last class, we started talking about constructors. They help us do "setting up work" for each of the objects we create.
When we instanctiate an object, the steps followed are:

  1. The new operator instantiates a class by allocating memory for a new object of that type. new requires a single argument: a call to a constructor method.
  2. The new operator creates the object
  3. The constructor initializes it.

Let's work with the following two classes: SimpleNode.java and SimpleNodeApp.java

public class SimpleNode {
  private static int numNodes = 0;
  private int nodeId;
  private int data;
  private SimpleNode next;
  
  // Init: Constructor section:
  
  // Your code here
  
  // End: Constructor section

  public void setData(int num) {
      data = num;
  }
  public int getData() {
      return data;
  }
  
  public void setNext(SimpleNode n) {
      next = n;
  }
  
  public SimpleNode getNext() {
      return next;
  }
  
  public void printNodes(){
    System.out.printf("node: %3d -> data: %3d \n", this.nodeId, this.data );
  }
  
}


and:

public class SimpleNodeApp {
  public static void main(String[] args) {
    SimpleNode n1 = new SimpleNode();
    SimpleNode n2 = new SimpleNode();

    n1.setData(11);
    n2.setData(22);

    n1.setNext(n2);
    n1.printNodes();
    n2.printNodes();
  }
}


In order to visualize the interaction of SimpleNode.java and SimpleNodeApp.java in the Java Visualizer, we will momentarilly transfer the main from SimpleNodeApp.java into SimpleNode.java and run it in a single file (this is so the Java Visualizer can run with a single file).
test it here: SimpleNode Test 1

Run over each step and notice the order of creation for the objects when we don't have constructors.
Activity 1 [Group work] (3-minutes):

Add the "default" constructor method to SimpleNode.java so that:

  1. it assigns a nodeId to the object equal to the current value of numNodes
  2. it increments numNodes

Check that the printout now has different nodeId's for each node.


Connecting objects

You will notice that we connected SimpleNode n2 to SimpleNode n1 by putting its address inside n1's next variable.




Classy Scope

Where is the problem in the following code?

public class SimpleNode {
  private static int numNodes = 0;
  private int nodeId;
  private int data;
  private SimpleNode next;
  
  public SimpleNode(){
    // default initialization
  }

  public SimpleNode(int data, SimpleNode next){
    data = data;
    next = next;
  }

  public void setData(int num) {
      data = num;
  }
  public int getData() {
      return data;
  }
  
  public void setNext(SimpleNode n) {
      next = n;
  }
  
  public SimpleNode getNext() {
      return next;
  }
  
  public void printNodes(){
    System.out.printf("node: %3d -> data: %3d \n", nodeId, data );
  }
  
}

Scope works the same with an object's variable as it did for global variables:

public class SimpleNode {
  private static int numNodes = 0;
  private int nodeId;
  private int data;
  private SimpleNode next;
  
  public SimpleNode(){
    // default initialization
  }

  public SimpleNode(int data, SimpleNode next){
    this.data = data;
    this.next = next;
  }

  public void setData(int num) {
      data = num;
  }
  public int getData() {
      return data;
  }
  
  public void setNext(SimpleNode n) {
      next = n;
  }
  
  public SimpleNode getNext() {
      return next;
  }
  
  public void printNodes(){
    System.out.printf("node: %3d -> data: %3d \n", nodeId, data );
  }
  
}

this is, generically, a variable that references the "current" object.

If a method takes a SimpleNode argument, or a variable holds a SimpleNode, this can be used.

public class SimpleNode {
  private static int numNodes = 0;
  private int nodeId;
  private int data;
  private SimpleNode next;
  
  public SimpleNode(){
    // default initialization
    next = this;
  }
}



An extra level of fancy

What if we wanted to print all the nodes that get connected?
Let's modify printNodes together so that:

  1. it prints the ccurrent node
  2. checks to see if the next node is not null or equal to the current one
  3. if it is different, we call the next node's printNode method.

Activity 2 [Class Participation] (3-minutes): Make the printNodes method print and call the next node's printNode method.:




Zombies. Objectified Zombies. Yeeeees.

Zombies...

Humans...

Activity 3 [Class Participation] (5-minutes):
  1. Make a Person class, and a ZombiesMain class. The latter has the main that will use the Person class.
  2. Each Person object is either a human or a zombie.
  3. If a Person object is a human, it has a name.
  4. Humans can shoot Person objects.
  5. Zombies can bite humans, which turns them into a zombie.
  6. Person objects created with constructors with no arguments are zombies.
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
}

An example execution of this program would be:
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...