i need help with method sequential_add(). JAVA LANG public boolean sequential.ad
ID: 3750674 • Letter: I
Question
i need help with method sequential_add(). JAVA LANG
public boolean sequential.addCdouble newVal) o if this list does not contain the newVal, add it to the correct position of the list and return true o remember that the list is required to be sorted at all times - that specifies the correct position for the newVal o returns false, if the list already includes the value o use a sequential search in this phase (you will write an efficient add for your assignment) o be careful about what states need to be changed after the removeExplanation / Answer
Below is your code
public boolean sequential_add(double newVal){
if((numItems+1) >= capacity)
this.grow();
if(this.contains(newVal))
return false;
if(numItems == 0){
storage[0] = newVal;
numItems ++;
return true;
}
if(numItems == 1) {
if(storage[0] > newVal) {
storage[1] = storage[0];
storage[0] = newVal;
numItems++;
return true;
} else {
storage[1] = newVal;
return true;
}
}
int i;
for(i = numItems -1; i >= 0 && storage[i] > newVal; i--){
storage[i + 1] = storage[i];
}
storage[i+1] = newVal;
numItems++;
return true;
}
public boolean contains(double value) {
int min = 0;
int max = numItems-1;
while(min <= max) {
int mid = (max + min)/2;
if(storage[mid] == value) {
return true;
} else if(storage[mid] < value) {
min = mid + 1;
} else {
max = mid -1;
}
}
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.