Question: 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
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.
- E remove( ) method that removes a random entry from the bag. Returns the removed entry. Return null if bag is empty.
- E get(int i) that returns the element at the ith position in the bag.
- 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 but does not include the capacity( ) method.
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.
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 LinkedBag the client will specify the actual type.
- For the instance variable list use the SinglyLinkedList of Generic type from Task 1. This structure will hold the items in the bag.
- You should not declare an instance variable for size in this LinkedBag 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 instance variable bag 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 Clients perspective.
- Implement the methods specified in the interface.
- The method that removes a specific item which is passed as a parameter should use the objects 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 NDSUs Mens 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 NDSUs Womens 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 << put your name here
* @version 20220127 << put date you transcribed file here
*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
