-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxList.java
More file actions
139 lines (122 loc) · 4.12 KB
/
Copy pathBoxList.java
File metadata and controls
139 lines (122 loc) · 4.12 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.Random;
/**
* Creates a list of Box items that are used in the game
*
* @author Sean DeZurik
*/
public class BoxList {
/** List of all the boxes in the game */
private Box[] boxes;
/**
* Constructor for BoxList class
*
* @param monetaryAmounts a double with the list
* of values for each box in the game
*/
public BoxList(double[] monetaryAmounts) {
/** Number of boxes to create */
int numBoxes = monetaryAmounts.length;
// Initialize array of Box items
boxes = new Box[numBoxes];
// Instantiate each Box in the array and set its value
for (int i = 0; i < numBoxes; i++) {
boxes[i] = new Box(monetaryAmounts[i]);
}
}
/**
* Get value of the box at the particular index
*
* @param index an int that is an index in the Box array
* @return a double with the value in the box
*/
public double getValue(int index) {
return boxes[index].getValue();
}
/**
* Show whether the box at the index has been opened or not
*
* @param index an int that is an index in the Box array
* @return a boolean that is true if the box has been opened
* and false if it has not
*/
public boolean isOpen(int index) {
return boxes[index].isOpen();
}
/**
* Opens the box at the particular index
*
* @param index an int that is an index in the Box array
*/
public void open(int index) {
boxes[index].open();
}
/**
* Provides the average value in the boxes that have not
* yet been opened
*
* @return a double with the average value of unopened boxes
*/
public double averageValueOfUnopenedBoxes() {
/** Sum of values in unopened boxes */
double total = 0.0;
/** Count of unopened boxes */
int count = 0;
// Check every box in the array
for (int i = 0; i < boxes.length; i++) {
// If the box has not been opened, add its value
// to the total sum
if (!boxes[i].isOpen()) {
total += boxes[i].getValue();
count++;
}
}
// Divide total sum by count of unopened to get the average
if (count == 0) {
return 0.0;
} else {
return total / count;
}
}
/**
* Randomly swaps the positions of two boxes in the array
*
* @param numberOfSwaps an int with the number of times that
* two random boxes will be swapped
*/
public void shuffle(int numberOfSwaps) {
/** Create a Random number object */
Random randomNumber = new Random();
for (int i = 0; i < numberOfSwaps; i++) {
/** Get a random number between zero and the number of boxes */
int random1 = randomNumber.nextInt(boxes.length);
/** A second random number, initially set to the first random number */
int random2 = random1;
// Generate a new second random number as long as it is the same value
// as the first random number
while (random2 == random1) {
// Between zero and number of boxes
random2 = randomNumber.nextInt(boxes.length);
}
// Swap the boxes
Box temp = boxes[random1];
boxes[random1] = boxes[random2];
boxes[random2] = temp;
}
}
/**
* Textual representation of the list of boxes
*
* @return String that is textual representation of each
* box in the list of boxes
*/
public String toString() {
/** String to return with textual representation of each box */
String representation = "";
// Get toString for each box in the box list
for (int i = 0; i < boxes.length; i++) {
// Append box toString to previous ones
representation += boxes[i].toString() + "\n";
}
return representation;
}
}