View Single Post
AsLan^
Not a tame lion...
 
Join Date: May 2004
Location: Narnia
 
2006-01-25, 22:33

Quote:
Originally Posted by chucker
And your two-class approach doesn't strike me as particularly object-oriented either. After all, my code is already pretty much split in two (a procedure that returns the results of the check, and a main block that handles the input).
Perhaps, the Palindrome example seems pointless, but it can be split up into two objects, but perhaps it is not obvious.

How about a zoo, similar to baseplayingmacfiends bookshelf with books.

For whatever reason, you want to create a zoo object, and that zoo object contains animals, the animals all have a type etc. and reflect the basic animal attributes.

Let's start with the animal class...

Code:
public class Animal { //these following class variables make up our Animal object String name; boolean hasBeenFed; public Animal() // our default constructor... creates primordial ooze { name = "Primordial Ooze - why dont you subclass Animal"; hasBeenFed = false; } public Animal(String aName, boolean fed) // another constructor, for creating an Animal with custom specs { name = aName; hasBeenFed = fed; } public String getInfo() // a method (similar to a function) that returns a String built from this animals instance variables { if (hasBeenFed) { return (name + "and it is not hungry."; } else { return (name + "and it is hungry !"; } }
Okay... now we have an animal.... let's create a zoo to put it (and it's friends) too.

Code:
public class Zoo { Animal animalArray[50]; // we want 50 animals in our zoo, stored in an array public Zoo() // a constructor method to create the zoo object. { //use a for loop to seed our array with animal objects for (int i = 0; i < animalArray.length; i++) { animalArray[i] = new Animal(); // the "new" keyword in java instantiates a new object, calling its constructor method } } public static void main(String[] args) // this is a standard line in java for defiing your "main" method in java, the "main" method is called automatically at runtime. { Zoo myZoo = new Zoo(); // creates a zoo object and calls its default constructor, we created that above... for (int i = 0; i < animalArray.length; i++) { System.out.println( animalArray[i].getInfo() ); } } }
Okay... there we have a Zoo object storing 50 Animal objects in an array, the only problem is all our animals are hungry Primordial Ooze, which isnt too interesting.

Notice to get the information string from the animal we call the .getInfo() method... this operates like a
function but its specific to the object where we defined it.

To liven up our zoo we should subclass Animal into the different kinds of animals we want. A subclassed Animal needs to be defined like any other class but it will "inherit" the methods from the parent...

Code:
public class Tiger extends Animal { public Tiger() { super("Tiger", true); // this call to super will call the constructor in the "superclass" (Animal) and specifically will use the constructor that is looking for a String and a boolean (the one we didnt use before) } //no need to re-write the getInfo method, the Tiger object has inherited it }
and perhaps a Lion object too...

Code:
public class Lion extends Animal { public Lion() { super("Lion", false); // someone forgot to feed the lion } }
So now we have two kinds of animal, and some Primordial Ooze, because they are all derived from the Animal superclass, I can keep them in the Animal array. So now I modify the constructor in my Zoo object...

Code:
public Zoo() // a constructor method to create the zoo object. { //use a for loop to seed our array with animal objects for (int i = 0; i < animalArray.length; i++) { if (i % 2 == 0) // This says if the remainder of i divided by 2 is equal to zero.... { animalArray[i] = new Tiger(); } else { animalArray[i] = new Lion(); } } }
Okay now... we have a zoo, with 25 tigers and 25 lions... super

How does this develop into larger applications with EventListeners and GUI's etc... well all the elements of a GUI have already been created and encapsulated into objects. By knowing the syntax of the language and basic OOP principles, you can instantiate and use these GUI objects (IDE's can obscure this somewhat.. best to do it by hand first).
  quote