PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD
OR REMOVE NAME. DO NOT HARD CODE NAME. AGAIN MAKE SURE THE
PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR REMOVE NAME. PLEASE
MAKE SURE THE CODE RUNS WITHOUT ANY ERRORS.
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.
Ask the user to add or remove users to update 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
{
/**
* 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 implements List
{
private DLink head; // Pointer to list header
private DLink tail; // Pointer to last element in list
protected DLink curr; // Pointer ahead of current element
int cnt; // Size of list
// Constructors
LList(int size)
{
th.
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
1. PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD
OR REMOVE NAME. DO NOT HARD CODE NAME. AGAIN MAKE SURE THE
PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR REMOVE NAME. PLEASE
MAKE SURE THE CODE RUNS WITHOUT ANY ERRORS.
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.
Ask the user to add or remove users to update 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
{
/**
* 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);
2. /**
* 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.
3. */
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 implements List
{
private DLink head; // Pointer to list header
private DLink tail; // Pointer to last element in list
protected DLink curr; // Pointer ahead of current element
int cnt; // Size of list
// Constructors
LList(int size)
{
this();
} // Ignore size
LList()
{
curr = head = new DLink(null, null); // Create header node
tail = new DLink(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(null, null); // Create header node
tail = new DLink(head, null);
head.setNext(tail);
4. 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(it, curr, curr.next()));
curr.next().next().setPrev(curr.next());
cnt++;
}
/** Append "it" to list */
public void append(E it)
{
tail.setPrev(new DLink(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 */
5. 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 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)
6. return null;
return curr.next().element();
}
// reverseList() method that reverses the LList
public void reverseList()
{
LList revList = new LList();
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)
{
7. 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
{
private E element; // Value for this node
private DLink next; // Pointer to next node in list
private DLink prev; // Pointer to previous node
/** Constructors */
DLink(E it, DLink p, DLink n)
{
element = it;
prev = p;
next = n;
}
8. DLink(DLink p, DLink n)
{
prev = p;
next = n;
}
/** Get and set methods for the data members */
DLink next()
{
return next;
}
DLink setNext(DLink nextval)
{
return next = nextval;
}
DLink prev()
{
return prev;
}
DLink setPrev(DLink 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) {
9. 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
Solution
PROGRAM CODE:
GameSimulator.java
package list;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class GameSimulator {
public static void main(String[] args) {
Scanner filereader;
try {
filereader = new Scanner(new File("gamescore.txt"));
GameApplication application = new GameApplication(20);
while(filereader.hasNextLine())
{
String next = filereader.next();
int index = next.indexOf(",");
String tokens[] = new String[2];
10. tokens[0] = next.substring(0, index);
tokens[1] = next.substring(index+1, next.length());
GameEntry entry = new GameEntry(tokens[0], Integer.parseInt(tokens[1]));
application.add(entry);
}
System.out.println("After adding from file:");
application.print();
String option = "Y";
while(option.charAt(0)=='Y' || option.charAt(0) == 'y')
{
Scanner keyboard = new Scanner(System.in);
System.out.print("1. Add 2. Remove Enter your choice: ");
int choice = Integer.parseInt(keyboard.nextLine());
String name;
int score;
if(choice == 1)
{
System.out.print("Enter the name: ");
name = keyboard.nextLine();
System.out.print("Enter the score: ");
score = Integer.parseInt(keyboard.nextLine());
GameEntry entry = new GameEntry(name, score);
application.add(entry);
application.print();
}
else if(choice == 2)
{
System.out.print("Enter the name: ");
name = keyboard.nextLine();
application.remove(name);
application.print();
}
else
System.out.println("Invalid choice");
System.out.print("Try Again: (Y/N): ");
option = keyboard.next();
12. public void remove(String name)
{
//checking if the name exits or not
if(!checkDuplicates(name))
{
System.out.println("Name does not exist");
return;
}
for(int i=0; i
1. Add
2. Remove
Enter your choice: 1
Enter the name: Jack
Enter the score: 320
Name exist
< | (Jack,510) (Rose,590) (Anna,660) (Paul,720) (Rob,750) (Mike,1105) >
Try Again: (Y/N): Y
1. Add
2. Remove
Enter your choice: 1
Enter the name: Mila
Enter the score: 320
< | (Mila,320) (Jack,510) (Rose,590) (Anna,660) (Paul,720) (Rob,750) (Mike,1105) >
Try Again: (Y/N): Y
1. Add
2. Remove
Enter your choice: 2
Enter the name: Anna
< (Mila,320) (Jack,510) (Rose,590) (Paul,720) (Rob,750) | (Mike,1105) >
Try Again: (Y/N): N