Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Inheritance Program Question

    Hey guys, I'm extremely new to Java and this is my second program I have to write for the semester. Here are the instructions (very long sorry!):
    Here is a description of the classes you will implement:

    Animal is an abstract class whose constructor should take as parameters a Zoo object called myZoo (more on that in Activity 2) representing the zoo that animal is located in, and a String that is the name of the animal. The class should also include a field of type int called hungerLevel (0 is full and 10 is starving) that is initialized to 0.
    Make sure to include get/set methods for hungerLevel and name. The setter methods should check to make sure that none of their variables is being set outside the above parameters and correct them if they are (ie, trying to set hungerLevel to 11 would result in it being set to 10, and trying to set it to -4 would result in it being set to 0).


    Additional methods in the Animal class include a sleep() method that simply println's "sleeping..." and maximizes the hungerLevel. The class also has a roam() method that simply println's "moving around..." and increases hungerLevel by one. The class has an abstract makeNoise() method and an abstract eat() method. Only the get methods described above should return anything. Only the set methods described above should take a parameter.

    Canine is an abstract class that overrides the roam() method to println "like canines roam in packs..." (and still decreases hungerLevel by one). Like the Animal class, its constructor contains the parameters Zoo myzoo and name. This is true for every Animal sub-class.
    Feline is an abstract class that overrides the roam() method with “felines like to roam alone…”
    Dog is a concrete canine class. The method makeNoise() should "bark...". The method eat() should "slop..." and decreases hunger level by three using mutator and accessor.
    Wolf is a concrete canine class. The method makeNoise() should "growl...". The method eat() should "rip with teeth..." and decreases hunger by two.
    Cat is a concrete feline class. The method make Noise() should “meow…”. The method eat() should “pick…” and decrease the hunger level by three.
    Hippo is a concrete animal class. The method makeNoise() should "blub...". The method eat() should "slurp..." and decreases hunger by one.
    Add a Coyote class (think "howl" and "gnaws").
    Add a Lion class (think “roar” and “rip with teeth …”, just like the wolf
    Add a FeralCat and WildDog class similar to the Cat and Dog classes above.

    Activity 2 - Polymorphism
    Write a Zoo class whose constructor takes a String as the name of the zoo and two doubles: the latitude and longitude. This class will contain your main method which creates a new Zoo object. Add one animal object of each kind of animal, using the Zoo object you created in the main method as the myZoo parameter for each animal. Use a field of type ArrayList<Animal> called zooAnimals to hold these animals. Choose an appropriate name of each animal that clearly indicates the type of animal, such as Leo the Lion. Create a testAnimals() method which prints the name of the zoo, the location of the zoo in latitude and longitude, the number of animals in the zoo, and uses a for-each loop to print the behavior for each animal. First have them all go to sleep(). They will then be hungry (sleep changes hungerLevel, remember!) so have them makeNoise() and then eat(). After eating they are full and will roam().

    There, you’ve tested all the methods on all the objects! Add any other tests you think you might find appropriate. Each of these method calls are polymorphic since you get an Animal out of the zooAnimals ArrayList and call the method but not really knowing (or caring) the specific type of animal.


    Activity 3 - Interfaces
    Add a Pet interface that defines a play() and beFriendly() method to the Cat and Dog classes.The feral cat and wild dog will not implement this interface.
    Have the appropriate animal(s) above implement the interface (just println something appropriate) and then test this out in your Zoo class. Because not all animals will implement the Pet interface, you’ll have to use the instanceof keyword to determine if the current Animal contains the play() and beFriendly() methods.
    Add an AnimalLocation interface. The AnimalLocation interface should define the following methods: String getName(), setName(String), int getNumOfAnimals(). Now modify your Zoo class to implement this interface.


    Okay, so just to clarify and put myself on the right track, does this mean I need to make a separate class for each animal? For example, would I have the main class Animal, and then separate classes for each animal such as canine, feline, cat, dog, zoo, pet, etc?

    Any help is appreciated

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Inheritance Program Question

    If you are unsure what the assingment is asking then why are you asking a bunch of strangers on the net? If I was you I would seek clarification from the teacher.

  3. #3
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: Inheritance Program Question

    Because my teacher doesn't teach, we have to learn it ourselves. This week it is interfaces, and I'm confused about the basic concept. Do I need separate classes for each animal?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Inheritance Program Question

    I find it hard to believe that if you were to ask your teacher the very same question you asked us that they would just say "figure it out for yourself". Im sure if you ask for some guidance they would help you.

  5. #5
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: Inheritance Program Question

    Quote Originally Posted by Junky View Post
    I find it hard to believe that if you were to ask your teacher the very same question you asked us that they would just say "figure it out for yourself". Im sure if you ask for some guidance they would help you.
    I guess I could ask him tomorrow, I just wanted to figure it out for myself and get some work done on it tonight. Thanks though

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Inheritance Program Question

    Regardless, isn't the assignment reasonably self explanatory. It says to create an abstract Animal class. Have you done that? It also says to add a Canine class. Have you done that?

  7. #7
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: Inheritance Program Question

    Yep! I made and started writing basic methods for the main abstract class animal, abstract classes feline and canine, a class for each animal, the zoo class, and the pet and animal location interfaces. I was just wondering if you could "stack" the classes into one, because they're all extending/implementing each other anyways.

    Ex)
    Java Code:
    public abstract class Animal { 
    	// string
    	// zoo param 
    	// methods/constructors, etc 
    	
    	public abstract class Canine extends Animal { 
    			// string
    			// zoo param 
    			// methods/constructors, etc 
    			
    		public class Dog extends Canine implements Pet { 
    		// and so on... 
    		}
    	} 
    }
    This is probably ridiculous, but it seemed like it would work in theory...

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Inheritance Program Question

    Quote Originally Posted by simon.black325 View Post
    I was just wondering if you could "stack" the classes into one, because they're all extending/implementing each other anyways.
    Try it and see. What does the compiler say?

  9. #9
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: Inheritance Program Question

    Well I haven't finished writing all the classes (which I'm currently writing separately) yet, so I still have a lot of basic compiling errors I'm working on. I was just wondering if stacking them was even allowed before I tried it, or if that was what I was supposed to be doing in the first place. Just curious I guess.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Inheritance Program Question

    You don't have to complete the classes to test them.
    Java Code:
    class Abc {
        class Def extends Abc {
            class Ghi extends Def {
    
            }
        }
    }
    Does that compile?

  11. #11
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: Inheritance Program Question

    It says "cannot reference this before super type constructor has been called"

    So I guess you can't do that?

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Inheritance Program Question

    While it is possible to have a class inside another class (inner or nested classes) I strongly urge you to place each class in its own file. I imagine that the person marking your assignment will have the same opinion and you will lose marks if you don't.

    Btw, the above code will compile if you remove the Ghi class.

Similar Threads

  1. inheritance question
    By Jaevko in forum New To Java
    Replies: 7
    Last Post: 08-31-2011, 08:30 AM
  2. A question about inheritance / polymorphism
    By gabriels in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 12:53 PM
  3. Question about inheritance
    By hei1233212000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 12:38 PM
  4. Generics & Inheritance Question
    By Lee Rhodes in forum Advanced Java
    Replies: 3
    Last Post: 07-03-2009, 06:04 AM
  5. a question about inheritance
    By litojs in forum New To Java
    Replies: 8
    Last Post: 11-10-2008, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •