Skip to content

Commit 3026bf3

Browse files
author
Ben Deitch
committed
Merge branch 'create_factbook_class'
2 parents 0577ee1 + faa70e3 commit 3026bf3

File tree

2 files changed

+36
-19
lines changed

2 files changed

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

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
public class FunFactsActivity extends Activity {
1616

17+
private FactBook mFactBook = new FactBook();
18+
1719
@Override
1820
protected void onCreate(Bundle savedInstanceState) {
1921
super.onCreate(savedInstanceState);
@@ -26,25 +28,7 @@ protected void onCreate(Bundle savedInstanceState) {
2628
View.OnClickListener listener = new View.OnClickListener() {
2729
@Override
2830
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-
31+
String fact = mFactBook.getFact();
4832
factLabel.setText(fact);
4933
}
5034
};

0 commit comments

Comments
 (0)