Further abstraction techniques Further abstraction techniques
Abstract classes and interfaces
1.0
Further abstraction techniques Further abstraction techniques - - PowerPoint PPT Presentation
Further abstraction techniques Further abstraction techniques Abstract classes and interfaces 1.0 Main concepts to be covered Main concepts to be covered Abstract classes Interfaces Multiple inheritance 01/12/2005 Lecture 9:
1.0
01/12/2005 Lecture 9: Abstraction Techniques 2
01/12/2005 Lecture 9: Abstraction Techniques 3
01/12/2005 Lecture 9: Abstraction Techniques 4
01/12/2005 Lecture 9: Abstraction Techniques 5
01/12/2005 Lecture 9: Abstraction Techniques 6
01/12/2005 Lecture 9: Abstraction Techniques 7
01/12/2005 Lecture 9: Abstraction Techniques 8
01/12/2005 Lecture 9: Abstraction Techniques 9
01/12/2005 Lecture 9: Abstraction Techniques 10
01/12/2005 Lecture 9: Abstraction Techniques 11
// Characteristics shared by all rabbits (static fields). // The age at which a rabbit can start to breed. private static final int BREEDING_AGE = 5; // The age to which a rabbit can live. private static final int MAX_AGE = 50; // The likelihood of a rabbit breeding. private static final double BREEDING_PROBABILITY = 0.15; // The maximum number of births. private static final int MAX_LITTER_SIZE = 5; // A shared random number generator to control breeding. private static final Random rand = new Random();
01/12/2005 Lecture 9: Abstraction Techniques 12
// Individual characteristics (instance fields). // The rabbit's age. private int age; // Whether the rabbit is alive or not. private boolean alive; // The rabbit's position private Location location;
01/12/2005 Lecture 9: Abstraction Techniques 13
01/12/2005 Lecture 9: Abstraction Techniques 14
public Rabbit(boolean randomAge){…} public void run(Field updatedField, List newRabbits) { incrementAge(); if(alive) { int births = breed(); for(int b = 0; b < births; b++) { Rabbit newRabbit = new Rabbit(false); newRabbits.add(newRabbit); Location loc = updatedField.randomAdjacentLocation(location); newRabbit.setLocation(loc); updatedField.place(newRabbit, loc); }
01/12/2005 Lecture 9: Abstraction Techniques 15
Location newLocation = updatedField.freeAdjacentLocation(location); // Only transfer to the updated field if //there was a free location if(newLocation != null) { setLocation(newLocation); updatedField.place(this, newLocation); } else { // can neither move nor stay – //overcrowding - all locations taken alive = false; }}}
01/12/2005 Lecture 9: Abstraction Techniques 16
private void incrementAge() { age++; if(age > MAX_AGE) { alive = false; } } private int breed() { int births = 0; if(canBreed() && rand.nextDouble() <= BREEDING_PROBABILITY) { births = rand.nextInt(MAX_LITTER_SIZE) + 1; } return births; }
01/12/2005 Lecture 9: Abstraction Techniques 17
01/12/2005 Lecture 9: Abstraction Techniques 18
public class Fox { Static fields omitted // The fox's age. private int age; // Whether the fox is alive or not. private boolean alive; // The fox's position private Location location; // The fox's food level, which is increased // by eating rabbits. private int foodLevel; Methods omitted. }
01/12/2005 Lecture 9: Abstraction Techniques 19
01/12/2005 Lecture 9: Abstraction Techniques 20
public void hunt(Field currentField, Field updatedField, List newFoxes) { incrementAge(); incrementHunger(); if(isAlive()) { // New foxes are born into adjacent locations. int births = breed(); for(int b = 0; b < births; b++) { Fox newFox = new Fox(false); newFoxes.add(newFox); Location loc = updatedField.randomAdjacentLocation(location); newFox.setLocation(loc); updatedField.place(newFox, loc); }
01/12/2005 Lecture 9: Abstraction Techniques 21
// Move towards the source of food if found. Location newLocation = findFood(currentField, location); if(newLocation == null) {// no food found –move randomly newLocation = updatedField.freeAdjacentLocation(location); } if(newLocation != null) { setLocation(newLocation); updatedField.place(this, newLocation); } else { // can neither move nor stay - overcrowding – all // locations taken alive = false; }}}
01/12/2005 Lecture 9: Abstraction Techniques 22
private Location findFood(Field field, Location location) { Iterator adjacentLocations = field.adjacentLocations(location); while(adjacentLocations.hasNext()) { Location where = (Location) adjacentLocations.next(); Object animal = field.getObjectAt(where); if(animal instanceof Rabbit) { Rabbit rabbit = (Rabbit) animal; if(rabbit.isAlive()) { rabbit.setEaten(); foodLevel = RABBIT_FOOD_VALUE; return where; }}} return null; }
01/12/2005 Lecture 9: Abstraction Techniques 23
01/12/2005 Lecture 9: Abstraction Techniques 24
01/12/2005 Lecture 9: Abstraction Techniques 25
public class Simulator { … public void simulateOneStep() { step++; newAnimals.clear(); // let all animals act for(Iterator iter = animals.iterator(); iter.hasNext(); ) { Object animal = iter.next();
01/12/2005 Lecture 9: Abstraction Techniques 26
if(animal instanceof Rabbit) { Rabbit rabbit = (Rabbit)animal; if(rabbit.isAlive()) { rabbit.run(updatedField, newAnimals); } else { iter.remove(); } } else if(animal instanceof Fox) { Fox fox = (Fox)animal; if(fox.isAlive()) { fox.hunt(field, updatedField, newAnimals); } else { iter.remove(); } }
01/12/2005 Lecture 9: Abstraction Techniques 27
01/12/2005 Lecture 9: Abstraction Techniques 28
01/12/2005 Lecture 9: Abstraction Techniques 29
01/12/2005 Lecture 9: Abstraction Techniques 30
for(Iterator iter = animals.iterator(); iter.hasNext(); ) { Animal animal = (Animal)iter.next(); if(animal.isAlive()) { animal.act(field, updatedField, newAnimals); } else { iter.remove(); } }
01/12/2005 Lecture 9: Abstraction Techniques 31
abstract public void act(Field currentField, Field updatedField, List newAnimals);
01/12/2005 Lecture 9: Abstraction Techniques 32
01/12/2005 Lecture 9: Abstraction Techniques 33
public abstract class Animal { fields omitted /** * Make this animal act - that is: make it do * whatever it wants/needs to do. */ abstract public void act(Field currentField, Field updatedField, List newAnimals);
}
01/12/2005 Lecture 9: Abstraction Techniques 34
public boolean canBreed() { return age >= getbreedingAge() } abstract public int getBreedingAge();
– both Rabbit and Fox have their own static field BREEDING_AGE and Animal has none.
01/12/2005 Lecture 9: Abstraction Techniques 35
01/12/2005 Lecture 9: Abstraction Techniques 36
// let all animals act for(Iterator iter = actors.iterator(); iter.hasNext();) { Actor actor = (Actor) iter.next(); actor.act(…); } for(Iterator iter = drawables.iterator(); iter.hasNext();) { Drawable item = (Drawable) iter.hasNext(); item.draw(…); }
01/12/2005 Lecture 9: Abstraction Techniques 37
01/12/2005 Lecture 9: Abstraction Techniques 38
01/12/2005 Lecture 9: Abstraction Techniques 39
public interface Actor { /** * Perform the actor's daily behavior. * Transfer the actor to updatedField if it is * to participate in further steps of the simulation. * @param currentField The current state of the field. * @param location The actor's location in the field. * @param updatedField The updated state of the field. */ void act(Field currentField, Location location, Field updatedField); }
01/12/2005 Lecture 9: Abstraction Techniques 40
01/12/2005 Lecture 9: Abstraction Techniques 41
public class Fox extends Animal implements Drawable { ... } public class Hunter implements Actor, Drawable { ... }
01/12/2005 Lecture 9: Abstraction Techniques 42
01/12/2005 Lecture 9: Abstraction Techniques 43
01/12/2005 Lecture 9: Abstraction Techniques 44
01/12/2005 Lecture 9: Abstraction Techniques 45
01/12/2005 Lecture 9: Abstraction Techniques 46
01/12/2005 Lecture 9: Abstraction Techniques 47
01/12/2005 Lecture 9: Abstraction Techniques 48