Question
Task 2 Animal Selection Your second task is to write the code that asks the user to specify how many animals are present in the
Task 2 Animal Selection
Your second task is to write the code that asks the user to specify how many animals are present in the zoo, as well as the name and species of each animal.
A Scanner object scan has already been defined for your needs inside the VirtualZoo
class. Do not create any additional Scanner objects. Make use of the Scanner operations to capture the user input.
You will need to implement the following four public methods inside the VirtualZoo
class:
askNumberAnimals()
This method should ask the user "How many animals are at your zoo?", and then return their response as an integer. This method should also verify that the response provided by the user is a positive integer. If the value provided is not an integer, print the message "Please enter a valid number of animals". If the value provided is a negative integer, print the message "Please enter a positive number of animals". The method should repeatedly perform these instructions until the user enters a positive integer response. For example, calling this method using the statement:
System.out.println("Number of animals = " + askNumberAnimals());
Could give the following output (user input in bold highlighted text):
How many animals are at your zoo? three Please enter a valid number of animals How many animals are at your zoo? -5 Please enter a positive number of animals How many animals are at your zoo? Lots of animals Please enter a valid number of animals How many animals are at your zoo? 13 Number of animals = 13
askAnimalName(int id)
This method should ask the user "What is the name of animal #X?" (where X represents the value of the integer parameter id) and then return their response as a string. This method should also verify that the name provided by the user is unique and has not been used for any previous animal. One way to do this is to define an ArrayList instance variable inside the VirtualZoo
class that stores all previously entered names. If the name provided by the user is present in this ArrayList, then you know that the name has already been used for a previous animal. If the name specified by the user has already been used previously, print the message "That name is already taken". This method should repeatedly perform these instructions until the user enters a unique name for the animal. Please remember that animal names as case-sensitive. For example, calling this method using the statements:
System.out.println("Animal 1 name = " + askAnimalName(1)); System.out.println("Animal 2 name = " + askAnimalName(2));
could give the following output (user input in bold highlighted text):
What is the name of animal #1? Rover Animal 1 name = Rover What is the name of animal #2? Rover That name is already taken What is the name of animal #2? Mittens Animal 2 name = Mittens
askAnimalSpecies(String name)
This method should ask the user "What is the species of X?" (where X represents the value of the string parameter name) and then return their response as an Animal object. This method should also verify that the species provided by the user is one of the five valid animal species (Tiger, Giraffe, Hippo, Panda or Monkey). If the species specified by the user is not valid, print the message "That is not a valid species". This method should repeatedly perform these instructions until the user enters a valid species for the animal. Please remember that animal species inputs should be case-insensitive, so the inputs "Tiger", "tiger" and "TIGER" are all valid. Once a valid species has been specified, the method should create an Animal object using the previously specified constructor inside the Animal
class. When doing this, the species parameter should always be specified with the first letter in uppercase and the remaining letters in lowercase. For example, calling this method using the statement:
System.out.println(askAnimalSpecies("Rover"));
could give the following output (user input in bold highlighted text):
What is the species of Rover? pig That is not a valid species What is the species of Rover? SNAKE That is not a valid species What is the species of Rover? HIPPO Name = Rover Species = Hippo Hunger = 50 Thirst = 50 Boredom = 50
animalSelection()
This method should ask the user how many animals are present in the zoo, as well as the name and species of each animal. This can be done be calling each of the three methods described above (askNumberAnimals, askAnimalName, askAnimalSpecies) to get the required information from the user. This method should then return an arraylist of Animal objects, each of which represents an animal that is present in the zoo.
For example, calling this method using the statements:
ArrayList
Could give the following output (user input in bold highlighted text):
How many animals are at your zoo? three Please enter a valid number of animals How many animals are at your zoo? -5 Please enter a positive number of animals How many animals are at your zoo? 3 What is the name of animal #1? Andy What is the species of Andy? panda What is the name of animal #2? Andy That name is already taken What is the name of animal #2? HENRY What is the species of HENRY? hippo That is not a valid species What is the species of HENRY? HIPPO What is the name of animal #3? tony What is the species of tony? tiger HENRY
REPORT TASK
Answer the following questions:
- How did you ensure that the user provided a positive integer for the number of animals?
- How did you ensure that the user provided a unique name for each animal?
- How did you ensure that the user provided a valid species for each animal?
Task 3 - Item Selection
Your third task is to write the code that asks the user to select what item they want to give to a specific animal, and then apply any effects that this has on the animal and week's total cost.
To begin with, define a static integer variable called totalCost inside the VirtualZoo
class. This variable will be used to store to accumulative cost of looking after all animals in the zoo throughout the week. Create static getter and setter methods for accessing this variable.
Next, define three public methods inside the Animal
class called giveFood(), giveWater() and giveToy(). For the time being, implement these methods such that calling each of them has the following effect:
- calling giveFood() decreases hunger by 30, increases thirst by 15, increases boredom by 15, increases totalCost by 30.
- calling giveWater() increases hunger by 15, decreases thirst by 30, increases boredom by 15, increases totalCost by 20.
- calling giveToy() increases hunger by 15, increases thirst by 15, decreases boredom by 30, increases totalCost by 40.
You should notice that this follows the same status property modifications that were defined for the Tiger species in the complete tasks specifications.
When decreasing the hunger, thirst or boredom properties of an animal, the value for the property cannot drop below 0. For example, calling giveFood() on an animal with a current hunger property value of 5, will reduce the animals hunger to 0 but no lower.
You can test your code for this by running the following statements:
Animal myPet = new Animal("Tony", "Tiger"); myPet.giveFood(); System.out.println(myPet); myPet.giveFood(); System.out.println(myPet); myPet.giveWater(); System.out.println(myPet); myPet.giveToy(); System.out.println(myPet); System.out.println(VirtualZoo.getTotalCost());
Which should give the following output:
Name = Tony Species = Tiger Hunger = 20 Thirst = 65 Boredom = 65 Name = Tony Species = Tiger Hunger = 0 Thirst = 80 Boredom = 80 Name = Tony Species = Tiger Hunger = 15 Thirst = 50 Boredom = 95 Name = Tony Species = Tiger Hunger = 30 Thirst = 65 Boredom = 65 120
You will also need to implement the following public method inside the VirtualZoo
class:
askItem(Animal animal)
This method should ask the user "What item would you like to give to X?" (where X represents the name of the animal parameter). This method should also verify that the item specified by the user is one of the three valid items (Food, Water or Toy), and that this item was not the same as the previous item that was given to animal. If the item specified by the user is not valid, print the message "That is not a valid item". If the item specified by the user is the same as the most recent previous item that was given to animal, then print the message "You cannot give the same item as yesterday". This method should repeatedly perform these instructions until the user enters a valid response. Please remember that item name inputs should be case-insensitive, so the inputs "Food", "food" and "FOOD" are all valid. Once a valid item has been specified, the method should call either the giveFood(), giveWater() or giveToy() method on animal, depending on the item that was selected. If giving an item to an animal has resulted in its death, add 1000 to the totalCost variable and print the message "X has died" (where X represents the name of the Animal parameter animal).
You can test your code for this task by running the following statements (user input in bold highlighted text):
Animal myPet = new Animal("Tony", "Tiger"); askItem(myPet); System.out.println(myPet); askItem(myPet); System.out.println(myPet); System.out.println(VirtualZoo.getTotalCost());
Which should give the following output:
What item would you like to give to Tony? toast That is not a valid item What item would you like to give to Tony? FOOD Name = Tony Species = Tiger Hunger = 20 Thirst = 65 Boredom = 65 What item would you like to give to Tony? MORE FOOD That is not a valid item What item would you like to give to Tony? food You cannot give the same item as yesterday What item would you like to give to Tony? water Name = Tony Species = Tiger Hunger = 35 Thirst = 35 Boredom = 80 50
REPORT TASK
Answer the following questions:
- How did you check whether an item specified by the user for an animal was different from the most recent item that was given to that animal.
- The zoo has asked you to modify the program so that an item can only be given to an animal if it was not the same as either of the previous two items it was given. How would you need to change the code to achieve this?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started