public class OneWayNoRepeatsList
1: //OneWayNoRepeatList.java
2:
3: /**
4: * An object of this class is a special kind of list of strings. You
5: * can write the list only from beginning to end. You can add only to
6: * the end of the list. You cannot change individual entries, but you
7: * can erase the entire list and start over. No entry may appear more
8: * than once on the list. You can use int variables as position markers
9: * into the list. Position markers are similar to array indices, but are
10: * numbered starting with 1.
11: */
12: public class OneWayNoRepeatsList
13: {
14: public static int START_POSITION = 1;
15: public static int DEFAULT_SIZE = 50;
16:
17: //entry.length is the total number of items you have room
18: //for on the list (its capacity); countOfEntries is the number of
19: //items currently on the list.
20: private int countOfEntries; //can be less than entry.length.
21: private String[] entry;
22:
23: /**
24: * Creates an empty list with a given capacity.
25: */
26: public OneWayNoRepeatsList(int maximumNumberOfEntries)
27: {
28: entry = new String[maximumNumberOfEntries];
29: countOfEntries = 0;
30: }
31:
32: /**
33: * Creates an empty list with a capacity of DEFAULT_SIZE.
34: */
35: public OneWayNoRepeatsList()
36: {
37: entry = new String[DEFAULT_SIZE];
38: countOfEntries = 0;
39: // or replace these two statements with this(DEFAULT_SIZE);
40: }
41:
42: public boolean isFull()
43: {
44: return countOfEntries == entry.length;
45: }
46:
47: public boolean isEmpty()
48: {
49: return countOfEntries == 0;
50: }
51:
52: /**
53: * Precondition: List is not full.
54: * Postcondition: If item was not on the list,
55: * it has been added to the list.
56: */
57: public void addItem
58: (
59: String item
60: )
61: {
62: if (!isOnList(item))
63: {
64: if (countOfEntries == entry.length)
65: {
66: System.out.println("Attempt to add to a full list!");
67: System.exit(0);
68: }
69: else
70: {
71: entry[countOfEntries] = item;
72: countOfEntries++;
73: }
74: } //else do nothing. Item is already on the list.
75: }
76:
77: /**
78: * If the argument indicates a position on the list,
79: * the entry at that specified position is returned;
80: * otherwise, null is returned.
81: */
82: public String getEntryAt
83: (
84: int position
85: )
86: {
87: String result = null;
88: if ((1 <= position) && (position <= countOfEntries))
89: result = entry[position - 1];
90: return result;
91: }
92:
93: /**
94: * Returns true if position indicates the last item
95: * on the list; otherwise, returns false.
96: */
97: public boolean atLastEntry
98: (
99: int position
100: )
101: {
102: return position == countOfEntries;
103: }
104:
105: /**
106: * Returns true if item is on the list;
107: * otherwise, returns false. Does not differentiate
108: * between uppercase and lowercase letters.
109: */
110: public boolean isOnList
111: (
112: String item
113: )
114: {
115: boolean found = false;
116: int i = 0;
117: while (!found && (i < countOfEntries))
118: {
119: if (item.equalsIgnoreCase(entry[i]))
120: found = true;
121: else
122: i++;
123: }
124: return found;
125: }
126:
127: public int getMaximumNumberOfEntries()
128: {
129: return entry.length;
130: }
131:
132: public int getNumberOfEntries()
133: {
134: return countOfEntries;
135: }
136:
137: public void eraseList()
138: {
139: countOfEntries = 0;
140: }
141: }