Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 004ac3f

Browse files
Add files via upload
1 parent e5dfece commit 004ac3f

File tree

71 files changed

+2977
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2977
-0
lines changed

‎part05-Part05_09.HealthStation/pom.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project>
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>tkt</groupId>
5+
<artifactId>Part05_09.HealthStation</artifactId>
6+
<name>Part05_09.HealthStation</name>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>fi.helsinki.cs.tmc</groupId>
25+
<artifactId>edu-test-utils</artifactId>
26+
<version>0.4.2</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.8.0</version>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<version>1.6.0</version>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
<repositories>
47+
<repository>
48+
<id>tmc</id>
49+
<name>TMC repo</name>
50+
<url>https://maven.mooc.fi/releases</url>
51+
<releases>
52+
<enabled>true</enabled>
53+
</releases>
54+
<snapshots>
55+
<enabled>true</enabled>
56+
</snapshots>
57+
</repository>
58+
</repositories>
59+
60+
<pluginRepositories>
61+
<pluginRepository>
62+
<id>tmc</id>
63+
<name>TMC repo</name>
64+
<url>https://maven.mooc.fi/releases</url>
65+
<releases>
66+
<enabled>true</enabled>
67+
</releases>
68+
<snapshots>
69+
<enabled>true</enabled>
70+
</snapshots>
71+
</pluginRepository>
72+
</pluginRepositories>
73+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
public class HealthStation {
3+
private int weighingCounter = 0;
4+
5+
public int weigh(Person person) {
6+
// return the weight of the person passed as the parameter
7+
weighingCounter++;
8+
return person.getWeight();
9+
}
10+
11+
public void feed(Person person) {
12+
person.setWeight(person.getWeight() + 1);
13+
}
14+
15+
public int weighings() {
16+
return weighingCounter;
17+
}
18+
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
public class Main {
3+
4+
public static void main(String[] args) {
5+
// write experimental code here to check how your program functions
6+
}
7+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Do not change the behavior of this class
3+
*/
4+
5+
public class Person {
6+
7+
private String name;
8+
private int age;
9+
private int height;
10+
private int weight;
11+
12+
public Person(String name, int age, int height, int weight) {
13+
this.name = name;
14+
this.age = age;
15+
this.height = height;
16+
this.weight = weight;
17+
}
18+
19+
public void printPerson() {
20+
System.out.println("My name is " + this.name + " and I am " + this.age + " years old");
21+
}
22+
23+
public void growOlder() {
24+
this.age++;
25+
}
26+
27+
public boolean isOfLegalAge() {
28+
if (this.age > 17) {
29+
return true;
30+
}
31+
32+
return false;
33+
}
34+
35+
public void setHeight(int height) {
36+
this.height = height;
37+
}
38+
39+
public int getHeight() {
40+
return this.height;
41+
}
42+
43+
public int getWeight() {
44+
return this.weight;
45+
}
46+
47+
public void setWeight(int weight) {
48+
this.weight = weight;
49+
}
50+
51+
public double bmi() {
52+
double heightInMeters = this.height / 100.0;
53+
54+
return this.weight / (heightInMeters * heightInMeters);
55+
}
56+
57+
public String getName() {
58+
return this.name;
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return "My name is " + this.name + " and I am " + this.age + " years old. My BMI is " + this.bmi();
64+
}
65+
}
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
2+
import fi.helsinki.cs.tmc.edutestutils.Points;
3+
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
4+
import fi.helsinki.cs.tmc.edutestutils.Reflex;
5+
import java.lang.reflect.Field;
6+
import java.lang.reflect.Method;
7+
import java.util.Random;
8+
import org.junit.*;
9+
import static org.junit.Assert.*;
10+
11+
public class HealthStationTest {
12+
13+
HealthStation station;
14+
Person peter;
15+
Random rand = new Random();
16+
17+
Reflex.ClassRef<Object> klass;
18+
String klassName = "HealthStation";
19+
20+
@Before
21+
public void findClass() {
22+
klass = Reflex.reflect(klassName);
23+
}
24+
25+
@Before
26+
public void setUp() {
27+
station = new HealthStation();
28+
peter = new Person("Peter", 33, 175, 78);
29+
}
30+
31+
@Points("05-09.1")
32+
@Test
33+
public void noExtraVariables1() {
34+
sanityCheck();
35+
}
36+
37+
@Test
38+
@Points("05-09.1")
39+
public void canWeigh() {
40+
assertEquals("check the code: station = HealthStation(); "
41+
+ "p = new Person(\"Peter\", 33, 175, 78); "
42+
+ "System.out.println( station.weigh(p) )", peter.getWeight(), station.weigh(peter));
43+
44+
for (int i = 0; i < 5; i++) {
45+
int weight = 60 + rand.nextInt(60);
46+
Person ethan = new Person("Ethan", 45, 181, weight);
47+
48+
assertEquals("check the code: station = HealthStation(); "
49+
+ "p = new Person(\"Ethan\", 45, 181, " + weight + "); "
50+
+ "System.out.println( station.weigh(p) )", ethan.getWeight(), station.weigh(ethan));
51+
}
52+
}
53+
54+
@Points("05-09.2")
55+
@Test
56+
public void noExtraVariables2() {
57+
sanityCheck();
58+
}
59+
60+
@Points("05-09.2")
61+
@Test
62+
public void aMethodCalledFeedExists() throws Throwable {
63+
String method = "feed";
64+
65+
Person p = new Person("Peter", 20, 175, 85);
66+
HealthStation s = new HealthStation();
67+
68+
assertTrue("implement a method public void " + method + "(Person p) for the class " + klassName,
69+
klass.method(s, method).returningVoid().taking(Person.class).isPublic());
70+
71+
String e = "\nThe code that caused the error "
72+
+ "s = new HealthStation; p = new Person(\"Peter\", 20, 175, 85); s.weigh(p);";
73+
74+
klass.method(s, method).returningVoid().taking(Person.class).withNiceError(e).invoke(p);
75+
}
76+
77+
@Test
78+
@Points("05-09.2")
79+
public void canFeed() {
80+
int expected = peter.getWeight() + 1;
81+
feed(station, peter);
82+
83+
assertEquals("Feeding should increase the weight of the person by one kilo. Check the code: \n"
84+
+ "station = HealthStation(); "
85+
+ "p = new Person(\"Peter\", 33, 175, 78); "
86+
+ "station.feed(p); "
87+
+ "System.out.println( p.getPaino() )",
88+
expected, peter.getWeight());
89+
90+
assertEquals("Feeding should increase the weight of the person by one kilo. Check the code: \n"
91+
+ "station = HealthStation(); "
92+
+ "p = new Person(\"Peter\", 33, 175, 78); "
93+
+ "station.feed(p); "
94+
+ "System.out.println( station.weigh(p) )",
95+
expected, station.weigh(peter));
96+
97+
feed(station, peter);
98+
feed(station, peter);
99+
feed(station, peter);
100+
feed(station, peter);
101+
102+
assertEquals("Feeding should increase the weight of the person by one kilo. Check the code: \n"
103+
+ "station = HealthStation(); "
104+
+ "peter = new Person(\"Peter\", 33, 175, 78); "
105+
+ "station.feed(peter); "
106+
+ "station.feed(peter); "
107+
+ "station.feed(peter); "
108+
+ "station.feed(peter); "
109+
+ "station.feed(peter); "
110+
+ "System.out.println( peter.getPaino() )",
111+
expected + 4, peter.getWeight());
112+
}
113+
114+
@Points("05-09.3")
115+
@Test
116+
public void noExtraVariables3() {
117+
sanityCheck();
118+
}
119+
120+
@Test
121+
@Points("05-09.3")
122+
public void aMethodCalledWeighingsExists() throws Throwable {
123+
String method = "weighings";
124+
125+
HealthStation s = new HealthStation();
126+
127+
assertTrue("implement a method public int " + method + "() for the class " + klassName,
128+
klass.method(s, method).returning(int.class).takingNoParams().isPublic());
129+
130+
String e = "\nThe code that caused the error: "
131+
+ "s = new HealthStation; s.weighings();";
132+
133+
klass.method(s, method).returning(int.class).takingNoParams().withNiceError(e).invoke();
134+
}
135+
136+
@Test
137+
@Points("05-09.3")
138+
public void numberOfWeighingsInMemory() {
139+
assertEquals("Does the method weighings() work as intended? Initially no one has been weighed! Try out the code "
140+
+ "station = HealthStation(); "
141+
+ "System.out.println( station.weighings() )",
142+
0, weighings(station));
143+
144+
station.weigh(peter);
145+
assertEquals("Does the method weighings() work as intended? The method should tell how many times the method weigh() has been called "
146+
+ "Try out the code\n"
147+
+ "station = HealthStation(); "
148+
+ "p1 = new Person(\"Peter\", 33, 175, 78); "
149+
+ "station.weigh(p1);"
150+
+ "System.out.println( station.weighings() )",
151+
1, weighings(station));
152+
153+
Person ethan = new Person("Ethan", 0, 52, 4);
154+
station.weigh(ethan);
155+
assertEquals("Does the method weighings() work as intended? The method should tell how many times the method weigh() has been called "
156+
+ "Try out the code\n"
157+
+ "station = HealthStation(); "
158+
+ "p1 = new Person(\"Peter\", 33, 175, 78); "
159+
+ "p2 = new Person(\"Ethan\", 0, 52, 4); "
160+
+ "station.weigh(p1);"
161+
+ "station.weigh(p2);"
162+
+ "System.out.println( station.weighings() )",
163+
2, weighings(station));
164+
165+
station.weigh(ethan);
166+
station.weigh(peter);
167+
station.weigh(peter);
168+
assertEquals("Does the method weighings() work as intended? The method should tell how many times the method weigh() has been called "
169+
+ "Try out the code\n"
170+
+ "station = HealthStation(); "
171+
+ "p1 = new Person(\"Peter\", 33, 175, 78); "
172+
+ "p2 = new Person(\"Ethan\", 0, 52, 4); "
173+
+ "station.weigh(p1);"
174+
+ "station.weigh(p2);"
175+
+ "station.weigh(p2);"
176+
+ "station.weigh(p1);"
177+
+ "station.weigh(p1);"
178+
+ "System.out.println( station.weighings() )",
179+
5, weighings(station));
180+
}
181+
182+
String nameOfTheClass = "HealthStation";
183+
184+
private void feed(Object station, Person hlo) {
185+
try {
186+
Method feed = ReflectionUtils.requireMethod(HealthStation.class, "feed", Person.class);
187+
ReflectionUtils.invokeMethod(void.class, feed, station, hlo);
188+
} catch (Throwable t) {
189+
}
190+
}
191+
192+
private int weighings(Object station) {
193+
try {
194+
Method weighings = ReflectionUtils.requireMethod(HealthStation.class, "weighings");
195+
return ReflectionUtils.invokeMethod(int.class, weighings, station);
196+
} catch (Throwable t) {
197+
}
198+
return -1;
199+
}
200+
201+
private void sanityCheck() throws SecurityException {
202+
String nameOfTheClass = "HealthStation";
203+
204+
Field[] fields = ReflectionUtils.findClass(nameOfTheClass).getDeclaredFields();
205+
206+
for (Field field : fields) {
207+
assertFalse("you don't need \"static variables\", delete " + fieldName(field.toString()), field.toString().contains("static") && !field.toString().contains("final"));
208+
assertTrue("visibility of every object variable of the class must be private, change " + fieldName(field.toString()), field.toString().contains("private"));
209+
}
210+
211+
if (fields.length > 1) {
212+
int var = 0;
213+
for (Field field : fields) {
214+
if (!field.toString().contains("final")) {
215+
var++;
216+
}
217+
}
218+
assertTrue("The class " + nameOfTheClass + " only need the object variable that remembers the number of weighings. Delete unnecessary variables.", var < 2);
219+
}
220+
}
221+
222+
private String fieldName(String toString) {
223+
return toString.replace(nameOfTheClass + ".", "");
224+
}
225+
}
688 Bytes
Binary file not shown.
355 Bytes
Binary file not shown.
1.85 KB
Binary file not shown.
9.47 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /