Lab 04
In this lab you'll be revising the extraction of class hierarchies from informal descriptions, and they associated java syntax. This lab unit is shorter, as it contains a graded evaluation.
Carnivores and Herbivores
Carefully read through the following requirement description:
- Contractually, all animals are required to be able to consume water and food. All animals must have:
- a method "drink" (common to all animals, prints "Glug, glug, glug");
- a method "eat" (implementation varies)
- Animals fall into two sub-categories. They can be either herbivores or carnivores (we assume they can never be both).
- Herbivores and carnivores share a common implementation for drinking.
- Herbivores and carnivores do not share a common implementation for eating.
- Herbivores print "Oooh yummy salad!"
- Carnivores print "Roar, juicy steak!"
- Only specific animal types must be instantiable (with new). These are:
Lion(Carnivore)Tiger(Carnivore)Zebra(Herbivore)Gazelle(Herbivore)
Instructions
Your goal is to translate the above requirements into a hierarchy of java classes. There must not be any code duplication, i.e. methods copy-pasted from one method to the other. List all java files (classes or interfaces) you will need for the above implementation.
Create a new file inheritance.txt
For each class-file indicate whether it is: 1. What kind of class it is: * A standard class * An abstract class * An interface 2. What methods must be specified in the class 3. Whether the class inherits from other classes * If yes which other class, and which keyword is used.
Concept extraction
- Translate the following technical requirements into conceptual java code, based on the concepts seen in class
- The goal is to avoid any code replication (copy-pasted method code across classes).
- Use the UML notation seen in class:
Recap requirements first
For each class-file decide on:
1. What kind of class it is: standard/abstract/interface
2. What methods must be specified in the class
3. Whether the class inherits from elswhere: keyword
Implementing an interface
Notation sample:
classDiagram
class Interface {
+foo()
}
class Class {
+foo()
}
Interface <|.. Class : implements
main
Extending a class
Notation sample:
classDiagram
class BaseClass {
+bar()
}
class ChildClass {
+foo()
}
BaseClass <|-- ChildClass : extends
Implementation
After you have elicited a fitting class hierarchy on paper, translate your conceptual solution into an implementation.
- Create the required hierarchy of java classes.
- Ensure there is no code duplication between methods.