Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- Q: Rewrite this code in two classes (one class that have the code and the othe

ID: 3750293 • Letter: #

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 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

Explanation / Answer

/*Java program to read array elements and display unique elements. I had made some updations in the previous code for easy coding and understanding */

import java.io.*;

class OrdArray

{

private int[] a;

private int nElems;

public OrdArray (int max) // initializing the object

{

a= new int[max];

nElems = 0;

}

public int size() //Finding array length

{

return nElems;

}

public int find(int searchKey)

{

int pos=0,n=0;

for(int i=0;i<nElems;i++)

{

if(a[i] == searchKey)

{

pos=i;

n++;

}

}

if(n>1)

return pos; //return position of the extra insertion of number

else

return 0; only unique values are there in the array even after insertion. so return 0

} // end find()

public void insert(int value) // sort the array elements from least to greatest.

{

int j;

if(nElems == 0)

{

a[nElems]=value;

nElems++;

}

else

{

for(j=0;j<nElems;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 void checkElement(int value)

{

int j = find (value); //can't find it

if(j==0)

return;  

else // found it

{

for(int k=j; k<=j;k++)

{

a[k] = a[k+1];

}

nElems--; // decrement size

System.out.println(" The number was entered before ");

}

}// end delete()

public void display() //display array content

{

for(int j=0;j<nElems;j++)

{

System.out.print(a[j]+ " "); // display it

System.out.println("");

}

}

} // end of class OrdArray

public class ArrayImp

{

public static void main(String args[])throws IOException

{

InputStreamReader in = new InputStreamReader(System.in);

BufferedReader obj = new BufferedReader(in);

System.out.println(" Enter the maximum number of elements you wanted to enter: "); //enter the array limit

int max = Integer.parseInt(obj.readLine());

OrdArray ob = new OrdArray(max);

for(int i=0;i<=max;i++)

{

System.out.println("Enter a number between 10 and 100 inclusively ");

int num = Integer.parseInt(obj.readLine());

ob.insert(num);

ob.checkElement(num);

ob.display();

}

}

}