Arrays In Java

timer Asked: Apr 14th, 2017

Question description

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.

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. Assignto“values”anewarraythatisthesamesizeastheparameter.
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 aren’t then you won’t 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
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) {

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<values.length; arrayPos++) {
values[arrayPos] = original.values[arrayPos];

//Size getter
public int getSize() {

throw new RuntimeException("Not yet implemented...");


//Single-value getter
public int getAt(int index) {

throw new RuntimeException("Not yet implemented...");


//Process the values to get the sum
public long getSum() {

throw new RuntimeException("Not yet implemented...");


//See if the given value is anywhere within the array
public boolean contains(int searchVal) {
boolean found = false;


return found;

//Append a new element - the array must "grow" somehow first!
public void append(int newValue) {
int[] newBiggerArray = new int[values.length + 1];


values = newBiggerArray;

Tutor Answer

(Top Tutor) Studypool Tutor
School: UC Berkeley
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors