Purchase Solution

Java Simulation of Cat and Mice Battle

Not what you're looking for?

Ask Custom Question

Write a Java program that simulates the battle between a cat and mice.

Cat Kills 1 mouse a day, and does not reproduce.

Mice have a chance to reproduce as long as required conditions are met. Reproduction only happens when mice are over 1, and 1 of each sex is present.

Simulation continues as long as mice population is greater than 1 and less than 10.

Simulation should generate following output depending on if the population of mice is greater than or equal to 10, or no mice left:

Mice RULE, Cats Drool Mice Population: ## (integer value)

Or

Cats RULE, Mice Drool Cat Weight (in mice): ##.## (double value, 2 decimal places)

Sample session (requires no user input):

Mice RULE, Cats Drool Mice Population: 11
Press any key to continue . . .

Cats RULE, Mice Drool Cat Weight (in mice): 2.03
Press any key to continue . . .

Mice RULE, Cats Drool Mice Population: 10
Press any key to continue . . .

Cats RULE, Mice Drool Cat Weight (in mice): 2.05
Press any key to continue . . .

The program should consist of following files:

1. LastFirstWeek5CatMouse.java (driver program)

Driver main method should be as shown below. Add appropriate code in it to generate simulation output.

import java.util.ArrayList;

public class LastFirstWeek5CatMouse
{
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);
}
}
}

2. Mammal.java

Instance variables: name (string), age (integer), weight (double), isMale (Boolean)

class mammal constructor : (default constructor) Set age to 1.

grow method : Increases age of mammal by 1.

Accessor / mutator methods for each instance variable above, set or return values as appropriate for data type specified.

3. Cat.java (extends Mammal class)

eat method: Receives mouse arraylist as argument.

Randomly removes a mouse from the population 70% of the time and increases cat weight by the chosen mouse weight. Only increase weight if mouse is removed/eaten.

grow method: Set the cat's age to the current age plus 1. (Use accessor/mutator methods.)

4. Mouse.java (extends Mammal class)

class mouse constructor: (default constructor) Randomly choose sex and assign to isMale as appropriate. Set age to 1. Set weight to 1.

grow method: Increase age of mouse by 1 and weight of mouse by 1% of current weight.

mate method: (static method, receive mouse arraylist as argument) Randomly choose 2 mouse objects from arraylist and if the required conditions are met, proceed with mating. Successful mating conditions are: 1 male and 1 female mouse, both mice older than 1 day. If successful mating happens, randomly create between 0-4 mice and append to arraylist received as argument.

Purchase this Solution

Solution Summary

This solution provides a sample Java program that simulates a battle between a cat and mice.

Purchase this Solution


Free BrainMass Quizzes
Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.