Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. package test_stuff;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Random;
  5.  
  6. public class semesteroppgave2 {
  7.  
  8. public static void main(String[] args) {
  9. //Problem 5
  10. /*
  11. double[] array = {2.0088799960771184, 2.121420889236832, 1.9396865921089017,
  12. 2.4044747294759574, 2.2430778650951178, 2.083040119880876,
  13. 2.0595035785038114, 2.1782979876210806, 1.8812817807415378,
  14. 2.232108837421659};
  15. double[] counts = countsFromArray(array, 10);
  16. String[][] array2d = array2dFromCounts(counts);
  17. printArray2d(array2d);
  18. */
  19. //Problem 6
  20. double[] array = cooldownSamples(27, 100000);
  21. double[] counts = countsFromArray(array, 20);
  22. String[][] array2d = array2dFromCounts(counts);
  23. printReport(array2d, minFromArray(array), maxFromArray(array));
  24. }
  25.  
  26. /**
  27. * Compute the time in hours required for a body to cool down to temperature
  28. * degrees. Gaussian noise is added to simulate parameter uncertainty.
  29. *
  30. * @param temperature
  31. * The temperature of the body when found. 5 * @return the time in
  32. * hours required for the body to cool down to temperature degrees.
  33. */
  34. public static double cooldown(double temperature) {
  35. // we need this object to generate Gaussian random variables
  36. // (remember to import java.util.Random)
  37. Random random = new Random();
  38. // the average body temperature of a (living) human
  39. double bodyTemperature = 37;
  40.  
  41. // add noise to simulate that the body temperature of the victim at the
  42. // time of death is uncertain
  43. bodyTemperature += random.nextGaussian();
  44. // compute the time required for the body to cool down from
  45. // bodyTemperature to temperature using Newton’s law of cooling.
  46. double cooldownTime = Math.log(bodyTemperature / temperature);
  47. cooldownTime *= 1 / bodyTemperature;
  48. // normalize this value such that cooling down from 37 to 32 degrees
  49. // takes 1 hour. we assume that we have measured this for the
  50. // environment that the body is found in. we add Gaussian noise to
  51. // simulate measurement uncertainty.
  52. cooldownTime *= 255 + random.nextGaussian();
  53. return cooldownTime;
  54. }
  55.  
  56. private final static double[] cooldownSamples(int temperature, int numSamples) {
  57. // creates an array with a size of numSamples
  58. double[] toReturn = new double[numSamples];
  59.  
  60. // loops numSamples amount of times
  61. for (int i = 0; i < numSamples; i++) {
  62. // puts the cooldown double into the i index of the toReturn array
  63. toReturn[i] = cooldown(temperature);
  64. }
  65.  
  66. // returns the double array
  67. return toReturn;
  68. }
  69.  
  70. private final static double minFromArray(double[] array) {
  71. double toReturn = array[0];
  72.  
  73. for (double d : array) {
  74. if (d < toReturn) {
  75. toReturn = d;
  76. }
  77. }
  78.  
  79. return toReturn;
  80. }
  81.  
  82. private final static double maxFromArray(double[] array) {
  83. double toReturn = 0.0;
  84.  
  85. for (double d : array) {
  86. if (d > toReturn) {
  87. toReturn = d;
  88. }
  89. }
  90.  
  91. return toReturn;
  92. }
  93.  
  94. private final static double[] countsFromArray(double[] array, int numRanges) {
  95. double[] counts = new double[numRanges];
  96.  
  97. double min = minFromArray(array);
  98. double max = maxFromArray(array);
  99. double rangeSize = (max - min) / (numRanges - 1);
  100. for (int i = 0; i < array.length; i++) {
  101. counts[(int) ((array[i] - min) / rangeSize)] += 1;
  102. }
  103.  
  104.  
  105. return counts;
  106. }
  107.  
  108. public static void printArray2d(String[][] array2d) {
  109. for (int firstIndex = 0; firstIndex < array2d.length; firstIndex++) {
  110. for (int secondIndex = 0; secondIndex < array2d[firstIndex].length; secondIndex++) {
  111. if(array2d[firstIndex][secondIndex] == null)
  112. array2d[firstIndex][secondIndex] = ""; //To set default value from null to ""
  113. System.out.print((array2d[firstIndex][secondIndex]));
  114. }
  115. System.out.printf("\n");
  116. }
  117. }
  118. //Problem 5
  119. /*
  120. public static String[][] array2dFromCounts(double[] counts) {
  121. final int PRINT_WIDTH = 50;
  122. String[][] array2d = new String[counts.length][PRINT_WIDTH];
  123.  
  124. for (int firstIndex = 0; firstIndex < counts.length; firstIndex++) {
  125. if(counts[firstIndex] >= 2.0 && counts[firstIndex] <= 3.0) {
  126. for(int i = 0; i < (int)(2 * PRINT_WIDTH / 3); i++) {
  127. array2d[firstIndex][i] = "#";
  128. }
  129. for(int i = 0; i < (int)PRINT_WIDTH - (2 * PRINT_WIDTH / 3); i++) {
  130. array2d[firstIndex][(2 * PRINT_WIDTH / 3)] = " ";
  131. }
  132. }
  133. for(int i = 0; i < (int)(counts[firstIndex] * PRINT_WIDTH / 3); i++) {
  134. array2d[firstIndex][i] = "#";
  135. }
  136. }
  137.  
  138. return array2d;
  139. }*/
  140. //Problem 6
  141. public static String[][] array2dFromCounts(double[] counts) {
  142. final int PRINT_WIDTH = 50;
  143. String[][] array2d = new String[counts.length][PRINT_WIDTH];
  144.  
  145. for (int firstIndex = 0; firstIndex < counts.length; firstIndex++) {
  146. for(int secondIndex = 0; secondIndex < (int)(counts[firstIndex] * PRINT_WIDTH / maxFromArray(counts)); secondIndex++) {
  147. array2d[firstIndex][secondIndex] = "#";
  148. }
  149. for(int secondIndex = 0; secondIndex < PRINT_WIDTH - (int)(counts[firstIndex] * PRINT_WIDTH / maxFromArray(counts)); secondIndex++) {
  150. array2d[firstIndex][(int)(counts[firstIndex] * PRINT_WIDTH / maxFromArray(counts))] = "#";
  151. }
  152. }
  153.  
  154. return array2d;
  155. }
  156.  
  157. public static void printReport(String[][] array2d, double arrayMin, double arrayMax) {
  158. System.out.println("Time since death probability distribution");
  159. System.out.println("- Each line corresponds to 0.06 hours.");
  160. System.out.println(new String(new char[46]).replace("\0", "="));
  161. System.out.println(arrayMin + " hours");
  162. printArray2d(array2d);
  163. System.out.println(arrayMax + " hours");
  164. System.out.println(new String(new char[46]).replace("\0", "="));
  165. //printArray2d(array2d);
  166. }
  167.  
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement