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