Simple Zombie program
I have created a simple zombie program to demostrate some basic OO principles. The key principles illustrated in this program are:
- class design
- constructor design
- constructor initilization
- mutator and access methods
- A main or driver class
- object instantiation
- object reference
- outputting object state
Here is the Zombie class
public class Zombie {
//public fields
public int speed;
public int damage;
public String name;
//constructor
public Zombie(int s, int d, String n){
speed = s;
damage = d;
name = n;
}
//accessors
public int get_speed(){
return this.speed;
}
public int get_damage(){
return this.damage;
}
public String get_name(){
return this.name;
}
//mutators
public void set_speed(int s){
this.speed = s;
}
public void set_damage(int d){
this.damage = d;
}
}//end class
Here is the Main or driver class
public class Main {
public static void main(String[] args) {
//create zombie instances
Zombie bob = new Zombie(5,3,”bob”);
Zombie fred = new Zombie(10,2,”fred”);
Zombie bill = new Zombie(15,1,”bill”);
//demonstrate the 3 zombies at play
System.out.println(“I am zombie ” + bob.get_name() + ” I run ” + bob.get_speed()
+ ” mph, if i hit you, I will do ” + bob.get_damage()+ ” damage.”);
System.out.println(“\n”);
System.out.println(“I am zombie ” + fred.get_name() + ” I run ” + fred.get_speed()
+ ” mph, if i hit you, I will do ” + fred.get_damage()+ ” damage.”);
System.out.println(“\n”);
System.out.println(“I am zombie ” + bill.get_name() + ” I run ” + bill.get_speed()
+ ” mph, if i hit you, I will do ” + bill.get_damage()+ ” damage.”);
}//end main
}//end class