Question: - Q: Rewrite this code in two classes (one class that have the code and the other class call the method that have public static

 

- Q: Rewrite this code in two classes (one class that have the code and the other class call the method that have public static void main) that inputs five each of which is between 10 and 100 inclusive,as each number i read, display it only if it is not a duplicate number already read, provide for the "worst case," in which all five numbers are different, use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs each new value.

public class OrdArray {

private long [] a; private int nElems;

public OrdArray (int max) { a= new long[max]; nElems = 0; }

public int size (){ return nElems; }

public int find( long searchKey) // divide and conquer { int lowerBound = 0; int upperBound = nElems-1; int curIn; while(true) { curIn = (lowerBound + upperBound ) / 2; // find the middle 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; // its in upper half else upperBound = curIn-1; // its in the lower half }// end else divide range } // end while } // end find() public void insert(long value) // sort the array elements from least to greatest. { int j; for(j=0; j if (a[j] > value ) // linear search break; // move bigger ones for ( int k = nElems; k>j; k--) // to move the array content a[k] = a[k-1]; a[j] = value; //insert the value nElems++; // increment size } public boolean delete (long value) { int j = find (value); //can't find it if(j==nElems) return false; else // found it { for(int k=j; k a[k] = a[k+1]; nElems--; // decrement size return true;

} }// end delete() public void display() //display array content { for(int j=0; j System.out.print(a[j]+ " "); // display it System.out.println(""); } } // end of class OrdArray //////////////////////////////////////////////////////////////////////////

Note: The output should be in order

Enter number:

11

11

Enter number:

21

11 21

Enter number:

34

11 21 34

Enter number:

11

the number was entered before

11 21 34

Enter number:

44

11 21 34 44

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!