GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 21: Even More 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:




Zombies. Objectified Zombies. Yeeeees.

Zombies...

Humans...

Activity 1 [Group Work]:
  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...




OO Design

Why does Object-Oriented Programming (OOP) exist?

Activity 2 [Group Work]: 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. Activity 3 [Group Work]:

Take your design from the previous Exercise.

Redesign your objects so that many of them can be reused between games

Other factors to consider: