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
  • Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design...

    Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...

  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • Please help me with the following question. This is for Java programming. In this assignment you...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • Using Python you will create a simple class pet which will contain: Type of pet (a...

    Using Python you will create a simple class pet which will contain: Type of pet (a string) Name of pet(a string) Age of pet (an int) Tricks (a list of tricks the pet can perform) Fleas of pet (an int) All of these data attributes except for the last one (fleas) will be unique to each object. The fleas will be common to all pets, as they like to travel. In your main part of your program, you will create...

  • Program in Java: Use​ ​the​ ​Factory​ ​pattern​ ​to​ ​address​ ​the​ ​following​ ​challenge... Your job is to...

    Program in Java: Use​ ​the​ ​Factory​ ​pattern​ ​to​ ​address​ ​the​ ​following​ ​challenge... Your job is to make it easy to obtain a Pet from a factory-like class: ● Pet is an abstract class that has 2 attributes ○ Name ○ Sound ● You should create 3 concrete types of pets ○ Parakeet (makes the sound "Tweet tweet") ○ Dog (makes the sound "Woof woof") ○ Lion (makes the sound "Roar roar") ● You should create a factory class that allows...

  • I want to know how to translate the design into a computer program write an interface...

    I want to know how to translate the design into a computer program write an interface class java and please put on the comments I would use the eclipse This lab is a stepping stone to project 01. The ADT Bag is a group of items, much like what you might have with a bag of groceries. The ADT Bag should have the following operations. You should use the same operation names that are shown below. • create an empty...

  • USING Java, create a program with the following: (I would like a different answer than the...

    USING Java, create a program with the following: (I would like a different answer than the answer on a similar question asked from someone else) 1-The first class is an abstract class called Weapon: a-Weapon has two different abstract methods: i-The first abstract method is void and is called attackType ii-The second abstract method is void and is called range b-Weapon has a constructor that accepts the weaponName c-Weapon also has a regular method that calculatesDamange and takes in attackType...

  • Can someone help me in java Net Beans IDE witht the code andjava comments. Thanks...

    Can someone help me in java Net Beans IDE witht the code and java comments. ThanksMany of these classes will just need to be copied from previous assignments. When you implement the Tree interface you may import the java.util.Iterator class. When you implement the AbstractBinaryTree class you may import the java.util.ArrayList class and the java.util.List class.In this project you will be doing the following:Create a NetBeans project named lab07 and ensure it is imported into your SVN.In this lab assignment...

  • I am having trouble with this assignment. Please help. For this assignment, you will select either...

    I am having trouble with this assignment. Please help. For this assignment, you will select either the Cat or Dog Java class from the UML diagram provided in Project One and implement it. Open the Virtual Lab by clicking on the link in the Virtual Lab Access module. Then open your IDE and create a new class. Use the Eclipse IDE and Downloading Files From Eclipse tutorials to help you with this project. Review the UML Class Diagram, paying special...

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