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

I have the main method code, as stated in the instructions, please let me know i

ID: 3546263 • Letter: I

Question


I have the main method code, as stated in the instructions, please let me know if posting that part of the code would help in writing this class. Thanks ahead of time!

Your assignment is to create a class called NumberCollection in a file called NumberCollection.java (there is no main method in this class). The NumberCollection class will act as a resizable array of unique integers and provide methods to act on the collection. Therefore the NumberCollection class needs the following instance variables: numberArray - an array of integers to hold the collection. count - an int to hold how many integers have been stored in the array so far. Note: The instance variable count will usually be different than the capacity of the array (numberArray.length). In order to provide access to the collection, 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.) Hint An example of most of these methods can be found in the subsections of Section 6.3 in the book beginning on page 258. Helpful hints for doing this assignment: work on it in steps - write one method, test it with a test driver and make sure it works before going on to the next method. always make sure your code compiles before you add another method your methods should be able to be called in any order Save the NumberCollection class in a file called NumberCollection.java and use the following program stored in Assignment7.java, which has the main method to create new NumberCollection objects and to test your class. You do NOT need to modify Assignment7.java. The program will ask a user to enter a size for the array. Then it will show the following menu to a user: Command Options add an integer in the array: display the array: compute and display the maximum: compute and display the minimum: compute and display the sum: display the menu again: quit this program Then it will ask a user to enter one of the above commands. Based on the user's choice, the program needs to perform corresponding operation. This will be done by using a method you define in the NumberCollection class. The program will terminate when a user enters 'q'. Sample Output: (the inputs entered by a user are shown in bold) Please enter a size for the array. Command Options: add an integer in the array : display the array : compute and display the maximum : compute and display the minimum : compute and display the sum : display the menu again Please enter a command or type ? b {} Please enter a command or type ? c The maximum is: 0 Please enter a command or type ? d The minimum is: 0 Please enter a command or type ? e The sum is: 0 Please enter a command or type ? a Please enter an integer to add. 5 5 successfully added. Please enter a command or type ? a Please enter an integer to add. 12 12 successfully added. Please enter a command or type ? a Please enter an integer to add. -2 -2 successfully added. Please enter a command or type ? a Please enter an integer to add. 41 successfully added. Please enter a command or type ? a Please enter an integer to add. 9 9 successfully added. Please enter a command or type ? a Please enter an integer to add. -21 -21 successfully added. Please enter a command or type ? b {5,12, -2,41,9, -21} Please enter a command or type ? c The maximum is: 41 Please enter a command or type ? d The minimum is: -21 Please enter a command or type ? e The sum is: 44 Please enter a command or type ? a Please enter an integer to add. 41 41 is already in the array. 41 was not added. Please enter a command or type ? c {5,12, -2,41,9, -21} Please enter a command or type ? ? Command Options: add an integer in the array : display the array : compute and display the maximum : compute and display the minimum compute and display the sum display the menu again quit this program Please enter a command or type? q

Explanation / Answer


public class NumberCollection

{

private int[] numberArray;

private int count;


public NumberCollection(int arraySize)

{

count = 0;

numberArray = new int[arraySize];

}

  

private int indexOf(int searchingNum)

{

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

{

if (numberArray[i] == searchingNum)

return i;

}

return -1;

}

public boolean addNumber(int numberToAdd)

{

if (indexOf(numberToAdd) != -1)

return false;

  

if (count == numberArray.length)

{

// update capacity

int[] newArray = new int[count*2];

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

newArray[i] = numberArray[i];

numberArray = newArray;

  

}

  

numberArray[count] = numberToAdd;

count++;

return true;

}

public int findMax()

{

if (count == 0)

return 0;

int max = numberArray[0];

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

{

if(numberArray[i]>max)

max=numberArray[i];

}

return max;

}

public int findMin()

{

if (count == 0)

return 0;

int min = numberArray[0];

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

{

if(numberArray[i]<min)

min=numberArray[i];

}

return min;

}

public int computeSum()

{

int sum = 0;

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

{

sum += numberArray[i];

}

return sum;

}

public String toString()

{

String result = "{";

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

{

if (i>0)

result += ", ";

result += numberArray[i];

}

result += "}";

return result;

}

}