In: Computer Science
Java.
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.
Hi,
Sample java classes below:
Overall structure of the code:
////////////////
public abstract class Pet {
private String hunger;
private String happiness;
private String name;
/**
* @param hunger
* @param happiness
* @param name
*/
public Pet(String name) {
super();
this.hunger ="";
this.happiness ="";
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the hunger
*/
public String getHunger() {
return hunger;
}
/**
* @param hunger the hunger to set
*/
public void setHunger(String hunger) {
this.hunger = hunger;
}
/**
* @return the happiness
*/
public String getHappiness() {
return happiness;
}
/**
* @param happiness the happiness to set
*/
public void setHappiness(String happiness) {
this.happiness = happiness;
}
}
/////////////////
public class Dog extends Pet implements Interact {
private int age;
/**
* @param hunger
* @param happiness
* @param name
* @param age
*/
public Dog(String name, int age) {
super(name);
this.age = age;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
@Override
public void feed() {
// TODO Auto-generated method
stub
}
@Override
public void play() {
// TODO Auto-generated method
stub
}
@Override
public void sleep() {
// TODO Auto-generated method
stub
}
}
/////////////////////////
/**
*
*/
public interface Interact {
public void feed() ;
public void play() ;
public void sleep() ;
}
///////////////////////
import java.util.Scanner;
public class PetMain {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner input=new
Scanner(System.in);
int choice;
Dog dog=null;
String petName="";
int petAge=0;
System.out.println("Interact with
Pet..");
System.out.println("1.Create
Pet..");
System.out.println("2.Feed
Pet..");
System.out.println("3.Play with
Pet..");
System.out.println("4.Make Pet
Sleep..");
choice=input.nextInt();
switch(choice) {
case 1:
System.out.println("Enter Pet name..");
petName=input.next();
System.out.println("Enter Pet age..");
petAge=input.nextInt();
dog=new
Dog(petName,petAge);
break;
case 2:
dog.feed();
break;
case 3:
dog.play();
break;
case 4:
dog.sleep();
break;
}
}
}
/////////////////////////////////////////
Kindly, provide some implementation to the methods.
Let me know if you need more help on this.
Hope this helps.