Please follow directions below and write comments in the program. The file must
ID: 3627095 • Letter: P
Question
Please follow directions below and write comments in the program.The file must be called <LastFirstWeek5CatMouse.java> (driver program)
Mammal.java
Cat.java (which extends Mammal)
Mouse.java (which extends Mammal)
Ensure you include ALL files required to make your program compile and run.
I would like to see your .java files only.
If possible, submit all programs as a single .zip file (not required)
Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.
5%
Overall Requirements
Write a program that simulates the battle between a cat and mice.
Use this class hierarchy:
Cat
· Kills 1 mouse a day
· Does not reproduce
Mice
· Have a chance to reproduce as long as conditions are met
· Reproduction only happens when mice are over 1 and 1 of each sex is present
Simulation Control
· Simulation continues as long as population is greater than 1 and less than 10
Driver main method should be as shown below: (replacing comment with missing piece)
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);
}
//INCLUDE CODE FOR OUTPUT HERE
}
}
Output code should output:
Depending on if the population of mice is greater than or equal 10:
Mice RULE, Cats Drool Mice Population: ## (integer value)
or
Cats RULE, Mice Drool Cat Weight (in mice): ##.## (double value, 2 decimal places)
15%
Mammal.java class
Instance variables:
name (string)
age (integer)
weight (double)
isMale (Boolean)
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 returns values as appropriate for data type specified.
10%
Cat.java class
eat method: (receive 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. (See chapter 5, lottery example, for random example)
grow method:
Set the cats age to the current age plus 1. (use accessor/mutator methods)
30%
Mouse.java 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 mice objects from arraylist and if conditions are correct, proceed with mating.
Successful mating conditions are:
· 1 male, 1 female mouse
· Both mice older than 1 day
If successful mating, randomly create between 0-4 mice and append to arraylist received as argument.
30%
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 . . .
As always, you should:
Limit your use of class variables and instance variables – only use them if appropriate.
Use appropriate modifiers for your methods. The modifiers we’ve discussed are private, public, static, and final.
Use helper methods if appropriate.
Follow the Java Coding Styles Document including comments and style
Mimic the sample session precisely.
Explanation / Answer
public class Mouse extends Mammal {
static Random r;
if((male && !female) || (!male && female))
{
if((m1.getAge() >=1 ) && (m2.getAge() >=1))
{
int num = r.nextInt(5);
for(int i=0;i<num;i++)
mice.add(new Mouse());
}
}
}
} //end of Mouse
//Cat.java
import java.util.ArrayList;
import java.util.Random;
public class Cat extends Mammal {
Random r;
/** Creates a new instance of Cat */
public Cat() {
}
public void eat(ArrayList<Mouse> mice){
r=new Random();
int ranNum = r.nextInt(mice.size());
//gets the random mouse
Mouse m=mice.get(ranNum);
//updates the cats weight
this.setWeight(this.getWeight() + m.getWeight());
//removes the mouce
mice.remove(ranNum);
}
//grows the cat age
public void grow() {
super.grow();
}
} //end of Cat class
//LastFirstWeek5CatMouse.java
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) {