Question & Answer: The book is Data Structures and Algorithms 6th Edition……

Java Homework Help:

The book is Data Structures and Algorithms 6th Edition.

The Assignment deals with generics and has 1 interface named Bag, and classes named ArrayBag, LinkedBag, Player, SinglyLinkedList, and the main method class called Client.

Here are the instructions:

1. Develop a Generic interface named Bag that can store certain number of items (type will be specified by the client). Provide the following methods in the interface:

A method that returns the current count of items in the bag

A method that checks if the bag is empty

A method that checks if the bag is full

A method that adds an item to the bag and returns true if the item was added successfully or false if the item was not added.

A method that removes a random item from the bag as long as the bag is not empty. This method would return the removed item from the bag.

A method that removes a specific item from bag. This method will take as parameter the item to be removed, find the first occurrence of the item. Finally, the method returns true if the removal was successful, false otherwise.

A method that removes all the items from the bag

A method that returns the count of occurrences of a specific item in the bag.

A method that checks if an item exists in the bag.

A toString method that returns a String representation of the contents of the bag.

An equals method that returns true if the contents of the two bags are equal, false otherwise.

2.

Design a Generic class called ArrayBag that implements the Generic Bag Interface created earlier. This class will include a Type Parameter as part of class declaration. A client class using ArrayBag will specify the actual type.

Declare an instance variable list – an array of Generic type: This structure will hold the items in the bag.

Declare another instance variable count: This will provide the count of items currently stored in the bag. This count will increment as a new item is added to the bag and decrement as an item is removed from the bag.

Provide a default constructor that will initialize the instance variable bag to a new array of length 50.

Provide an overloaded constructor that allows the client to specify the initial capacity of the bag.

Implement the methods received from the interface.

The method that adds an item to the bag should check if the bag is full. When the bag is full it should automatically double the capacity of the bag and add the item.

The method that removes a specific item which is passed as a parameter should use the 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 that item from the bag and returns true, and returns false if there is no item with the same contents.

The methods that remove items (randomly or specified item) should automatically allow the items to shift to replace the removed item.

Implement the following additional method

A method that returns an item at a specific index position in the bag.

3.

Design a Generic class called LinkedBag that implements the Generic Bag Interface created earlier. This class will include a Type Parameter as part of class declaration. A client class using LinkedBag will specify the actual type.

Declare an instance variable list – a Singly Linked List of Generic type: This structure will hold the items in the bag.

Declare another instance variable count: This will provide the count of items currently stored in the bag. This count will increment as a new item is added to the bag and decrement as an item is removed from the bag.

Provide a default constructor that will initialize the instance variable bag with an empty Singly Linked list.

Implement the methods received from the interface.

The method that checks to see if the bag is full should always return false.

The method that removes a specific item which is passed as a parameter should use the 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 that item from the bag and returns true,

If there is no item with the same contents it returns false.

Implement the following additional method

A method that returns an item at a specific index position in the bag.

4. 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.

5.

Create a client class named Client with the main( ) method. Inside the main method do the following:

Create an object of ArrayBag called footballTeam to store all 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.

Run a for loop to prompt the user for each Player’s information, create the Player object and finally add the player to the team. Enter information for at least 6 players.

Remove a random player from the team.

Add a new Player with some made up information.

Display the current count of players in the team.

Remove the Player that you just added earlier with made up information from the team using appropriate method.

Display the current count of players in the team.

To demonstrate that your generic class can support objects of different types:

Create an object of ArrayBag called courses to store the course ids of the courses that you are taking this semester (CSci 161, …..) as Strings.

Populate the bag with the course ids.

Remove a random course id from the Bag.

Use a for loop to print the course ids from the bag.

Create an object of LinkedBag called basketballTeam to store all the players’s information of NDSU’s Women’s basketball team.

Repeat steps 1 through 6 above for the basketballTeam that uses LinkedBag.

My main help needed is in the client class

Here’s my code for the Bag Interface

/**
*
* @author Robert.Ryden
* @version September 7, 2017
* Interface Bag that defines the methods for the array
*/

public interface Bag<T> {

/**
* method that returns a count of numbers in the bag
*
* @return
*/

public int getCurrentSize();

/**
* checks if bag is empty, returns true when empty
*
* @return
*/

public boolean isEmpty();

/**
* adds a new number num to the bag
*
* @param num
* @return
*/

public boolean add(T num);

/**
* checks if bag is full
* @param num
* @return
*/

public boolean isFull();

/**
* removes the first occurrence of the number num from the bag
*
* @param num
* @return
*/

public boolean removeI(T num);

/**
* removes a randomly selected entry from the bag
*
* @return
*/

public T remove();

/**
* removes all the numbers from the bag
*/

public void clear();

/**
* returns a count the number of times the number num exists in the bag.
*
* @param num
* @return
*/

public int getFrequencyOf(T num);

/**
* Tests whether the bag contains the number num. Returns true when the num
* is contained in the bag.
*
* @param num
* @return
*/

public boolean contains(T num);

/**
* Returns a String of the contents of the bag.
*
* @return
*/

public String toString();

/**
* returns true if the parameter o exactly matches the contents of the bag.
* (i.e. same numbers in the same order)
*
* @param o
* @return
*/

public boolean equals(Object o);

}

ArrayBag Class

import java.util.Random;

/**
*
* @author robert.ryden
* @version September 7, 2017
* Scores class that implements the methods from the interface
*/

public class ArrayBag<T> implements Bag<T> {

private T[] list;// structure that holds the numbers in the bag

private int count;// provides the count of numbers currently stored in the bag.

// default constructor

public ArrayBag() {

list = (T[]) new Object[50]; // initializes array length to 50.

}

// overloaded constructor

/**
* takes int value as a parameter initialize new array length
*
* @param capacity
*/

public ArrayBag(int capacity) {

list = (T[]) new Object[capacity];

}

/**
* Implements getCurrentSize Method
*
* @return
*/

@Override

public int getCurrentSize() {

return count;

}

/**
* Implements isEmpty method
*
* @return
*/

@Override
public boolean isEmpty() {
return count == 0;
}

/**
* method used to determine if array is full
*
* @return
*/

/**
* Implementing the clear method
*/

public void clear()
{
count = 0;

}

public boolean isFull() {

return list.length == count;

}

/**
* Method adds value to the end of the list.
* only adds if the array isn’t full
* @param item
* @return
*/

public boolean add(T num) {

if (!isFull())
{
list[count++] = num;

}
else
{
temp(list);

list[count++] = num;
}
return true;
}

/**
* Method that doubles capacity of array in case array is full.
*
* @param n
*/

private void temp(T[] n) {

list = (T[]) new Object[list.length * 2];

for (int i = 0; i < n.length; i++) {

list[i] = n[i];
}
}

/**
* Implementing frequency method from the interface
*
* @param item
* @return
*/

public int getFrequencyOf(T item) {

int freq = 0;
for (int y = 0; y < count; y++)
{
if (list[y] != null && list[y].equals(item))
{
freq++;
}
}
return freq;
}

/**
* Implementing contains method from the interface
*
* @param item
* @return
*/

public boolean contains(T num)
{
for (int z = 0; z < count; z++)
{
if (list[z] == num)
{
return true;
}
}
return false;
}

/**
* Implementing the removeI(int num) method
*
* @param item
* @return
*/

public boolean removeI(T num) {

for (int i = 0; i < count; i++) {
if (num.equals(list[i]))
{
removeHelper(i);
return true;
}
}
return false;
}

/**
* Implementing remove method
*
* @return
*/

public T remove() {

Random rand = new Random();

int rd_index = rand.nextInt(count);

return removeHelper(rd_index);

}

private T removeHelper(int index) {

T toReturn = null;

T[] bag1 = (T[]) new Object[list.length];

int i = 0;
boolean flag = false;
while (i < list.length – 1)
{
if (i == index & !flag)
{
toReturn = list[i];
flag = true;
i++;
continue;
}

bag1[i] = list[i];
i++;
}
count–;
list = bag1;
return toReturn;

}

/**
* toString method to display the contents of the array
*
* @return
*/

@Override

public String toString() {

String ContentList = “List: “;

for (int i = 0; i < count; i++)
{
ContentList += list[i] + ” “;
}
return ContentList;
}

/**
* Implementing equals method from the interface
*
* @param o
* @return
*/

@Override

public boolean equals(Object o) {

if (o == null || getClass() != o.getClass()) {

return false;

}

final ArrayBag<T> bag = (ArrayBag<T>) o;

if (bag.count != this.count) {

return false;
}

for (int i = 0; i < count; i++)
{
if (bag.list[i] != this.list[i])
{
return false;
}
}
return true;
}

/**
* new method that returns the number at the ith position of the list if
* index is out of bounds, it generates an ArrayIndexOutOfBoundsException
*
* @param i
* @return
*/

public T get(int i) {

if (i >= count) {
return null;
}
return list[i];
}

/**
* coincides with temp method for increasing the capacity.
*
* @return
*/

public int capacity() {
return list.length;
}

/**
* copy method that copies the contents from list array to temp array.
* copies in the same order.
*
* @return
*/

public T[] copy() {

return list;

}

}

LinkedBag Class

/**
*
* @author robert.ryden
*/
public class LinkedBag<T> implements Bag<T> {

private SinglyLinkedList<T> list;//instance variable of generic type
private int count;// instance variable of generic type

public LinkedBag() {
list = new SinglyLinkedList<T>();
count = 0;
}

@Override
public int getCurrentSize() {
return count;
}

@Override
public boolean isFull() {
return list.size() == count;
}

@Override
public boolean isEmpty() {
return count == 0;
}

@Override
public boolean add(T num) {
list.addLast(num);
count++;
return true;
}

@Override
public boolean removeI(T num) {
return true;
}

@Override
public T remove() {
return list.removeFirst();
}

@Override
public void clear() {
list.clear();
}

@Override
public int getFrequencyOf(T num) {
return list.frequency(num);
}

@Override
public boolean contains(T num) {
return list.contains(num);
}
}
Player Class

/**
*
* @author Robert
*/
public class Player {

private static int playerCount = 0;

private String name; //Declaring player contact info
private String position;
private String jersey;

public Player(String name, String position, String jersey ) //Storing the players in array
{
this.name = name;
this.position = position;
this.jersey = jersey;
playerCount++;

}
public static int getPlayerCount()
{return playerCount;}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPosition() {
return position;
}

public void setPosition(String position) {
this.position = position;
}

public String getJersey() {
return jersey;
}

public void setJersey(String jersey) {
this.jersey = jersey;
}

public String toString() //toString Method
{
return getClass().getName() + “@” +
+ playerCount + “:”
+ name + “:”
+ position + “:”
+ jersey;

}

public boolean equals ( Object o) //equals method
{
if (!( o instanceof Player))
return false;

Player p = ( Player ) o;

return name.equalsIgnoreCase( p.name ) //testing equals method
&& position.equalsIgnoreCase( p.position )
&& jersey.equalsIgnoreCase( p.jersey );
}
}

SinglyLinkedList Class

/**
*
* @author robert.ryden
*/
public class SinglyLinkedList<E> {

// instance variables of the SinglyLinkedList
private Node<E> head = null; // head node of the list (or null if empty)
private Node<E> tail = null; // last node of the list (or null if empty)
private int size = 0; // number of nodes in the list

public SinglyLinkedList() {
} // constructs an initially empty list

// access methods
public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

public E first() { // returns (but does not remove) the first element
if (isEmpty()) {
return null;
}
return head.getElement();
}

public E last() { // returns (but does not remove) the last element
if (isEmpty()) {
return null;
}
return tail.getElement();
} // update methods

public void addFirst(E e) { // adds element e to the front of the list
head = new Node<>(e, head); // create and link a new node
if (size == 0) {
tail = head; // special case: new node becomes tail also
}
size++;
}

public void addLast(E e) { // adds element e to the end of the list
Node<E> newest = new Node<>(e, null); // node will eventually be the tail
if (isEmpty()) {
head = newest; // special case: previously empty list
} else {
tail.setNext(newest); // new node after existing tail
}
tail = newest; // new node becomes the tail
size++;
}

public E removeFirst() { // removes and returns the first element
if (isEmpty()) {
return null; // nothing to remove
}
E answer = head.getElement();
head = head.getNext(); // will become null if list had only one node
size–;
if (size == 0) {
tail = null; // special case as list is now empty
}
return answer;
}

public void clear() {
size = 0;
head = null;
tail = null;
}

public boolean contains(E element) {
Node<E> temp = head;
while (temp != null) {
if (temp.element.equals(element)) {
return true;
}
}
return false;
}

public int frequency(E elem) {
int count = 0;
Node<E> temp = head;
while (temp != null) {
if (temp.element.equals(elem)) {
count++;
}
}
return count;
}

public static class Node<E> {

private E element; // reference to the element stored at this node
private Node<E> next; // reference to the subsequent node in the list

public Node(E e, Node<E> n) {
element = e;
next = n;
}

public E getElement() {
return element;
}

public Node<E> getNext() {
return next;
}

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

 

Expert Answer

——————————————-
import java.util.Scanner;

public class Client {

//Doesn’t need a public constructor.
private Client() {
}

public static void main(String[] args) {
//Scanner object for input.
Scanner scan = new Scanner(System.in);

ArrayBag<Player> team = new ArrayBag<>();

//Prompt user to enter each Player’s information.
System.out.println(“Please enter team info one player at a time: “);
for(int i=0; i<team.capacity(); i++){
//Temp information holders.
String[] info = new String[2];
int jersey = 0;

//Prompt user for info.
System.out.print(“Please enter the player’s name: “);
info[0] = scan.nextLine();

System.out.print(“Please enter the player’s position: “);
info[1] = scan.nextLine();

System.out.print(“Please enter the player’s jersey number: “);
while(!scan.hasNextInt()){ //Ensure input is an integer.
scan.nextLine();
System.out.print(“Please enter an integer: “);
}
jersey = scan.nextInt();

//Clears buffer
scan.nextLine();

//Add Player to team with collected info.
team.add(new Player(info[0], info[1], jersey));
System.out.println(“Added ” + info[0] + ” to the team.”);

//Break loop if all Players have been added.
System.out.print(“Do you have additional players to enter? (y/n): “);
if(!scan.nextLine().equalsIgnoreCase(“y”)){break;};
}
System.out.println(“All players added.”);
System.out.println(“”);

//Remove a random Player from the Team.
if(!team.isEmpty()){
System.out.println(“Randomly removing the following Player: ” + team.remove().getName());
} else {
System.out.println(“No players to remove!”);
}

//Add a player with made up information.
Player madeUp = new Player(“Jake Huesman”, “Running back”, 1);
team.add(madeUp);
System.out.println(“Added the following player to the team: ” + team.get(team.getCurrentSize()-1).getName());

//Display the current count of players in the team.
System.out.println(“The current count of players on the team is: ” + team.getCurrentSize());

//Remove Player added earlier with made up info.
System.out.println(“Removing the following player from the team: ” + madeUp.getName());
team.remove(madeUp);

//Display the current count of players in the team again.
System.out.println(“The current count of players on the team is: ” + team.getCurrentSize());

//Use a for loop to print the information of the Players in the team.
System.out.println(“The current Players on the team are: “);
for(int i=0; i<team.getCurrentSize(); i++){
System.out.format(“%-2s%30s%30s”, (i+1) + “) Name: ” + team.get(i).getName(), “| Postion: ” + team.get(i).getPosition(), “| Jersey Number: ” + team.get(i).getJerseyNumber() + “n”);
}

//
//
//        //LinkedBag to hold the Player objects.
//        LinkedBag<Player> team = new LinkedBag<>();
//
//        //Prompt user to enter each Player’s information.
//        System.out.println(“Please enter team info one player at a time: “);
//        while (true) {
//            //Temp information holders.
//            String[] info = new String[2];
//            int jersey = 0;
//
//            //Prompt user for info.
//            System.out.print(“Please enter the player’s name: “);
//            info[0] = scan.nextLine();
//
//            System.out.print(“Please enter the player’s position: “);
//            info[1] = scan.nextLine();
//
//            System.out.print(“Please enter the player’s jersey number: “);
//            while (!scan.hasNextInt()) { //Ensure input is an integer.
//                scan.nextLine();
//                System.out.print(“Please enter an integer: “);
//            }
//            jersey = scan.nextInt();
//
//            //Clears buffer
//            scan.nextLine();
//
//            //Add Player to team with collected info.
//            team.add(new Player(info[0], info[1], jersey));
//            System.out.println(“Added ” + info[0] + ” to the team.”);
//
//            //Break loop if all Players have been added.
//            System.out.print(“Do you have additional players to enter? (y/n): “);
//            if (!scan.nextLine().equalsIgnoreCase(“y”)) {
//                break;
//            }
//            ;
//        }
//        System.out.println(“All players added.”);
//        System.out.println(“”);
//
//        //Remove a first Player from the Team.
//        if (!team.isEmpty()) {
//            System.out.println(“Removing the first Player: ” + team.remove().getName());
//        } else {
//            System.out.println(“No players to remove!”);
//        }
//
//        //Add a player with made up information.
//        Player madeUp = new Player(“Jake Huesman”, “Center”, 1);
//        team.add(madeUp);
//        System.out.println(“Added the following player to the team: ” + team.getItem(0).getName());
//
//        //Display the current count of players in the team.
//        System.out.println(“The current count of players on the team is: ” + team.getCurrentSize());
//
//        //Remove Player added earlier with made up info.
//        System.out.println(“Removing the following player from the team: ” + madeUp.getName());
//        team.remove(madeUp);
//
//        //Display the current count of players in the team again.
//        System.out.println(“The current count of players on the team is: ” + team.getCurrentSize());
//
//        //Use a for loop to print the information of the Players in the team.
//        System.out.println(“The current Players on the team are: “);
//        for (int i = 0; i < team.getCurrentSize(); i++) {
//            System.out.format(“| %1$-30s | %2$-20s | %3$-20sn”, (i + 1) + “) Name: ” + team.getItem(i).getName(), “Postion: ” + team.getItem(i).getPosition(), “Jersey Number: ” + team.getItem(i).getJerseyNumber());

//Populate the bag with the course ids.

ArrayBag<String> courses = new ArrayBag<>();
System.out.println(“nPopulating courses with course ids.”);
courses.add(“Math 265”);
courses.add(“ECE 111”);
courses.add(“CSci 161”);
courses.add(“Math 129”);

//Remove a random course id from the bag.
System.out.println(“Randomly removing ” + courses.remove() + ” from the courses”);

//Print the course ids from the bag.
System.out.println(“Course ids in bag:”);
for(int j=0; j<courses.getCurrentSize(); j++){
System.out.println((j+1) + “) ” + courses.get(j));

}

}
}
——————————————————————————————————–
LinkedBag.java
——————————————————-
public class LinkedBag<T> implements Bag<T> {
/**
* The objects of this class form the nodes of the singly linked list.
*/
private static class Node<T> {
private final T element;
private Node<T> next;
public Node(T e, Node<T> n){
element = e;
next = n;
}
public T getElement(){ return element; }
public Node<T> getNext() { return next; }
public void setNext( Node<T> n ){ next = n; }
}

private Node<T> bagHead;
private Node<T> bagTail;
int count;

public LinkedBag(){
bagHead = null;
bagTail = null;
count = 0;
}

/**
* Returns a count of the items in the bag.
*/
@Override
public int getCurrentSize() {
return count;
}

/**
* Checks if the bag is empty.
*/
@Override
public boolean isEmpty() {
return count <= 0;
}

/**
* The bag will never be full.
* @return false – always
*/
@Override
public boolean isFull() {
return false;
}

/**
* Adds an element to the beginning of the list.
*/
@Override
public boolean add(T item) {
bagHead = new Node(item, bagHead);
count++;
return true;
}

/**
* Removes and returns the first node in the list.
*/
@Override
public T remove() {
if(bagHead == null){
return null;
}
T element = bagHead.getElement();
bagHead = bagHead.getNext();
count–;
return element;
}

/**
* Removes a given item and returns true or false according to the success of the operation.
*/
@Override
public boolean remove(T item) {
//Priming
Node<T> previous;
Node<T> current;
if(bagHead != null && bagHead.getElement().equals(item)){
bagHead = bagHead.getNext();
count–;
return true;
}
if(bagHead != null && bagHead.next != null){
previous = bagHead;
current = bagHead.next;
} else {
return false;
}
//Loop through SLL, looking for item.
while(current.next != null){
if(current.getElement().equals(item)){
previous.setNext(current.getNext());
current.setNext(null);
count–;
return true;
}
previous = current;
current = previous.getNext();
}
//Return false if the item was not found.
return false;
}

/**
* Clears the current bag.
*/
@Override
public void clear() {
bagHead = null;
bagTail = null;
count = 0;
}

/**
* Checks for the frequency of a particular item.
*/
@Override
public int getFrequencyOf(T item) {
//primer
Node<T> previous;
Node<T> current;
int freq = 0;
if(bagHead != null && bagHead.getElement().equals(item)){
freq++;
}
if(bagHead != null && bagHead.next != null){
previous = bagHead;
current = bagHead.next;
} else {
return freq;
}
//Loop through SLL and count the frequency of the item.
do {
if(current.getElement().equals(item)){
previous.setNext(current.getNext());
current = current.getNext();
freq++;
}
previous = current;
current = previous.getNext();
} while(current.next != null);
//Return false if the item was not found.
return freq;
}

/**
* Checks to see if the LinkedBag contains the item.
*/
@Override
public boolean contains(T item) {
//primer
Node<T> previous;
Node<T> current;
if(bagHead != null && bagHead.getElement().equals(item)){
return true;
}
if(bagHead != null && bagHead.next != null){
current = bagHead.next;
} else {
return false;
}
//Loop through SLL and count the frequency of the item.
do {
if(current.getElement().equals(item)){
return true;
}
previous = current;
current = previous.getNext();
} while(current.next != null);
//Return false if the item was not found.
return false;
}

/**
* Returns an item at a specific index position in the bag.
*/
public T getItem(int i){
//primer
Node<T> current;
int step = 0;
if (bagHead!= null && step == i){
return bagHead.getElement();
} else if(bagHead != null && bagHead.getNext() != null){
current = bagHead;
} else {
return null;
}
//Loop through SLL until the specified index is hit.
do {
step++;
if(step == i){
return current.getElement();
}
current = current.getNext();
} while(current.next != null);
return null;
}

/**
* Returns an array containing the copy of the items in the list.
*/
public T[] toArray(){
T[] array = (T[]) new Object[count];
Node<T> current = bagHead;

for(int i=0; i<count; i++){
array[i] = current.getElement();
current = current.getNext();
}
return array;
}
}
———————————————————————————————-
ArrayBag.java
——————————————
import java.util.Random;

/**
* Generic class that implements the generic Bag interface.
*/
public class ArrayBag<T> implements Bag<T> {

private T[] bag;
private int count;

/**
* Default constructor initiates bag to a capacity of 50.
*/
public ArrayBag(){
bag = (T[]) new Object[50];
}

/**
* Overloaded constructor initiates bag to capacity specified.
*/
public ArrayBag(int size){
bag = (T[]) new Object[size];
}

/**
* Returns a count of the items in the bag.
*/
@Override
public int getCurrentSize() {
return count;
}

/**
* Checks if the bag is empty.
*/
@Override
public boolean isEmpty() {
if(count <= 0){
return true;
}
return false;
}

/**
* Checks if the bag is full.
*/
@Override
public boolean isFull() {
if(count >= bag.length){
return true;
}
return false;
}

/**
* Adds a new item to the bag.
*/
@Override
public boolean add(T item) {
try{
if(bag.length <= count){
T[] temp = (T[]) new Object[bag.length*2];
for(int i=0; i<bag.length; i++){
temp[i] = bag[i];
}
bag = temp;
temp = null;
bag[count++] = item;
} else {
bag[count++] = item;
}
return true;
} catch (Exception e){
return false;
}
}

/**
* Removes the first occurrence of the item from the bag.
*/
@Override
public boolean remove(T item) {
for(int i=0; i<count; i++){
if(bag[i].equals(item)){
for(int a=i; a<count; a++){
if(bag.length == (a+1)){
bag[a] = null;
} else {
bag[a] = bag[a+1];
}
}
count–;
return true;
}
}
return false;
}

/**
* Removes randomly an item from the bag as long as the bag is not empty.
*/
@Override
public T remove() {
Random rand = new Random();
if(count > 0){
int i = rand.nextInt(count);
T item = bag[i];
for(int a=i; a<count; a++){
if(bag.length == (a+1)){
bag[a] = null;
} else {
bag[a] = bag[a+1];
}
}
count–;
return item;
}
return null;
}

/**
* Creates an empty bag and replaces the old one.
*/
@Override
public void clear() {
bag = (T[]) new Object[50];
}

/**
* Counts the number of times the item exists in the bag.
*/
@Override
public int getFrequencyOf(T item) {
int freq = 0;
for(int i=0; i<count; i++){
if(bag[i].equals(item)){
freq++;
}
}
return freq;
}

/**
* Tests whether the bag contains the item.
*/
@Override
public boolean contains(T item) {
if(this.getFrequencyOf(item) > 0){
return true;
}
return false;
}

/**
* Finds the item at index i and returns it.
*/
public T get(int i) throws ArrayIndexOutOfBoundsException {
if(i >= bag.length){
throw new ArrayIndexOutOfBoundsException();
} else {
return bag[i];
}
}

/**
* Returns the current capacity of the bag.
*/
public int capacity(){
return bag.length;
}

/**
* Returns a copy of the bag array instance variable.

*/
public ArrayBag copy(ArrayBag bag){
ArrayBag temp = new ArrayBag(bag.getCurrentSize());
for(int i=0; i<temp.getCurrentSize(); i++){
temp.add(bag.get(i));
}
return temp;
}

}

