Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Help me to write a java program Task 1 - Animal Class Take some time to read through the existing code and the comments. The

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Help me to write a java program

Task 1 - Animal Class Take some time to read through the existing code and the comments. The program's main method is located inside the VirtualZooDriver class. Running the provided program without any modifications or additions will result in the following output You will not need to change the VirtualZooDriver class, however you will be adding code to the class. You will also be creating your own classes. Your first task is to write the Animal class that defines the relevant functionality and properties that all animals have. This class should include the following string instance variables: - name - species This class should also include the following integer instance variables, each of which has an initial value of 50 : - hunger - thirst - boredom Write a constructor for the Animal class, that takes in two string parameters that define the animal's name and species. Create getter and setter methods for accessing and modifying all instance variables in the Animal class. Create a method called isDead ( ) that returns a boolean indicating if the animal is dead. An animal is dead if any of its properties (hunger, thirst or boredom) are above 100 . Copy the following tostring () method into the Animal class: public String tostring() \{ String statusMessage = isDead ()? "Name ="+ getName ()+"( Dead ) " : "Name = statusMessage += "Species ="+getSpecies()+ " "; statusMessage += "Hunger ="+getHunger()+ " "; statusMessage += "Thirst ="+getThirst()+ " "; statusMessage += "Boredom = " +getBoredom()+ " "; return statusMessage; \} You can test your code for this task by running the following statements: Animal myDog = new Animal("Rover", "Hippo"); Animal myCat = new Animal("Fluffy", "Tiger"); myDog . setBoredom (30); myDog. setHunger (70); myCat . setName ("Mittens"); myCat. setThirst (110); System. out . print ln (myDog); System. out.print ln (myCat); Which should give the following output: Name = Rover Species = Hippo Hunger =70 Thirst =50 Boredom =30 Name = Mittens (Dead) Species = Tiger Hunger =50 Thirst =110 Boredom =50 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.print ln("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.print ln("Animal 1 name = " + askAnimalName(1)); System. out.print ln(" 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 caseinsensitive, 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.print ln(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? HIPP0 Name = Rover Species = Hippo Hunger =50 Thirst =50 Boredom =50 animalSelection0 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> allZooAnimals = animalSelection ( ); System. out.print ln(" "+ allZooAnimals.get (1).getName()); 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? hippoo 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? 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 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, print ln (myPet); myPet.giveFood(); System. out.print ln (myPet); myPet.giveWater( ); System. out . print ln (myPet); myPet.giveToy(); System. out, print ln (myPet); System. out.print ln( 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: askltem(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. print ln (myPet); askItem(myPet); System. out.print ln (myPet); System. out.print ln( 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 Task 4 - Week Cycle Your fourth task is to write the code that will cycle through all 7 days of the week (Monday-Sunday), and each day print the status of all animals in the zoo and ask the user to select what item they want to give to each animal. You will need to implement the following public method inside the Virtualzoo class: weekCycle(ArrayList z00Animals) This method should carry out the following set of operations for each day of the week ( 7 days in total). - Print the message "The current day is X ", where X should be the current day of the week (Monday, Tuesday, Wednesday Thursday, Friday, Saturday or Sunday) - Print the status of each animal in zooAnimals. This can be done by calling the tostring () method on each animal in the ArrayList. - Ask the user what item they want to give to each animal in zooAnimals that is still alive, using the previously defined askItem(Animal animal) method. The user should not be asked to give an item to any animal that has died. Once the week has concluded, the method should print the message "Total week cost =$X, where X is the value of the totalcost static variable inside the VirtualZoo class. You can test your code for this task by replacing beginsimulation () method in the VirtualZoo class with the following updated version: public void beginsimulation() \{ displaywelcome(); ArrayList zooAnimals = animalselection(); weekCycle(zooAnimals); \} This will allow you to run the program from the VirtualZooDriver class and complete the application from start to finish, with the exception that all animals will be treated the same regardless of their species. Your output might look something like the following (user input in bold highlighted text): How many animals are at your zoo? 2 What is the name of animal \#1? Snowy What is the species of Snowy? monkey What is the name of animal \#2? TIM What is the species of TIM? Bat That is not a valid species What is the species of TIM? GIRAFFE The current day is Monday Name = Snowy Species = Monkey Hunger =50 Thirst =50 Boredom =50 Name =TIM Species = Giraffe Hunger =50 Thirst =50 Boredom =50 What item would you like to give to Snowy? food What item would you like to give to TIM? sleep Task 5 - Animal Species Your final tasks is to implement the different effects and costs that the available items have on each animal species. First convert the Animal class into an abstract class, and the methods giveFood (), giveWater () and giveToy () into abstract methods. Create five new classes called Tiger, Giraffe, Hippo, Panda and Monkey, which extend the class. Each of these new classes should override the giveFood (), givewater () and giveToy () abstract methods with their own species-specific implementations (as defined in the original Specification table). You will also need to modify the askAnimalspecies method in the class to call the constructors of these five new classes. Once this final task has been completed, your program should produce the complete example output provided with the original specifications

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions