Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

Explain the concept of shear force and bending moment in beams.

Answered: 1 week ago

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago