Question: NumberList Lab You will be implementing the class called NumberList, which represents a list of integers. You will see that a NumberList object has just
NumberList Lab You will be implementing the class called NumberList, which represents a list of integers. You will see that a NumberList object has just one instance variable, which is a reference to an array of int values. Note that this variable is set as a public variable. Please leave it that way. It's to make it easier to do simple JUnit testing here. In reality we would make it private, but for the lab keep it public. This assignment is being graded so be sure to submit your project to the submit server, release test it, etc! Below are the "stages" in which I suggest you design and test, but the whole list is for this exercise. 1. Look at the constructor that takes no arguments. Note that it must still instantiate the array values as an array of size zero. 2. Implement the constructor that takes an int array as an argument. You should: a. Assigntovaluesanewarraythatisthesamesizeastheparameter. b. Copy the elements from the parameter array into your new array. 3. Run the JUnit tests in PublicTests for the constructor to be sure your constructors are working if they arent then you wont be able to pass any of the subsequent tests. 4. Implement the getSize method. It simply returns the size of the list. 5. Implement the getAt method. It returns the element whose position in the list matches the parameter. We are using 0-based indexing. If the parameter is negative or exceeds the last index of the list then throw an IndexOutOfBoundsException with a simple message inside it. 6. Run the JUnit tests in PublicTests for these methods. 7. Implement the getSum method. It will return the sum of all entries in the list. 8. Implement the contains method. It will return true if the parameter is in the list, false otherwise. 9. Implement the add method. It should add the parameter to the end of the list. You will need to follow the steps below: a. Createanotherarraythatisoneunitlargerthantheexistingone. b. Copy all of the elements from the existing array over to the new one. c. Addtheparametertotheendofthenewarray. d. Re-assign the instance variable values so that it refers to the new array. (NOTE: Perhaps write your own JUnit tests and then "release test" to check these methods.
public class NumberList {
public int[] values; //Constructor for an empty list (provided for you) public NumberList() { values = new int[0]; } //Constructor passed in an array public NumberList(int[] a) {
//YOUR CODE HERE throw new RuntimeException("Not yet implemented..."); } //Copy constructor (provided for you) public NumberList(NumberList original) { values = new int[original.values.length]; for (int arrayPos=0; arrayPos //YOUR CODE HERE throw new RuntimeException("Not yet implemented..."); } //Single-value getter public int getAt(int index) { //YOUR CODE HERE throw new RuntimeException("Not yet implemented..."); } //Process the values to get the sum public long getSum() { //YOUR CODE HERE throw new RuntimeException("Not yet implemented..."); } //See if the given value is anywhere within the array public boolean contains(int searchVal) { boolean found = false; //YOUR CODE HERE return found; } //Append a new element - the array must "grow" somehow first! public void append(int newValue) { int[] newBiggerArray = new int[values.length + 1]; //YOUR CODE HERE values = newBiggerArray; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
