Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////// Animal Interface
- public interface Animal {
- /**
- * Returns the kind of the animal.
- *
- * @return the kind of the animal
- */
- public String animalKinde();
- /**
- * returns the current age of the animal.
- *
- * @return the age of the animal
- */
- public int getAge();
- /**
- * Returns true if the animal can eat the given food.
- *
- * @param food - the name of food
- * @return whether or not the animal can the given food
- */
- public boolean canEat(String food);
- /**
- *
- * @param food
- * @throws Exception
- */
- public void feed(String food) throws Exception;
- /**
- * The animal sleeps
- */
- public void sleep();
- /**
- * Increments the animal's age by one year.
- * @throws java.lang.Exception
- */
- public void ageOneYear() throws Exception;
- }
- //////////////////////////////////////////////////////////////////// Animal Implementation
- public class AnimalImpl implements Animal{
- private int age;
- private int maxAge;
- private boolean isAlive;
- public AnimalImpl(int ma){
- age=0;
- maxAge=ma;
- isAlive=true;
- }
- @Override
- public String animalKinde() {
- return "undefined.";
- }
- @Override
- public int getAge() {
- return age;
- }
- @Override
- public boolean canEat(String food) {
- return false;
- }
- @Override
- public void feed(String food) throws Exception{
- if (!this.canEat(food)){
- throw new Exception("The animal cannot eat "+food);
- }
- System.out.println("Nom Nom");
- }
- @Override
- public void sleep()
- {
- System.out.println("Z-z-z");
- }
- @Override
- public void ageOneYear() throws Exception{
- if (age>=maxAge){
- throw new Exception("The animal was died.");
- }
- age++;
- }
- public String toString(){
- return "Animal which is "+age+" years old.";
- }
- }
- /////////////////////////////////////////////////////////////////////////////// Tiger
- public class Tiger extends AnimalImpl{
- public Tiger(){
- super(12);
- }
- public String animalKinde(){
- return "Tiger";
- }
- public boolean canEat(String food){
- if (food.equals("Meat")){
- return true;
- }
- return false;
- }
- public void feed(String food) throws Exception{
- if (!this.canEat(food)){
- throw new Exception("Tigers cannot eat "+food);
- }
- System.out.println("Munch Munch!");
- }
- public String toString(){
- return "Tiger which is " + getAge() + " years old.";
- }
- }
- //////////////////////////////////////////////////////////////////////////// Cow
- public class Cow extends AnimalImpl{
- public Cow(){
- super(10);
- }
- public String animalKinde(){
- return "Cow";
- }
- public boolean canEat(String food){
- if (food.equals("Grass")){
- return true;
- }
- return false;
- }
- public void feed(String food) throws Exception{
- if (!this.canEat(food)){
- throw new Exception("Cows cannot eat "+food);
- }
- System.out.println("nooom nooom nooom");
- }
- public String toString(){
- return "Cow which is " + getAge() + " years old.";
- }
- }
- ////////////////////////////////////////////////////////////////////////////////// Monkey
- public class Monkey extends AnimalImpl{
- public Monkey(){
- super(15);
- }
- public String animalKinde(){
- return "Monkey";
- }
- public boolean canEat(String food){
- if (food.equals("Banana")){
- return true;
- }
- return false;
- }
- public void feed(String food) throws Exception{
- if (!this.canEat(food)){
- throw new Exception("Monkeys cannot eat "+food);
- }
- System.out.println("nom");
- }
- public String toString(){
- return "Monkey which is " + getAge() + " years old.";
- }
- }
- /////////////////////////////////////////////////////////////////////// Test Class
- public class TestAnimal {
- public static void feedMeat(Animal animal){
- try{
- animal.feed("Meat");
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- public static void main(String[] args){
- Animal cow=new Cow();
- Animal tiger=new Tiger();
- Animal monkey=new Monkey();
- for (int i=0; i<11; i++){
- try {
- cow.ageOneYear();
- System.out.println(cow);
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- try{
- feedMeat(tiger);
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- try{
- feedMeat(monkey);
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment