virtual pet

A VPet is a virtual pet that must be taken care of by an owner.

Page 1 of 1

9 Replies - 664 Views - Last Post: 16 March 2010 - 05:29 PM Rate Topic: -----

#1 twistlive  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 7
  • Joined: 11-December 08

virtual pet

Posted 16 March 2010 - 03:35 PM

A VPet is a virtual pet that must be taken care of by an owner. Each owner can have a number of pets which he/she has to take care of. The owner must feed the pets or they will go hungry and die. Some pets need different types of care, eg cats need to be played with. There are two sections in this assignment; you will develop the base classes for the program in the first section and then the graphical user interface for the classes in the second part of the assignment.

SECTION 1 - The following classes should be implemented:

Pet class - All pets have a number of characteristics:

petID – a unique identifier for each pet.
Name – the name of the pet.
Health – health condition of the pet.
Hunger – hunger level of the pet
Age – the age of the pet in hours.
uniquePetID – a static int variable which is unique for every pet.

Every hour the pet grows and its age as well as its hunger level will increase by one. There must be method to feed the pet and one to find out if the pet is still alive. At least one of the methods of the pet must be abstract. The initial health of all pets is 100 points; the initial age is 0 hours and the initial hunger level is 0.

The health should never go above 100 and the hunger level should not go above 20. A pets health and hunger level cannot go below 0. If the health is 0, it should be set to 0. The pet is dead if the health of the pet is zero. You are required to implement two types of the pet.

Cat class - Cats cannot be hungry for a long time, so after 5 hours they must be feed or they will start to lose health at 10points per hour. Cats also have happiness points. Feeding a cat will increase its happiness points by 5 points. Cats need attention trherefore for every hour they are not played with; their happiness will be reduced by 10points per hour. Cats alos have happiness points. Feeding a cat wil increase its happiness points by 5points. Cats need attention therefore for every hour they are not played with; their happiness will be reduced by 10points. Playing with the cat will give it 20 points of happiness. The initial happiness of the new cat is 100 points and the happiness should never go above 100. If the happiness of the cat goes below 20points, its health will reduced by 5 points every hour. A cat happiness never goes below zero, so any happiness below zero will be set to zero. Cats only live to the age of 100 hours. It their age is above 100 hours of their health is 0, they die.

TropicalFish class - Fish also go hungry but not as quickly as cats. Fish only need to be feed after 12 hours and if you forget to feed them, they will only lose 2 points of health per hour. When the fish are feed, they will lose their hunger and gain 1 point of health. Tropical fish must also be kept warm and the heater must be changed depending on the temperature of the water. The fish can only survive at temperature of between 20 and 40 degrees. If the heater is left on, its temperature will rise by 1 degree every hour. But if it is left off, it will increase by 1 degree very hour. For every hour, the fish outside their temperature range lose 20 points of health. Fish live to the age of 60 hours.



Owner class - The owners of the pets have the following characteristics:
ownerID – a unique identifier for the owner
username – the username of the owner
Pets – an woner owns a number of pets. (use a Hash Map for this)

Each owner can have a number of different pets. The owner should be able to add new pets and remove dead pets from the pets they own. The owner must alos be able to access the status of their pets’ variables. The owner class will take care of growing all the pets in the owners care as well. Pets grow every hour, so the owner class must provide a growPet method which calls the grow method for each pet and removes the dead pets when they die. The method will be executed every hour by the user interface program. The owner class must also provide a method getPet for returning a pet based on the petID and the method getPets that returns all the owners pets in an array.
A method saveOwner() and a static method loadOwner(String ownerUsername) should be provided to save and load the owner and pets from file using the following file format.


File format - The name of the file for each owner will be <username>.txt. The first line of the file will contain the owner’s username and a name separated by an “;” character. The second line will contain the unique petID variable which is incremented every time a new pet is created. The following lines wil list the pets. Each pet will be listed pon a separate line in the following format. Note the separating characters.

For cat - Cat;petID, name, health, hunger, age, happiness

For TropicalFish - TropicalFish;petID,name,health,hunger,age, temperature;heaterState

Requirements - The following classes should be implemented with appropriate methods and attributes as part of the new virtuPet package: Pet,TropicalFish,Cat,Owner.
At least one of the classes must be an abstract class with at least one abstract method and all classes will have one or more constructors.

The DeadPetException class must be created in the virtuPet package and will be used by the GUI.

Provide correct input and output from files.
Create the html javadoc for the virtuPet package.

SECTION 2 - Your application will execute for one owner at one time, the owner’s username and a program time interval will be passed in at the command line. If the owner is new then no user name should be given and a new owner will be created and a default time interval of 10 seconds is used. The time interval represents the real time it takes for 1 hour to elapse for the pet. For example, if 30 is entered at the command line, every 30 seconds represent 1 hour has passed. The ClockThread which is provided will take care of the timing for you and execute the ManagePetFrame.update() method at every time interval. The state of each owner’s pets is stored in a file which is loaded and saved when the application starts and exits.


Create Owner Frame - This frame allows a new owner to be created by entering a username and name as shown in the screen sot. A GridLayout could be used for this frame. When the Create Owner Button is pressed, the Manage Pet Frame should be created and the create Owner Frame should disappear. If the cancel button is pressed, the application should exit.

Create Pet Frame - This frame appears when the Create Pet Button is pressed in the Manage Pet Frame. This frame allows new pets to be created for the current owner. This frame may also use GridLayout. The pet type selector must be a JComboBox. Once the Create Pet Button is pressed, a new pet should appear in the manage Pet Frame. If the Cancel button is pressed, no new pet should be created.

Manage Pet Frame - The Manage Pet Frame is the main frame of the game. It contain two panels, a setting panel and a pets panel. The setting panel allows the owner to create new pets, save and exit the application. It also shows the current time in hours from when the application is started. The pets panel is where the pets that the owner owns are displayed. Each pet has its own panel within the pets panel. Each type of pet will extend the basic ppet panel. The pet panels must contain an update() method which will be called by the ClockThread every hour.

Pet Panel - The pet panel is an abstract class that extends Panel. A grid layout could be used to separate the three sections of the pet panel. The first section has an image of the pet with the pets ID and name on top of it as shown in the screen shot. The second section has a number of JProgressBar items which represent the health, hunger and age of the pet. The third section has the feed button and any other buttons the pet needs. Each of these three sections could use a BoxLayout to display the items. The pet panel should also contain an update() method that updates the progress bar when it is called.

Cat Panel - The cat panel should extend the pet panel and add a happiness progress bar and a play button as shown in the screen shot.

Fish panel - The fish should extend thePet Panels as well, but it should add a temperature progress bar and a heater on/off button.

Death of a Pet - If a pet dies, all of its button should no longer work. If the owner tries to feed, play or turn the heater on/off, a JOptionPane should be created and it should display a message that the pet is dead. The error message must include the Pets’ name. dead pets should not be save to file when the program exits.

Requirements -
1. The following classes should be implemented with appropriate methods and attributes: PetGame, PetPanel, CatPanel, FishPanel, CreateOwnerFrame, CreatePetFrame, ManagePetFrame.
2. The PetGame class should use the virtuPet package.
3. At least one of the class must be an abstract class with at least one abstract method.
4. All classes including PetGame, which has the main method, will have one or more constructors.
5. An error message should be displayed if the user tries to interact with a dead pet.

Is This A Good Question/Topic? -3
  • +

Replies To: virtual pet

#2 spor  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 9
  • Joined: 16-March 10

Re: virtual pet

Posted 16 March 2010 - 03:37 PM

Are you just posting your homework? That's the equivalent of turning up on a bricklaying forum and requesting a house.
Was This Post Helpful? 4
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,244
  • Joined: 27-December 08

Re: virtual pet

Posted 16 March 2010 - 03:41 PM

@twistlive: Rule #1 states that we will not do your homework, so do not ask us to give you the code. We will be happy to help you debug code YOU wrote, but we will not write it for you.

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
Was This Post Helpful? 1
  • +
  • -

#4 twistlive  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 7
  • Joined: 11-December 08

Re: virtual pet

Posted 16 March 2010 - 03:52 PM

here are some of the program created :-

PetGame.java

public class PetGame {

public static void main(String args[]) {

ManagePetFrame manageFrame;

manageFrame = new ManagePetFrame("awang", 1);
manageFrame.pack();
manageFrame.setVisible(true);
}
}


------------------------------------------------------------

Pet.java


public abstract class Pet {

static int uniquePetID;

public Pet() {
}

public static void setUniquePetID(int petID) {
uniquePetID = petID;
}

public static void incUniquePetID() {
uniquePetID++;
}

public static int getUniquePetID() {
return(uniquePetID);
}

}

-------------------------------------------------------------------

Owner.java

import java.io.*;
import java.util.*;

public class Owner {

// Missing Attributes


// Lots of missing methods


/** Grows all the pets that this owner owns, this should be
* called every time unit
*/
public void growPets() {
}

/** Returns all the pets the owner owns in an array.
* @return the pets the owner owns in an array
*/
public Pet []getPets() {
return null;
}

/** Returns the pet that the owner owns with the given petID
* @param petID the pets ID.
*/
public Pet getPet(int petID) {
return null;
}

/** Saves this owner to file including the owners pets
*/
public void saveOwner() {
}

/** Creates an Owner object and loads its attributes from a file.
* This includes loading pets.
* @param ownerUserName the username of the owner
*/
public static Owner loadOwner(String ownerUserName) {
return null;
}

}


-----------------------------------------------------------------------

ManagePetFrame.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ManagePetFrame extends JFrame implements ActionListener {

JPanel settingsPanel = new JPanel();
JButton createPetButton;
JButton saveExitButton;
JTextField timeField;

int hour = 0;
Owner owner;

public ManagePetFrame(String ownerUserName, long timeInterval)
{
this(Owner.loadOwner(ownerUserName), timeInterval);
}

public ManagePetFrame(Owner owner, long timeInterval)
{
super();
this.owner = owner;

Container container = getContentPane();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));

createSettingsPanel();

container.add(settingsPanel);

startClock(timeInterval);
}

/**
*
* * Spawns a new thread which acts as a clock.
*
*/

private void startClock(long timeInterval){

ClockThread clock = new ClockThread(timeInterval, this);
clock.start();
}

public void createSettingsPanel() {

settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS));

JPanel timePanel = new JPanel();
JLabel theTime = new JLabel("Current Time (hours):");
timeField = new JTextField(3);
timeField.setEditable(false);
timeField.setText("" + hour);

timePanel.add(theTime);
timePanel.add(timeField);

JPanel buttonPanel = new JPanel();
createPetButton = new JButton("Create Pet");
saveExitButton = new JButton("Save and Exit");
buttonPanel.add(createPetButton);
buttonPanel.add(saveExitButton);

settingsPanel.add(timePanel);
settingsPanel.add(buttonPanel);

saveExitButton.addActionListener(this);
createPetButton.addActionListener(this);
}

public void update() {
hour++;
timeField.setText("" + hour);
}

public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if (cmd.equals("Save and Exit")) {
//owner.saveOwner();
System.exit(0);
}
}

}



-----------------------------------------------------------------------

FishImage.java


import java.awt.*;
import javax.swing.*;

public class FishImage extends JPanel
{
Toolkit tk = Toolkit.getDefaultToolkit();
Image theImage = tk.getImage("fish.jpg");

public void paint(Graphics g)
{
g.drawImage(theImage, 0, 0, 68, 40, this);
setSize(68, 40);
setVisible(true);
}
}


------------------------------------------------------------------------

ClockThread.java



/**
* Updates all pets of an owner every specified unit of time.
*/
public class ClockThread extends Thread {

private ManagePetFrame manage;
private long unit;

/**
*/
public ClockThread(long seconds, ManagePetFrame manage)
{
this.unit = seconds * 1000;
this.manage = manage;
}

public void run(){

while(true){
try {
Thread.sleep(unit);

if (manage != null) {
manage.update();
}

} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

}


---------------------------------------------------------------------------
CatImage.java

import java.awt.*;
import javax.swing.*;

public class CatImage extends JPanel
{
Toolkit tk = Toolkit.getDefaultToolkit();
Image theImage = tk.getImage("cat.jpg");

public void paint(Graphics g)
{
g.drawImage(theImage, 0, 0, 68, 40, this);
setPreferredSize(new Dimension(68,40));
setVisible(true);
}
}


Appreciate if u can help me with the rest of the program.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,244
  • Joined: 27-December 08

Re: virtual pet

Posted 16 March 2010 - 03:54 PM

Please, :code:
Also, please specifically describe the error(s) or problem(s) you are encountering. Thanks for helping us help you! :)
Was This Post Helpful? 0
  • +
  • -

#6 twistlive  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 7
  • Joined: 11-December 08

Re: virtual pet

Posted 16 March 2010 - 03:57 PM

PetGame.java

public class PetGame {

public static void main(String args[]) {

ManagePetFrame manageFrame;

manageFrame = new ManagePetFrame("awang", 1);
manageFrame.pack();
manageFrame.setVisible(true);
}
}



------------------------------------------------------------

Pet.java

public abstract class Pet {

static int uniquePetID;

public Pet() {
}

public static void setUniquePetID(int petID) {
uniquePetID = petID;
}

public static void incUniquePetID() {
uniquePetID++;
}

public static int getUniquePetID() {
return(uniquePetID);
}

}


-------------------------------------------------------------------

Owner.java

import java.io.*;
import java.util.*;

public class Owner {

// Missing Attributes


// Lots of missing methods


/** Grows all the pets that this owner owns, this should be
* called every time unit
*/
public void growPets() {
}

/** Returns all the pets the owner owns in an array.
* @return the pets the owner owns in an array
*/
public Pet []getPets() {
return null;
}

/** Returns the pet that the owner owns with the given petID 
* @param petID the pets ID.
*/
public Pet getPet(int petID) {
return null;
}

/** Saves this owner to file including the owners pets 
*/
public void saveOwner() {
}

/** Creates an Owner object and loads its attributes from a file.
* This includes loading pets.
* @param ownerUserName the username of the owner
*/
public static Owner loadOwner(String ownerUserName) {
return null;
}

}



-----------------------------------------------------------------------

ManagePetFrame.java


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ManagePetFrame extends JFrame implements ActionListener {

JPanel settingsPanel = new JPanel();
JButton createPetButton;
JButton saveExitButton;
JTextField timeField;

int hour = 0;
Owner owner;

public ManagePetFrame(String ownerUserName, long timeInterval)
{
this(Owner.loadOwner(ownerUserName), timeInterval);
}

public ManagePetFrame(Owner owner, long timeInterval) 
{
super();
this.owner = owner;

Container container = getContentPane();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));

createSettingsPanel();

container.add(settingsPanel);

startClock(timeInterval);
}

/**
*
* * Spawns a new thread which acts as a clock. 
*
*/

private void startClock(long timeInterval){

ClockThread clock = new ClockThread(timeInterval, this);
clock.start();
}

public void createSettingsPanel() {

settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS));

JPanel timePanel = new JPanel();
JLabel theTime = new JLabel("Current Time (hours):");
timeField = new JTextField(3);
timeField.setEditable(false);
timeField.setText("" + hour);

timePanel.add(theTime);
timePanel.add(timeField);

JPanel buttonPanel = new JPanel();
createPetButton = new JButton("Create Pet");
saveExitButton = new JButton("Save and Exit");
buttonPanel.add(createPetButton);
buttonPanel.add(saveExitButton);

settingsPanel.add(timePanel);
settingsPanel.add(buttonPanel);

saveExitButton.addActionListener(this);
createPetButton.addActionListener(this);
}

public void update() {
hour++;
timeField.setText("" + hour);
}

public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if (cmd.equals("Save and Exit")) { 
//owner.saveOwner();
System.exit(0);
}
}

}




-----------------------------------------------------------------------

FishImage.java


import java.awt.*;
import javax.swing.*;

public class FishImage extends JPanel
{
Toolkit tk = Toolkit.getDefaultToolkit();
Image theImage = tk.getImage("fish.jpg");

public void paint(Graphics g)
{
g.drawImage(theImage, 0, 0, 68, 40, this);
setSize(68, 40);
setVisible(true);
}
}



------------------------------------------------------------------------

ClockThread.java


/**
* Updates all pets of an owner every specified unit of time.
*/
public class ClockThread extends Thread {

private ManagePetFrame manage;
private long unit;

/**
*/
public ClockThread(long seconds, ManagePetFrame manage) 
{
this.unit = seconds * 1000;
this.manage = manage;
}

public void run(){

while(true){
try {
Thread.sleep(unit);

if (manage != null) {
manage.update(); 
}

} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

}




---------------------------------------------------------------------------
CatImage.java


import java.awt.*;
import javax.swing.*;

public class CatImage extends JPanel
{
Toolkit tk = Toolkit.getDefaultToolkit();
Image theImage = tk.getImage("cat.jpg");

public void paint(Graphics g)
{
g.drawImage(theImage, 0, 0, 68, 40, this);
setPreferredSize(new Dimension(68,40));
setVisible(true);
}
}




Appreciate if u can help me with the rest of the program.
Was This Post Helpful? -2
  • +
  • -

#7 spor  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 9
  • Joined: 16-March 10

Re: virtual pet

Posted 16 March 2010 - 03:58 PM

As macosxnerd101 said, we're not here to write huge chunks of code for you, but rather to help with particular problems you might be having. Is there a particular area you're stuck on or is this just example code you've been given and you can't be bothered to write the rest?
Was This Post Helpful? 2
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,244
  • Joined: 27-December 08

Re: virtual pet

Posted 16 March 2010 - 04:14 PM

@twistlive: Homework or not, nobody is going to help you if you don't help us. This is the third time we've asked you to describe your errors. Also, please indent your code properly. No one is going to read large sections of unindented code.
Was This Post Helpful? 1
  • +
  • -

#9 tectonic.software  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 88
  • Joined: 12-February 10

Re: virtual pet

Posted 16 March 2010 - 04:16 PM

View Postmacosxnerd101, on 16 March 2010 - 03:14 PM, said:

Also, please indent your code properly. No one is going to read large sections of unindented code.

http://www.prettyprinter.de
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: virtual pet

Posted 16 March 2010 - 05:29 PM

I think spor and macosxnerd101 summed it up well:

Quote

As macosxnerd101 said, we're not here to write huge chunks of code for you, but rather to help with particular problems you might be having. Is there a particular area you're stuck on or is this just example code you've been given and you can't be bothered to write the rest?


If you need help with code, please post the code AND ASK A QUESTION, don't just ask for code. If you need help and follow the following rules, please start your own topic.

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you. Also, make sure that your code is nice an indented correctly or we can't read it.

Topic Closed.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1