-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinFlipper.java
More file actions
67 lines (57 loc) · 1.58 KB
/
CoinFlipper.java
File metadata and controls
67 lines (57 loc) · 1.58 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Driver class for Flippable Coins
* @author Lisa Miller
* @since 9/9/2017
*/
public class CoinFlipper{
public static void main( String [] args){
//array of coins can hold all subclasses
Coin[] coinArr = new Coin[2];
coinArr[0] = new Dime();
coinArr[1] = new Penny();
for (int i = 0; i < coinArr.length; i++){
System.out.print("Coin type: " + coinArr[i].getName() + "\tValue: " + coinArr[i].getValue());
System.out.println("\tColor: " + coinArr[i].getColor() + "\tUp Side: " + coinArr[i].getUpSide());
}
//do ten coin tosses:
for (int i = 0; i < 10; i++) {
System.out.print(printUpSide(coinArr[0].getUpSide()) + " -> ");
coinArr[0].toss();
System.out.println(printUpSide(coinArr[0].getUpSide()));
}
}//close main
/**
* private static method for getting upside as a string
* @param i the upSide int value
* @return the upSide String value
*/
private static String printUpSide(int i) {
if (i == 0){
return("Heads");
}else {
return("Tails");
}
}
/**
* mathod that takes in a flippable object, makes a new coin based on the side
* and returns it
* @param an object to get side
* @return a new COin object
*/
public static Flippable doubleFlip(Flippable f1){
int side = f1.getUpSide();
Coin c;
switch(side){
case 0:
c = new Penny();
break;
case 1:
c = new Nickel();
break;
default:
c = new Coin(0.0, "some other coin");
break;
}
return c;
}//end doubleFlip
}//close class