//******************************************************************** // Vehicle.java // // Represents a vehicle. //******************************************************************** class Vehicle { protected int maxSpeed; //---------------------------------------------------------------- // Sets up the vehicle with the specified maximum speed. //---------------------------------------------------------------- public Vehicle (int maxSpeed) { this.maxSpeed = maxSpeed; } //---------------------------------------------------------------- // Prints a message concerning the maximum speed of this vehicle. //---------------------------------------------------------------- public void travelSpeed () { System.out.println ("Maximum speed is: " + maxSpeed + " km/h"); } } //******************************************************************** // Bus.java // // Represents a Bus, which is a vehicle. //******************************************************************** class Bus extends Vehicle { private int numWheels; //----------------------------------------------------------------- // Sets up the bus with the specified maximum speed // (maintained by the Vehicle parent class) and number of wheels. //----------------------------------------------------------------- public Bus (int speed, int numWheels) { super (speed); this.numWheels = numWheels; } //----------------------------------------------------------------- // Prints a message using both local and inherited values. //----------------------------------------------------------------- public void minimumTime (int km) { System.out.print("Minimum time to cover " + km + " kilometers "); System.out.print("with all " + numWheels + " wheels "); System.out.println("is " + ((km/maxSpeed)+1) + " hours."); } } //******************************************************************** // ABusIsAVehicle.java // // Demonstrates the use of an inherited method, the super reference // and the protected modifier. //******************************************************************** public class ABusIsAVehicle { //----------------------------------------------------------------- // Instantiates a derived class and invokes its inherited and // local methods. //----------------------------------------------------------------- public static void main (String[] args) { Bus luckyBus = new Bus (100,6); luckyBus.travelSpeed(); luckyBus.minimumTime(380); } } //******************************************************************** // PastVsPresent.java // // Demonstrates the use of an overridden method. //******************************************************************** class PastVsPresent { //----------------------------------------------------------------- // Instatiates two objects and invokes the message method in each. //----------------------------------------------------------------- public static void main (String[] args) { Past outdated = new Past(); Present modern = new Present(); System.out.println ("*** The idiots used to believe anything: ***"); outdated.message (); System.out.println ("\n*** The human race has always known how to make progress: ***"); modern.message (); // overridden } } //******************************************************************** // Past.java // // Represents various past beliefs. //******************************************************************** class Past { //----------------------------------------------------------------- // Prints a message. //----------------------------------------------------------------- public void message() { System.out.println ("2000 B.C. - Here, eat this root."); System.out.println ("1000 A.D. - That root is heathen. Here, say this prayer."); System.out.println ("1850 A.D. - That prayer is superstition. Here, drink this potion."); System.out.println ("1940 A.D. - That potion is snake oil. Here, swallow this pill."); System.out.println ("1985 A.D. - That pill is ineffective. Here, take this antibiotic."); } } //******************************************************************** // Present.java // // Represents a modern belief. //******************************************************************** class Present extends Past { //----------------------------------------------------------------- // Prints a message. This method overrides the parent's version. // It also invokes the parent's version explicitly using super. //----------------------------------------------------------------- public void message() { super.message(); System.out.println ("2005 A.D. - That antibiotic is artificial. Here, eat this root."); } } //******************************************************************** // Student.java Author: Lewis and Loftus // // Represents a student. //******************************************************************** class Student { protected String name; protected int numCourses; //----------------------------------------------------------------- // Sets up a student with the specified name and number of // courses. //----------------------------------------------------------------- public Student (String name, int numCourses) { this.name = name; this.numCourses = numCourses; } //----------------------------------------------------------------- // Returns information about this student as a string. //----------------------------------------------------------------- public String toString () { String result = "Student name: " + name + "\n"; result += "Number of courses: " + numCourses; return result; } } //******************************************************************** // GradStudent.java Lewis and Loftus // // Represents a graduate student, with financial support. //******************************************************************** class GradStudent extends Student { private String source; private double rate; //----------------------------------------------------------------- // Sets up the graduate student using the specified information. //----------------------------------------------------------------- public GradStudent (String name, int numCourses, String source, double rate) { super (name, numCourses); this.source = source; this.rate = rate; } //----------------------------------------------------------------- // Returns a description of this graduate student as a string. //----------------------------------------------------------------- public String toString () { String result = super.toString(); result += "\nSupport source: " + source + "\n"; result += "Hourly pay rate: " + rate; return result; } } //******************************************************************** // Academia.java Author: Lewis and Loftus // // Demonstrates inheritance from the Object class. //******************************************************************** class Academia { //----------------------------------------------------------------- // Creates objects of two student types, prints some information // about them, then checks them for equality. //----------------------------------------------------------------- public static void main (String[] args) { Student susan = new Student ("Susan", 5); GradStudent frank = new GradStudent ("Frank", 3, "GTA", 8.75); System.out.println (susan); System.out.println (); System.out.println (frank); System.out.println (); if (! susan.equals(frank)) System.out.println ("These are two different students."); } } //******************************************************************** // FoodItem.java // // Demonstrates indirect referencing through inheritance. //******************************************************************** class FoodItem { final private int CALORIES_PER_GRAM = 4; private int proteinGrams; protected int servings; //----------------------------------------------------------------- // Sets up this food item with the specified number of protein grams // and number of servings. //----------------------------------------------------------------- public FoodItem (int proteinGrams, int servings) { this.proteinGrams = proteinGrams; this.servings = servings; } //----------------------------------------------------------------- // Computes and returns the number of calories in this food item // due to proteins. //----------------------------------------------------------------- private int calories () { return proteinGrams * CALORIES_PER_GRAM; } //----------------------------------------------------------------- // Computes and returns the number of protein calories per serving. //----------------------------------------------------------------- public int caloriesPerServing () { return (calories() / servings); } } //******************************************************************** // EarthWorm.java // // Demonstrates indirect referencing through inheritance. //******************************************************************** class EarthWorm extends FoodItem { //----------------------------------------------------------------- // Sets up an earthWorm which is about 70% protein; assumes // one servings. //----------------------------------------------------------------- public EarthWorm (int weightInGrams) { super ((int)(weightInGrams*0.7), 1); } } //******************************************************************** // FoodAnalysis.java // // Demonstrates indirect referencing through inheritance. //******************************************************************** class FoodAnalysis { //----------------------------------------------------------------- // Instantiates an EarthWorm object and prints its calories per // serving. //----------------------------------------------------------------- public static void main (String[] args) { EarthWorm special = new EarthWorm (10); System.out.println ("A nice BIG worm will give you " + special.caloriesPerServing() + " calories per serving."); } } //******************************************************************** // Animal.java // // Represents a generic animal. //******************************************************************** abstract class Animal { protected String name; protected double weight; //----------------------------------------------------------------- // Sets up an animal using the specified information. //----------------------------------------------------------------- public Animal (String name, double weight) { this.name = name; this.weight = weight; } //----------------------------------------------------------------- // Returns a string including the basic animal information. //----------------------------------------------------------------- public String toString () { String result = "Name: " + name + "\n"; result += "Weight: " + weight; return result; } //----------------------------------------------------------------- // Derived classes MUST define the feed method for each animal // type. //----------------------------------------------------------------- public abstract double feed(Prey menu); } //******************************************************************** // Prey.java // // Represents an animal that is a prey. //******************************************************************** class Prey extends Animal { //----------------------------------------------------------------- // Sets up a prey using the specified information. //----------------------------------------------------------------- public Prey (String name, double weight) { super (name, weight); } //----------------------------------------------------------------- // Returns amount eaten from body (max between weight & kilos) //----------------------------------------------------------------- public double takeABite (double kilos) { double amount = weight; if (kilos < weight) amount = kilos; weight -= amount; return amount; } //----------------------------------------------------------------- // Eats 10% of its weight of grass, whatever the prey. //----------------------------------------------------------------- public double feed(Prey todaysMenu) { double amount = (weight*10/100); weight += amount; return amount; } } //******************************************************************** // Predator.java // // Represents an animal that is a predator. //******************************************************************** class Predator extends Animal { protected String preys; protected double appetite; //----------------------------------------------------------------- // Sets up a predator with the specified information. //----------------------------------------------------------------- public Predator (String name, double weight, String preys, double appetite) { super (name, weight); this.preys = preys; this.appetite = appetite; } //----------------------------------------------------------------- // Returns information about a predator as a string. //----------------------------------------------------------------- public String toString () { String result = super.toString(); result += "\nPreys: " + preys; return result; } //----------------------------------------------------------------- // Eats appetite kg of the prey and returns that amount. //----------------------------------------------------------------- public double feed(Prey todaysMenu) { double eaten = todaysMenu.takeABite(appetite); weight += eaten; return eaten; } } //******************************************************************** // Lion.java // // Represents a Lion, a predator who can take // the time to eat a little extra if it feels like it. //******************************************************************** class Lion extends Predator { private double extra; //----------------------------------------------------------------- // Sets up a Lion with the specified information. //----------------------------------------------------------------- public Lion (String name, double weight, String preys, double appetite) { super (name, weight, preys, appetite); extra = 0; // extra has yet to be decided } //----------------------------------------------------------------- // Specifies how much this Lion can eat extra. //----------------------------------------------------------------- public void extraHungry (double hungry) { extra = hungry; } //----------------------------------------------------------------- // Try to eat appetite+extra kg of todaysMenu, extra gets reset to 0 // only if the Lion has eaten at least as much its appetite. //----------------------------------------------------------------- public double feed (Prey todaysMenu) { appetite += extra; double amount = super.feed(todaysMenu); appetite -= extra; if (amount >= appetite) extra = 0; return amount; } } //******************************************************************** // Hyena.java // // Represents a Hyena, a predator who can rarely take // the time to eat as much as it wants to if it is a coward. //******************************************************************** class Hyena extends Predator { private boolean isCoward; //----------------------------------------------------------------- // Sets up a Hyena using the specified information. //----------------------------------------------------------------- public Hyena (String name, double weight, String preys, double appetite, boolean coward) { super (name, weight, preys, appetite); isCoward = coward; } //----------------------------------------------------------------- // The calling laughter associated with having found food //----------------------------------------------------------------- public void foundFood() { System.out.println("\nHeeaaHeeaaHeeeHeee..."); } //----------------------------------------------------------------- // Try to eat some food, if this hyena is a coward, only eat half // its appetite and run away, if successful in eating food, call // the rest of the clan. //----------------------------------------------------------------- public double feed (Prey todaysMenu) { if (isCoward) appetite /= 2; double amount = super.feed(todaysMenu); if (isCoward) appetite *= 2; if (amount>0) foundFood(); return amount; } //----------------------------------------------------------------- // Overrides the parent method to print the specific Hyena info //----------------------------------------------------------------- public String toString() { String result = super.toString() + "\n"; result += (isCoward)? "Time to split!":"You wanna start something?"; return result; } } //******************************************************************** // Fauna.java // // Represents a couple of animals in a particular area //******************************************************************** class Fauna { Animal[] animalList; //----------------------------------------------------------------- // Sets up the list of animals. //----------------------------------------------------------------- public Fauna () { animalList = new Animal[6]; animalList[0] = new Lion ("Simba", 220, "Wildebeests, zebras, buffaloes, young elephants, rhinos, hippos, giraffes, warthogs, antelopes, hyenas, humans and even crocodiles",15.0); animalList[1] = new Predator ("Mamba",400,"Fish, antelopes, buffaloes, Wildebeests, zebras, and humans", 30.0); animalList[2] = new Predator ("Chui",60,"Baboons, zebras, hares, warthogs, antelopes and humans",15.0); animalList[3] = new Hyena ("Fisi", 80, "Wildebeests, humans, zebras, buffaloes, warthogs, antelopes, cheetahs, hyenas and almost anything", 8.0, true); animalList[4] = new Prey ("Digidigi",4); animalList[5] = new Prey ("Twiga",1350); ((Lion)animalList[0]).extraHungry (8.0); } //----------------------------------------------------------------- // Feeds all animals. //----------------------------------------------------------------- public void feedingTime () { double amount; Prey professor = new Prey ("Yannick",66); for (int count=0; count < animalList.length; count++) { System.out.println (animalList[count]); // polymorphic amount = animalList[count].feed(professor); // polymorphic System.out.println ("Fed: " + amount); System.out.println ("-----------------------------------"); } } } //******************************************************************** // TheBush.java // // Demonstrates polymorphic processing. //******************************************************************** class TheBush { //----------------------------------------------------------------- // Creates a bunch of animals, and feeds them. //----------------------------------------------------------------- public static void main (String[] args) { Fauna wildlife = new Fauna(); wildlife.feedingTime(); } }