-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPocket.java
More file actions
78 lines (70 loc) · 1.48 KB
/
Pocket.java
File metadata and controls
78 lines (70 loc) · 1.48 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
/**
* A container for coins.
*
* @author Lisa Miller
* @since 9/23/2017
*/
public class Pocket {
/** number of coins held. */
private int nCoins = 0;
/** array to hold the coins. */
private Coin[] coins = new Coin[10];
/** Uses default constructor. */
/**
* adds a Coin to the front of the array.
* @param c the Coin to be added
*/
public void addCoin(Coin c) {
if (nCoins > 0) {
relocateUp();
}
coins[0] = c;
nCoins++;
}
/**
* Takes out the Coin at the front of the array.
* Reduces count of Coins held.
*/
public Coin removeCoin() {
Coin c = coins[0];
if (nCoins > 0) {
relocateDown();
}
coins[nCoins] = null;
nCoins--;
return c;
}
/**
* Returns whole array of Coins.
* @return an array of Coins.
*/
public Coin[] getCoins() {
return coins;
}
/**
* Returns size of array of Coins.
* @return the size of the Pocket Array.
*/
public int getNCoins() {
return nCoins;
}
/**
* Moves all Coins in array up
* one index ( [i-1] -> [i]).
*/
private void relocateUp() {
for (int i = nCoins; i > 0; i--) {
coins[i] = coins[i - 1];
}
}
/**
* Moves all Coins in array down
* one index ( [i+1] -> [i]).
* Erases Coin at index 0.
*/
private void relocateDown() {
for (int i = 0; i < nCoins; i++) {
coins[i] = coins[i + 1];
}
}
}