Question
Java Homework Help: The book is Data Structures and Algorithms 6th Edition. The Assignment is an array that randomly populates numbers from -100 to 100
Java Homework Help:
The book is Data Structures and Algorithms 6th Edition.
The Assignment is an array that randomly populates numbers from -100 to 100 into the array.
It requires 1 interface named Bag, one class called Scores, and a main method class called Client.
Here is my code for the 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(int num); /** * removes the first occurrence of the number num from the bag * @param num * @return */ public boolean remove(int 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(int 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(int 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); }
Here is my code for the Scores class:
import java.util.Random;
/** * * @author robert.ryden */ public abstract class Scores
/** * */ public Scores() { 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 Scores(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 */ public boolean isFull() { return list.length == count; }
/** *Method adds value to the end of the list. * @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 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; continue; } bag1[i] = list[i]; i++; } count--; list = bag1; return toReturn; }
/** * Implementing the remove(int num) method * @param item * @return */ public boolean remove(T num) { for (int i = 0; i < count; i++) { if (num.equals(list[i])) { removeHelper(i); return true; } } return false; }
/** * Implementing the clear method */ public void clear() { list = (T[]) new Object[list.length]; }
/** * Implementing frequency method from the interface * @param item * @return */ public int getFrequencyOf(T item) { int freq = 0; for (int y = 0; y < list.length; y++) { if (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 < list.length; z++) { if (list[z] == num) { return true; } } return false; }
/** * 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 Scores
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 >= list.length) { 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; } }
I'm struggling with the Main Method Client Class
The requirements are as follows:
Create an Object of Type Scores using the overloaded constructor and pass the value 100.
Use a for loop to populate the list in Scores object with 100 random numbers between -100 and +100 inclusive. (Use the Random class from java.util package to generate pseudorandom numbers).
Call toString( ) to print the contents of the Scores object.
Call the add( ) method to add the number 86 to the Bag
Print the current size of the list in the Scores object.
Call the remove( ) method to randomly remove a number from the Bag
Get the number at the 75th index position
Print the frequency that the number returned by the previous step occurs in the Bag
Call the appropriate overloaded remove method to remove the first occurrence of number at the 75th index position from the Bag
Print the frequency that this number now occurs in the Bag
Print the frequency of the number 86
Check whether the list array in Scores object contains the number 86.
Thanks for the help!
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