Best writers. Best papers. Let professionals take care of your academic papers

Order a similar paper and get 15% discount on your first order with us
Use the following coupon "FIRST15"
ORDER NOW

The instructions state that is it mandatory to use the provided driver to create your class from. The driver is the code located at the end of these instructions. Please use that as the drive to create the working program.

****Note*****

The instructions state that is it mandatory to use the provided driver to create your class from. The driver is the code located at the end of these instructions. Please use that as the drive to create the working program.

Topics

  • Arrays
  • Classes

Coding Guidelines:

  • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).
  • Keep identifiers to a reasonably short length.
  • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).
  • Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
  • Use white space to make your program more readable.

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.
    • 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 as described in Section 6.3.9 of the book. 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.

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

  • 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:

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.

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

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 ? b

{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

****NOTE****

Here is the driver program that I was spoke about earlier at the begging of these instructions

Notes: 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.

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 Options\n”

                   + “———————————–\n”

                   + “a: add an integer in the array\n”

                   + “b: display the array\n”

                   + “c: compute and display the maximum\n”

                   + “d: compute and display the minimum\n”

                   + “e: compute and display the sum\n”

                   + “?: display the menu again\n”

                   + “q: quit this program\n\n”);

    } // end of the printMenu method

 } // end of the Assignment6 class

 
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"