Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

  1. Home >
  2. All Categories >
  3. Computers & Internet >
  4. Programming & Design >
  5. Resolved Question
shan_ali36 shan_ali...
Member since:
January 03, 2007
Total points:
363 (Level 2)

Resolved Question

Show me another »

How write Java code for various pets with different properties?

Mark aka jack573 by Mark aka jack573
Member since:
April 01, 2006
Total points:
55,435 (Level 7)

Best Answer - Chosen by Voters

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.
100% 1 Vote

There are currently no comments for this question.

Other Answers (1)

  • Sir by Sir
    Member since:
    September 07, 2006
    Total points:
    608 (Level 2)
    //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
    0% 0 Votes
    • 1 person rated this as good

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback