I'm working on a CyberPet assignment and I'm not sure how to do the following:

'Then write an encounter ( CyberPet ) method that allows one CyberPet to encounter another and will describe the encounter by returning a String data type. Note that the method should take a CyberPet parameter. If a small pet encounters a big pet, the method should return, “See yah!” If a large pet encounters a small pet, the method should return, “I’m going to eat you!” If they are the same size, the method should return, “Let’s go to McDonalds!”
For example: if pet 1 is big and pet2 is small, the following method pet1.encounter( pet2 ) should return “I’m going to eat you.”'

Here's my code so far:

public class CyberPet
{
   /*** Defaults/Constants ***/

   public static final String DEFAULT_SIZE         = "small";

   public static final boolean DEFAULT_IS_EATING   = false;
   public static final boolean DEFAULT_IS_SLEEPING = false;
   public static final boolean DEFAULT_IS_THINKING = true;


   /*** Instance variables ***/

   private String  name;
   private String  size;

   private boolean isEating;
   private boolean isSleeping;
   private boolean isThinking;

   /*** Constructor Methods ***/

   public CyberPet( String petName )
   {
      setName( petName );
      size       = DEFAULT_SIZE;

      isEating   = DEFAULT_IS_EATING;
      isSleeping = DEFAULT_IS_SLEEPING;
      isThinking = DEFAULT_IS_THINKING;
   }

   public CyberPet( String petName, String activity )
   {
      setName( petName );
      size       = DEFAULT_SIZE;
      currentActivity( activity );
   }

   public CyberPet( String petName, String size, String activity )
   {
      setName( petName );
      size       = size;
      currentActivity( activity );
   }

   /*** Transformer or Mutator Methods ***/

   public void setName( String petName )
   {
   	  name = petName;
   }

   public void eat()
   {
      isEating   = true;
      isSleeping = false;
      isThinking = false;
   }

   public void sleep()
   {
      isEating   = false;
      isSleeping = true;
      isThinking = false;
   }

   public void think()
   {
      isEating   = false;
      isSleeping = false;
      isThinking = true;
   }

   /*** Observor or Accessor Methods ***/

   public String getState()
   {
      String state = "Error in state";

      if ( isEating )
         state = "Eating";

      if ( isSleeping )
         state = "Sleeping";

      if ( isThinking )
      	 state = "Thinking";

      return state;
   }

   public String getName()
   {
   	  return name;
   }

   public String getSize()
   {
   	  return size;
   }

   /*** Helper methods ***/

   public void currentActivity( String activity )
   {
   	  if ( activity == "eating" )
   	  {
   	     eat();
   	  }
   	  else if ( activity == "sleeping" )
   	  {
   	  	 sleep();
   	  }
   	  else
   	  {
   	  	 think();
   	  }
   }

   public String encounter( CyberPet )
   {
      ???
   }
}

I don't even know how to go about creating the encounter method. This is just one of a number of things my professor decided to add on to the standard assignment from the textbook. How do I make two objects of the same type have the ability to interact before they're even instantiated (which they will be in the TestCyberPet class later)? I'm not looking to have someone write the method for me, just some ideas on how to go about doing this or where to look for more information that will help me. Any help would be greatly appreciated! Thanks in advance!

In your method you have access to the size of the current CyberPet, and you also have access to the size of the other CyberPet that is passed as a parameter, so you can write a couple of if tests comparing those two things to decide what String to return. You may be looking for something difficult, but this is more simple than you realise.
Here's a starting point:
you method signature is invalid. It should be something like
public String encounter( CyberPet other )

Edited 5 Years Ago by JamesCherrill: n/a

Just as JamesCherrill has said, u're gonna have to make use of the CyberPet's size. An idea for your encounter() :

public String encounter( CyberPet x)
   {
      string output="Default string";
      /*if caller's size is greater than x's, 
      output = "I'd eat you now!!"
      else if caller's size is less than x's,
      output = "See ya!!"
      else if caller's size == x's size,
      output = "let's go to McDonalds!!"
      return output; */
   }

Edited 5 Years Ago by tkud: n/a

you method signature is invalid. It should be something like
public String encounter( CyberPet other )

Between what you both said and what I've gotten out of my professor (very little; he tends to get pissed and yell and usually doesn't even understand what we're asking) I think I understand for the most part. Now my only question is how do I make other refer to the second pet when calling from the first pet but refer to the first when I'm calling from the second? And what if I have three of them?

Your encounter can occur outside of the CyberPet class, using a collaboration pattern. Basically, create another class that has some method which accepts a list of CyberPet objects, compares them, and prints out (or returns) the result.

Your encounter can occur outside of the CyberPet class, using a collaboration pattern. Basically, create another class that has some method which accepts a list of CyberPet objects, compares them, and prints out (or returns) the result.

While this may be the best or most efficient way, there are two problems:

First, I don't think I can do that using only what we've covered in class.

Second, I have to follow the specifications for the assignment. The specs say two classes; CyberPet and TestCyberPet .

Could I somehow use if statements so that it passes in the "other" pet and then just live with it only being capable of handling two pets at a time? Are there other options other than this and making a new class?

Then, in your TestCyberPet class, instantiate two CyberPet objects. Your "encounter" method signature should look something like

public String encounter( CyberPet aCyberPet) {
...
}

In the encounter method, the incoming CyberPet's size with it's own, and print the result. This way, your test method can decide which CyberPet's instance is "contolling" the encounter; IOW, you could pass a large CyberPet to a small one, or vise-versa.

I figured it out. I had been trying to make it more difficult than it was. Here's what I came up with:

public String encounter( CyberPet otherPet )
   {
      String phrase;

      if ( getSize() == "small" )
      {
      	 if ( otherPet.getSize() == "big" )
      	 	phrase = "See yah!";

      	 else if ( otherPet.getSize() == "small" )
      	 	phrase = "Let's go to McDonalds!";

      	 else
      	 	phrase = "*** unable to determine size of other pet ***";
      }

      else if ( getSize() == "big" )
      {
      	 if ( otherPet.getSize() == "small" )
      	 	phrase = "I'm going to eat you!";

      	 else if ( otherPet.getSize() == "big" )
      	 	phrase = "Let's go to McDonalds!";

      	 else
      	 	phrase = "*** unable to determine size of other pet ***";
      }

      else
      	 phrase = "*** unable to determine size of " + getName();

      return phrase;
   }
This question has already been answered. Start a new discussion instead.