COM6506 Week02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Person {
String name;
double weight;
double height;

public Person(String name, double weight, double height) {
this.name = name;
this.weight = weight;
this.height = height;
}

public String getName() {
return name;
}

public double getWeight() {
return weight;
}

public double getHeight() {
return height;
}

public static void main(String[] args) {
Person jeff = new Person("Jeff", 72.4, 2.2);
Person jim = new Person("Jim", 65, 1.7);
System.out.println("Jeff is " + jeff.getHeight() + "m tall.");
System.out.println("Jim is " + jim.getHeight() + "m tall.");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class BMICalculator {
Person person;

public BMICalculator(Person p) {
this.person = p;
}

public double calculateBMI() {
double bmi = person.getWeight() / (person.getHeight() * person.getHeight());
return bmi;
}

public static void main(String[] args) {
Person jeff = new Person("Jeff", 85.4, 1.9);
BMICalculator calculator = new BMICalculator(jeff);
System.out.println("Jeff's BMI is: " + calculator.calculateBMI());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class BMICalculatorTest {
/*
* BMI should be somewhere between 20 and 30.
*/
@Test
void testPersonInNormalCategory() {
Person jeff = new Person("Jeff", 85.4, 1.9);
BMICalculator calculator = new BMICalculator(jeff);
double result = calculator.calculateBMI();
assertTrue(result > 20);
assertTrue(result < 30);
}

/*
* BMI should be zero
*/
@Test
void testWeightlessPerson() {
Person jeff = new Person("Jeff", 0, 1.9);
BMICalculator calculator = new BMICalculator(jeff);
double result = calculator.calculateBMI();
assertEquals(result, 0D);
}

/*
* BMI should be infinite
*/
@Test
void testHeightlessPerson() {
Person jeff = new Person("Jeff", 85.4, 0);
BMICalculator calculator = new BMICalculator(jeff);
double result = calculator.calculateBMI();
assertTrue(Double.isInfinite(result));
}
}
0%