How write Java code for various pets with different properties?

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Ok, so you can choose which 3 pets you want to create.

    I will give you an example of 1 and let you do the other 2.

    Let's say we have a cat.

    What is something a cat can do? It can purr. So let's create a private variable to say if the cat is purring or not.

    public class Cat extends Pet

    {

    private boolean purring;

    // You need getter and setter methods for purring.

    boolean getPurring()

    // Although, since it is a boolean method, it should be written as

    // boolean isPurring()

    {

    return purring;

    }

    void setPurring(boolean purr)

    {

    purring = purr;

    }

    // That is that out of the way.

    // You need to overwrite the eat method

    void eat()

    {

    if (getPurring())

    // OR

    // if (isPurring())

    {

    System.out.println("Purr, purr, purr");

    }

    System.out.println("Much, munch, munch"); // change this if you want.

    if (getPurring())

    // OR

    // if (isPurring())

    {

    System.out.println("Purr, purr, purr");

    }

    // can you think of something that may happen since the cat has eaten?

    // Hint: does it gain weight?

    // Add something in here if you think you should.

    }

    }

    That is the end of the cat class. Think of 2 other pets and do similar things.

  • 1 decade ago

    //I think Mr. Chun is clear in his assignment.

    //You can have a

    fish class

    //, a

    dog class

    //, and a

    monkey class

    //The above classes will inherit from the pet class the the eat method().

    //The fish class will have its on unique instance variable

    private String swim;

    //the dog class will have

    private String run;

    //the monkey class will have

    private String swingToBranch;

    etc.

    the point is to make sure to understand the importance of inheritance.

    Source(s): www.vikta.org
Still have questions? Get your answers by asking now.