Cat Mouse Program freezes command prompt

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 4500 Views - Last Post: 18 September 2011 - 07:38 PM Rate Topic: -----

#1 svkrahnke   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-August 11

Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 12:17 PM

Hi there. So I started working on this program that simulates the battle between a cat and some mice. I think I have most of it figured out, however when I run the driver it freezes the command prompt without displaying anything and I have to restart it. Any suggestions would be appreciated!

Here is the driver:

import java.util.ArrayList;

public class KrahnkeStephanieWeek5CatMouse
{
     public static void main(String [] args)
     {

          Cat sylvester = new Cat();
          ArrayList<Mouse> mice = new ArrayList<Mouse>();
          mice.add(new Mouse());
          mice.add(new Mouse());
          mice.add(new Mouse());
          mice.get(0).setSex(true);
          mice.get(1).setSex(false);
          mice.get(2).setSex(false);

          while (mice.size() > 1 && mice.size() < 10)
          {
              for (Mouse m:mice)
              {
				  m.grow();
				  sylvester.grow();
				  Mouse.mate(mice);
				  sylvester.eat(mice);
              } // end for
          } // end while

		for (int i = 0; i<10; i++)
		{
			if (mice.size() >= 10)
			{
				System.out.println("Mice RULE, Cats Drool Mice Population: " + mice.size());
			} // end if
			else
			{
				System.out.println("Cats RULE, Mice Drool Cat Weight (in mice): %.2f" + sylvester.getWeight());
			} // end else
		} // end for


          System.out.println("Press any key to continue...");

     }
}



Here is the Mammal class:

public class Mammal
{
	private String name = "";
	private int age = 0;
	private double weight = 0.0;
	private boolean isMale = false;

	//public Mammal()
	//	{} // end mammal()


	public Mammal()
	{
		age = 1;
	} // end mammal()

	public void grow()
	{
		this.age = age++;
	} // end grow

	public void setName(String name)
	{
		this.name = name;
	} // end setName

	public void setAge(int age)
	{
		this.age = age;
	} // end setAge

	public void setWeight(double weight)
	{
		this.weight = weight;
	} // end setWeight

	public void setIsMale(boolean isMale)
	{
		this.isMale = isMale;
	} // end setIsMale

	public String getName()
	{
		return name;
	} // end getName

	public int getAge()
	{
		return age;
	} // end getAge

	public double getWeight()
	{
		return weight;
	} // end getWeight

	public boolean getIsMale()
	{
		return isMale;
	} // end getIsMale

} // end Mammal



Here is the Cat class:

import java.util.Random;
import java.util.ArrayList;

public class Cat extends Mammal
{
	Random random = new Random();

	public void eat(ArrayList<Mouse> mice)
	{
		double catWeight;
		double mouseWeight;
		int deadMouse;
		int percentage = (int) (Math.random() * 100);

		if (percentage >= 70)
		{
			deadMouse = (int) (random.nextInt(mice.size()));

			catWeight = this.getWeight();
			mouseWeight = (mice.get(deadMouse)).getWeight();
			this.setWeight(catWeight + mouseWeight);
		} // end if
	} // end eat

	public void grow()
	{
		int age = this.getAge();
		this.setAge(age++);

	} // end grow

} // end Cat



and Here is the Mouse class:

import java.util.ArrayList;
import java.util.Random;

public class Mouse extends Mammal
{
	boolean sex;

	public Mouse()
	{
		this.setIsMale(sex);
		this.setAge(1);
		this.setWeight(1);
	} // end mouse constructor

	public void grow()
	{
		int mouseAge = this.getAge();
		double mouseWeight = this.getWeight();
		this.setAge(mouseAge++);
		this.setWeight(mouseWeight + (mouseWeight * 0.01));
	} // end grow

	public static void mate(ArrayList<Mouse> mice)
	{
		Random random2 = new Random();
		int mouse1 = random2.nextInt(mice.size());
		int mouse2 = random2.nextInt(mice.size());

		if ((mice.get(mouse1)).getIsMale() == true && (mice.get(mouse2)).getIsMale() == false && (mice.get(mouse1)).getAge() > 1 && (mice.get(mouse2)).getAge() > 1)
		{
			int randomBabyAmount = random2.nextInt(4);
			for (int i = 0; i < randomBabyAmount; i++)
			{
				mice.add(new Mouse());
			} // end for
		} // end if
	} // end mate

	public void setSex(boolean sex)
	{
		this.setIsMale(sex);
	} // end setSex
} // end Mouse



Is This A Good Question/Topic? 0
  • +

Replies To: Cat Mouse Program freezes command prompt

#2 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 12:26 PM

So you never change the size in any of those methods or in the while loop at all. So since the size is 3, and never changes. You have created an infinite loop which is why it freezes. Realized the mate function brb new solution

This post has been edited by Fuzzyness: 13 September 2011 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 cmpshr   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 12:45 PM

When the Cat eats a mouse you do not remove that mouse from the ArrayList is that what you want ?
If you do so it will screw up your

for (Mouse m:mice)

loop

But when you add babies to the ArrayList you will screw up your loop.

eat() and mate() should return a boolean saying if the arrayList was changed. If it is the case, exit the loop and start it over gain

This post has been edited by cmpshr: 13 September 2011 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 svkrahnke   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-August 11

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 12:49 PM

View Postcmpshr, on 13 September 2011 - 12:45 PM, said:

When the Cat eats a mouse you do not remove that mouse from the ArrayList is that what you want ?
If you do so it will screw up your

for (Mouse m:mice)

loop



Well, I went into the eat method in the cat class and added:

mice.remove(deadMouse);



I also fixed the printf call for cats rule and mice drool and commented out the while, but all it is printing is "Cats RULE, Mice Drool Cat Weight (in mice): 1.01"
Was This Post Helpful? 0
  • +
  • -

#5 cmpshr   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 12:52 PM

A few println will show you the behavior

         while (mice.size() > 1 && mice.size() < 10)
          {
        	  System.out.println("Mouse.size() " + mice.size());
              for (Mouse m:mice)



		if (percentage >= 70)
		{
			deadMouse = (int) (random.nextInt(mice.size()));

			catWeight = this.getWeight();
			mouseWeight = (mice.get(deadMouse)).getWeight();
			this.setWeight(catWeight + mouseWeight);
			System.out.println("Silvester eats");
		} // end if


			int randomBabyAmount = random2.nextInt(4);
			System.out.println("Babies added: " + randomBabyAmount);
			for (int i = 0; i < randomBabyAmount; i++)
			{
				mice.add(new Mouse());
			} // end for



Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Mouse.size() 3
Mouse.size() 3
Mouse.size() 3
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Mouse.size() 3
Silvester eats
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Silvester eats
Mouse.size() 3
Mouse.size() 3
Was This Post Helpful? 1
  • +
  • -

#6 cmpshr   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 01:00 PM

Also avoid calling the arrayList get() method 4 times when 2 calls are enough

	public static void mate(ArrayList<Mouse> mice)
	{
		Random random2 = new Random();
		Mouse mouse1 = mice.get(random2.nextInt(mice.size()));
		Mouse mouse2 = mice.get(random2.nextInt(mice.size()));

		if (mouse1.getIsMale() == true && mouse2.getIsMale() == false && mouse1.getAge() > 1 && mouse2.getAge() > 1)
		{
			int randomBabyAmount = random2.nextInt(4);
			System.out.println("Babies added: " + randomBabyAmount);
			for (int i = 0; i < randomBabyAmount; i++)
			{
				mice.add(new Mouse());
			} // end for
		} // end if
	} // end mate



but, again, you cannnot add/remvove from a Collection (your ArrayList) while you are in an Iterator for that collection

for(Object o : Collection)

at least used an old fashionned for(int i = 0; i < mice.size(); i++)

This post has been edited by cmpshr: 13 September 2011 - 01:01 PM

Was This Post Helpful? 1
  • +
  • -

#7 svkrahnke   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-August 11

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 01:09 PM

Quote

but, again, you cannnot add/remvove from a Collection (your ArrayList) while you are in an Iterator for that collection

for(Object o : Collection)

at least used an old fashionned for(int i = 0; i < mice.size(); i++)


What do you mean you can't add or remove from an ArrayList while you are in an interator for it?

Your first tip to get rid of the multiple calls was very helpful!!
Was This Post Helpful? 0
  • +
  • -

#8 cmpshr   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 01:24 PM

Try this
	public static void main(String[] args) {
		ArrayList<Integer> al = new ArrayList<Integer>();
		
		for(int i = 1; i <= 5; i++)
			al.add(i);
		
		int k = 0;
		for(Integer i : al) {
			// at first iteration let's add elements
			if(k == 0)
				al.add(100);
			
			System.out.println(k++ + ") " + i);
		}
	}


and you will have a ConcurrentModificationException

When you use the JRE 1.5 new feature for(Object o : Collection)
the code behind is to make an Iterator on the Collection and you cannot modify the Collection while you iterate through the different elements of the Iterator

If you do not want to make a for(int i = 0; i < mice.size(); i++)
you will have to make mate() and eat() boolean methods that return true if the ArrayList was modified and do something like that

              for (Mouse m:mice)
              {
				  m.grow();
				  sylvester.grow();
				  if(Mouse.mate(mice))
					  break;
				  if(sylvester.eat(mice))
					  break;
              } // end for


Was This Post Helpful? 0
  • +
  • -

#9 svkrahnke   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-August 11

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 02:37 PM

Quote

              for (Mouse m:mice)
              {
				  m.grow();
				  sylvester.grow();
				  if(Mouse.mate(mice))
					  break;
				  if(sylvester.eat(mice))
					  break;
              } // end for



Wouldn't the above code make it break before letting sylvester eat a mouse if the mouse had babies?
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 03:36 PM

Alright, I am back sorry went to second job where I don't have access to a computer. You are correct in your statement about it not letting Sylvester eat if they had babies. If you want him to do that then you need to put the sylvester eat method inside of that if statement about the babies. Only then will it eat if the mice mate.
Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Cat Mouse Program freezes command prompt

Posted 13 September 2011 - 08:23 PM

Or simply use for(int i = 0; i < mice.size(); ++i) instead of
for(Mouse m : mice)
Was This Post Helpful? 0
  • +
  • -

#12 svkrahnke   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-August 11

Re: Cat Mouse Program freezes command prompt

Posted 15 September 2011 - 05:54 AM

There is something wrong with the way my Mouse class's mate method is working.

Is there any way to make it "if mice contains true and false".... So long as there is a female and a male mouse they can reproduce, but I can't figure out how to write it that way...

Also I can't figure out how to make it construct the mouse with a random sex...

Here is my Mouse class:
import java.util.ArrayList;
import java.util.Random;

public class Mouse extends Mammal
{
	boolean sex; // sex of mouse

//**************************************************************************************
// This constructor creates a mouse that is 1 day old, has a weight of 1, and is a
// random sex
	public Mouse()
	{
		this.setIsMale(sex);
		this.setAge(1);
		this.setWeight(1);
	} // end mouse constructor

//**************************************************************************************
// This method causes the mouse to age one day and gain 1% of its weight
	public void grow()
	{
		Mouse mouse1 = new Mouse();
		int mouseAge = mouse1.getAge();						// mouseAge is age of mouse
		double mouseWeight = mouse1.getWeight();				// mouseWeight is weight of mouse

		mouse1.setAge(mouseAge++);							// sets mouse's age to age + 1
		mouse1.setWeight(mouseWeight + (mouseWeight * 0.01));	// sets mouse's weight to weight + 1%
	} // end grow

//**************************************************************************************
// This method causes two random mice to create a random number of babies
	public static void mate(ArrayList<Mouse> mice)
	{
		Random random2 = new Random();
		Mouse mouse1 = mice.get(random2.nextInt(mice.size()));			// mouse1 is a random mouse from the mice ArrayList
		Mouse mouse2 = mice.get(random2.nextInt(mice.size()));			// mouse2 is a random mouse from the mice ArrayList

		if (mouse1 != mouse2)
		{
			int randomBabyAmount = random2.nextInt(5);		// the amount of babies made is a random int between 0-4
			System.out.println("Babies added = " + randomBabyAmount);
			if (randomBabyAmount > 0)
			{
				for (int i = 0; i <= randomBabyAmount - 1; i++)
				{
					mice.add(new Mouse());
				} // end for
			} // end if
		} // end if
	} // end mate

//**************************************************************************************
// This method sets the sex of the mouse
	public void setSex(boolean sex)
	{
		this.setIsMale(sex);
	} // end setSex
} // end Mouse


This post has been edited by svkrahnke: 15 September 2011 - 05:56 AM

Was This Post Helpful? 0
  • +
  • -

#13 cmpshr   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Cat Mouse Program freezes command prompt

Posted 15 September 2011 - 10:07 AM

What about
if (mouse1.getSex() != mouse2.getSex())

obviously they will have the same sex if mouse1 == mouse2
Was This Post Helpful? 1
  • +
  • -

#14 svkrahnke   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-August 11

Re: Cat Mouse Program freezes command prompt

Posted 15 September 2011 - 02:09 PM

View Postcmpshr, on 15 September 2011 - 10:07 AM, said:

What about
if (mouse1.getSex() != mouse2.getSex())

obviously they will have the same sex if mouse1 == mouse2


Well that does work for the two of them, however, I need it to be so long as there is a male and a female mouse in the mice ArrayList, they can reproduce...
Was This Post Helpful? 0
  • +
  • -

#15 cmpshr   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Cat Mouse Program freezes command prompt

Posted 15 September 2011 - 02:22 PM

Don't understand why you worry about it.
If all the mice in the arraylist are of the same sex, than the condition will never be true
It is not 2 calls to Random() and ArrayList.get() that will slow down your program.
You can return immediatly fronm the method if arrayList.size() <= 1 thow
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2