Tsuki11

Untitled

Jul 8th, 2020
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. Problem 6. Animals
  2. NOTE: You need a public class Main.
  3. 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.
  4. 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.
  5. Output
  6. • Print the information for each animal on three lines. On the first line, print: "{animalType}"
  7. • On the second line print: "{name} {age} {gender}"
  8. • On the third line print the sounds it produces: "{produceSound()}"
  9. Constraints
  10. • Each Animal should have a name, an age and a gender
  11. • All input values should not be blank (e.g. name, age and so on…)
  12. • If you receive an input for the gender of a Tomcat or a Kitten, ignore it but create the animal
  13. • If the input is invalid for one of the properties, throw an exception with message: "Invalid input!"
  14. • Each animal should have the functionality to produceSound()
  15. • Here is the type of sound each animal should produce:
  16. o Dog: "Woof!"
  17. o Cat: "Meow meow"
  18. o Frog: "Ribbit"
  19. o Kittens: "Meow"
  20. o Tomcat: "MEOW"
  21. Examples
  22. Input Output
  23. Cat
  24. Sharo
  25. Dog
  26. Rex 132
  27. Beast! Cat
  28. Tom 12 Male
  29. Meow meow
  30. Dog
  31. Rex 132 Male
  32. Woof!
  33. Frog
  34. Kermit 12 Male
  35. Beast! Frog
  36. Kermit 12 Male
  37. Ribbit
  38. Frog
  39. Froakie -2 Male
  40. Frog
  41. Froakie 2 Male
  42. Beast! Invalid input!
  43. Frog
  44. Froakie 2 Male
  45. Ribbit
  46. Hint
  47. 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