ArrayList.get() returning wrong object type
Posted: Wed Oct 18, 2006 9:39 am
Code: Select all
//We use an array iterator
import java.util.ArrayList;
/**
* Main Entry point for Application
*/
class App
{
/**
* Static method main() executed when app starts
* @param String arguments
*/
public static void main(String[] args)
{
Zoo myZoo = new Zoo("Chester");
myZoo.attachActivityMonitor(new ZooActivityDisplay());
myZoo.addAnimal(new Monkey("Fred"));
}
}
/**
* Abstract class ZooActivityMonitor
* A plugin API to allow objects to observe changes in the Zoo
*/
abstract class ZooActivityMonitor
{
/**
* An instance of Zoo
*/
protected Zoo zoo;
public ZooActivityMonitor() {}
/**
* Set the Zoo object for observation
* @param Zoo
*/
public void setZoo(Zoo theZoo)
{
this.zoo = theZoo;
}
/**
* Abstract method scan() must be overridden by child
*/
abstract public void scan();
}
/**
* An observer which views changes in Zoo and displays info
*/
class ZooActivityDisplay extends ZooActivityMonitor
{
public ZooActivityDisplay() {}
/**
* Abstract method scan()
*/
public void scan()
{
for (int i = 0; i < this.zoo.animals.size(); i++)
{
System.out.println("Here");
}
}
}
/**
* Zoo class
* Stores animals
*/
class Zoo
{
/**
* The name of this Zoo
*/
private String name;
/**
* A container for animals in this Zoo
*/
public ArrayList animals;
private ArrayList monitors;
/**
* Constructor
* @param String name
*/
public Zoo(String myName)
{
this.monitors = new ArrayList();
this.animals = new ArrayList();
this.name = myName;
}
/**
* Get the name of this Zoo
* @return String
*/
public String getName()
{
return this.name;
}
/**
* Add a new Animal to this Zoo
* @param String animal
*/
public void addAnimal(Animal animal)
{
this.animals.add(animal);
this.runMonitors();
}
/**
* Add a new activity monitor to this zoo
* @param ZooActivityMonitor
*/
public void attachActivityMonitor(ZooActivityMonitor a)
{
a.setZoo(this);
this.monitors.add(a);
}
/**
* Run all monitors currently attached
*/
protected void runMonitors()
{
for (int i = 0; i < this.monitors.size(); i++)
{
this.monitors.get(i).scan();
}
}
}
/**
* An animal which lives in the Zoo
*/
abstract class Animal
{
/**
* The name of this animal
*/
protected String name;
protected int maxTemp = 0;
protected ArrayList food;
/**
* Constructor
*/
public Animal(String itsName)
{
this.food = new ArrayList();
this.name = itsName;
}
/**
* Get the name of this anmial
* @return String
*/
public String getName()
{
return this.name;
}
/**
* Get the maximum temperature at which this animal can live
* @return int
*/
public int getMaxTemp()
{
return this.maxTemp;
}
}
//Bit of food for the monkey (not yet used)
abstract class Vegetation {}
class Banana extends Vegetation {}
/**
* Yeah; it's a Monkey
*/
class Monkey extends Animal
{
protected int maxTemp = 50;
public Monkey(String itsName)
{
super(itsName);
this.food.add("Banana");
}
}Code: Select all
gcj --main=App -o runme ZooApplication.java
ZooApplication.java: In class 'Zoo':
ZooApplication.java: In method 'Zoo.runMonitors()':
ZooApplication.java:107: error: Can't find method 'scan()' in type 'java.lang.Object'.
this.monitors.get(i).scan();
^
1 error