Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. /**
  2. * Author: Connor Vaughn
  3. * Date: 10/1/19
  4. * Purpose: Determine Animal Population Using Given Trials
  5. */
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.io.File;
  9. import java.util.Scanner;
  10. import java.util.Random;
  11. public class AnimalPopulation
  12. {
  13. public static void main(String [] args) throws IOException
  14. {
  15. //Declare objects
  16. Scanner in = new Scanner(System.in);
  17. PrintWriter outFile = new PrintWriter(new File("dataCollection.txt"));
  18. File dataCollection = new File("dataCollection.txt");
  19. Scanner inFile = new Scanner(dataCollection);
  20. Random rand = new Random ();
  21.  
  22. //Declare variables
  23. boolean reLoop = true;
  24. int numTrials = 0;
  25. int randomInt = 0;
  26. int readInt = 0;
  27. int totalsquirrell = 0;
  28. double average = 0;
  29. String readLine = "";
  30.  
  31.  
  32. //User input
  33. System.out.println("Welcome to the Fox Squirrel Simulator");
  34. while (reLoop)
  35. {
  36. System.out.println();
  37. System.out.println("How many trials should be simulated?");
  38. System.out.print("Enter a value greater than 1000: ");
  39. numTrials = in.nextInt();
  40.  
  41. if(numTrials > 1000)
  42. {
  43. reLoop = false;
  44. }
  45. else
  46. {
  47. System.out.println();
  48. System.out.println(" Please try again. Enter a number greater than 1000.");
  49. }
  50. }
  51. System.out.println();
  52. System.out.println("simulating trials now... one moment please...");
  53. System.out.println();
  54.  
  55. //Write to file
  56. for (int trial = 0; trial < numTrials; trial++)
  57. {
  58. randomInt = 0;
  59. for(int fishBefore = 0; randomInt != 15; fishBefore++)
  60. {
  61. randomInt = rand.nextInt(16);
  62. if (randomInt == 15)
  63. {
  64. outFile.println(fishBefore);
  65. }
  66. else
  67. {
  68. ;
  69. }
  70. }
  71. }
  72. outFile.close();
  73.  
  74. //Read file
  75. while(inFile.hasNextLine())
  76. {
  77. readLine = inFile.nextLine();
  78. readInt = Integer.parseInt(readLine);
  79. totalsquirrell += readInt;
  80. }
  81. inFile.close();
  82.  
  83. //Compute average
  84. average = (double)totalsquirrell / (double)numTrials;
  85.  
  86. //Display results
  87. System.out.println("The results!");
  88. System.out.println("The average number of squirrels observed until spotting");
  89. System.out.println("a Fox Squirrel at the city park is: " + average);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement