Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your assignment is to create a class called Numbers in a file called NumberColle

ID: 3688750 • Letter: Y

Question

Your assignment is to create a class called Numbers in a file called NumberCollection.java. (there is no main method in this class). A class NumberCollection has an array of integers and a count (integer) as instance variables. The variable count keeps track how many integers are stored in the array. The variable name for the array of integers is numberArray. Note: You need to distinguish the array size (capacity) and "count" that keeps track of numbers added to this array so far. The class NumberCollection must include the following constructor and methods. (If your class does not contain any of the following methods, points will be deducted.)

public NumberCollection(int arraySize): It constructs an empty NumberCollection object with an array capacity specified by the integer parameter "arraySize".

private int indexOf(int searchingNum): It returns the index of the number specified by the parameter is located. If the number is not found, it returns -1. It is a service (helper) method.

public boolean addNumber(int numberToAdd): The method checks if the integer specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and also checks if the array has not reached its capacity. If both are satisfied, the number is added to the array at the smallest available index. If the array reached its capacity, double its size by calling the method doubleArrayCapacity() and add the number. If the number is added successfully, then the method returns true. If the number already exists in the array, the new number will not be added, and the method returns false.

public boolean remove(int numberToRemove): The method checks if the integer specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and if it does, it removes the number and then it shifts all the integers to the left and returns true. Otherwise, it returns false.

private void doubleArrayCapacity(): It is a service (helper) method and doubles the capacity of the numberArray. Please see the example in page 353, the method increaseSize() as a reference.

public int findRange(): It finds the range of values in the array. The range is defined as 1 more than the difference between the maximum and minimum in the array.

Public doublet computeAvg(): It computes and returns the average of numbers stored in the numberArray so far (at the time when this method is called.) If the array is empty, return 0.

public String toString( ): Returns a String containing a list of numbers stored in the numberArray. An example of such string can be: {3, 6, -1, 3, 23, -50, 43} The string should start with a '{' and end with a '}'

Explanation / Answer

public class NumberCollection

{

            private int[] numberArray;

            private static int count;

           

//It constructs an empty NumberCollection object

// with an array capacity specified by the integer parameter "arraySize".

            public NumberCollection(int arraySize)

            {

                        numberArray = new int[arraySize];

                        count = 0;

            }

//It returns the index of the number specified by the parameter is located.

//If the number is not found, it returns -1      

           

private int indexOf(int searchingNum)

            {

                        int value = 0;

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

                        {

                                    if (numberArray[i] == searchingNum)

                                    {

                                                value = i;

                                    }

                                    else

                                    {

                                                value = -1;

                                    }

                        }

                        return value;

            }

//The method checks if the integer specified by the parameter exists in the array

//and also checks if the array has not reached its capacity. If both are satisfied, the number is added to the array at the smallest available index.

//If the array reached its capacity, double its size by calling the method doubleArrayCapacity() and add the number. If the number is added successfully, then the method returns true.

//If the number already exists in the array, the new number will not be added, and the method returns false.

           

public boolean addNumber(int numberToAdd)

            {

                        boolean add;

                        if (indexOf(numberToAdd) != -1)

                        {

                                    add = false;

                        }

                        else

                        {

                                    if (numberArray.length == count)

                                    {

                                                doubleArrayCapacity();

                                    }

                                    numberArray[count] = numberToAdd;

                                    count++;

                                    add = true;

                        }

                        return add;

            }

           

//The method checks if the integer specified by the parameter exists in the array

//and if it does, it removes the number and then it shifts all the integers to the left and returns true. Otherwise, it returns false.

public boolean remove(int numberToRemove)

            {

                        boolean remove;

                        if (indexOf(numberToRemove) == -1)

                        {

                                    remove = false;

                        }

                        else

                        {

                                    int index = indexOf(numberToRemove);

                                    for (int i = index; i<count-1; i++)

                                    {

                                                numberArray[i] = numberArray[i+1];

                                    }

                                    count--;

                                    remove = true;

                        }

                        return remove;

            }

           

//It is a service (helper) method and doubles the capacity of the numberArray.

private void doubleArrayCapacity()

            {

                        if (count== numberArray.length)

                        {

                                    int[] temp = new int[numberArray.length*2];

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

                                    {

                                                temp[i] = numberArray[i];

                                    }

                                    numberArray=temp;

                        }

            }

           

//It finds the range of values in the array.

//The range is defined as 1 more than the difference between the maximum and minimum in the array.

           

public int findRange()

            {

                        int max = 0;

                        for (int i=0; i<count; i++)

                        {

                                    while(numberArray[i]>max)

                                    {

                                                max=numberArray[i];

                                    }

                        }

                                   

           

                        int min = 2147483647;

                        for (int i=0; i<count; i++)

                        {

                                    while(numberArray[i]<min)

                                    {

                                                min=numberArray[i];

                                    }

                        }

                        int diff;

                        diff=max-min;

                        return diff;

            }

           

           

// It computes and returns the average of numbers stored in the numberArray

public int computeAvg()

            {

                        int sum = 0;float avg;

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

                        {

                                    sum += numberArray[i];

                        }

                        avg=sum/numberArray.length;

                        return avg;

            }

           

//Returns a String containing a list of numbers stored in the numberArray.

public String toString()

            {

                        String result = "{";

                        for (int i=0; i<count-1; i++)

                        {

                                    result += numberArray[i] + ", ";

                        }

                        result += numberArray[count-1] + "}";

                        return result;

            }

}

note- the above code can help to answer the given question.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote