-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleXIFO.java
More file actions
180 lines (157 loc) · 5.98 KB
/
Copy pathSimpleXIFO.java
File metadata and controls
180 lines (157 loc) · 5.98 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.util.Arrays;
import java.util.Random;
/**
* An array-based demonstration for FIFO and LIFO operations
* @version 202210181100
*/
/**
* Code added by Max Simmer
* Completed the removeFirst and addLast methods.
* Notes: Arrays are fixed sizes, need to shift the elements within the stack
* in order to move them.
*/
public class SimpleXIFO {
/**
* An array that stores strings added to it either on a first-available
* basis (using the addLast method); or inserted as the first element
* (using the addFirst method).
*/
private String[] xifo;
/** A counter that tells us when the xifo array is full or empty */
private int usage;
/** Default size for the array if someone calls the default constructor */
private static final int DEFAULT_SIZE = 4;
/**
* Default constructor. Passes the default size value to the basic constructor.
*
* DO NOT MODIFY THIS CONSTRUCTOR
*/
public SimpleXIFO() {
this(DEFAULT_SIZE);
} // Default constructor ... DO NOT MODIFY
/**
* Basic constructor. Initializes an array with the specified size and sets
* its usage to 0.
*
* @param size int value of size for the xifo array.
*
* DO NOT MODIFY THIS CONSTRUCTOR
*/
public SimpleXIFO(int size) {
this.xifo = new String[size];
this.usage = 0;
} // Basic constructor ... DO NOT MODIFY
/**
* Last-in.
*
* Method adds a string to the first available position in the array, if there
* is room in the array. Note that the method does not add elements at the
* last position in the array but at the first available. As more elements
* are added, they move closer and closer to the end fo the array.
*
* @param string String value to the end of the array
*
* DO NOT MODIFY THIS METHOD
*/
public void addLast(String string) {
// Check that there is room in the array
if (this.usage < this.xifo.length) {
this.xifo[usage] = string;
// Update usage
this.usage++;
}
} // method addLast ... DO NOT MODIFY
/**
* First out.
*
* Method removes the first element of the array.
*
* @return String with the value of the first element in the array.
*/
public String removeFirst() {
String removed = null;
// if usage (counter) is not empty (if there is room to add)
if (this.usage > 0) {
// set removed to the FIRST element of xifo array
removed = xifo[0];
// for loop to shift all elements one position left
// essentially pushing the first element out of the stack
// {1, 2, 3, 4} -> 1 {X, 2, 3, 4},
for (int i=0; i<this.xifo.length-1; i++){
// push elements to the left
this.xifo[i] = this.xifo[i+1];
}
// decrease counter
this.usage--;
}
return removed;
} // method removeFirst
/**
* First in
*
* Adds an element at the beginning of the array, if there is room in the
* array. The method must ensure that existing elements in the array are not
* lost. For example, consider the array ["C", "B", "A", null]. If we use
* addFirst("D"), the array must become ["D", "C", "B", "A"].
*
* @param string String value to add to the beginning of the array.
*/
public void addFirst(String string) {
// if usage (counter) is less than the length of the array
if (this.usage<this.xifo.length){
// for loop to push the stack right
for (int i=this.xifo.length-1; i>0; i--){
// push elements right
this.xifo[i] = this.xifo[i-1];
}
// increase counter
this.usage++;
// set the first element to the given string in parameters
this.xifo[0] = string;
}
} // method addFirst
/** FOR TESTING PURPOSES ONLY. DO NOT MODIFY THIS METHOD */
public String[] getXifo() {
return this.xifo;
} // method getXifo ... DO NOT MODIFY
/** FOR TESTING PURPOSES ONLY. DO NOT MODIFY THIS METHOD */
public String toString() {
return Arrays.toString(this.xifo);
} // method toString ... DO NOT MODIFY
/**
* TEST CODE. DO NOT MOFIFY THE MAIN METHOD BUT USE IT TO TEST IF YOUR
* CODE WORKS AS EXPECTED.
*/
public static void main(String[] args) {
int testSize = 1024;
int ascii_A = (int) 'A';
int letters = 26;
Random rng = new Random();
SimpleXIFO fifo = new SimpleXIFO(testSize);
SimpleXIFO lifo = new SimpleXIFO(testSize);
for (int i = 0; i < testSize; i++) {
String dataToAdd = String.valueOf((char) (ascii_A+rng.nextInt(letters)));
fifo.addFirst(dataToAdd);
lifo.addLast(dataToAdd);
}
String[] ofif = fifo.getXifo().clone();
String[] ofil = lifo.getXifo().clone();
boolean fifoTest = true;
boolean lifoTest = true;
for (int i = 0; i < testSize; i++) {
fifoTest = fifoTest && fifo.removeFirst().equals(ofif[i]);
lifoTest = lifoTest && lifo.removeFirst().equals(ofil[i]);
}
boolean symmetry = true;
for (int i = 0; i < testSize; i++) {
symmetry = symmetry && ofil[i].equals(ofif[ofif.length-1-i]);
}
if (symmetry && fifoTest && lifoTest) {
System.out.printf("\nYour code seems to be working as expected.\n");
} else if (!symmetry) {
System.out.printf("\nYour addFirst method is not quite there yet.\n");
} else {
System.out.printf("\nYour removeFirst method is not quite there yet.\n");
}
} // method main ... DO NOT MODIFY
}