Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==================================================//
- // Author: Christian Calles and Xiaoqing Lin //
- // Date: February 27, 2015 //
- // Program: Program 4 assignment //
- // Description: This programs runs a predator //
- // prey simulation using multiple classes. //
- // //
- //==================================================//
- //imported from java library
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.text.DecimalFormat;
- import java.util.*;
- //============================START OF THE SIMULATION CLASS===================================
- class Simulation {
- //===================================//
- // Some global variables //
- //===================================//
- static int years, numWolf, ageofWolf, numRab, ageofRabbit, rabbitsBorn, wolvesBorn;
- static double killRatio;
- static Scanner scan;
- static ArrayList<Wolf> wolfPack;
- static ArrayList<Rabbit> bunnies;
- static int nori;
- static int newestpup;
- static int deadbyAge;
- static int deadHunger;
- static int newestbun;
- static int oldbunny;
- public static void main(String[] args) {
- Random r = new Random(100);
- //===================================//
- // reads in text file from args[0] //
- //==================================//
- try {
- scan = new Scanner(new FileInputStream(args[0]));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- years = scan.nextInt(); //number of years the program runs
- killRatio = scan.nextDouble(); //the kill ratio
- numWolf = scan.nextInt(); //initial number of wolves
- ageofWolf = scan.nextInt(); //initial age of wolves
- numRab = scan.nextInt(); //initial number of rabbits
- ageofRabbit = scan.nextInt(); //initial age of rabbit
- wolfPack = new ArrayList<Wolf>();
- bunnies = new ArrayList<Rabbit>();
- //=================================================//
- // creates an array list the size of numwolf //
- //=================================================//
- for (int i = 0; i < numWolf; i++){
- Wolf newWolf = new Wolf(ageofWolf);
- wolfPack.add(newWolf);
- }
- //=================================================//
- // creates an array list the size of numRab //
- //=================================================//
- for (int i = 0; i < numRab; i++){
- Rabbit newRabbit = new Rabbit(ageofRabbit);
- bunnies.add(newRabbit);
- }
- System.out.println("Welcome to SimWorld\n");
- System.out.println("At the start of the simulation: ");
- System.out.println("There are " + wolfPack.size() + " wolves with average age of " + (String.format("%.2f", getWolfAverage(wolfPack))));
- System.out.println("There are " + bunnies.size() + " rabbits with average age of " + (String.format("%.2f", getRabbitAverage(bunnies))) + "\n");
- //use a for loop to run through all our season methods and our stats method
- for (int i = 1; i < years + 1; i++ ){
- simulateSpring();
- simulateSummer(r);
- simulateFall();
- simulateWinter();
- System.out.println("At the end of year " + i );
- printStatistics();
- nori = 0;
- newestpup = 0;
- newestbun = 0;
- oldbunny = 0;
- deadbyAge = 0;
- deadHunger = 0;
- }
- }
- //====================ANIMAL CLASS===================================================
- //bunny and Wolf class inheret from this class
- public static class Animal{
- public int age;
- public Animal(){
- age = 0;
- }
- public Animal(int age){
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- public void increaseAge() {
- age++;
- }
- }
- //==============================WOLF CLASS======================================
- public static class Wolf extends Animal{
- int hungerLevel = 0;
- public Wolf(int age){
- this.age = age;
- }
- public Wolf(){
- age = 0;
- }
- public void resetHungerLevel(){
- hungerLevel = 0;
- }
- public int getHungerLevel() {
- return hungerLevel;
- }
- public void increaseHungerLevel(){
- hungerLevel++;
- }
- }
- //====================RABBIT CLASS===================================================
- public static class Rabbit extends Animal{
- public Rabbit(int age){
- this.age = age;
- }
- public Rabbit(){
- age = 0;
- }
- }
- //===================SEASONS METHODS=====================================================
- static void simulateSpring(){
- //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
- for(int i = 0; i < bunnies.size(); i++){
- if(bunnies.get(i).getAge() >= 1) {
- bunnies.add(new Rabbit(0));
- bunnies.add(new Rabbit(0));
- newestbun+=2;
- }
- }
- //creates 1 new wolf at age 0 if the wolf is >= 3
- for(int j = 0; j < wolfPack.size(); j++){
- if(wolfPack.get(j).getAge() >= 3){
- wolfPack.add(new Wolf(0));
- newestpup++;
- }
- }
- }
- static void simulateFall(){
- //wolf dies froms hunger
- int size = wolfPack.size();
- int size2 = bunnies.size();
- for(int i = 0; i < size; i++){
- if(wolfPack.get(i).getAge() >= 5){
- wolfPack.remove(i);
- deadbyAge++; //keeps track of how many wolves died from old age
- i--;
- }
- else if(wolfPack.get(i).getHungerLevel() == 3){
- wolfPack.remove(i);
- i--;
- deadHunger++; //keeps track of how many wolves died from hunger
- }
- size--;
- //if the wolf is 5 years old it will die
- }
- //if bunnies are age 5 they die
- for(int j = 0; j < size2; j++){
- if(bunnies.get(j).getAge() >= 5){
- bunnies.remove(j);
- oldbunny++; //keeps track of how many died from old age
- j--;
- size2--;
- }
- }
- }
- static void simulateSummer(Random r){
- int kimkardashian = 0; //kim k is the amount of rabbits that get hunted by one wolf
- for(int i = 0; i < wolfPack.size(); i++){
- float kanyeWest = r.nextFloat(); //kanye is a random number
- // System.out.println(kanyeWest);
- double wSize = wolfPack.size();
- double rSize = bunnies.size();
- if (kanyeWest < (rSize/(rSize + wSize))){
- kimkardashian = (int) (bunnies.size()*killRatio);
- for(int j = 0; j < kimkardashian; j++){
- bunnies.remove(0);
- wolfPack.get(i).resetHungerLevel();
- nori++;
- }
- if(kimkardashian < 1){
- bunnies.remove(0);
- wolfPack.get(i).resetHungerLevel();
- nori++; //keeps track of how many rabbits get hunted
- }
- }
- else{
- wolfPack.get(i).increaseHungerLevel();
- }
- }
- }
- static void simulateWinter(){
- //increases the age of the wolves
- if(wolfPack.size() > 0){
- for(int i = 0; i < wolfPack.size(); i++){
- wolfPack.get(i).increaseAge();
- }
- }
- else{
- System.out.println("No more wolves");
- System.exit(0);
- }
- //increases the age of the bunnies
- if(bunnies.size() > 0){
- for(int i = 0; i < bunnies.size(); i++){
- bunnies.get(i).increaseAge();
- }
- }
- else{
- System.out.println("No more rabbits");
- System.exit(0);
- }
- }
- static void printStatistics() {
- System.out.println("There are " + wolfPack.size() + " wolves with average age of " + (String.format("%.2f", getWolfAverage(wolfPack))));
- System.out.println("There are " + bunnies.size() + " rabbits with average age of " + (String.format("%.2f", getRabbitAverage(bunnies))));
- System.out.println("There were " + newestpup + " " + "wolves born, and " + deadbyAge + " wolves died of old age, and " + deadHunger + " wolves died of hunger.");
- System.out.println("There were " + newestbun + " rabbits born, and " + oldbunny + " rabbits died of old age, and " + nori + " rabbits killed by wolves.\n" );
- }
- //================================HELPER METHODS====================================================
- public static double getWolfAverage(ArrayList<Wolf> myAnimals){
- double sum = 0;
- if(myAnimals.size() == 0){
- return 0;
- }
- for (Wolf myAnimal : myAnimals){
- sum += (double) myAnimal.getAge();
- }
- return sum/(double)myAnimals.size();
- }
- public static double getRabbitAverage(ArrayList<Rabbit> myAnimals ){
- double sum = 0;
- if(myAnimals.size() == 0){
- return 0;
- }
- for (Rabbit myAnimal : myAnimals){
- sum += (double) myAnimal.getAge();
- }
- return sum/(double)myAnimals.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement