Question

n JAVA, students will create a linked list structure that will be used to support a...

n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as a whole or by category, request the number of items in inventory, and request to see values for specific inventory items. The categories for items are food, coins, archeology tools, artifacts, and documents. Each item should have an item number (unique integer value in priority order), category (string or integer), a description (string), and a value (integer). The program will use the linked list structure to allow the user to do the functions mentioned above; add (beginning/end), remove (from end), insert (based on ID number), delete (based on ID number), print inventory, print inventory by category, tell if the inventory is empty, tell how many items are in inventory, tell how many items in each category, and search for an item by name or ID and return its value. The code must validate the user input for valid choices and valid input type and throw exceptions that result in an appropriate message to the user. The code must also throw exceptions for trying to retrieve/print a node from an empty list.

Use the code below to finish the rest of the code.

/**
   The LinkedList class implements a Linked list that contains inventory
*/

class LinkedList
{
/**
   The Node class stores a list element
   and a reference to the next node.
*/

private class Node
{
int id; //Item Number
String Category; //category
String Description; // Description
int value;//value
Node next;

/**
   Constructor.
   @param val The element to store in the node.
   @param n The reference to the successor node.
*/

Node(int itnum, String Catg, String Desc, int val, Node n)
{
id = itnum;
Category = Catg;
Description = Desc;
value = val;
next = n;
}

/**
   Constructor.
   @param val The element to store in the node.
   */

Node(int itnum, String Catg, String Desc, int val)
{
   // Call the other (sister) constructor.
   id = itnum;
Category = Catg;
Description = Desc;
value = val;
}
}

private Node first; // list head
private Node last; // last element in list

/**
   Constructor.
*/

public LinkedList()
{
first = null;
last = null;
}

/**
   The isEmpty method checks to see
   if the list is empty.
   @return true if list is empty,
   false otherwise.
*/

public boolean isEmpty()
{
return first == null;
}

/**
   The size method returns the length of the list.
   @return The number of elements in the list.
*/

public int size()
{
   int count = 0;
   Node p = first;
   while (p != null)
   {
   // There is an element at p
   count ++;
   p = p.next;
   }
   return count;
}

/**
   The add method adds an element to
   the end of the list.
   @param e The value to add to the
   end of the list.
*/

public void add(int itnum, String Catg, String Desc, int val)
{
if (isEmpty())
{
first = new Node(itnum,Catg,Desc,val);
last = first;
}
else
{
// Add to end of existing list
last.next = new Node(itnum,Catg,Desc,val);
last = last.next;
}
}



/**
   The toString method computes the string
   representation of the list.
   @return The string form of the list.
*/

public String toString()
{
StringBuilder strBuilder = new StringBuilder();

// Use p to walk down the linked list
Node p = first;
while (p != null)
{
   strBuilder.append(p.id + "\t");
   strBuilder.append(p.Category + "\t");
   strBuilder.append(p.Description + "\t");
   strBuilder.append(p.value + "\n");
   p = p.next;
}
return strBuilder.toString();
}



// Main method to test the linkedlist
public static void main(String [] args)
{
LinkedList ll = new LinkedList();
ll.add(1,"food","Apple",2);
ll.add(2,"Coins","GoldCoins",2);
System.out.println("The members of the list are:");
System.out.print(ll);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**
The LinkedList class implements a Linked list that contains inventory
*/

class LinkedList
{
/**
The Node class stores a list element
and a reference to the next node.
*/

private class Node
{
int id; //Item Number
String Category; //category
String Description; // Description
int value;//value
Node next;

/**
Constructor.
@param val The element to store in the node.
@param n The reference to the successor node.
*/

Node(int itnum, String Catg, String Desc, int val, Node n)
{
id = itnum;
Category = Catg;
Description = Desc;
value = val;
next = n;
}

/**
Constructor.
@param val The element to store in the node.
*/

Node(int itnum, String Catg, String Desc, int val)
{
// Call the other (sister) constructor.
id = itnum;
Category = Catg;
Description = Desc;
value = val;
}
}

private Node first; // list head
private Node last; // last element in list

/**
Constructor.
*/

public LinkedList()
{
first = null;
last = null;
}

/**
The isEmpty method checks to see
if the list is empty.
@return true if list is empty,
false otherwise.
*/

public boolean isEmpty()
{
return first == null;
}

/**
The size method returns the length of the list.
@return The number of elements in the list.
*/

public int size()
{
int count = 0;
Node p = first;
while (p != null)
{
// There is an element at p
count ++;
p = p.next;
}
return count;
}

/**
The add method adds an element to
the end of the list or start of list
depends on addTostart value
@param e The value to add to the
end of the list.
*/

public void add(int itnum, String Catg, String Desc, int val, boolean addTostart )
{
if (isEmpty())
{
first = new Node(itnum,Catg,Desc,val);
last = first;
}
else
{
if(!addTostart){
// Add to end of existing list
last.next = new Node(itnum,Catg,Desc,val);
last = last.next;
}else{
// Add to start of existing list
   Node temp = new Node(itnum,Catg,Desc,val);
   temp.next = first;
   first = temp;
}
}
}
/**
The remove method removes an element from
the end of the list.
  
*/
public void remove(){
if (isEmpty())
{
   return;
}else{
   Node p = first;
   Node n = first.next; // to point to last but one node
   if(n != null){
       Node nn = n.next; //to point to last node
   while ( nn != null)
   {
           //System.out.print(nn.id);
       p = n;
       n = nn;
       nn = nn.next;
      
   }
   //nn is null ie n is last but one node
   //System.out.print(p.id);
   p.next = null;
}else{
       first = null;
}
}
}

/**
The delete method removes an element from
the list based on itnum.
  
*/
public void delete(int itnum){
if (isEmpty())
{
   return;
}else{
   Node p = first;
   Node n = first.next; // to point to last but one node
   boolean matchFound = false;
   if(first.id == itnum){
       first = first.next;
   }
   if(n != null){
       Node nn = n.next; //to point to last node
   while ( nn != null )
   {
       if(nn.id == itnum){
           matchFound = true;
           break;
       }
       p = n;
       n = nn;
       nn = nn.next;
      
   }
   if(matchFound)
   //nn is the node to be deleted ie n is last but one node
   p.next = n.next;
}else{
       if(first.id == itnum){
       first = null;
       }
}
}
}

/**
Print inventory by category
  
*/
public void printInventory(String Catg){
   Node p = first;
   StringBuilder strBuilder = new StringBuilder();
while (p != null )
{
       if(p.Category.equals(Catg)){
       strBuilder.append(p.id + "\t");
   strBuilder.append(p.Category + "\t");
   strBuilder.append(p.Description + "\t");
   strBuilder.append(p.value + "\n");
   }
   p = p.next;
     
}
System.out.print("printing inventory by category "+Catg+"\n"+strBuilder.toString());
}

/**
How many items in each category
*/

public int sizeByCategory(String catg)
{
int count = 0;
Node p = first;
while (p != null )
{
// There is an element at p
if(p.Category.equals(catg))
count ++;
p = p.next;
}
return count;
}

/**
The toString method computes the string
representation of the list.
@return The string form of the list.
*/

public String toString()
{
StringBuilder strBuilder = new StringBuilder();

// Use p to walk down the linked list
Node p = first;
while (p != null)
{
strBuilder.append(p.id + "\t");
strBuilder.append(p.Category + "\t");
strBuilder.append(p.Description + "\t");
strBuilder.append(p.value + "\n");
p = p.next;
}
return strBuilder.toString();
}

// Main method to test the linkedlist
public static void main(String [] args)
{
LinkedList ll = new LinkedList();
ll.add(1,"food","Apple",2,false);
ll.add(2,"Coins","GoldCoins",2,false);
ll.add(3,"Coins","SilverCoins",2,false);
ll.add(4,"food","Banana",2,false);
System.out.println("The members of the list are:");
System.out.print(ll);

int coinsCount = ll.sizeByCategory("Coins");
System.out.println("number of items in Coins category:"+coinsCount);
ll.printInventory("Coins");
ll.remove();
System.out.println("After remove The members of the list are:");
System.out.print(ll);
ll.delete(1);

System.out.println("After delete The members of the list are:");
System.out.print(ll);
}
}

Add a comment
Know the answer?
Add Answer to:
n JAVA, students will create a linked list structure that will be used to support a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT