1. Create a program that will implement the HighArray Class, declare three objec
ID: 3746258 • Letter: 1
Question
1. Create a program that will implement the HighArray Class, declare three objects name Arr1 with an array size of 5, Arr2 with an array size of 5 and Arr3 with an array size of 10. Enter numbers for each object Arr1 and Arr2. After filling the object Arr1 and Arr2, your next task is to fill in the object Arr3 using the content of the Arr1 and Arr2, keep in mind that you need to fill in Arr3 in alternate values comming from Arr1 and Arr2.
Example: Assuming the content of Arr1 = {1,2,3,4,5} and Arr2= {9,8,7,6,5}
The result of Arr3 is {1,9,2,8,3,7,4,6,5,5}
note: it should be in two classes like:
Theoretical Background:
// highArray.java
////////////////////////////////////////////////////////////////
classHighArray
{
privateint[] a; // ref to array a
privateintnElems; // number of data items
//-----------------------------------------------------------
publicHighArray(intmax) // constructor
{
a = newint[max]; // create the array
nElems = 0; // no items yet
}
publicvoidinsert(intvalue) // put element into array
{
if(nElems < a.length)
{
a[nElems] = value; // insert it
nElems++; // increment size
}
}
publicvoiddisplay() // displays array contents
{
for(intj=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
publicbooleanfind(intsearchKey)
{
for(inti=0; i < nElems; i++)
{
if(a[i] == searchKey)
returntrue;
}
returnfalse;
}
publicbooleandelete(intvalue)
{
inti;
for(i=0; i<nElems; i++) // look for it
if( value == a[i] )
break;
if(i<nElems) // can't find it
{
for(intj=i; j<nElems-1; j++) // move higher ones down
a[j] = a[j+1];
nElems--; // decrement size
returntrue;
}
else
returnfalse;
}
} // end class HighArray
publicclassHighArrayTest {
publicstaticvoidmain(String[] args) {
intmaxSize = 10; // array size
HighArray arr; // reference to array
arr = newHighArray(maxSize); // create the array
arr.insert(77); // insert 5 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.display(); // display items
System.out.println();
if(arr.find(100))
System.out.println("Found");
else
System.out.println("Not Found");
arr.delete(22);
arr.delete(100);
arr.display();
}
}
Explanation / Answer
import java.io.*;
public class AlternativeMergeForThird {
static void alterMerge(int arr1[], int arr2[],int n1, int n2, int arr3[])
{
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) //walk through both the array
{
arr3[k++] = arr1[i++];
arr3[k++] = arr2[j++];
}
while (i < n1) // stores elements of first array
arr3[k++] = arr1[i++];
while (j < n2) //stores elemnts of second array
arr3[k++] = arr2[j++];
}
public static void main(String args[])
{
int arr1[] = {10,12,14,18,19};
int n1 = arr1.length;
int arr2[] = {20,25,26,27,28};
int n2 = arr2.length;
int arr3[] = new int[n1+n2];
alterMerge(arr1, arr2, n1, n2, arr3);
System.out.println("Alternative array merging");
for (int i = 0; i < n1 + n2; i++)
System.out.print( arr3[i] + " ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.