Question

Use Java and please try and show how to do each section. You are creating a 'virtual pet' program...

Use Java and please try and show how to do each section.

You are creating a 'virtual pet' program. The pet object will have a number of attributes, representing the state of the pet. You will need to create some entity to represent attributes in general, and you will also need to create some specific attributes. You will then create a generic pet class (or interface) which has these specific attributes. Finally you will make at least one subclass of the pet class which will be a specific type of pet, and at least one instance of that class.

Attributes
An attribute is a characteristic of a pet. Each attribute is essentially a list of values. For example, a hunger attribute may have values from "famished" through "content" to "bloated." There should be some way to check the value of the attribute, as well as to increase and decrease the value. Note that you will be expected to create some sort of abstract data type (ADT) to represent the attribute. You are not yet building SPECIFIC attributes (like hunger or happiness) but a type that represents the common characteristics of all attributes. You might use an interface or an abstract class to generate your abstraction, but plan it first as an abstract data type.

Specific Attributes
Once you have created a generic attribute class, make some subclasses to represent the specific attributes you want your pets to have. If you designed the ADT well, creating specific subclasses should be quite easy. The main method of the specific classes should test the main functionality of the class.

Making your abstract pet
Now create a pet class that uses the attributes you have generated. This class is also abstract, as it represents just a type of pet. It will include attributes as data members, and it may also have other characteristics. You may also add methods that indicate actions the user can take with the pet, including things like feed and play (which may affect attributes) and perhaps other activities like rename (which might change another data member of the pet) and sleep (which might indicate the passage of time.)

Build a specific pet class
Finally you should be able to build a specific type of pet (like a lizard or a unicorn) that inherits from the pet class. This should begin (of course) with characteristics derived from the abstract pet, but you could then add new attributes or behaviors specific to your type of pet.

The main method of this pet class should instantiate an instance of the pet and indicate all the things it can do.

Create an interface for interacting with the pet.
Build some type of tool for interacting with the pet. At minimum this should allow the user to create a pet, interact with it in the ways you have defined, save its current status for future play (using object serialization) and load a previously saved pet.

0 1
Add a comment Improve this question Transcribed image text
Answer #1

package InheritanceAbstract;

// Abstract class defined

abstract class Pet

{

// Instance variable to store pet data

String name;

String color;

// Parameterized constructor to assign

// parameter data to instance variable

Pet(String na, String co)

{

name = na;

color = co;

}// End of parameterized constructor

// Overrides toString() method to return pet information

public String toString()

{

return "\n Name: " + name + "\n Color: " + color;

}// End of method

// Abstract method

abstract String canDo();

abstract String sound();

}// End of abstract class Pet

// Defines a class Cat derived from Pet class

class Cat extends Pet

{

// Instance variable to store pet data

boolean available;

// Parameterized constructor to assign

// parameter data to instance variable

Cat(String na, String co, boolean av)

{

super(na, co);

available = av;

}// End of parameterized constructor

// Overrides method to return cat sound

public String sound()

{

return "Miauw";

}// End of method

// Overrides the method to return what can can do

String canDo()

{

// Checks if cat is available then returns "Dance"

if(available)

return "Dance.";

// Otherwise not available returns "Kill rat."

else

return "Kill rat.";

}// End of method

// Overrides toString() method to return pet information

public String toString()

{

String res = "";

// Checks if available then assign message "Available"

if(available)

res = "Available";

// Otherwise not available assign message "Not Available"

else

res = "Not available";

// Calls the base class toString() method

// and concatenates available status

return super.toString() + "\n Available Status: " + res;

}// End of method

}// End of class Cat

// Defines a class Dog derived from Pet class

class Dog extends Pet

{

// Instance variable to store pet data

String breed;

// Parameterized constructor to assign

// parameter data to instance variable

Dog(String na, String co, String br)

{

super(na, co);

breed = br;

}// End of parameterized constructor

// Overrides method to return cat sound

public String sound()

{

return "Woof";

}// End of method

// Overrides the method to return what can can do

String canDo()

{

// Checks if breed is "pamorian" then returns "Play cricket."

if(breed.compareToIgnoreCase("pamorian") == 0)

return "Play cricket.";

// Otherwise checks if breed is "dogerman" then returns "Swim."

else if(breed.compareToIgnoreCase("dogerman") == 0)

return "Swim.";

// Otherwise returns "Dance"

else

return "Dance.";

}// End of method

// Overrides toString() method to return pet information

public String toString()

{

return super.toString() + "\n Breed: " + breed;

}// End of method

}// End of class Dog

// Driver class definition

public class AnimalTest

{

// main method definition

public static void main(String ss[])

{

// Declares an array of object of class Pet of size 4

Pet myPet[] = new Pet[4];

// Creates two cats using parameterized constructor

myPet[0] = new Cat("Pusy", "white", true);

myPet[1] = new Cat("Buly", "black", false);

// Creates two dogs using parameterized constructor

myPet[2] = new Dog("Stalin", "brown", "pamorian");

myPet[3] = new Dog("Banodi", "white", "dogerman");

// Loops till number of Pets

for(Pet pp : myPet)

{

// Displays information

System.out.println(pp);

System.out.println(" My pet sounds: " + pp.sound());

System.out.println(" My pet can: " + pp.canDo());

}// End of for loop

}// End of main method

}// End of driver class

Sample Output:


Name: Pusy
Color: white
Available Status: Available
My pet sounds: Miauw
My pet can: Dance.

Name: Buly
Color: black
Available Status: Not available
My pet sounds: Miauw
My pet can: Kill rat.

Name: Stalin
Color: brown
Breed: pamorian
My pet sounds: Woof
My pet can: Play cricket.

Name: Banodi
Color: white
Breed: dogerman
My pet sounds: Woof
My pet can: Swim.

Add a comment
Know the answer?
Add Answer to:
Use Java and please try and show how to do each section. You are creating a 'virtual pet' program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT