Question

In: Computer Science

DO NOT HARD CODE ANYTHING! Write a class that maintains the scores for a game application....

DO NOT HARD CODE ANYTHING!

Write a class that maintains the scores for a game application. Implement the addition and removal function to update the database. The gamescore.txt contains player’ name and score data record fields separated by comma. For Removal function, uses the name field to select record to remove the game score record.

Use the List.java, LList.java, Dlink.java, GameEntry.java and gamescore.txt found below

Read gamescore.txt to initialize the Linked list in sorted order by score.

Important****ASK the user to Remove, Add and update function to the sorted linked list.

Display “Name exist” when add an exist name to the list.

Display “Name does not exist” when remove a name not on the list.

List.java File:

/** Source code example for "A Practical Introduction to Data

Structures and Algorithm Analysis, 3rd Edition (Java)"

by Clifford A. Shaffer

Copyright 2008-2011 by Clifford A. Shaffer

*/

/** List ADT */

public interface List<E>

{

/**

* Remove all contents from the list, so it is once again empty. Client is

* responsible for reclaiming storage used by the list elements.

*/

public void clear();

/**

* 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 File:

/**

* Source code example for "A Practical Introduction to Data Structures and

* Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright

* 2008-2011 by Clifford A. Shaffer

*/

// Doubly linked list implementation

class LList<E> implements List<E>

{

private DLink<E> head; // Pointer to list header

private DLink<E> tail; // Pointer to last element in list

protected DLink<E> curr; // Pointer ahead of current element

int cnt; // Size of list

// Constructors

LList(int size)

{

this();

} // Ignore size

LList()

{

curr = head = new DLink<E>(null, null); // Create header node

tail = new DLink<E>(head, null);

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<E>(null, null); // Create header node

tail = new DLink<E>(head, null);

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<E>(it, curr, curr.next()));

curr.next().next().setPrev(curr.next());

cnt++;

}

/** Append "it" to list */

public void append(E it)

{

tail.setPrev(new DLink<E>(it, tail.prev(), tail));

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<E> temp = head;

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 < pos; i++)

curr = curr.next();

}

public E getValue()

{

// Return current element

if (curr.next() == tail)

return null;

return curr.next().element();

}

// reverseList() method that reverses the LList

public void reverseList()

{

LList<E> revList = new LList<E>();

curr = tail.prev();

while (curr != head)

{

revList.append(curr.element());

curr = curr.prev();

}

head.setNext(revList.head.next());

}

// 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 File:

/** Source code example for "A Practical Introduction to Data

Structures and Algorithm Analysis, 3rd Edition (Java)"

by Clifford A. Shaffer

Copyright 2008-2011 by Clifford A. Shaffer

*/

/** Doubly linked list node */

class DLink<E>

{

private E element; // Value for this node

private DLink<E> next; // Pointer to next node in list

private DLink<E> prev; // Pointer to previous node

/** Constructors */

DLink(E it, DLink<E> p, DLink<E> n)

{

element = it;

prev = p;

next = n;

}

DLink(DLink<E> p, DLink<E> n)

{

prev = p;

next = n;

}

/** Get and set methods for the data members */

DLink<E> next()

{

return next;

}

DLink<E> setNext(DLink<E> nextval)

{

return next = nextval;

}

DLink<E> prev()

{

return prev;

}

DLink<E> setPrev(DLink<E> prevval)

{

return prev = prevval;

}

E element()

{

return element;

}

E setElement(E it)

{

return element = it;

}

}

GameEntry.java File:

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+")";
}

}

gamescore.txt File:

Mike,1105
Rob,750
Paul,720
Anna,660
Rose,590
Jack,510

Solutions

Expert Solution

package gamescore;


/**
* List ADT
*
* @param <E>
*/
public interface List<E> {
  
   /**
   * Clear all elements from the list
   */
   public void clear();
  
   /**
   * Insert item on the current position of the list
   * @param item
   */
   public void insert(E item);
  
   /**
   * Append item to the end of the list
   *
   * @param item
   */
   public void append(E item);
  
   /**
   * Remove element from the current position
   *
   * @return element
   */
   public E remove();
  
   /**
   * Move to start of the list
   */
   public void moveToStart();
  
   /**
   * Move to end of the list
   */
   public void moveToEnd();
  
   /**
   * Move current pointer to previous node of the list
   */
   public void prev();
  
   /**
   * Move current pointer to next node of the list
   */
   public void next();
  
   /**
   * Return total elements in the list
   *
   * @return length
   */
   public int length();
  
   /**
   * Return current position of the list
   *
   * @return currPos
   */
   public int currPos();
  
   /**
   * Move current pointer to specified position in the list
   *
   * @param pos
   */
   public void moveToPos(int pos);
  
   /**
   * Get the value stored at the current position
   *
   * @return element
   */
   public E getValue();
}


package gamescore;

/**
* Doubly linked list node
*
* @param <E>
*/
public class DLink<E> {
  
   private E element; // Stored element
   private DLink<E> next; // Pointer to next node
   private DLink<E> prev; // Pointer to previous node
  
   public DLink(E element, DLink<E> prev, DLink<E> next) {
       this.element = element;
       this.prev = prev;
       this.next = next;
   }

   public DLink(DLink<E> prev, DLink<E> next) {
       this.prev = prev;
       this.next = next;
   }
  
   DLink<E> next() {
       return next;
   }
  
   DLink<E> prev() {
       return prev;
   }

   public E element() {
       return element;
   }

   public void setElement(E element) {
       this.element = element;
   }

   public void setNext(DLink<E> next) {
       this.next = next;
   }

   public void setPrev(DLink<E> prev) {
       this.prev = prev;
   }
}

package gamescore;

public class LList<E> implements List<E> {

   private DLink<E> head;
   private DLink<E> tail;
   private DLink<E> curr;
  
   int cnt;
  
   public LList() {
       curr = head = new DLink<E>(null, null);
       tail = new DLink<E>(head, null);
       head.setNext(tail);
       cnt = 0;
   }

   @Override
   public void clear() {
       head.setNext(null);
       curr = head = new DLink<E>(null, null);
       tail = new DLink<E>(head, null);
       head.setNext(tail);
       cnt = 0;
   }

   @Override
   public void insert(E item) {
       DLink<E> newNode = new DLink<E>(item, curr, curr.next());
       curr.setNext(newNode);
       curr.next().next().setPrev(newNode);
       cnt++;
   }

   @Override
   public void append(E item) {
       DLink<E> newNode = new DLink<E>(item, tail.prev(), tail);
       tail.setPrev(newNode);
       tail.prev().prev().setNext(newNode);
       cnt++;
   }

   @Override
   public E remove() {
       if(curr.next() == tail) {
           return null;
       }
      
       E item = curr.next().element();
       curr.next().next().setPrev(curr);
       curr.setNext(curr.next().next());
       cnt--;
      
       return item;
   }

   @Override
   public void moveToStart() {
       curr = head;
   }

   @Override
   public void moveToEnd() {
       curr = tail.prev().prev();
   }

   @Override
   public void prev() {
       if(curr != head) {
           curr = curr.prev();
       }
   }

   @Override
   public void next() {
       if(curr != tail.prev()) {
           curr = curr.next();
       }
   }

   @Override
   public int length() {
       return cnt;
   }

   @Override
   public int currPos() {
       DLink<E> temp = head;
       int i;
       for(i = 0; curr != temp; i++) {
           temp = temp.next();
       }
       return i;
   }

   @Override
   public void moveToPos(int pos) {
       curr = head;
       for(int i = 0; i < pos; i++) {
           curr = curr.next();
       }
   }

   @Override
   public E getValue() {
       if(curr.next() == tail)
           return null;
      
       return curr.next().element();
   }
  
   /*
   * Representation of the list as format <1,2,3|4,5,6>
   * where | represent current position of the list pointer
   */
   @Override
   public String toString() {
       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++) {
           if(getValue() != null) {
               out.append(getValue());
               out.append(" ");
           }
           next();
       }
       out.append(">");
       moveToPos(oldPos);
      
       return out.toString();
   }
}

package gamescore;

public class GameEntry {
   private String name;
   private int score;
  
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getScore() {
       return score;
   }
   public void setScore(int score) {
       this.score = score;
   }
   @Override
   public String toString() {
       return "(" + name + "," + score + ")";
   }
}

package gamescore;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;

public class GameAppllication {

   /**
   * @param args
   * @throws IOException
   * @throws NumberFormatException
   */
   public static void main(String[] args) throws NumberFormatException, IOException {
       LList<GameEntry> games = loadGame("gamescore.txt");
       System.out.println(games);
      
       games.moveToStart();
       System.out.println(games);
       System.out.println(games.getValue());
      
       games.moveToEnd();
       System.out.println(games);
       System.out.println(games.getValue());
      
       games.moveToPos(3);
       System.out.println(games.getValue());
   }
  
   private static LList<GameEntry> loadGame(String fileName) throws NumberFormatException, IOException {
       File file = new File(fileName);
      
       FileInputStream fis = null;
       try {
           fis = new FileInputStream(file);
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       BufferedReader br = new BufferedReader(new InputStreamReader(fis));
      
       String gameScore;
       String gamer;
       String score;
       LList<GameEntry> list = new LList<GameEntry>();
       while((gameScore = br.readLine()) != null) {
           GameEntry gameEntry = new GameEntry();
           gamer = gameScore.split(",")[0];
           score = gameScore.split(",")[1];
           gameEntry.setName(gamer);
           gameEntry.setScore(Integer.parseInt(score));
           list.insert(gameEntry);
       }
      
       return list;
   }

}


gamescore.txt
Mike,1105
Rob,750
Paul,720
Anna,660
Rose,590
Jack,510


Related Solutions

Write a class that maintains the scores for a game application. Implement the addition and removal...
Write a class that maintains the scores for a game application. Implement the addition and removal function to update the database. The gamescore.txt contains player’ name and score data record fields separated by comma. For Removal function, uses the name field to select record to remove the game score record. Use the List.java, LList.java, Dlink.java, GameEntry.java and gamescore.txt found below Read gamescore.txt to initialize the Linked list in sorted order by score. Provide Remove and Add function for user to...
JAVA CODE PLEASE write a casino membership application. in the casino membership class: data: membership ID...
JAVA CODE PLEASE write a casino membership application. in the casino membership class: data: membership ID points method: add points display points Main: create a membership object (a client) give initial points (600) increase the points by (try both 200 and 500)
C++: Do not change anything in the supplied code below that will be the Ch16_Ex5_MainProgram.cpp except...
C++: Do not change anything in the supplied code below that will be the Ch16_Ex5_MainProgram.cpp except to add documentation. PLEASE DO NOT CHANGE THE CODE JUST ADD DOCUMENTATION. Thank you. The last few have changed the code. Appreciate you all. Please use the file names listed below since your file will have the following components: Ch16_Ex5_MainProgram.cpp - given file //22 34 56 4 19 2 89 90 0 14 32 88 125 56 11 43 55 -999 #include <iostream> #include...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...
write a code for a typing game in plain C coding.
write a code for a typing game in plain C coding.
Write a class encapsulating a board game. A board game has the following additional attributes: the...
Write a class encapsulating a board game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. Code the constructor and the toString method of the new class. You also need to include a client class(with the main method) to test your code. code in Python
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will...
Please make my Code working and pass the test but do NOT change anything in main...
Please make my Code working and pass the test but do NOT change anything in main function, thank you. #include <iostream> using namespace std; void sort(int *A, int n){    for(int passes = 0;passes < 2;passes++) { // shift can have only two values either 0 or 16, used for shifting purpose int shift = passes * 16; int N = 1<<(16 + 1);    // Temporary array for storing frequency of upper or lower 16 bits int temp[N];   ...
Write a class named TestScores. The class constructor should accept an array of test scores as...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program. Use TestScoresDemo.java to test your code public class TestScoresDemo { public static void main(String[] args) { // An array with test scores. //...
Write an application that allows a user to enter any number of student quiz scores, as...
Write an application that allows a user to enter any number of student quiz scores, as integers, until the user enters 99. If the score entered is less than 0 or more than 10, display Score must be between 10 and 0 and do not use the score. After all the scores have been entered, display the number of valid scores entered, the highest score, the lowest score, and the arithmetic average.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT