Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Author: Connor Vaughn
- * Date: 10/1/19
- * Purpose: Determine Animal Population Using Given Trials
- */
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.File;
- import java.util.Scanner;
- import java.util.Random;
- public class AnimalPopulation
- {
- public static void main(String [] args) throws IOException
- {
- //Declare objects
- Scanner in = new Scanner(System.in);
- PrintWriter outFile = new PrintWriter(new File("dataCollection.txt"));
- File dataCollection = new File("dataCollection.txt");
- Scanner inFile = new Scanner(dataCollection);
- Random rand = new Random ();
- //Declare variables
- boolean reLoop = true;
- int numTrials = 0;
- int randomInt = 0;
- int readInt = 0;
- int totalsquirrell = 0;
- double average = 0;
- String readLine = "";
- //User input
- System.out.println("Welcome to the Fox Squirrel Simulator");
- while (reLoop)
- {
- System.out.println();
- System.out.println("How many trials should be simulated?");
- System.out.print("Enter a value greater than 1000: ");
- numTrials = in.nextInt();
- if(numTrials > 1000)
- {
- reLoop = false;
- }
- else
- {
- System.out.println();
- System.out.println(" Please try again. Enter a number greater than 1000.");
- }
- }
- System.out.println();
- System.out.println("simulating trials now... one moment please...");
- System.out.println();
- //Write to file
- for (int trial = 0; trial < numTrials; trial++)
- {
- randomInt = 0;
- for(int fishBefore = 0; randomInt != 15; fishBefore++)
- {
- randomInt = rand.nextInt(16);
- if (randomInt == 15)
- {
- outFile.println(fishBefore);
- }
- else
- {
- ;
- }
- }
- }
- outFile.close();
- //Read file
- while(inFile.hasNextLine())
- {
- readLine = inFile.nextLine();
- readInt = Integer.parseInt(readLine);
- totalsquirrell += readInt;
- }
- inFile.close();
- //Compute average
- average = (double)totalsquirrell / (double)numTrials;
- //Display results
- System.out.println("The results!");
- System.out.println("The average number of squirrels observed until spotting");
- System.out.println("a Fox Squirrel at the city park is: " + average);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement