Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem 6. Animals
- NOTE: You need a public class Main.
- Create a hierarchy(package) of animals. Your program should have three different animals – Dog, Frog and Cat. Deeper in the hierarchy you should have two additional classes – Kitten and Tomcat. Kittens are "Female" and Tomcats are "Male". All types of animals should be able to produce some kind of sound - String produceSound(). For example, the dog should be able to bark. Your task is to model the hierarchy and test its functionality. Create an animal of each kind and make them all produce sound and create getters for all fields.
- You will be given some lines of input. Each two lines will represent an animal. On the first line will be the type of animal and on the second – the name, the age and the gender. When the command "Beast!" is given, stop the input and print all the animals in the format shown below.
- Output
- • Print the information for each animal on three lines. On the first line, print: "{animalType}"
- • On the second line print: "{name} {age} {gender}"
- • On the third line print the sounds it produces: "{produceSound()}"
- Constraints
- • Each Animal should have a name, an age and a gender
- • All input values should not be blank (e.g. name, age and so on…)
- • If you receive an input for the gender of a Tomcat or a Kitten, ignore it but create the animal
- • If the input is invalid for one of the properties, throw an exception with message: "Invalid input!"
- • Each animal should have the functionality to produceSound()
- • Here is the type of sound each animal should produce:
- o Dog: "Woof!"
- o Cat: "Meow meow"
- o Frog: "Ribbit"
- o Kittens: "Meow"
- o Tomcat: "MEOW"
- Examples
- Input Output
- Cat
- Sharo
- Dog
- Rex 132
- Beast! Cat
- Tom 12 Male
- Meow meow
- Dog
- Rex 132 Male
- Woof!
- Frog
- Kermit 12 Male
- Beast! Frog
- Kermit 12 Male
- Ribbit
- Frog
- Froakie -2 Male
- Frog
- Froakie 2 Male
- Beast! Invalid input!
- Frog
- Froakie 2 Male
- Ribbit
- Hint
- To find the name of the class you can use this.getClass().getSimpleName() in toString() method inside Animal class.
Add Comment
Please, Sign In to add comment