QuestionQuestion

Part #1 - Programming
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.)
• public NumberCollection(int arraySize) - It constructs an empty NumberCollection object with an array capacity specified by the integer parameter arraySize. That is, it stores a new int array with size arraySize in the numberArray instance variable.
• 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. This is a helper method.
• public boolean addNumber(int numberToAdd) - The method will add numberToAdd at the smallest available index in the array, if the number is not in already in the array. Be sure to check if the number is already in the array:
– Hint: This can be done using the indexOf method to see if it returns -1 or not. 1
– If the number is in the array, then it will not be added again and the method returns false to indicate that nothing was added.
– If the number is NOT in the array, continue.
If the number is not in the array, be sure to check if the array has reached its capacity.
– Hint: What is the relationship between the instance variable count and the length of numberArray if the capacity is reached.
– If the capacity is reached, double the length of numberArray.
– Hint: You can use the Arrays.copyOf() method for this
– After updating the capacity, if necessary, continue.
If the number is not in the array and after updating the capacity, if necessary, then add numberToAdd to numberArray at the smallest available index.
– Hint: What’s the smallest available index’s relationship to count? – Hint: Be sure to increase count after adding a new element.
If the number is added successfully, then the method returns true.
• public int findMax() - It finds the maximum number among the numbers stored so far (at the time
when this method is called), and returns it. If the array is empty, return 0.
• public int findMin() - It finds the minimum number among the numbers stored so far (at the time when this method is called), and returns it. If the array is empty, return 0.
• public int computeSum() - It computes and returns the sum of numbers stored in 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 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 ’}’.
Note: Arrays.toString(numberArray) cannot be used since will not use the required delimiters. That is, you have to write your own loop.

Helpful Hints
• 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 Assignment6.java, which has the main method to create new NumberCollection objects and to test your class. You do NOT need to modify Assignment6.java.

The program will ask a user to enter a size for the array. Then it will show the following menu to a user:
2
Command Options
-----------------------------------
a: add an integer in the array
b: display the array
c: compute and display the maximum
d: compute and display the minimum
e: compute and display the sum
?: display the menu again
q: quit this program
Sample Outputs
(user input is in bold)
Please enter a size for the array.
3
Command Options ———————————–
a: add an integer in the array
b: display the array
c: compute and display the maximum d: compute and display the minimum e: 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.
3
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
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
4
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 ———————————–
a: add an integer in the array
b: display the array
c: compute and display the maximum d: compute and display the minimum e: compute and display the sum
?: display the menu again
q: quit this program
Please enter a command or type ?
q

and this is the Assignment6.java file

import java.util.Scanner;
public class Assignment6
{
public static void main(String[] args)
   {
      int number, size;
      String choice;
      char command;

      Scanner keyboard = new Scanner(System.in);
      // ask a user for a array size
      System.out.println("Please enter a size for the array.n");
      size = keyboard.nextInt();

      // instantiate a NumberCollection object
      NumberCollection collection = new NumberCollection(size);

      // print the menu
      printMenu();

      do
       {
          // ask a user to choose a command
          System.out.println("nPlease enter a command or type ?");
          choice = keyboard.next().toLowerCase();
          command = choice.charAt(0);

          switch (command)
            {
                case 'a': // add a number
                      System.out.println("nPlease enter an integer to add.");
                      number = keyboard.nextInt();
                      if(collection.addNumber(number))
                        System.out.println("n" + number + " successfully added.");
                      else
                        System.out.println("n" + number + " is already in the array. " + number + " was not added.");
                      break;
                case 'b': // print the collection
                      System.out.println("n" + collection);
                      break;
                case 'c': // compute and display the maximum
                      System.out.println("nThe maximum is: " + collection.findMax());
                      break;
                case 'd': // compute and display the minimum
                      System.out.println("nThe minimum is: " + collection.findMin());
                      break;
                case 'e': // compute and display the sum
                      System.out.println("nThe sum is: " + collection.computeSum());
                      break;
                case '?':
                      printMenu();
                      break;
                case 'q':
                      break;
                default:
                System.out.println("Invalid input!");

            }

       } while (command != 'q');

    } //end of the main method


// this method prints out the menu to a user
public static void printMenu()
   {
    System.out.print("nCommand Optionsn"
                   + "-----------------------------------n"
                   + "a: add an integer in the arrayn"
                   + "b: display the arrayn"
                   + "c: compute and display the maximumn"
                   + "d: compute and display the minimumn"
                   + "e: compute and display the sumn"
                   + "?: display the menu againn"
                   + "q: quit this programnn");

    } // end of the printMenu method


} // end of the Assignment6 class.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.util.Scanner;
public class Assignment6
{
    public static void main(String[] args)
    {
       int number, size;
       String choice;
       char command;

       Scanner keyboard = new Scanner(System.in);
       // ask a user for a array size
       System.out.println("Please enter a size for the array.\n");
       size = keyboard.nextInt();

       // instantiate a NumberCollection object
       NumberCollection collection = new NumberCollection(size);

       // print the menu
       printMenu();

       do
       {
            // ask a user to choose a command
            System.out.println("\nPlease enter a command or type ?");
            choice = keyboard.next().toLowerCase();
            command = choice.charAt(0);

            switch (command)
            {...
$25.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats