CS 245: Advanced Programming and Data Structures
Fall 2024
Assignment 2
Assignment Task:
Mouse and Cat Simulation
This assignment will have you practice developing an OOP as a Java solution and have safe trip . You are to write classes that define a Mouse, Mouse.java, and a Cat, Cat.java. These classes will not have a main method.
Subsequently, develop Jave code on that will run a simulation, MouseSimulation.java, using your Mouse and Cat classes. This class will be your driver / runner to verify / test the project you’ve developed.
You are required to use those file and class names.
What are Mice?
They are tiny mammals with long tails, whiskers, and round ears! Only females can have a litter of baby mice. The gestation period for a mouse is 20 days on average. Female mice can get pregnant/breed again a week (7 days) after giving birth to a litter. Mice will breed for the first time at 40 days old. The probability of producing a female in a litter is equal to the probability of producing a male. Litter sizes can vary between 6 and 8. The litter size can be different for each pregnancy.
What are Cats?
They are small mammals with long tails, claws, and whiskers! Cats hunt & eat mice. Each day, a cat goes hunting, with a chance of failure which is dependent on the number of cats and mice in the population. If a cat doesn’t eat for 5 days in a row, they will die. Only female cats can have a litter of kittens. The gestation period for a cat is 65 days on average. Female cats can get pregnant / breed again 4 months (120 days) after giving birth. Cats will breed for the first time at 180 days old. The probability of producing a female in a litter is equal to the probability of producing a male. Litter sizes vary between 2 and 10. Litter size can be different for each pregnancy.
What you need to do:
Create classes called Mice.java to model the Mouse and Cat.java to model the Cat. Your mice and cats will need a biological sex, age, and other fields to help you determine if it is pregnant, how many days of the gestation has passed, and how many days after the delivery of the last litter. You will also need to have methods to give the animals the ability to have variable numbers of offspring per litter. For each pregnancy, the litter size can vary. You will need to have some way of tracking when a cat last ate, and what happens when a cat eats a mouse. The chance of failure each day a cat goes hunting is equal to:
failure rate (f) =
where the number of cats and mice are equal to the total values in the population on that specific day.
You will need to write a Client (Driver) Class called MouseSimulation.java that will simulate the growth of a population of mice and cats that starts with a given number of male and female animals at day zero.
The seed (initial) population of cats and mice will be read from an input text file (Assign-1_New-Input-Values.txt). In the input.txt file, the first integer will be the initial population of female mice, the second integer will be the initial population of male mice, the third integer will be the initial population of female cats, and the fourth integer will be the initial population of male cats. All animals in the seed population are adults (they can breed / become-pregnant immediately). The numbers will be tab delimited. Please remember to save the input.txt file directly in the IntelliJ project folder.
An example of input.txt is shown below:
23 10 2 1
55 29 1 5
12 3 3 2
In the example above, there are only three initial seed populations provided. The first row, for example, has an initial population of 23 female mice, 10 male mice, 2 female cats, and 1 male cat. Ensure that your solution can handle input files that may have a varying number of rows!
For each of these different seed populations, you must run different trials.
A single trial:
Increase the elapsed time by 1 day at a time and answer the following questions:
· How many mice will there be produced after 1 year (365 days – should this be a constant?)
· How many cats will be produced after 1 year (365 days – should this be a constant?)
To answer the question, you must have an approach of storing your mice and cats as you produce them. Use an ArrayList of type Mouse or Cat to do this. Seed this ArrayList with the initial population of mice and cats and add the new litters to it as they are produced.
Report the final population of total animals (Mice and Cats), female animals and male animals. (You need to return an integer array you used for tallying).
Now repeat the trial 10 times:
Your simulation should repeat this trial 10 times (This should be a constant). Reporting the stats for each round (total mice, female mice, male mice, total cats, female cats, male cats).
Use a 2-D array to store the trial data (Example index 0 stores the array of tallies for the first trial). This organizes your data like a spreadsheet. The first column is the total mouse population, the second column is the female mouse population, the third is the male mouse population, and so on. Each row is a different trial.
Given the data for the 10 trials, report the average number of each animal (mice and cats) with its standard deviation, the average number of female animals with its standard deviation, and the average number of male animals with its standard deviation. The averages and standard deviations should report only have three digits after the decimal point.
Sample outputs below are based on the given sample input file above (remember there is randomness in determining sex, litter size, and predation so your output will vary. There will be another set of this output for each line in the input file):
Starting with 23 female mice, 10 male mice, 2 female cats, and 1 male cat:
Trial 0: 431 total mice; 201 females, 230 males. 20 total cats; 8 females, 12 males.
Trial 1: 340 total mice; 173 females, 167 males. 24 total cats; 13 females, 11 males.
Trial 2: 498 total mice; 256 females, 242 males. 17 total cats; 9 females, 8 males.
Trial 3: 323 total mice; 162 females, 161 males. 27 total cats; 15 females, 12 males.
Trial 4: 418 total mice; 209 females, 209 males. 19 total cats; 7 females, 12 males.
Trial 5: 361 total mice; 179 females, 182 males. 24 total cats; 11 females, 13 males.
Trial 6: 443 total mice; 235 females, 208 males. 18 total cats; 7 females, 11 males.
Trial 7: 407 total mice; 212 females, 195 males. 20 total cats; 13 females, 7 males.
Trial 8: 397 total mice; 204 females, 193 males. 23 total cats; 11 females, 12 males.
Trial 9: 313 total mice; 159 females, 154 males. 28 total cats; 14 females, 14 males.
Average number of mice: 393.100 with standard deviation of 55.623.
Average number of female mice: 199.000 with standard deviation of 29.813.
Average number of male mice: 194.100 with standard deviation of 27.541.
Average number of cats: 22.000 with standard deviation of 3.578
Average number of female cats: 10.800 with standard deviation of 2.786
The format of your output should match the above example.
Note: The values shown are only examples and may differ from what you get as output values.
Strategy:
· Create your Mouse class.
o Write your getter and setters and helper methods.
· In your Driver create a Mouse object and test that your methods work correctly.
Once you have verified your Mouse class works correctly, do the same for the Cat class, and then,
· Write a method to run one trial of your experiment.
o Populate your ArrayList of Mice and Cats with the correct number of seeds read from the input.text file.
o For the number of days in your trial
§ Age each animal.
§ If it is pregnant – add a day to its gestational period.
§ If it can breed – let it breed.
§ If it can give birth – let it give birth (add litter to animal population)
§ See if it has a successful hunt that day.
§ Did it die?
· Report/record the results of that trial in an integer array (print to verify then for final comment it out)
· Repeat this trial 10 times recording each trial data in a 2dD array (print to verify then for final comment it out)
· Report the averages and standard deviations.
· Make sure your output is formatted like mine (see above).
Use meaningful names for methods and variables. Incorporate good horizontal and vertical indentation and whitespace in your code layout structure.
Include a header comment block at the top of all your .java files, and in that, include your full name and the purpose of that file.
Submission:
Please zip your IntelliJ Java project folder and submit the zipped file under the Assignment 2 on Canvas folder. To grade your work, I should be able to simply unzip your Java project, load it in the IntelliJ environment, and run it.
Academic Integrity Note:
Every student must work independently (strictly on their own) and must not collaborate or communicate with their peers (students) in CS 245 or other courses.
If you have any questions, please seek guidance from any of the two TAs and/or me.