Skip to content

Commit c158081

Browse files
author
Ben Deitch
committed
Refactor fact selection into new FactBook class
1 parent 3b12a63 commit c158081

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.teamtreehouse.funfacts;
2+
3+
import java.util.Random;
4+
5+
public class FactBook {
6+
7+
// Member variables (properties about the object)
8+
9+
// Method (abilities: things the object can do)
10+
public String getFact() {
11+
String[] facts = {
12+
"Ants stretch when they wake up in the morning.",
13+
"Ostriches can run faster than horses.",
14+
"Olympic gold medals are actually made mostly of silver.",
15+
"You are born with 300 bones; by the time you are an adult you will have 206.",
16+
"It takes about 8 minutes for light from the Sun to reach Earth.",
17+
"Some bamboo plants can grow almost a meter in just one day.",
18+
"The state of Florida is bigger than England.",
19+
"Some penguins can leap 2-3 meters out of the water.",
20+
"On average, it takes 66 days to form a new habit.",
21+
"Mammoths still walked the earth when the Great Pyramid was being built.",
22+
"A group of crows is called a murder." };
23+
24+
String fact = "";
25+
26+
// Randomly select a fact
27+
Random randomGenerator = new Random(); // Construct a new Random number generator
28+
int randomNumber = randomGenerator.nextInt(facts.length);
29+
30+
fact = facts[randomNumber];
31+
32+
return fact;
33+
}
34+
}

app/src/main/java/com/teamtreehouse/funfacts/FunFactsActivity.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
2626
View.OnClickListener listener = new View.OnClickListener() {
2727
@Override
2828
public void onClick(View v) {
29-
String[] facts = {
30-
"Ants stretch when they wake up in the morning.",
31-
"Ostriches can run faster than horses.",
32-
"Olympic gold medals are actually made mostly of silver.",
33-
"You are born with 300 bones; by the time you are an adult you will have 206.",
34-
"It takes about 8 minutes for light from the Sun to reach Earth.",
35-
"Some bamboo plants can grow almost a meter in just one day.",
36-
"The state of Florida is bigger than England.",
37-
"Some penguins can leap 2-3 meters out of the water.",
38-
"On average, it takes 66 days to form a new habit.",
39-
"Mammoths still walked the earth when the Great Pyramid was being built." };
40-
41-
String fact = "";
42-
// Update the label with our dynamic fact
43-
Random randomGenerator = new Random();
44-
int randomNumber = randomGenerator.nextInt(facts.length);
45-
46-
fact = facts[randomNumber];
47-
48-
factLabel.setText(fact);
29+
factLabel.setText("");
4930
}
5031
};
5132
showFactButton.setOnClickListener(listener);

0 commit comments

Comments
 (0)