Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. //==================================================//
  2. // Author: Christian Calles and Xiaoqing Lin //
  3. // Date: February 27, 2015 //
  4. // Program: Program 4 assignment //
  5. // Description: This programs runs a predator //
  6. // prey simulation using multiple classes. //
  7. // //
  8. //==================================================//
  9.  
  10.  
  11.  
  12. //imported from java library
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileNotFoundException;
  16. import java.text.DecimalFormat;
  17. import java.util.*;
  18.  
  19.  
  20. //============================START OF THE SIMULATION CLASS===================================
  21. class Simulation {
  22.  
  23. //===================================//
  24. // Some global variables //
  25. //===================================//
  26.  
  27. static int years, numWolf, ageofWolf, numRab, ageofRabbit, rabbitsBorn, wolvesBorn;
  28. static double killRatio;
  29. static Scanner scan;
  30. static ArrayList<Wolf> wolfPack;
  31. static ArrayList<Rabbit> bunnies;
  32. static int nori;
  33. static int newestpup;
  34. static int deadbyAge;
  35. static int deadHunger;
  36. static int newestbun;
  37. static int oldbunny;
  38.  
  39.  
  40.  
  41.  
  42. public static void main(String[] args) {
  43. Random r = new Random(100);
  44.  
  45. //===================================//
  46. // reads in text file from args[0] //
  47. //==================================//
  48.  
  49.  
  50.  
  51.  
  52. try {
  53. scan = new Scanner(new FileInputStream(args[0]));
  54. } catch (FileNotFoundException e) {
  55. e.printStackTrace();
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. years = scan.nextInt(); //number of years the program runs
  65. killRatio = scan.nextDouble(); //the kill ratio
  66. numWolf = scan.nextInt(); //initial number of wolves
  67. ageofWolf = scan.nextInt(); //initial age of wolves
  68. numRab = scan.nextInt(); //initial number of rabbits
  69. ageofRabbit = scan.nextInt(); //initial age of rabbit
  70.  
  71. wolfPack = new ArrayList<Wolf>();
  72. bunnies = new ArrayList<Rabbit>();
  73.  
  74. //=================================================//
  75. // creates an array list the size of numwolf //
  76. //=================================================//
  77. for (int i = 0; i < numWolf; i++){
  78. Wolf newWolf = new Wolf(ageofWolf);
  79. wolfPack.add(newWolf);
  80. }
  81.  
  82.  
  83. //=================================================//
  84. // creates an array list the size of numRab //
  85. //=================================================//
  86.  
  87. for (int i = 0; i < numRab; i++){
  88. Rabbit newRabbit = new Rabbit(ageofRabbit);
  89. bunnies.add(newRabbit);
  90. }
  91.  
  92. System.out.println("Welcome to SimWorld\n");
  93. System.out.println("At the start of the simulation: ");
  94. System.out.println("There are " + wolfPack.size() + " wolves with average age of " + (String.format("%.2f", getWolfAverage(wolfPack))));
  95. System.out.println("There are " + bunnies.size() + " rabbits with average age of " + (String.format("%.2f", getRabbitAverage(bunnies))) + "\n");
  96.  
  97.  
  98. //use a for loop to run through all our season methods and our stats method
  99.  
  100.  
  101. for (int i = 1; i < years + 1; i++ ){
  102. simulateSpring();
  103. simulateSummer(r);
  104. simulateFall();
  105. simulateWinter();
  106. System.out.println("At the end of year " + i );
  107. printStatistics();
  108. nori = 0;
  109. newestpup = 0;
  110. newestbun = 0;
  111. oldbunny = 0;
  112. deadbyAge = 0;
  113. deadHunger = 0;
  114. }
  115. }
  116.  
  117. //====================ANIMAL CLASS===================================================
  118. //bunny and Wolf class inheret from this class
  119. public static class Animal{
  120. public int age;
  121.  
  122. public Animal(){
  123. age = 0;
  124. }
  125.  
  126. public Animal(int age){
  127. this.age = age;
  128. }
  129.  
  130. public int getAge() {
  131. return age;
  132. }
  133.  
  134. public void increaseAge() {
  135. age++;
  136. }
  137.  
  138. }
  139.  
  140. //==============================WOLF CLASS======================================
  141. public static class Wolf extends Animal{
  142. int hungerLevel = 0;
  143.  
  144. public Wolf(int age){
  145. this.age = age;
  146.  
  147. }
  148.  
  149. public Wolf(){
  150. age = 0;
  151.  
  152. }
  153.  
  154. public void resetHungerLevel(){
  155. hungerLevel = 0;
  156. }
  157.  
  158. public int getHungerLevel() {
  159. return hungerLevel;
  160. }
  161.  
  162. public void increaseHungerLevel(){
  163. hungerLevel++;
  164. }
  165. }
  166.  
  167. //====================RABBIT CLASS===================================================
  168.  
  169. public static class Rabbit extends Animal{
  170.  
  171. public Rabbit(int age){
  172. this.age = age;
  173. }
  174.  
  175. public Rabbit(){
  176. age = 0;
  177. }
  178. }
  179.  
  180. //===================SEASONS METHODS=====================================================
  181.  
  182. static void simulateSpring(){
  183. //this for loop creates two new bunnies at age 0 if the age of the rabbit at a specific index is greater than or equal to 1
  184. for(int i = 0; i < bunnies.size(); i++){
  185. if(bunnies.get(i).getAge() >= 1) {
  186. bunnies.add(new Rabbit(0));
  187. bunnies.add(new Rabbit(0));
  188. newestbun+=2;
  189. }
  190. }
  191. //creates 1 new wolf at age 0 if the wolf is >= 3
  192. for(int j = 0; j < wolfPack.size(); j++){
  193. if(wolfPack.get(j).getAge() >= 3){
  194. wolfPack.add(new Wolf(0));
  195. newestpup++;
  196. }
  197. }
  198. }
  199.  
  200.  
  201. static void simulateFall(){
  202.  
  203. //wolf dies froms hunger
  204. int size = wolfPack.size();
  205. int size2 = bunnies.size();
  206.  
  207. for(int i = 0; i < size; i++){
  208.  
  209.  
  210. if(wolfPack.get(i).getAge() >= 5){
  211. wolfPack.remove(i);
  212. deadbyAge++; //keeps track of how many wolves died from old age
  213. i--;
  214. }
  215.  
  216. else if(wolfPack.get(i).getHungerLevel() == 3){
  217. wolfPack.remove(i);
  218. i--;
  219. deadHunger++; //keeps track of how many wolves died from hunger
  220. }
  221.  
  222. size--;
  223.  
  224. //if the wolf is 5 years old it will die
  225.  
  226. }
  227. //if bunnies are age 5 they die
  228. for(int j = 0; j < size2; j++){
  229. if(bunnies.get(j).getAge() >= 5){
  230. bunnies.remove(j);
  231. oldbunny++; //keeps track of how many died from old age
  232. j--;
  233. size2--;
  234. }
  235. }
  236.  
  237. }
  238.  
  239. static void simulateSummer(Random r){
  240. int kimkardashian = 0; //kim k is the amount of rabbits that get hunted by one wolf
  241.  
  242. for(int i = 0; i < wolfPack.size(); i++){
  243. float kanyeWest = r.nextFloat(); //kanye is a random number
  244. // System.out.println(kanyeWest);
  245. double wSize = wolfPack.size();
  246. double rSize = bunnies.size();
  247.  
  248. if (kanyeWest < (rSize/(rSize + wSize))){
  249. kimkardashian = (int) (bunnies.size()*killRatio);
  250.  
  251. for(int j = 0; j < kimkardashian; j++){
  252. bunnies.remove(0);
  253. wolfPack.get(i).resetHungerLevel();
  254. nori++;
  255. }
  256.  
  257. if(kimkardashian < 1){
  258. bunnies.remove(0);
  259. wolfPack.get(i).resetHungerLevel();
  260. nori++; //keeps track of how many rabbits get hunted
  261. }
  262. }
  263.  
  264. else{
  265. wolfPack.get(i).increaseHungerLevel();
  266.  
  267. }
  268. }
  269.  
  270. }
  271.  
  272. static void simulateWinter(){
  273. //increases the age of the wolves
  274. if(wolfPack.size() > 0){
  275. for(int i = 0; i < wolfPack.size(); i++){
  276. wolfPack.get(i).increaseAge();
  277. }
  278. }
  279.  
  280. else{
  281. System.out.println("No more wolves");
  282. System.exit(0);
  283. }
  284.  
  285. //increases the age of the bunnies
  286. if(bunnies.size() > 0){
  287. for(int i = 0; i < bunnies.size(); i++){
  288. bunnies.get(i).increaseAge();
  289. }
  290. }
  291.  
  292. else{
  293. System.out.println("No more rabbits");
  294. System.exit(0);
  295. }
  296. }
  297.  
  298. static void printStatistics() {
  299. System.out.println("There are " + wolfPack.size() + " wolves with average age of " + (String.format("%.2f", getWolfAverage(wolfPack))));
  300. System.out.println("There are " + bunnies.size() + " rabbits with average age of " + (String.format("%.2f", getRabbitAverage(bunnies))));
  301. System.out.println("There were " + newestpup + " " + "wolves born, and " + deadbyAge + " wolves died of old age, and " + deadHunger + " wolves died of hunger.");
  302. System.out.println("There were " + newestbun + " rabbits born, and " + oldbunny + " rabbits died of old age, and " + nori + " rabbits killed by wolves.\n" );
  303.  
  304. }
  305.  
  306. //================================HELPER METHODS====================================================
  307.  
  308. public static double getWolfAverage(ArrayList<Wolf> myAnimals){
  309. double sum = 0;
  310. if(myAnimals.size() == 0){
  311. return 0;
  312.  
  313. }
  314. for (Wolf myAnimal : myAnimals){
  315. sum += (double) myAnimal.getAge();
  316. }
  317.  
  318. return sum/(double)myAnimals.size();
  319.  
  320. }
  321.  
  322. public static double getRabbitAverage(ArrayList<Rabbit> myAnimals ){
  323. double sum = 0;
  324. if(myAnimals.size() == 0){
  325. return 0;
  326. }
  327.  
  328. for (Rabbit myAnimal : myAnimals){
  329. sum += (double) myAnimal.getAge();
  330. }
  331.  
  332. return sum/(double)myAnimals.size();
  333.  
  334. }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement