Question: For each task, zip and submit all your source java code files. Objective: Implement Link List Write a class that maintains the scores for a
For each task, zip and submit all your source java code files.
Objective: Implement Link List Write a class that maintains the scores for a game application. The gamescore.txt contains player name and score data record fields separated by comma. Implement the addition and removal function to update the database. Reject adding duplicate name to the list. For Removal function, uses the name field to select record to remove the game score record. Display error message if user tried to delete a name that was not on the list. Optional sort the list by score.
Download List.java, LList.java, DLink.java, GameEntry.java and gamescore.txt from Moodle
Objective: Manipulate the Linked List Pointer. Add a reverse list method in LList.java or create a subclass to reverse the order of the linked list. Print the original linked list and the reverse ordered linked list at the end of program. You can either use the test.dat or use the gamescore.txt to test the reverse method.
gamescore.txt : Mike,1105 Rob,750 Paul,720 Anna,660 Rose,590 Jack,510
List.java : /** List ADT */ public interface List
/** Insert an element at the current location. The client must ensure that the list's capacity is not exceeded. @param item The element to be inserted. */ public void insert(E item);
/** Append an element at the end of the list. The client must ensure that the list's capacity is not exceeded. @param item The element to be appended. */ public void append(E item);
/** Remove and return the current element. @return The element that was removed. */ public E remove();
/** Set the current position to the start of the list */ public void moveToStart();
/** Set the current position to the end of the list */ public void moveToEnd();
/** Move the current position one step left. No change if already at beginning. */ public void prev();
/** Move the current position one step right. No change if already at end. */ public void next();
/** @return The number of elements in the list. */ public int length();
/** @return The position of the current element. */ public int currPos();
/** Set current position. @param pos The position to make current. */ public void moveToPos(int pos);
/** @return The current element. */ public E getValue(); }
LList.java :
// Doubly linked list implementation
class LList
private DLink
private DLink
protected DLink
int cnt; // Size of list
//Constructors
LList(int size) { this(); } // Ignore size
LList() {
curr = head = new DLink
tail = new DLink
head.setNext(tail);
cnt = 0;
}
public void clear() { // Remove all elements from list
head.setNext(null); // Drop access to rest of links
curr = head = new DLink
tail = new DLink
head.setNext(tail);
cnt = 0;
}
public void moveToStart() // Set curr at list start
{ curr = head; }
public void moveToEnd() // Set curr at list end
{ curr = tail.prev(); }
/** Insert "it" at current position */
public void insert(E it) {
curr.setNext(new DLink
curr.next().next().setPrev(curr.next());
cnt++;
}
/** Append "it" to list */
public void append(E it) {
tail.setPrev(new DLink
tail.prev().prev().setNext(tail.prev());
cnt++;
}
/** Remove and return current element */
public E remove() {
if (curr.next() == tail) return null; // Nothing to remove
E it = curr.next().element(); // Remember value
curr.next().next().setPrev(curr);
curr.setNext(curr.next().next()); // Remove from list
cnt--; // Decrement the count
return it; // Return value removed
}
/** Move curr one step left; no change if at front */
public void prev() {
if (curr != head) // Can't back up from list head
curr = curr.prev();
}
// Move curr one step right; no change if at end
public void next()
{ if (curr != tail.prev()) curr = curr.next(); }
public int length() { return cnt; }
// Return the position of the current element
public int currPos() {
DLink
int i;
for (i=0; curr != temp; i++)
temp = temp.next();
return i;
}
// Move down list to "pos" position
public void moveToPos(int pos) {
assert (pos>=0) && (pos<=cnt) : "Position out of range";
curr = head;
for(int i=0; i } public E getValue() { // Return current element if (curr.next() == tail) return null; return curr.next().element(); } // Extra stuff not printed in the book. /** * Generate a human-readable representation of this list's contents * that looks something like this: < 1 2 3 | 4 5 6 >. The vertical * bar represents the current location of the fence. This method * uses toString() on the individual elements. * @return The string representation of this list */ public String toString() { // Save the current position of the list int oldPos = currPos(); int length = length(); StringBuffer out = new StringBuffer((length() + 1) * 4); moveToStart(); out.append("< "); for (int i = 0; i < oldPos; i++) { if (getValue()!=null) { out.append(getValue()); out.append(" "); } next(); } out.append("| "); for (int i = oldPos; i < length; i++) { out.append(getValue()); out.append(" "); next(); } out.append(">"); moveToPos(oldPos); // Reset the fence to its original position return out.toString(); } } DLink.java : /** Doubly linked list node */ class DLink /** Constructors */ DLink(E it, DLink /** Get and set methods for the data members */ DLink GameEntry.java : public class GameEntry { protected String name; protected int score; public GameEntry(String n, int s) { name = n; score = s; } public String getName() {return name;} public int getScore() {return score;} public String toString() { return "("+name+","+score+")"; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
