Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please create a unique response and use the following code within the assignment. Please do not answer with the common answer i've seen floating around.
Please create a unique response and use the following code within the assignment. Please do not answer with the common answer i've seen floating around. The answer and code must imply this following material: (3.14 & 3.15 CODE MUST BE USED IN THE ANSWER)
The code for 3.14 and 3.15: (MUST USE)
3.14:
1 public class SinglyLinkedList{ 2 //---------------- nested Node class ---------------- 3 private static class Node { 4 private E element; // reference to the element stored at this node 5 private Node next; // reference to the subsequent node in the list 6 public Node(E e, Node n) { 7 element = e; 8 next = n; 9 } 10 public E getElement( ) { return element; } 11 public Node getNext( ) { return next; } 12 public void setNext(Node n) { next = n; } 13 } //----------- end of nested Node class -----------
3.15:
1 public class SinglyLinkedListRestrictions: Unless directed otherwise, you cannot use any Java class libraries in this assignment. In particular: - You cannot use the ArrayList class nor can you use any methods from the Arrays class. - You cannot use the SinglyLinkedList class defined by Java nor can you use any other collection class from the Java library. If needed: - You may create and use one or more instances of an array and access the length instance variable for each array. - You may use the Java Random class, the String class, Scanner class and the print methods. Task 0: Create a NetBeans project for the Lab103 assignment. Use the naming convention Lab103-LastFM Task 1: Duplicate the SinglyLinkedList class from the textbook (code fragments 3.14&3.15). Do not add any additional methods to this class. Do not modify any methods in this class. Add a comment header to this class (example shown in Task 7) that: - briefly describes what the class does - gives credit the textbook authors and lists the Code Fragments used - includes your name as the transcribing author Task 2: Create a generic version of a Bag interface. This Bag interface will be a Generic interface with a Generic TYpe Placeholder. Include the following methods in this Bag interface: - int size( ) - returns a count of items in the bag - boolean isEmpty( ) - checks if the bag is empty, returns true when empty - void clear( )-removes all the items from the bag - int getFrequencyof (E e) - returns a count the number of times an item, e, exists in the bag - boolean contains (E e) - Tests whether the bag contains the item e. Returns true when the e is contained in the bag. - void add (E e)-adds item, e, to the "next available" position in the bag. - E remove (E e) - removes the first occurrence of the item e in the bag. Returns null if e is not in the bag or if called on a empty bag. - E remove ( ) method that removes a random entry from the bag. Returns the removed entry. Returns null if bag is empty. - Eget(int i) that returns the element at the ith position in the bag. Throws an index out of bounds entry if passed an invalid index. - string tostring ( ) - returns a String of the contents of the bag. - boolean equals(Object o) - returns a true if the parameter o exactly matches the contents of the bag (i.e. same elements in the same order) Note that the above methods are similar to the methods that you implemented in the Lab102 assignment. Task 3 (very similar to Lab102 but generic): - Copy the Scores class from your Lab102 assignment into your Lab103 assignment. - Rename this copy of the Scores class to ArrayBag - Modify the ArrayBag class so that it: - implements the Bag interface created in Task 2 - is a generic class. - Make appropriate adjustments to the methods so that they match the interface. Import Note: - You cannot directly create an array of a generic type. - To crate an array of a generic type you must - Create an array of Object type - Cast the Object array to the generic type, e.g. E[ list = (E[]) new Object[ capacity ]; Task 4: Design a new Generic class called LinkedBag that implements the Generic Bag Interface created earlier. This class will include a generic type Parameter as part of class declaration. When a client class uses the IinkedBag the client will specify the actual type. - For the instance variable Iist use the SinglyLinkedList of Generic type from Task 1. This structure will hold the items placed in the bag. - You should not declare an instance variable for size in this L inkedBag class. - When you need to know how many items are in the list you can call the size () method from the SinglyLinkedList class. - Let the SinglyLinkedList class keep track of the number of items in the LinkedBag, i.e. let the data structure do the work. - Adding a separate size variable to the LinkedBag class will require you to do unnecessary work to maintain the value of the size variable, and if you incorrectly maintain a size variable in the LinkedBag class you may run into the situation where the two sizes variables are not equal. - Provide a default constructor that will initialize the list instance variable with an empty Singly Linked list. - Provide and overload constructor that allows the client to specify the initial capacity of the bag. - This constructor can ignore the parameter and just call the default constructor. - This constructor allows the LinkedBag to provide the Client with the same functionality as the ArrayBag. - Your Client should not care if it uses an ArrayBag or a LinkedBag, they should work exactly the same from the Client's perspective. - Implement the methods specified in the interface. - The method that removes a specific item which is passed as a parameter should use the object's equals ( ) method to compare the contents of object in the bag with the contents of the parameter. If there is an object in the bag with the same contents then, it removes the first occurrence of that item from the bag and returns a reference to the object removed. It returns a null of there is no item with equal contents. - The method that removes a random item should return a reference to the object being removed. If the list is empty it should return null. TASK 5: Create a user-defined class called Player. Each Player object will have the following attributes (instance variables): name, position played, and jersey number. Use appropriate data types to define these instance variables and use recommended naming conventions for the variable names. Provide a constructor, implement the accessor and mutator methods for each of the instance variables, and include the toString ( ) and equals( ) methods. TASK 6: Create a client class named Client with the main method. Inside the main method do the following: - Create an instance of ArrayBag called mensTeam to store players' information of NDSU's Men's football team using the overload constructor to make the initial length of the list array equal to 2 . - Add eight players to mensTeam by hardcoding eight calls to the add method (one for each player). - Display the contents of mensTeam. - Remove a random player from mensTeam. - Display the contents of mensTeam. - Get but do not remove a reference to the 5th item in the bag. - Display the contents of the reference you "got" it step 5. - Add another Player with another hardcoded add method call. - Display the contents of mensTeam. - Remove the Player that you "got" in step 5 using a call to the remove (E e) method. - Display the contents of mensTeam. - To demonstrate that your generic class can support objects of different types: - Create an instance of an ArrayBag called courses to store the course ids of the courses that you are taking this semester (CSci 161,..)as Strings. - Populate courses with each of your courses. - Display the contents of courses. - Remove a random course id from courses. - Display the contents of courses. - Create an instance of LinkedBag called womensTeam to store the players' information of NDSU's Women's basketball team. - Repeat steps 1 through 9 above for the womensTeam that uses an instance of the LinkedBag class. TASK 7: Make sure that each of your classes is correctly documented. - The interface should have a Javadocs header - Each class should have a Javadocs header - Each method should have a Javadocs header - For the SinglyLinkedList class - the documentation (headers and inline comments) should match what is in the textbook. - You may add additional documentation for your own benefit if you wish. - You must add a Javadocs header to the class that gives credit to the authors of the code. - See example on next page. / * SinglyLinkedList Class * Code Fragments 3.14, 3.15 * from * Data Structures \& Algorithms, 6th edition * by Michael T. Goodrich, Roberto Tamassia \& Michael H. Goldwasser * Wiley 2014 * Transcribed by *@author joseph.latimer{ (nested Node class goes here) 14 // instance variables of the SinglyLinkedList 15 private Node head = null; // head node of the list (or null if empty) 16 private Node tail = null; // last node of the list (or null if empty) 17 private int size = 0; // number of nodes in the list 18 public SinglyLinkedList( ) { } // constructs an initially empty list 19 // access methods 20 public int size( ) { return size; } 21 public boolean isEmpty( ) { return size == 0; } 22 public E first( ) { // returns (but does not remove) the first element 23 if (isEmpty( )) return null; 24 return head.getElement( ); 25 } 26 public E last( ) { // returns (but does not remove) the last element 27 if (isEmpty( )) return null; 28 return tail.getElement( ); 29 } 30 // update methods 31 public void addFirst(E e) { // adds element e to the front of the list 32 head = new Node(e, head); // create and link a new node 33 if (size == 0) 34 tail = head; // special case: new node becomes tail also 35 size++; 36 } 37 public void addLast(E e) { // adds element e to the end of the list 38 Node newest = new Node(e, null); // node will eventually be the tail 39 if (isEmpty( )) 40 head = newest; // special case: previously empty list 41 else 42 tail.setNext(newest); // new node after existing tail 43 tail = newest; // new node becomes the tail 44 size++; 45 } 46 public E removeFirst( ) { // removes and returns the first element 47 if (isEmpty( )) return null; // nothing to remove 48 E answer = head.getElement( ); 49 head = head.getNext( ); // will become null if list had only one node 50 size; 51 if (size == 0) 52 tail = null; // special case as list is now empty 53 return answer; 54 } 55 } THE ANSWER MUST CONTAIN THIS FOLLOWING CODE^^^^^^^^ Again please follow these steps and use the code given. The same code being pasted everywhere will receive a downvote. Thank you
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started