Представление биометрических данных в Java


Этот код представляет биометрические данные усреднены за 1мин период, начиная с данного мирового времени.

Пожалуйста, просмотрите мой код:

import java.io.*;
import java.util.*;

class Solution {
    public static void main(String[] args) {
        RangeFinder ranger = new RangeFinder();
        ranger.showInput();
        ranger.run();
        ranger.showOutput();
    }
}

// represents biometric data averaged over a 1min period, beginning at the given UTC time
class Bio {
    public short bpm; // heart rate average in beats per minute
    public long utc;  // UTC time in seconds
}

// generates two data sets, starting from a common time value.
class RangeFinder {
    private final long commonOffset = 1501606800; // the offset should be long

    private Bio[] source1, source2;
    private short inner0, inner1, outer0, outer1;

    public RangeFinder() {
        generateSource1();
        generateSource2();
    }

    // generate continuous data for 60min
    // storing results in source1
    public void generateSource1() {
        source1 = new Bio[60];
        for (int k=0;k<source1.length;k++) {
            Bio record = new Bio();
            record.bpm = 80;
            record.utc = commonOffset + k*60; // need add 1 min (60 sec)
            source1[k] = record;
        }
    }

    // generate sparse data for 60min with random gaps, using bpm value 80±10
    // rejecting values outside 77 ≤ x ≤ 83
    // storing results in source2
    public void generateSource2() {
        Bio[] records = new Bio[60];
        for (int k=0;k<records.length;k++) {
            Bio record = new Bio();
            record.bpm = (short) (80 + Math.random() * 21 - 10); // the random will return [0,1), so need times 21 here.
            record.utc = commonOffset + k*60; //// need add 1 min (60 sec)
            records[k] = record;
        }
        ArrayList<Bio> filteredRecords = new ArrayList<Bio>();
        for (int k=0;k<records.length;k++) {
            if( records[k].bpm <= 83 && records[k].bpm >= 77 ) { //rejecting values outside 77 ≤ x ≤ 83
                filteredRecords.add(records[k]);
            }
        }
        source2 = new Bio[filteredRecords.size()]; // storing results in source2
        for(int k = 0;k < filteredRecords.size();k++){
            source2[k] = filteredRecords.get(k);
        }

    }

    // find inner/outer bounds
    //
    // ex1:     -------- ------  --  ------
    // ex2:   ------ -- -------- -----
    //
    // inner:   ^                    ^
    // outer: ^                           ^
    //
    public void run() {
        // compute inner and outer values
        // I think you want to present the range of bmp, not utc. 
        // So sorting the source2 by its bmp then calculate the inner0, inner1, outer0, outer1
        Arrays.sort(source2, new Comparator<Bio>() {
            @Override
            public int compare(Bio o1, Bio o2) {
                return o1.bpm - o2.bpm;
            }
        });
        inner0 = (short) Math.max(source1[0].bpm, source2[0].bpm);
        inner1 = (short) Math.min(source1[source1.length-1].bpm, source2[source2.length-1].bpm);
        outer0 = (short) Math.min(source1[0].bpm, source2[0].bpm);
        outer1 = (short) Math.max(source1[source1.length-1].bpm, source2[source2.length-1].bpm);
    }

    // print both source1 and source2 data sets, showing each element on a separate line as "bpm,utc"
    public void showInput() {
        StringBuilder sb = new StringBuilder();
        for (int k=0;k<source1.length;k++) {
            sb.append(source1[k].bpm);
            sb.append(",");
            sb.append(source1[k].utc);
            sb.append("\n");
        }
        System.out.println("source1: \n"+sb.toString());

        sb = new StringBuilder();
        for (int k=0;k<source2.length;k++) {
            sb.append(source2[k].bpm);
            sb.append(",");
            sb.append(source2[k].utc);
            sb.append("\n");
        }
        System.out.println("source2: \n"+sb.toString());
    }

    public void showOutput() {
        System.out.println("inner: "+inner0+", "+inner1);
        System.out.println("outer: "+outer0+", "+outer1);
    }
}


147
2
задан 17 февраля 2018 в 09:02 Источник Поделиться
Комментарии