Introduction to Software Development
GWU Computer Science
By the end of this module, you will be able to:
If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:
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:
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
Add the "default" constructor method to SimpleNode.java so that:
You will notice that we connected SimpleNode n2 to SimpleNode n1 by putting its address inside n1's next variable.
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 );
}
}
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;
}
}
What if we wanted to print all the nodes that get connected?
Let's modify printNodes together so that:
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.
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...