Docsity

Abstraction Techniques - Introduction to Programming in Java - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Abstraction Techniques - Introduction to Programming in Java - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Objectives

  • use a foxes-and-rabbits simulation to introduce

abstract classes, interfaces, and multiple

inheritance

  1. More Abstraction

Techniques

Topics

• 1. Benefits of Simulation

• 2. Foxes and Rabbits Simulation

• 3. Improving the Code

• 4. Further Abstractions

• 5. From Abstract to Interface

• 6. Why Have Interface Types?

Predator-prey Simulations

• There is often a natural balance between

wild animals which varies over time.

e.g. rabbits

e.g. foxes

2. Foxes and Rabbits Simulation

Version 1

application

details are not

important

The Remaining Classes

• Field

– the field where the foxes and rabbits live, breed,

get eaten, die

• Location

– a 2D position in the field

• SimulatorView

– a graphical view of the field

• FieldStats, Counter

– calculates statistics shown in the GUI

SimulatorView Visualization

Rabbits =

orange/yellow

Foxes = blue

A Rabbit’s State

public class Rabbit
// constants...
private int age;
private boolean alive; // alive or not?
private Location location; // position in field
// methods...

A Rabbit’s Behaviour

• Implemented in Rabbit.act():

– a rabbit gets older when act() is called

• it may die of old age

– a rabbit may create new rabbits

– a rabbit tries to move to an empty adjacent

square in the field

– overcrowding will kill a rabbit

// try to move this rabbit Location newLoc = updatedField.freeAdjLoc(location); // find a new location if (newLoc != null) { // if new location is free setLocation(newLoc); updatedField.place(this, newLoc); } else // can't move - so die due to overcrowding isAlive = false; }

} // end of act()

A Fox’s State

public class Fox
// constants...
private int age;
private boolean alive; // alive or not?
private Location location; // position in field
private int foodLevel;
// increased by eating rabbits
// methods...

Fox.act()

public void act (Field currentField, Field updatedField, List newFoxes) /* A fox breeds, moves about looking for food, or dies of old age, hunger, or overcrowding. */ { incrementAge(); // may die incrementHunger(); // may die if (isAlive) { int numBirths = breed(); // have fox breed for (int b = 0; b < numBirths; b++) { Fox newFox = new Fox(false); // create new fox newFoxes.add(newFox); Location loc = updatedField.randomAdjLoc(location); newFox.setLocation(loc); // place new fox in field updatedField.place(newFox, loc); } :

The details are not

important.

continued Docsity.com

// try to move this fox Location newLoc = findFood(currentField, location); if (newLoc == null) // if no food found then move randomly newLoc = updatedField.freeAdjLoc(location);

if (newLoc != null) { // if new location is free setLocation(newLoc); updatedField.place(this, newLoc); } else // can't move - so die due to overcrowding isAlive = false; }

} // end of act()

Part of simulateOneStep()

for(Iterator it = rabbits.iterator(); it.hasNext(); ){
Rabbit rabbit = it.next();
rabbit.act (updatedField, newRabbits);
if(! rabbit.isAlive())
it.remove();
for(Iterator it = foxes.iterator(); it.hasNext(); ) {
Fox fox = it.next();
fox.act (field, updatedField, newFoxes);
if(! fox.isAlive())
it.remove();

3. Improving the Code

• Fox and Rabbit are very similar but do not

have a common superclass.

• simulateOneStep() uses similar-looking code

for manipulating both animal lists.

• Simulator is tightly coupled to specific

classes

– it ‘knows’ a lot about the behaviour of foxes and

rabbits