———————————————————————————————————–
Bag.java
—————————————-
/**
* A generic Bag interface.
*/
public interface Bag<T> {
/**
* Returns a count of the items in the bag.
*/
public int getCurrentSize();

/**
* Checks if the bag is empty.
*/
public boolean isEmpty();

/**
* Checks if the bag is full.
*/
public boolean isFull();

/**
* Adds a new item to the bag.
*/
public boolean add(T item);

/**
* Removes the first occurrence of the item from the bag.
*/
public boolean remove(T item);

/**
* Removes randomly an item from the bag as long as the bag is not empty.
*/
public T remove();

/**
* Creates an empty bag and replaces the old one.
*/
public void clear();

/**
* Counts the number of times the item exists in the bag.
*/
public int getFrequencyOf(T item);

/**
* Tests whether the bag contains the item.
*/
public boolean contains(T item);
}
——————————————————————————-
Player.java
—————————————-
public class Player {
/**
* Declare instance variables.
*/
private String name, position;
private int jerseyNumber;

/**
* Construct a new Player instance.
*/
public Player(String name, String position, int jerseyNumber){
this.name = name;
this.position = position;
this.jerseyNumber = jerseyNumber;
}

/**
* Returns the name of the player.
*/
public String getName(){
return name;
}

/**
* Returns position of the player.
*/
public String getPosition(){
return position;
}

/**
* Returns the jersey number of the player.
*/
public int getJerseyNumber(){
return jerseyNumber;
}

/**
* Changes the name of the player.
*/
public void setName(String name){
this.name = name;
}

/**
* Changes the position of the player.
*/
public void setPosition(String position){
this.position = position;
}

/**
* Changes the jerseyNumber of the player.
*/
public void setJerseyNumber(int jerseyNumber){
this.jerseyNumber = jerseyNumber;
}

/**
* Checks to see if the two Player objects are equal.
*/
public boolean equals(Player player2){
return !(this.getJerseyNumber() != player2.getJerseyNumber() || (this.getName() == null ? player2.getName() != null : !this.getName().equals(player2.getName())) || (this.getPosition() == null ? player2.getPosition() != null : !this.getPosition().equals(player2.getPosition())));
}
}

Question & Answer: The book is Data Structures and Algorithms 6th Edition...... 1

Still stressed from student homework?
Get quality assistance from academic writers!