Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 NDSUs Mens 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 Players 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 playerss information of NDSUs Womens 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 {

/** * 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 implements Bag {

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 bag = (ArrayBag) 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 implements Bag { private SinglyLinkedList list;//instance variable of generic type private int count;// instance variable of generic type

public LinkedBag() { list = new SinglyLinkedList(); 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 { // instance variables of the SinglyLinkedList private Node head = null; // head node of the list (or null if empty) private Node 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 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 temp = head; while (temp != null) { if (temp.element.equals(element)) { return true; } } return false; }

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

public static class Node {

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

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

public E getElement() { return element; }

public Node getNext() { return next; }

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Algorithmic Trading Navigating The Digital Frontier

Authors: Alex Thompson

1st Edition

B0CHXR6CXX, 979-8223284987

More Books

Students also viewed these Databases questions