summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormo <mo.khan@gmail.com>2019-07-19 15:51:12 -0600
committermo <mo.khan@gmail.com>2019-07-19 15:51:12 -0600
commit77113b1659a5f8b87650c3249e0a5295dc48303b (patch)
tree5e265129ed70c687992edf5638bc30271c6ec421 /src
parent12634f895cb7959aef9e82964a6d466bbc4a266b (diff)
calculate BMI
Diffstat (limited to 'src')
-rw-r--r--src/Q7/Person.java70
-rw-r--r--src/Q7/PersonTest.java29
2 files changed, 99 insertions, 0 deletions
diff --git a/src/Q7/Person.java b/src/Q7/Person.java
new file mode 100644
index 0000000..c447cc2
--- /dev/null
+++ b/src/Q7/Person.java
@@ -0,0 +1,70 @@
+package Q7;
+
+public class Person {
+ private double bmi;
+ private double height;
+ private double weight;
+ private String category;
+ private String name;
+
+ public Person(String name, double weight, double height) {
+ this.name = name;
+ this.weight = weight;
+ this.height = height;
+ this.updateBMI();
+ }
+
+ public String getCategory() {
+ return this.getCategory(this.bmi);
+ }
+
+ public String getCategory(double bmi) {
+ if (bmi < 18.5) {
+ return "Underweight";
+ } else if (bmi < 25.0) {
+ return "Normal";
+ } else if (bmi < 30.0) {
+ return "Overweight";
+ }
+ return "Obese";
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public double getBMI() {
+ return this.bmi;
+ }
+
+ public double getHeight() {
+ return this.height;
+ }
+
+ public double getWeight() {
+ return this.weight;
+ }
+
+ public void setBMI(double bmi) {
+ this.bmi = bmi;
+ }
+
+ public void setHeight(double height) {
+ this.height = height;
+ this.updateBMI();
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setWeight(double weight) {
+ this.weight = weight;
+ this.updateBMI();
+ }
+
+ // BMI = (weight (lb) * 703) / ((height (in))^2)
+ private void updateBMI() {
+ this.setBMI((this.weight * 703) / Math.pow(height, 2));
+ }
+}
diff --git a/src/Q7/PersonTest.java b/src/Q7/PersonTest.java
new file mode 100644
index 0000000..1d79c9d
--- /dev/null
+++ b/src/Q7/PersonTest.java
@@ -0,0 +1,29 @@
+package ca.mokhan.test;
+
+import Q7.*;
+import java.io.*;
+import java.text.*;
+import java.util.*;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class PersonTest extends TestCase {
+ private Person subject;
+
+ public PersonTest(String testName) {
+ super(testName);
+ this.subject = new Person("mo", 1, 1);
+ }
+
+ public static Test suite() {
+ return new TestSuite(PersonTest.class);
+ }
+
+ public void test_getBMI_underweight() {
+ this.subject.setHeight(72);
+ this.subject.setWeight(100);
+
+ assertEquals("Underweight", this.subject.getCategory());
+ }
+}