Question: Given the Class Below (OrdArray.java) : Implement one of its method addTwo, so that given any target number, find in the array two elements that

Given the Class Below (OrdArray.java) : Implement one of its method addTwo, so that given any target number, find in the array two elements that can add up to the target number. Return and display the index of the two elements onto screen. Return {- 1, -1} if no such two elements can be found in the array

OUTPUT Examples:

The content of the array is: 0 2 5 6 7 10 12 24 Please enter a target number: 15 The two elements that can add up to 15 are at indexes: 2 5

_____________________________________

public class OrdArray { private int[] a; // ref to array a private int nElems; // number of data items //----------------------------------------------------------- public OrdArray(int max) { // constructor a = new int[max]; // create array nElems = 0; } //----------------------------------------------------------- public int size() { return nElems; } //----------------------------------------------------------- public int find(int searchKey) { int lowerBound = 0; int upperBound = nElems-1; int curIn; while(true) { curIn = (lowerBound + upperBound ) / 2; if (a[curIn]==searchKey) return curIn; // found it else if (lowerBound > upperBound) return nElems; // can't find it else { // divide range if (a[curIn] < searchKey) lowerBound = curIn + 1; // it's in upper half else upperBound = curIn - 1; // it's in lower half } // end else divide range } // end while } // end find() //----------------------------------------------------------- public void insert(int value) { // put element into array int j; for (j=0; j value) // (linear search) break; for (int k=nElems; k>j; k--) // move bigger ones up a[k] = a[k-1]; a[j] = value; // insert it nElems++; // increment size } // end insert() //----------------------------------------------------------- public boolean delete(int value) { int j = find(value); if (j==nElems) // can't find it return false; else // found it { for(int k=j; k                                            

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!