1. Write the method add in the class NumberList. The method add should append it
ID: 3633378 • Letter: 1
Question
1. Write the method add in the class NumberList. The method add should append its integer parameter to the end of its current object NumberList, using the following procedure:i. Create another array that is one element larger than the existing one in the NumberList object.
ii. Copy all of the elements from the existing array over to the corresponding positions in the new one.
iii. Store the value of the parameter in the last element of the new array.
iv. Reassign the field values so that it refers to the new array.
Below is my code so far.
public class NumberList {
public int[] values; // leave this field public
public NumberList() {
this.values = new int[0];
}
public NumberList(int[] a) {
this.values = new int[a.length];
for(int i = 0; i <a.length; i++){
this.values[i] = a[i];
}
}
public int getSize() {
return values.length;
}
public void add(int number) {
//this is the method I need written
}
}
Explanation / Answer
public void add(int number) { int[] anotherArray; int newLength = values.length + 1; anotherArray = new int[newLength]; for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.