Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test_stuff;
- import java.util.Arrays;
- import java.util.Random;
- public class semesteroppgave2 {
- public static void main(String[] args) {
- //Problem 5
- /*
- double[] array = {2.0088799960771184, 2.121420889236832, 1.9396865921089017,
- 2.4044747294759574, 2.2430778650951178, 2.083040119880876,
- 2.0595035785038114, 2.1782979876210806, 1.8812817807415378,
- 2.232108837421659};
- double[] counts = countsFromArray(array, 10);
- String[][] array2d = array2dFromCounts(counts);
- printArray2d(array2d);
- */
- //Problem 6
- double[] array = cooldownSamples(27, 100000);
- double[] counts = countsFromArray(array, 20);
- String[][] array2d = array2dFromCounts(counts);
- printReport(array2d, minFromArray(array), maxFromArray(array));
- }
- /**
- * Compute the time in hours required for a body to cool down to temperature
- * degrees. Gaussian noise is added to simulate parameter uncertainty.
- *
- * @param temperature
- * The temperature of the body when found. 5 * @return the time in
- * hours required for the body to cool down to temperature degrees.
- */
- public static double cooldown(double temperature) {
- // we need this object to generate Gaussian random variables
- // (remember to import java.util.Random)
- Random random = new Random();
- // the average body temperature of a (living) human
- double bodyTemperature = 37;
- // add noise to simulate that the body temperature of the victim at the
- // time of death is uncertain
- bodyTemperature += random.nextGaussian();
- // compute the time required for the body to cool down from
- // bodyTemperature to temperature using Newton’s law of cooling.
- double cooldownTime = Math.log(bodyTemperature / temperature);
- cooldownTime *= 1 / bodyTemperature;
- // normalize this value such that cooling down from 37 to 32 degrees
- // takes 1 hour. we assume that we have measured this for the
- // environment that the body is found in. we add Gaussian noise to
- // simulate measurement uncertainty.
- cooldownTime *= 255 + random.nextGaussian();
- return cooldownTime;
- }
- private final static double[] cooldownSamples(int temperature, int numSamples) {
- // creates an array with a size of numSamples
- double[] toReturn = new double[numSamples];
- // loops numSamples amount of times
- for (int i = 0; i < numSamples; i++) {
- // puts the cooldown double into the i index of the toReturn array
- toReturn[i] = cooldown(temperature);
- }
- // returns the double array
- return toReturn;
- }
- private final static double minFromArray(double[] array) {
- double toReturn = array[0];
- for (double d : array) {
- if (d < toReturn) {
- toReturn = d;
- }
- }
- return toReturn;
- }
- private final static double maxFromArray(double[] array) {
- double toReturn = 0.0;
- for (double d : array) {
- if (d > toReturn) {
- toReturn = d;
- }
- }
- return toReturn;
- }
- private final static double[] countsFromArray(double[] array, int numRanges) {
- double[] counts = new double[numRanges];
- double min = minFromArray(array);
- double max = maxFromArray(array);
- double rangeSize = (max - min) / (numRanges - 1);
- for (int i = 0; i < array.length; i++) {
- counts[(int) ((array[i] - min) / rangeSize)] += 1;
- }
- return counts;
- }
- public static void printArray2d(String[][] array2d) {
- for (int firstIndex = 0; firstIndex < array2d.length; firstIndex++) {
- for (int secondIndex = 0; secondIndex < array2d[firstIndex].length; secondIndex++) {
- if(array2d[firstIndex][secondIndex] == null)
- array2d[firstIndex][secondIndex] = ""; //To set default value from null to ""
- System.out.print((array2d[firstIndex][secondIndex]));
- }
- System.out.printf("\n");
- }
- }
- //Problem 5
- /*
- public static String[][] array2dFromCounts(double[] counts) {
- final int PRINT_WIDTH = 50;
- String[][] array2d = new String[counts.length][PRINT_WIDTH];
- for (int firstIndex = 0; firstIndex < counts.length; firstIndex++) {
- if(counts[firstIndex] >= 2.0 && counts[firstIndex] <= 3.0) {
- for(int i = 0; i < (int)(2 * PRINT_WIDTH / 3); i++) {
- array2d[firstIndex][i] = "#";
- }
- for(int i = 0; i < (int)PRINT_WIDTH - (2 * PRINT_WIDTH / 3); i++) {
- array2d[firstIndex][(2 * PRINT_WIDTH / 3)] = " ";
- }
- }
- for(int i = 0; i < (int)(counts[firstIndex] * PRINT_WIDTH / 3); i++) {
- array2d[firstIndex][i] = "#";
- }
- }
- return array2d;
- }*/
- //Problem 6
- public static String[][] array2dFromCounts(double[] counts) {
- final int PRINT_WIDTH = 50;
- String[][] array2d = new String[counts.length][PRINT_WIDTH];
- for (int firstIndex = 0; firstIndex < counts.length; firstIndex++) {
- for(int secondIndex = 0; secondIndex < (int)(counts[firstIndex] * PRINT_WIDTH / maxFromArray(counts)); secondIndex++) {
- array2d[firstIndex][secondIndex] = "#";
- }
- for(int secondIndex = 0; secondIndex < PRINT_WIDTH - (int)(counts[firstIndex] * PRINT_WIDTH / maxFromArray(counts)); secondIndex++) {
- array2d[firstIndex][(int)(counts[firstIndex] * PRINT_WIDTH / maxFromArray(counts))] = "#";
- }
- }
- return array2d;
- }
- public static void printReport(String[][] array2d, double arrayMin, double arrayMax) {
- System.out.println("Time since death probability distribution");
- System.out.println("- Each line corresponds to 0.06 hours.");
- System.out.println(new String(new char[46]).replace("\0", "="));
- System.out.println(arrayMin + " hours");
- printArray2d(array2d);
- System.out.println(arrayMax + " hours");
- System.out.println(new String(new char[46]).replace("\0", "="));
- //printArray2d(array2d);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement