3 Replies - 340 Views - Last Post: 25 November 2012 - 10:20 PM Rate Topic: -----

#1 BarryDM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 30-April 11

Cat and Mouse Battle program

Posted 25 November 2012 - 08:48 PM

I pretty much have this program done, the only problem is that it will not run 10 times and then quit like it is supposed to. This program is due tonight and any help would be apreciated.

This is my driver.
/***********************************************
 * BarryDustinWeek5CatMouse.java
 * Dustin M. Barry
 *
 * Driver for cat and mouse battle program
 *
 ***********************************************/

import java.util.ArrayList;

public class BarryDustinWeek5CatMouse
{
   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 while
			 if (mice.size() >= 10)
			 {
			 	System.out.println("Mice RULE, Cats Drool Mice Population: " + mice.size());
			 } // end if
			 else
			 {
		   	 	System.out.printf("Cats RULE, Mice Drool Cat Weight (in mice): %.2f\n", sylvester.getWeight());
			 } // end else
		} // end main
} // end class


this is the Mammal class.
/***********************************************
 * Mammal.java
 * Dustin M. Barry
 *
 * Mammal class for cat and mouse battle program
 * 
 ***********************************************/

public class Mammal
{
	private String name = ""; 			// name of the mammal
	private int age = 0;				// age of the mammal
	private double weight = 0.0;		// weight of the mammal
	private boolean isMale = false;		// sex of the mammal

//**************************************************************************************
// This constructor creates a mammal whose age = 1

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

//**************************************************************************************
// This method sets the mammal's age to age + 1

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

//**************************************************************************************
// This method sets the mammal's name

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

//**************************************************************************************
// This method sets the mammal's age

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

//**************************************************************************************
// This method sets the mammal's weight

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

//**************************************************************************************
// This method sets the mammal's sex

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

//**************************************************************************************
// This method returns the mammal's name

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

//**************************************************************************************
// This method returns the mammal's age

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

//**************************************************************************************
// This method returns the mammal's weight

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

//**************************************************************************************
// This method returns the mammal's sex

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

} // end Mammal


The Cat class.
/***********************************************
 * Cat.java
 * Dustin M. Barry
 *
 * Cat class for cat and mouse battle program
 *
 ***********************************************/

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

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

//**************************************************************************************
// This method causes the cat to eat a random mouse and increases the cat's weight by
// the weight of the mouse it ate

	public void eat(ArrayList<Mouse> mice)
	{
		double catWeight = 0.0;								// the weight of the cat
		double mouseWeight = 0.0;							// the weight of the mouse
		int deadMouse = 0;									// the mouse selected to be eaten
		int percentage = (int) (Math.random() * 100);		// the percentage that causes the cat to eat a mouse
		boolean eaten = false;
		if (mice.size() > 0)
		{
			if (eaten == false)
			{
				if (percentage >= 70)
				{
					deadMouse = (int) (random.nextInt(mice.size()));	// the mouse to be eaten is chosen from the ArrayList
					catWeight = this.getWeight();						// gets the cat's weight and sets it to catWeight
					mouseWeight = (mice.get(deadMouse)).getWeight();	// gets the weight of the mouse to be eaten and sets it to mouseWeight
					this.setWeight(catWeight + mouseWeight);				// sets the cat's weight to its weight plus the mouse it ate
					mice.remove(deadMouse);
					eaten = true;
				} // end if
			}
			else
			{
				eaten = false;
			} // end else
		} // end if
	} // end eat

//**************************************************************************************
// This method causes the cat to age one day

	public void grow()
	{
		int age = this.getAge();	// gets the cat's age
		this.setAge(age++);			// sets the cat's age to age + 1

	} // end grow

} // end Cat


And the Mouse class.
/***********************************************
 * Mouse.java
 * Dustin M. Barry
 *
 * Mouse class for cat and mouse battle program
 *
 ***********************************************/

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()
	{
		Random random = new Random();
		int randomNum = random.nextInt(2);
		if (randomNum == 1)
		{
			sex = true;
		}
		else
		{
			sex = false;
		}
		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()
	{
		int mouseAge = this.getAge();						// mouseAge is age of mouse
		double mouseWeight = this.getWeight();				// mouseWeight is weight of mouse

		this.setAge(++mouseAge);							// sets mouse's age to age + 1
		this.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.getIsMale() != mouse2.getIsMale() && mouse1.getAge() >= 1 && mouse2.getAge() >=1)
		{
			int randomBabyAmount = random2.nextInt(5);		// the amount of babies made is a random int between 0-4
			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


Is This A Good Question/Topic? 0
  • +

Replies To: Cat and Mouse Battle program

#2 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Cat and Mouse Battle program

Posted 25 November 2012 - 08:59 PM

View PostBarryDM, on 25 November 2012 - 11:48 PM, said:

This program is due tonight and any help would be apreciated.

And since when do you have this assignment ? :) MAy be a week ? And now tonight we switch to panic mode. After all IT didn't changed in the last 40 years ?
Was This Post Helpful? 0
  • +
  • -

#3 BarryDM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 30-April 11

Re: Cat and Mouse Battle program

Posted 25 November 2012 - 09:11 PM

View Postpbl, on 25 November 2012 - 08:59 PM, said:

View PostBarryDM, on 25 November 2012 - 11:48 PM, said:

This program is due tonight and any help would be apreciated.

And since when do you have this assignment ? :)/> MAy be a week ? And now tonight we switch to panic mode. After all IT didn't changed in the last 40 years ?


Not really panic mode, if I have to turn it in the way it is I'm not upset I will get the grade I deserve. Yes I have had a week to work on it, and have worked on it obviously. Just thought that someone could help me out to make it right.
Was This Post Helpful? 0
  • +
  • -

#4 heinbau  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 08-November 12

Re: Cat and Mouse Battle program

Posted 25 November 2012 - 10:20 PM

Why don't you just have a for loop set up with the number of days you've gone through and have it stop when you hit ten?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1