Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Restrictions: Unless directed otherwise, you cannot use any Java class libraries in this assignment. In particular, you cannot use the ArrayList class nor can you

Restrictions:

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.

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 NOT access any other methods from the Arrays class.
  • you may use the Java Random class.
  • Of course, you may use the System.print, println, and printf methods.

In this project you will be doing the following:

Create a NetBeans project using the naming convention Lab102-LastFM

Create a class named Scores that has the following properties:

  • an instance variable list an array of int type: This structure will hold the numbers.
  • an instance variable count of int type: This will provide the count of numbers currently stored in Scores class. This count will increment when a new number is added to the list and decrement when a number is removed from the list
  • Provide a default constructor that will initialize the instance variable list to a new array of length 2.
  • Provide an overloaded constructor that will take an int value as parameter and initialize list to a new array of that length.

Implement the following methods:

  • size( ) method that returns a count of numbers in Scores class
  • capacity( ) method that returns the length of the array in Scores class
  • isEmpty( ) method that checks if Scores class is empty, returns true when empty
  • clear( ) method that removes all the numbers from Scores class
  • getFrequencyOf(int num) method that returns a count the number of times the number num exists in Scores class
  • contains(int num) method that Tests whether Scores class contains the number num. Returns true when the num is contained in Scores class.
  • add (int num) method that always add to the next available slot in the array. This method should be able to add a new number to the end of the list ONLY IF the array is not full.
    • If the array list is full (when the count equals the length of the array) then, create a new bigger array - temp with double the length of list array.
    • Copy the contents from list to temp array in the same order.
    • Assign the reference of temp to list and set temp to null.
    • Add the new number to the end of the list.
  • remove(int num) method that removes the first occurrence of the number num in the list array.
    • If the number num does not exist then the method does not do anything.
    • If removal is successful and the number removed is not the last number in the list, then shift the elements to the right of the number being removed by one place to the left in the list (i.e. fill in the hole). In this structure, we never want any gaps in the array.
  • remove( ) method that removes a random number from the list array,
    • If Scores class is empty,
    • the method should not do anything.
    • Use the Random class from java.util package to generate pseudorandom index. This index should be based on the number of items in list, not the length of the list array (i.e. we dont want to try and remove from an empty cell).
    • After the number is removed shift the elements by one place to the left in the list (i.e. fill in the hole).
  • get(int i) method that returns the number at the ith index position in the list. This method does not remove the number from the list, it just returns the value at the ith position. If the index i is outside the bounds of the array, it generates (throws) an ArrayIndexOutOfBoundsException. Note in this case the bounds of the array are determined by the number of items currently in the array and not by the length of the array.
  • toString( ) method that returns a String of the contents of Scores class
  • equals(Object o) method that returns a true if the parameter o exactly matches the contents of Scores class (i.e. same numbers in the same order)

More on next page.

Finally design a java class Client with the main( ) method that does the following:

  • Create an Object of Type Scores using the overloaded constructor and pass the value 16.
  • Use a for loop to populate the list in Scores object with 32 random numbers between -10 and +10 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 6 to Scores object.
  • Print the current size of the list in the Scores object.
  • Call the remove( ) method to randomly remove a number from Scores class
  • Get the number at the 15th index position
  • Print the frequency that the number, returned by the previous step, occurs in Scores class
  • Call the appropriate overloaded remove method to remove the first occurrence of number at the 15th index position from Scores class
  • Print the frequency that this number now occurs in Scores class
  • Check whether the array in Scores object contains the number 5
  • Print the capacity of the array in the Scores object

Use JavaDoc commenting styles in the Scores class and the Client. Make sure to provide a block comment at the top that provides description of each class and a JavaDoc comment for each method.

Use single line commenting style as needed.

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

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

a. How do you think these stereotypes developed?

Answered: 1 week ago