-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise19.java
More file actions
52 lines (44 loc) · 1.01 KB
/
Exercise19.java
File metadata and controls
52 lines (44 loc) · 1.01 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
interface Games {
void play();
}
interface GamesFactory {
Games getGames();
}
class CoinToss implements Games {
Random rand = new Random();
public void play() {
System.out.println("Toss Coin: ");
switch(rand.nextInt(2)) {
case 0 : System.out.println("Heads"); return;
case 1 : System.out.println("Tails"); return;
default: System.out.println("OnEdge"); return;
}
}
}
class CoinTossFactory implements GamesFactory {
public Games getGames() {
return new CoinToss();
}
}
class DiceThrow implements Games {
Random rand = new Random();
public void play() {
System.out.println("Throw Dice: " + (rand.nextInt(6) + 1));
}
}
class DiceThrowFactory implements GamesFactory {
public Games getGames() {
return new DiceThrow();
}
}
public class Exercise19 {
public static void playGame(GamesFactory factory) {
Games g = factory.getGames();
g.play();
}
public static void main(String [] args) {
playGame(new CoinTossFactory());
playGame(new DiceThrowFactory());
}
}