1. Engineering
  2. Computer Science
  3. part a suppose you are working for a zoo and...

Question: part a suppose you are working for a zoo and...

Question details

Part A (Java programming)

Suppose you are working for a Zoo  and you are asked to write the class for keeping information about animals. You decide to call your class as Animal.

  • Write a class to represent a Animal.
  • The attributes are: the animal id, the species, price, and a flag indicating whether it is currently being in a show.
    • Note that, when an animal is created, a unique new id number is allocated to id. This id  number will be generated by adding one to the previously used id number, which is kept stored in a variable shared by all Animal objects.
  • Include accessors for all attributes, a mutator to change the flag, and a method to increase the price.
  • Two most necessary constructors (that will be used create new animal(s));



Part B 

Given a class named AnimalDatabase, which will be used to provide the responsibility of data management of a set of Animal objects. Internally, it should use an ArrayList of Animal as follows:

Public class AnimalDatabase{

   private ArrayList<Animal> animalList = new  ArrayList<Animal>();

You  should provide:

  •  implementation for a method to add a not null  animal object into the animalList. 

    • public void add(Animal a){........}

  •  implementation for a method to report the average price  of the animals. Assume that there is a method getPrice() in Class Animal. 

    • public double getAveragePrice(){....}

  •  implementation for a method to retrieve a specific animal by id. Assume that there is a method getId() in Class Animal. 

    • public Animal getAnimalById(int id){....}

  •  implementation for a safe way method to obtain an ArrayList of all the animals within a given range of prices. 

    • public ArrayList<Animal> getAnimalsInRange(double minPrice, double maxPrice);

  • implementation for a method called reportAnimlas () which should write the animal objects stored   in the animalList  to a text file named "animals-report.txt". You should use the printf() method to format your data in the following structure:  

    • public void reportAnimlas () ;


ID         Species     Price          Show
100 Cat $100.0 true
101 Dog $120.0 false


Part C 

There is a text file containing the details of several animal details. Each animal uses 3 lines of the file. The first of the three lines is a string giving the species of the animal, for example  “Dog” or “Cat”.  The second of the three lines  contains double giving the price, the third line is boolean  indicating whether it is currently being in a show. However, the very first line of the file is an integer number, which says how many animals are given in the file in the lines which follow it  (i.e. how many records will follow).

Text file example for two animals










  • Write code for a method named processTextFile() which will open the file named ‘animals.txt’, from which it will read the data of  animals. It will create Animal objects  using  this data by calling the constructor that takes the parameters (species,  price,  show) , placing them into an  ArrayList<Animal>  animalList. 


Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution