Tsuki11

Untitled

Jul 8th, 2020
742
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.  
  22. Examples
  23. Input Output
  24. Cat
  25. Sharo
  26. Dog
  27. Rex 132
  28. Beast! Cat
  29. Tom 12 Male
  30. Meow meow
  31. Dog
  32. Rex 132 Male
  33. Woof!
  34. Frog
  35. Kermit 12 Male
  36. Beast! Frog
  37. Kermit 12 Male
  38. Ribbit
  39. Frog
  40. Froakie -2 Male
  41. Frog
  42. Froakie 2 Male
  43. Beast! Invalid input!
  44. Frog
  45. Froakie 2 Male
  46. Ribbit
  47. Hint
  48. 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