Lab 3 - Inheritance, abstract classes
and I/O streams
1. Abstract classes, interfaces and a zoo...
This exercise is continued in the next lab, and you will have to send your solution for all the
questions. You will have a note for this exercise, which will count for
a minor part in your evaluation.
As in the example given in the lecture, we will build some classes
representing
animals. Each animal has a weight and an age. We will also create zoos
to
put animals. You will create 3 classes for animals,
- a class Animal,
superclass of all animals: it has a static field nbAnimals containing the
number
of Animal
objects, and 2 instance fields, weight and age. Do create a
constructor for this class, initializing weight and age, also incrementing
the number of Animal
objects. You will override toString
for this class,
returning
a String
containing
information about the instance of the class, its class (method getClass()),
age and weight.
- and 2 classes for animals, the ones you want (for example Monkey and Lion),
extending Animal:
their constructors will call the constructor of their superclass, Animal, and
you will add a
method public boolean
isOld(),
returning true if
the
animal is too old (you will test if the age is above a certain limit).
You will also create a ZooArray
class. It has a Animal
array which will contain the animals of the zoo. So the zoo can contain
only
a fixed number of animals, passed as argument of the constructor. You
will
add the following methods,
- addAnimal(Animal);
for the addition of animals in the zoo, checking if the maximum
number
of animals is not reached,
- and override toString,
returning a String
describing the zoo, the number of animals, the animals themselves and
if
they are old.
You will create a TestZoo
class to test your ZooArray
class and the animal
classes.
You will especially create a ZooArray, add more
animals
than allowed for this instance, and write the ZooArray instance.
Then, we will create another kind of zoo, in which there is
virtually
no limit for the number of animals. To implement that, you will create
a ZooUnlimited
class, and
use
a Vector field to
contain
the animals. This class must have the same functionnality as the
previous
one. You
will test this new class in the same way as ZooArray in TestZoo.
At last, you will turn you Animal class to an
abstract
class, because you're not interested in animals for themselves, but
subclasses,
and you don't want people to instantiate this class. You will
declare
the
method isOld() in
the
new abstract class, as an abastract method, which will also ensure that
all Animal
subclasses have
this
method. You shouldn't change your TestZoo class, and see
if
the results are the same.
For the quickest, you can also add encapsulation and accessor methods
to
hide the fields of all classes.
2. (optional) Write a text file
counter.dat
which contains the integer 0 (for example with the command line echo "0" > counter.dat).
Then write a Java application that reads
this
file and increments the integer by one. Then the new integer is written
back to the file counter.dat
. This means every time we
run
the application, the integer number in the file is incremented by 1.
Hint:
When you open a file for writing, and a file of the same name already
exists,
by default, the previous file is erased (which is what we want).
3. (optional) For the quickest, write a Java
program
that copies
a file. This program takes the name of the original file and the
destination
file on the command line.
End of Lab 3.