Question: INSTRUCTIONS : For the OrdArray class in the orderedArray.java program (Listing 2.4 on page 59), you are asked to add the following methods A merge()

INSTRUCTIONS: For the OrdArray class in the orderedArray.java program (Listing 2.4 on page 59), you are asked to add the following methods

A merge() method so you can merge two ordered source arrays into an ordered destination array. To simplify, for duplicate values (no mater in an array or two arrays), only count once.

A common() method so you can get the common values owned by the two ordered source arrays into an ordered destination array. For duplicate values, only count once.

Using the objected oriented analysis and design, using the available insert(), find(). delete() and display() methods.

You cannot use any available library.

Write code in main() of class OrderedApp that inserts some random numbers into the two source arrays, invokes merge() and common() and displays the contents of the resulting arrays. The source arrays may hold different numbers of data items. In your algorithm you will need to compare the keys of the source arrays, picking the smallest one to copy to the destination. Youll also need to handle the situation when one source array exhausts its contents before the other.

Example:

Source array A = {3, 6, 8, 8, 11, 17, 25, 34, 38, 46, 48, 48, 48, 57, 62, 69, 72, 72, 77, 83};

Source array B = {5, 8, 14, 25, 31, 37, 41, 48, 48, 52, 77, 82, 94};

Then, Destination array C (after merge) = {3, 5, 6, 8, 11, 14, 17, 25, 31, 34, 37, 38, 41, 46, 48, 52, 57, 62, 69, 72, 77, 82, 83, 94};

Destination array D (after common) = {8, 25, 48, 77};

BELOW IS MY PROGRAM

//orderedArray.java

import java.util.Arrays;

public class orderedArray {

static int[] merge(int[] a, int[] b) {

int[] result = new int[a.length + b.length];

int i=0, j=0, count=0;

int last = -1;

while(i

if(a[i]

if(a[i] != last) {

result[count++] = a[i];

last = a[i];

}

i++;

} else if(a[i] > b[j]){

if(b[j] != last) {

result[count++] = b[j];

last = b[j];

}

j++;

} else {

if(b[j] != last) {

result[count++] = b[j];

last = b[j];

}

j++;

i++;

}

}

if(i == a.length) {

while(j

if(b[j] != last) {

result[count++] = b[j];

last = b[j];

}

j++;

}

} else if(j == b.length){

while(i

if(a[i] != last) {

result[count++] = a[i];

last = a[i];

}

i++;

}

}

// there may be duplicate elements in result

// so create new result

int newResult[] = Arrays.copyOf(result, count);

return newResult;

}

static int[] common(int[] a, int[] b) {

// find max length

int l = (a.length > b.length ? a.length : b.length);

int[] result = new int[l];

int i=0, j=0, count=0;

int last = -1;

while(i

if(a[i]

i++;

} else if(a[i] > b[j]){

j++;

} else {

if(b[j] != last) {

result[count++] = b[j];

last = b[j];

}

j++;

i++;

}

}

// there may be duplicate elements in result

// so create new result

int newResult[] = Arrays.copyOf(result, count);

return newResult;

}

public static void main(String args[]) {

int[] A = {3, 6, 8, 8, 11, 17, 25, 34, 38, 46, 48, 48, 48, 57, 62, 69, 72, 72, 77, 83};

int[] B = {5, 8, 14, 25, 31, 37, 41, 48, 48, 52, 77, 82, 94};

System.out.println("Merged Array:");

System.out.println(Arrays.toString(merge(A, B)));

System.out.println(" Common Elements:");

System.out.println(Arrays.toString(common(A, B)));

}

}

I need help with making a report that includes problem statement, analysis, algorithm design, class prototype (class contract), program Input/Output, and tested results (analysis your result to see if it is correct). 

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!