By Tony Bevis on Monday, 09 October 2023
Category: Java

Java programming course: 4.7 Managing the zoo using ZooAdminstrator

In the previous lesson you learnt about Java interfaces.

Managing the zoo using ZooAdminstrator

From this point in the course, you will start to make more use of the ZooAdministrator class by using it to store example animals, zookeepers, visitors, etc.

The VirtualZoo class, which is the entry point into the project, will be modified to simply instantiate a ZooAdministrator object and invoke certain of its functions. Later in the course, VirtualZoo will instead instantiate a graphical user interface which you will develop, and which will allow manipulation of the various aspects of the zoo.

ZooAdminstrator

Define new instance variables in the ZooAdministrator class to store some example zookeepers and visitors:

The syntax rules of Java allow you to specify more than one instance variable as part of the same statement where each is separated by a comma, provided the variables are all of the same type. Hence:


Create a new private helper method called createExampleZooKeepers() to instantiate the zookeepers:

You will need to import the java.math package because the above code uses the BigDecimal class.

Create a new private helper method called createExampleVisitors() to instantiate the visitors:

Define a constructor that invokes the two private methods you just entered:

​VirtualZoo

All the VirtualZoo class needs to do for the moment is instantiate a ZooAdministrator object. Later, you will then invoke methods upon this object as new functionality is built to see the results. You will also define a new Experiments class which you can use to enter sample code.

The entire VirtualZoo class should look as follows:

In package virtualzoo you should now create new class called Experiments where you can enter and test temporary code for the remainder of the course:

In the next section you will learn about static members, constants, and conditionals.

Next lesson 5.1: Static members, constants, and conditionals 

Related Posts

Leave Comments