Hey guys, I seem to be having a bit of trouble with my assignement involving linked lists in java. The problem is with creating the node. My professor asks that we create a string and int in the node, but all examples that I have found only use one node. Heres the assignment:

In addition to linked lists, you will demonstrate your understanding of many components discussed this semester including inheritance. Everyone must use inheritance to create a linked list class and an application that tracks the name and score of the top 10 gamers using the linked list class. Duplicate entries are allowed in the list. Only the top 10 scores will be maintained. When the list contains 10 entries and a new entry is attempted, the application should only add the new entry if the score exceeds the lowest score on the list. In that case, the lowest score currently on the list should be dropped.

Java defines a LinkedList class in java.util. Your GameLinkedList class should extend this class and implement the GameLinkedListInterface (click link to download the interface.)

Eligible points for this project are based upon the items completed. All projects must extend the java.util.LinkedList class and implement the GameLinkedListInterface (option 1).

Option Items Completed
Percentage of the available points
1 Node, GameLinkedList 70%
2 Exception Handling using exception classes + 15%
3 GUI client + 15%

For those not completing Option 3, a keyboard/monitor client is required.



Classes:

Node class

The Node class should define a single name, score, and the next Node object in the Linked List. All Node objects must contain a non-blank name and a score greater than zero.

Options

•(Option 1) Display an error message to System.err object whenever an illegal name or illegal score is encountered.

•(Option 2) The class should throw a IllegalDataFieldException exception (see below) whenever an illegal name or illegal score is encountered.



GameLinkedList class

A customized version of the java.util.LinkedList class, the GameLinkedList class should maintain an ordered linked list of Node objects. The class must implement the GameLinkedListInterface. The class will be responsible for creating new Node objects and adding them to the list when space is available. When the list is full and the new Node's score is greater than the lowest score, the Node containing the lowest score should be removed from the list.

Options

•(Option 1) Display an error message to System.err object whenever a new Node can not be added to the list. Remove the comment from the applicable add method described in the GameLinkedListInterface.

•(Option 2) The class should throw a LinkedListException exception (see below) when a Node can not be created due to illegal data. The exception should also be thrown when the list is full and the score to be added is less than the lowest score on the list. Remove the comment from the applicable add method described in the GameLinkedListInterface.



Exception classes (Option 2 only)

Two exception classes are required, one for Node errors and one for LinkedList errors to respond to the following:

•The IllegalDataFieldException class reports Node errors to the LinkedList class

◦null or blank name

◦score less than 0



•The LinkedListException class reports Linked List errors to the client

◦unsuccessful add due to a Node error or a full list



Client Application:

The client is responsible for retrieving a new name and score entry from the user, sending the information to the Linked List for addition, and displaying the current content of the Link List class.

Options

•(Option 1) The client should utilize the keyboard and monitor. Individual classes are responsible for error reporting.

•(Option 2) The client should utilize the keyboard and monitor. The client is also responsible for displaying any errors that occur during processing.

•(Option 3) Implement the client using a simple GUI interface such as the one shown below. When option 2 is also completed, error reporting should be displayed in the window.

Hints:

The Linked List must be maintained in score order. Therefore, the Node containing the lowest score will be at one end of the list or the other. Before developing the add method, take into consideration the removeLowest method (is it easier to remove a node from the beginning or the end?)

When completing option three, keep it simple. A JTextArea and the GameLinkedList toString method can be used to display the list. Since the data is being managed by the GameLinkedList class, there is no need for JPanel classes.

Read the lab specifications again. There is not a lot of coding! Plus, by completing all three options, you will have reviewed a substantial amount of the material covered on the Final exam ;)

Evaluation:

Classes must compile without syntax errors or will not be evaluated. This lab is worth 30 points. All class should be implemented as outlined above and must adhere to the style guide. Points will be distributed as follows:

•Node class (4 points)
•LinkedList class (15 points)
•IllegalDataFieldException and IllegalLinkedListException classes (4.5 points)
•Client application (2 points) or GUI client application (6.5 points)
•On time (late penalty of 10 points assessed for projects submitted late)

Here is the node code so far:

import java.util.LinkedList;

public class Node
{
private String name;
private int score;
private Node next;

public Node()
{
name = null;
score = 0;
next = null;
}

public Node(String nam, int sco) throws IllegalDataFieldException
{
if (nam == null || sco < 0)
throw new IllegalDataFieldException("No name entered or negative score posted");
else
{
name = nam;
score = sco;
}

next = null;
}

public void setName(String nam)
{
name = nam;
}

public String getName()
{
return name;
}

public void setScore(int sco)
{
score = sco;
}

public int getScore()
{
return score;
}

public void setNext(Node n)
{
next = n;
}

public Node getNext()
{
return next;
}

}

Anyone?

What is the question?

Have you compiled the code and encountered some problems?

The Node class looks OK to me, complete with constructors and get/set methods. The state variables are as required by the instructions.

I have not read through everything in detail, but I do not seem to see code that places the node in a linkedlist, or in fact, there is no code that instantiates nodes.

In case you have not started reading about java's LinkedList class, here's a link to the API. Notice that the java's LinkedList class does not have an upper limit to the number of nodes. If the initial limit is exceeded, it will extend the list automatically. So you will have to programme the limit in your code as instructed by your project requirements.

You are on the right track. Please tell us more. For example, you could post your pseudocode for discussion.

Actually I have a few questions:

How do I create the removeLowest() method if the maximum number of nodes can be up to ten.

I seem to be having problems with the toString() method, Any suggestions? Heres my code so far:

import java.util.LinkedList;

public class GameLinkedList implements GameLinkedListInterface
{
private Node head;
public int MAXSIZE = 10;

public GameLinkedList()
{
head = null;
}

public boolean isEmpty()
{
if (head == null)
return true;
else
return false;
}

public void add(String newName, int newScore) throws LinkedListException
{
Node newNode = new Node(newName, newScore);
if (newName == null || newScore < 0)
{
throw new LinkedListException("Illegal Data Given");
}
if (isEmpty())
head = newNode;
else
{
if (head.getScore() > newNode.getScore())
{
newNode.setNext(head);
head = newNode;
}
else
{
Node current = head;
while (current.getNext() != null && current.getNext().getScore() < newNode.getScore())
current = current.getNext();
newNode.setNext(current.getNext());
current.setNext(newNode);
}
}
}

public void removeLowest()
{
return;
}

public String toString()
{
String str;

Node p = head;
while (p != null)
{
str = (p.getName() + ":" + p.getScore());
}
return str;
}
}

Also here is the interface that I have to implement.

public interface GameLinkedListInterface
{
public int MAXSIZE = 10;
public boolean isEmpty();
public void removeLowest();
public String toString();

public void add(String newName, int newScore) throws LinkedListException;

}

To create a Node class for your linked list assignment, you need to define a class with the following attributes:

- A name field (String data type)
- A score field (int data type)
- A next field (Reference to the next Node object)

Here is an explanation of the code you have provided so far:

```java
import java.util.LinkedList;

public class Node {
private String name;
private int score;
private Node next;

// Default constructor
public Node() {
name = null;
score = 0;
next = null;
}

// Parameterized constructor with name and score
public Node(String nam, int sco) throws IllegalDataFieldException {
if (nam == null || sco < 0) {
throw new IllegalDataFieldException("No name entered or negative score posted");
} else {
name = nam;
score = sco;
}
next = null;
}

// Setter and Getter methods for name
public void setName(String nam) {
name = nam;
}

public String getName() {
return name;
}

// Setter and Getter methods for score
public void setScore(int sco) {
score = sco;
}

public int getScore() {
return score;
}

// Setter and Getter methods for next Node
public void setNext(Node n) {
next = n;
}

public Node getNext() {
return next;
}
}
```

Here's what each part of the code does:

- The `Node` class is declared with private attributes `name`, `score`, and `next`.
- The class has two constructors: the default constructor that initializes the attributes to their default values, and a parameterized constructor that takes `nam` (name) and `sco` (score) as arguments.
- The parameterized constructor also checks if the name is null or the score is less than 0. If either condition is true, it throws an `IllegalDataFieldException` with an appropriate error message. Otherwise, it sets the name and score fields.
- Setter and getter methods are provided for the name, score, and next attributes.
- The `IllegalDataFieldException` is not provided in the code you have shared, so you will need to implement it separately based on the instructions given in the assignment.

To use this `Node` class in your linked list, you can create instances of the `Node` class and link them together using the `next` field.