From 4d6aea87baa3729dead12b197ef08b51cfb8db16 Mon Sep 17 00:00:00 2001 From: Dorian Heinrichs Date: Thu, 3 Oct 2019 01:12:34 +0200 Subject: [PATCH 1/2] Cleaned up the code and moved the BMI-, BAC- and Widmark-Factor calculator to a seperate class --- .gitignore | 35 +++++++++++++++ JavaVersion/Addition.java | 93 ++++++++++++++------------------------- JavaVersion/Util.java | 65 +++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 60 deletions(-) create mode 100644 .gitignore create mode 100644 JavaVersion/Util.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96a3e94 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +# Eclipse +.classpath +.project +.settings/ + +# Netbeans +nbproject/ +nbactions.xml + +# IntelliJ Idea +*.iml +*.ipr +*.iws +.idea/ + +# Maven +build.xml +target/ +dependency-reduced-pom.xml +pom.xml.versionsBackup + +# vim +.*.sw[a-p] + +# Mac filesystem dust +.DS_Store/ + +# (potential) Build files +build/ +bin/ +dist/ +test/ +manifest.mf +*.log* +out/ \ No newline at end of file diff --git a/JavaVersion/Addition.java b/JavaVersion/Addition.java index 4d57cc3..aa95064 100644 --- a/JavaVersion/Addition.java +++ b/JavaVersion/Addition.java @@ -1,62 +1,49 @@ import java.util.ArrayList; -import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; -// adds drinks consumed by user public class Addition { + private double height; private double weight; private double minute; - private ArrayList timeList=new ArrayList(); - - private double eliminationRate = 0.018; - - + private List timeList = new ArrayList<>(); + private double eliminationRate = 0.018; - // init - public Addition(double height, double weight, int minute){ + public Addition(double height, double weight, int minute) { this.height = height; this.weight = weight; this.minute = minute; } - // find widmark factor - private double widmark(double weight, double height, boolean gender){ - double temp = weight/Math.pow((height/100d),2); // find BMI - if(gender){ - return 1.0181 - 0.01213 * temp; // return male widmark factor - } - else{ - return 0.9367 - 0.01240 * temp; // return female widmark factor + public static void main(String[] args) { + Addition add = new Addition(170, 60, 800); + List a = add.totalConsumed(330, 20, 10, 12, true); + a = add.eliminateAlc(a); + for (int i = 0; i < a.size(); i++) { + System.out.println(a.get(i)); } } // adds the drink - private double drink(int volume, double percent, int minutes, double halfLife){ - double temp = minutes/halfLife; // find how many halflives passed - return (volume/1000d)*(percent/100d)- (volume/1000d)*(percent/100d) * Math.pow(0.5, temp); // find alcohol is absorbed + private double drink(int volume, double percent, int minutes, double halfLife) { + double temp = minutes / halfLife; // find how many halflives passed + return (volume / 1000d) * (percent / 100d) - (volume / 1000d) * (percent / 100d) * Math.pow(0.5, temp); // find alcohol is absorbed } - // calculated bac based on how much alcohol is in the body at the moment - private double bacCalculator(double alcohol, double widmarkFactor, double weight){ - // find bac and if it is below zero return 0 - double C= Math.max((100d * alcohol * 0.78974) / (widmarkFactor * weight),0); // 0.78974 = density of alcohol - return C; - } - - // creates an array containing the total amount of alcohol absorbed by the body - public ArrayList totalConsumed(int volume, double percent, int time, double halfLife, boolean gender){ - ArrayList tempTimeList=new ArrayList(); + public List totalConsumed(int volume, double percent, int time, double halfLife, boolean gender) { + List tempTimeList = new ArrayList<>(); time = -time; - while (tempTimeList.size() < 1440 + minute){ // iterates through 24 hours after alcohol intake + + while (tempTimeList.size() < TimeUnit.DAYS.toMinutes(1) + minute) { double absoluteConcentration = drink(volume, percent, time, halfLife); - tempTimeList.add(bacCalculator(absoluteConcentration, widmark(weight,height,gender), weight)); + tempTimeList.add(Util.calculateBAC(absoluteConcentration, weight, height, gender)); time++; } - if(timeList.size() == 0){ + if (timeList.size() == 0) { timeList = tempTimeList; - } - else{ // add lists together + } else { // add lists together for (int i = 0; i < timeList.size(); i++) { timeList.set(i, timeList.get(i) + tempTimeList.get(i)); } @@ -65,45 +52,31 @@ public ArrayList totalConsumed(int volume, double percent, int time, dou } // creates an array of the amount of blood in the alcohol (based on the 'totalConsumed method' - public ArrayList eliminateAlc(ArrayList timeList){ - ArrayList eliminationList = new ArrayList(); + public List eliminateAlc(List timeList) { + List eliminationList = new ArrayList(); eliminationList.add(0d); // add 0 as starting point for (int i = 0; i < timeList.size(); i++) { // if there is alcohol in the blood, eleminate one minute worth of alcohol by increasing // the value appended in eleminationList, otherwise append the same value (e.g. don't eliminate // alcohol since the is none) - if(timeList.get(i) - eliminationList.get(i)/60*eliminationRate > 0){ - eliminationList.add(eliminationList.get(i)+1); - } - else{ + if (timeList.get(i) - eliminationList.get(i) / 60 * eliminationRate > 0) { + eliminationList.add(eliminationList.get(i) + 1); + } else { eliminationList.add(eliminationList.get(i)); } } eliminationList.remove(0); // remove the inital 0 stored in eliminationList for (int i = 0; i < timeList.size(); i++) { - eliminationList.set(i, eliminationList.get(i)/60*eliminationRate); - eliminationList.set(i,Math.max(0d, timeList.get(i)-eliminationList.get(i))); + eliminationList.set(i, eliminationList.get(i) / 60 * eliminationRate); + eliminationList.set(i, Math.max(0d, timeList.get(i) - eliminationList.get(i))); } return eliminationList; } - public double getMaxtBAC(ArrayList bacList){ - return Collections.max(bacList); - } - - public int timeUntilSober(ArrayList bacList, int currentTime){ - for (int i = bacList.size(); i < currentTime; i--){ - if(bacList.get(i)<=0) return i; + public int timeUntilSober(ArrayList bacList, int currentTime) { + for (int i = bacList.size(); i < currentTime; i--) { + if (bacList.get(i) <= 0) return i; } + return 0; } - - public static void main(String[] args) { - Addition add = new Addition(170, 60, 800); - ArrayList a = add.totalConsumed(330, 20, 10, 12, true); - a = add.eliminateAlc(a); - for (int i = 0; i < a.size(); i++) { - System.out.println(a.get(i)); - } - } - } diff --git a/JavaVersion/Util.java b/JavaVersion/Util.java new file mode 100644 index 0000000..54f719c --- /dev/null +++ b/JavaVersion/Util.java @@ -0,0 +1,65 @@ +public class Util { + + private final static double widmarkFactorMale = 1.0181D - 0.01213D; + private final static double widmarkFactorFemale = 0.9367D - 0.01240D; + private final static double alcoholDensity = 0.78974D; + + /** + * Calculates the BMI using the given values + * + * @param weight weight in kg + * @param height height in cm + * @return bmi of the given values + */ + public static double calculateBMI(double weight, double height) { + return weight / Math.pow(height / 100D, 2); + } + + /** + * Calculates the widmark factor using the given values + * + * @param weight weight in kg + * @param height height in cm + * @param gender true for male, false for female + * @return widmark factor of the given values + */ + public static double calculateWidmarkFactor(double weight, double height, boolean gender) { + return calculateWidmarkFactor(calculateBMI(weight, height), gender); + } + + /** + * Calculates the widmark factor using the given values + * + * @param bmi body mass index + * @param gender true for male, false for female + * @return widmark factor of the given values + */ + public static double calculateWidmarkFactor(double bmi, boolean gender) { + return (gender ? widmarkFactorMale : widmarkFactorFemale) * bmi; + } + + /** + * Calculates the blood alcohol concentration for the given values + * + * @param absoluteConcentration absolute concentration of blood + * @param weight weight in kg + * @param height height in cm + * @param gender true for male, false for female + * @return blood alcohol concentration of the given values + */ + public static Double calculateBAC(double absoluteConcentration, double weight, double height, boolean gender) { + return calculateBAC(absoluteConcentration, calculateWidmarkFactor(weight, height, gender), weight); + } + + /** + * Calculates the blood alcohol concentration for the given values + * + * @param absoluteConcentration absolute concentration of blood + * @param widmarkFactor widmark factor + * @param weight weight in kg + * @return blood alcohol concentration of the given values + */ + public static double calculateBAC(double absoluteConcentration, double widmarkFactor, double weight) { + return Math.max((100D * absoluteConcentration * alcoholDensity) / (widmarkFactor * weight), 0); + } +} From 6125e80eaa453533f7fff66e857a2a04d477eab8 Mon Sep 17 00:00:00 2001 From: Dorian Heinrichs Date: Thu, 3 Oct 2019 01:23:32 +0200 Subject: [PATCH 2/2] Moved java version to a seperate branch called java-version --- JavaVersion/Addition.java | 82 --------------------------------------- JavaVersion/Util.java | 65 ------------------------------- 2 files changed, 147 deletions(-) delete mode 100644 JavaVersion/Addition.java delete mode 100644 JavaVersion/Util.java diff --git a/JavaVersion/Addition.java b/JavaVersion/Addition.java deleted file mode 100644 index aa95064..0000000 --- a/JavaVersion/Addition.java +++ /dev/null @@ -1,82 +0,0 @@ -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -public class Addition { - - private double height; - private double weight; - private double minute; - private List timeList = new ArrayList<>(); - private double eliminationRate = 0.018; - - public Addition(double height, double weight, int minute) { - this.height = height; - this.weight = weight; - this.minute = minute; - } - - public static void main(String[] args) { - Addition add = new Addition(170, 60, 800); - List a = add.totalConsumed(330, 20, 10, 12, true); - a = add.eliminateAlc(a); - for (int i = 0; i < a.size(); i++) { - System.out.println(a.get(i)); - } - } - - // adds the drink - private double drink(int volume, double percent, int minutes, double halfLife) { - double temp = minutes / halfLife; // find how many halflives passed - return (volume / 1000d) * (percent / 100d) - (volume / 1000d) * (percent / 100d) * Math.pow(0.5, temp); // find alcohol is absorbed - } - - // creates an array containing the total amount of alcohol absorbed by the body - public List totalConsumed(int volume, double percent, int time, double halfLife, boolean gender) { - List tempTimeList = new ArrayList<>(); - time = -time; - - while (tempTimeList.size() < TimeUnit.DAYS.toMinutes(1) + minute) { - double absoluteConcentration = drink(volume, percent, time, halfLife); - tempTimeList.add(Util.calculateBAC(absoluteConcentration, weight, height, gender)); - time++; - } - if (timeList.size() == 0) { - timeList = tempTimeList; - } else { // add lists together - for (int i = 0; i < timeList.size(); i++) { - timeList.set(i, timeList.get(i) + tempTimeList.get(i)); - } - } - return timeList; - } - - // creates an array of the amount of blood in the alcohol (based on the 'totalConsumed method' - public List eliminateAlc(List timeList) { - List eliminationList = new ArrayList(); - eliminationList.add(0d); // add 0 as starting point - for (int i = 0; i < timeList.size(); i++) { - // if there is alcohol in the blood, eleminate one minute worth of alcohol by increasing - // the value appended in eleminationList, otherwise append the same value (e.g. don't eliminate - // alcohol since the is none) - if (timeList.get(i) - eliminationList.get(i) / 60 * eliminationRate > 0) { - eliminationList.add(eliminationList.get(i) + 1); - } else { - eliminationList.add(eliminationList.get(i)); - } - } - eliminationList.remove(0); // remove the inital 0 stored in eliminationList - for (int i = 0; i < timeList.size(); i++) { - eliminationList.set(i, eliminationList.get(i) / 60 * eliminationRate); - eliminationList.set(i, Math.max(0d, timeList.get(i) - eliminationList.get(i))); - } - return eliminationList; - } - - public int timeUntilSober(ArrayList bacList, int currentTime) { - for (int i = bacList.size(); i < currentTime; i--) { - if (bacList.get(i) <= 0) return i; - } - return 0; - } -} diff --git a/JavaVersion/Util.java b/JavaVersion/Util.java deleted file mode 100644 index 54f719c..0000000 --- a/JavaVersion/Util.java +++ /dev/null @@ -1,65 +0,0 @@ -public class Util { - - private final static double widmarkFactorMale = 1.0181D - 0.01213D; - private final static double widmarkFactorFemale = 0.9367D - 0.01240D; - private final static double alcoholDensity = 0.78974D; - - /** - * Calculates the BMI using the given values - * - * @param weight weight in kg - * @param height height in cm - * @return bmi of the given values - */ - public static double calculateBMI(double weight, double height) { - return weight / Math.pow(height / 100D, 2); - } - - /** - * Calculates the widmark factor using the given values - * - * @param weight weight in kg - * @param height height in cm - * @param gender true for male, false for female - * @return widmark factor of the given values - */ - public static double calculateWidmarkFactor(double weight, double height, boolean gender) { - return calculateWidmarkFactor(calculateBMI(weight, height), gender); - } - - /** - * Calculates the widmark factor using the given values - * - * @param bmi body mass index - * @param gender true for male, false for female - * @return widmark factor of the given values - */ - public static double calculateWidmarkFactor(double bmi, boolean gender) { - return (gender ? widmarkFactorMale : widmarkFactorFemale) * bmi; - } - - /** - * Calculates the blood alcohol concentration for the given values - * - * @param absoluteConcentration absolute concentration of blood - * @param weight weight in kg - * @param height height in cm - * @param gender true for male, false for female - * @return blood alcohol concentration of the given values - */ - public static Double calculateBAC(double absoluteConcentration, double weight, double height, boolean gender) { - return calculateBAC(absoluteConcentration, calculateWidmarkFactor(weight, height, gender), weight); - } - - /** - * Calculates the blood alcohol concentration for the given values - * - * @param absoluteConcentration absolute concentration of blood - * @param widmarkFactor widmark factor - * @param weight weight in kg - * @return blood alcohol concentration of the given values - */ - public static double calculateBAC(double absoluteConcentration, double widmarkFactor, double weight) { - return Math.max((100D * absoluteConcentration * alcoholDensity) / (widmarkFactor * weight), 0); - } -}