0% found this document useful (0 votes)
6 views6 pages

Resit Project

The MyZoo project involves creating a Java application to manage a zoo's animal data using classes MyZoo and Animal, with a focus on reading and displaying animal information from text files. Part 1 emphasizes completing methods for printing and reading animal data, while Part 2 expands the project by implementing an animal classification system using inheritance for different animal types. The project culminates in submitting the completed work by July 14, 2025, and includes guidelines for ensuring robustness and handling potential errors.

Uploaded by

sub939247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Resit Project

The MyZoo project involves creating a Java application to manage a zoo's animal data using classes MyZoo and Animal, with a focus on reading and displaying animal information from text files. Part 1 emphasizes completing methods for printing and reading animal data, while Part 2 expands the project by implementing an animal classification system using inheritance for different animal types. The project culminates in submitting the completed work by July 14, 2025, and includes guidelines for ensuring robustness and handling potential errors.

Uploaded by

sub939247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Programming : MyZoo Project

This project is concerned with the same scenario as that modelled by the Zoo and Animal classes of
the Zoo project discussed in Chapter 4 Grouping objects. But simply to distinguish between the two
situations and to avoid confusion, you will use a class called MyZoo for this project.

It is expected that you will use BlueJ for this project.

Part 1 of this project is primarily concerned with preparing you for Part 2. It should only take a short
while (once you have found  and re-read your Chapter 4 and Supplementary Material 4 notes) and is
in five steps.

Part 1
The zip file my_zoo_p1.zip is on BB under the Resit folder in Programming 2. The file contains two
Java classes, MyZoo and Animal, and two text files import001.txt and import002.txt
containing "animal" data. Download these into your filespace.

Although the MyZoo and Animal classes are similar to the classes in the original Zoo project, there are
some important differences. For example, the Animal objects in the zoo are stored in a TreeMap
rather than an ArrayList. Animals are added to the TreeMap using the put() method which
requires a unique key value to be specified (similar to the HashMap discussed in Chapter 6). You can
examine the documentation for TreeMap if you wish, but no detailed knowledge of this class is
required for this project. Another difference is that the MyZoo class has a three letter identifier,
provided to the constructor, and a method allocateId() for creating unique identifiers for animals
of the zoo.

Part 1 of this project is basically concerned with completing methods printAllAnimals() and
readDataFromFile(). The readDataFromFile() method uses a FileDialog object so that
you can easily select a file to read data from, but the code for reading the data is missing. However, if
you create a MyZoo object and execute the readDataFromFile() then you should see a message
stating that 10 animals have been read from the file (but remember that a common problem is that the
"open file" dialog gets hidden behind other windows). It doesn't matter which input file you select as
no data is read from the file, instead some "temporary" code will "populate" the zoo. After executing
readDataFromFile(), if you next execute the printAllAnimals() method then only a "header"
will be printed -- it will not actually show any animals as this method is also incomplete.

Proceed by completing the following stages. Note that no changes should be needed for class Animal.

Step 1: Complete the method printAllAnimals() using the Collection object c, provided in the
code, so that animals stored in the zoo can easily be checked. Note that when you complete this
method, the animals are listed in alphabetical order by name. This is because the values() method
will return a collection with the animals in alphabetical order by name since the key values for the
TreeMap are the names of the Animal objects: see method addAnimal().

-1-
Step 2: Start the development of the method readDataFromFile() by first deleting or
"commenting out" the temporary code. You should proceed in the following two steps.
(i) You will need a loop and you should first concentrate on successfully reading the data from the
file. At this step do not attempt to create an Animal object from the data. Here is the basic
structure needed:
read an initial line of text into a string
while there is something in the file
print out the string using System.out.println()
read the next line
You might also need to consult slides SM5 on how to handle exceptions. Get this working,
reading from import001.txt and showing the original data, before proceeding to the next
step.

(ii) The data in the text file is "tab separated" and looks like this:
Bird Golden Eagle Eddie
Spaces at the beginning or end of the line should be ignored. You should extract the three
substrings and then create and add an Animal object to the zoo. You should ignore the first
substring ("Bird" in the above example) as it is not required for this part of this project. Also
note that you should introduce an extra parameter - representing the owner of the collection –
for the constructor of your Animal object. Its value in this case will be "this".

On successful completion of this step you will have a "basic" working version of
readDataFromFile().

Step 3: A refinement of method readDataFromFile(). In these next stages, the data file you
should use is import002.txt where the first line of data in the text file provides some information
about the animals e.g.
import shipment of June 30, 2023
So, amend your code so that this first line is not treated as "animal" data but is printed out to the
terminal window. Also, subsequent lines of text may include "comment lines" that start with the #
symbol e.g.
# this line is a comment
Such lines should be ignored when reading data from the file.

Step 4: If you now "test" your project by reading from the file import002.txt then you might
notice that although two animals named Eddie are read from the file, only one appears in the listing if
you execute printAllAnimals(). This is because we have used a Map object TreeMap and the
call to put() in addAnimal() uses the name field and consequently the second Eddie "replaces" the
first one. Solve this problem without losing the alphabetical ordering by name. This is a very simple
task  once you see what is needed, but you should first re-read Part 1 from the beginning. Hint:
currently the "key" used for each animal, in the put() method, is its name -- look at the code for the
Animal class and think about how a unique key for each animal could be obtained; just one line of
code (in the MyZoo class) needs modifying !

-2-
Step 5: Unless you have already dealt with potential problems as you proceeded through the earlier
stages, it is certain that even if your project "works", it is not robust i.e. it will not be difficult for a user
to cause a runtime error. So the last stage is to test and again test to try to make your code as robust as
possible.

Part 2

This part of the project continues the scenario of Part 1 and is in five steps. In this part, we are going
to use a simplified "animal kingdom" classification system that looks like this:
animal

mammal bird

lion tiger golden eagle parrot

You should now download from BB the data file import003.txt.

Step 1

Create a MyZoo object and if you read the data from the file import003.txt using
readFromTextFile() you will find that it will read data for 10 animals (or at least it will tell you
that ). However, if you then execute the printAllAnimals() method, you should obtain output
similar to that below.

Details for all animals in Zoo SAL


===================================
id name species other attributes
==========================================================

SAL_000010 Colin: a Cobra


SAL_000001 Eddie: a Golden Eagle
SAL_000007 Eddie: a Elephant
SAL_000004 Emily: a Golden Eagle

Whilst we do not expect the data to be read back from the Map in the same order as in the file, clearly
not all the data that we expected to be output (e.g. the other attributes) is being output. Let's firstly look
at the input file before trying to work out what is wrong.

The data in import003.txt is of the form: class, species and name followed by each animal's
particular characteristics. For the latter, you should note that
 all mammals are characterised by their colour;
o lions are further characterised by the length of their mane;
o tigers are further characterised by their number of stripes;
 birds are characterised by whether they can fly or not;
o golden eagles are further characterised by whether they have a mate or not;
o parrots are further characterised by their IQ.

-3-
If you inspect the data file, you will see that there is indeed data for 10 animals  though not all these
animals are lions, tigers, eagles and parrots.

As stated above, not every animal's details (from the input file) are being displayed correctly . This
means that either
 the data is not being read in correctly, or that
 the data has been read in correctly and it is not being displayed correctly, or
 both !! 

Your first job is to work out which of these options it actually is. It should be relatively easy for you to
do this. However, before starting to code a solution, you need to consider the bigger picture: see the
next stage.

Step 2

The project – as it stands – does not use the animal kingdom classification above nor does it use
inheritance. The structure of the hierarchy above should be familiar to you from the hierarchy of your
project in Semester 2. It would be a very good idea if you were to re-read this project (and look at your
work on this project) before you proceed.

Now you need to create a structure similar to the hierarchy above. Eventually, you will need classes
Animal, Mammal, Bird, Lion, Tiger, GoldenEagle and Parrot but for now start by writing code
for the classes Mammal and Lion only. Write your code so that if data for any other type of animal is
met then it is ignored – for this stage anyway.

Hints:
 this will require an amount of refactoring e.g. adding new fields, moving fields, writing new
methods etc; you decide where to place each field by following the information about the data
written above.
 Remember to make full use of inheritance wherever appropriate !
 check it works correctly by calling printAllAnimals() (which will also need amending and
other methods adding to your new classes).
o if it works correctly, then it should read in details only for the lions and so print out
only their details.
 you will have completed this stage correctly if you can get output as follows:

Details for all animals in Zoo SAL


=====================================
species name id other attributes
==========================================================
Lion Leo SAL_000001 Hair is rusty Mane length is 105 cm

Lion Leslie SAL_000002 Hair is copper Mane length is 98 cm

Step 3

We now add more classes to allow our model to represent more animals. So, you now need to add
Bird and GoldenEagle classes. This should be a simple task provided you have written your
readFromTextFile() method to use the "mammal" or "bird" data so that the code for creating
"mammals" is separated from the code for creating "birds" which is logically sensible.
-4-
Your code should now allow the details of five animals to be read in and with a little effort, you can
obtain output similar to

Details for all animals in Zoo SAL


====================================
species name id other attributes
==========================================================
Golden Eagle Eddie SAL_000001 Can fly Has partner

Golden Eagle Gordon SAL_000005 Can fly No partner

Golden Eagle Jimmy SAL_000004 Can fly Has partner

Lion Leo SAL_000002 Hair is rusty Length of mane is 105 cm

Lion Leslie SAL_000003 Hair is copper Length of mane is 98 cm

Finally, for this stage, add classes Tiger and Parrot. Test your code and you should find that it reads
and prints details out regarding seven animals.

Step 4

At the moment, although we store data on whether each golden eagle has a partner or not, we have not
written code that allows us to store that partner's data. Write a method setPartner() plus any other
necessary code that would do this.

Step 5

This final stage is to write a simple Java application for your project which is based on Slide SM4.25
and the Worksheet SM4.

Submission details

You should upload your work to a new folder on BB called “Resit project upload folder”.

There are upload instructions – identical to the instructions for the previous upload folder – on the
Assignment Content front page of that folder.

Last date for submission: 16.00, Monday 14th July 2025.

Note that by the act of following these instructions and handing your work in, it is deemed that
you have read and understand the rules on academic misconduct as written in your student
handbook.

-5-
-6-

You might also like