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:

private ZooKeeper alice, bob, charles;
private Visitor mary, peter, richard, tanya;
 

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:

  • alice, bob and charles will each be separate ZooKeeper objects; and
  • mary, peter, richard and tanya will each be separate Visitor objects

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

private void createExampleZooKeepers() {
    alice = new ZooKeeper("Alice Smith", "Some City",
                "", new BigDecimal("20000"));        
    bob = new ZooKeeper("Bob Jones", "2 The Road",
            "", new BigDecimal(22000));
    charles = new ZooKeeper("Charles Green", "3 The Avenue",
            "", new BigDecimal(18000));
}
 

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:

private void createExampleVisitors() {
    mary = new Visitor("Mary Roberts", "");
    peter = new Visitor("Peter Harris", "");
    richard = new Visitor("Richard York", "");
    tanya = new Visitor("Tanya West", "”);
}
 

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

public ZooAdministrator() {
    createExampleZooKeepers();
    createExampleVisitors();
}
 

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:

package virtualzoo;

import virtualzoo.core.*;

public class VirtualZoo {

    public static void main(String[] args) {
        ZooAdministrator admin = new ZooAdministrator();
	  
        // methods to be invoked on admin will go here...
    }

}
 

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:

package virtualzoo;

import virtualzoo.core.*;

public class Experiments {

    public static void main(String[] args) {
        // experimental code will go here...
    }
}
 

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

Next lesson 5.1: Static members, constants, and conditionals 


Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Saturday, 13 December 2025

Captcha Image