Merge Arrays ISC Computer Science 2014 Theory

A class Mixer has been defined to merge two sorted integer arrays in ascending order.

Some of the members of the class are given below:

Class name: Mixer

Data members/instance variables:
int arr[]: to store the elements of an array
int n: to store the size of the array

Member functions:
Mixer(int num): constructor to assign n = num
void accept(): to accept the elements of the array in ascending order without any duplicates
Mixer mix(Mixer a): to merge the current object array elements with the parameterized array elements and return the resultant object
void display(): to display the elements of the array

Specify the class Mixer, giving details of the constructor(int), void accept(), Mixer mix(Mixer) and void display(). Define the main() function to create an object and call the function accordingly to enable the task.

import java.util.Scanner;
class Mixer{
    int arr[];
    int n;
    public Mixer(int num){
        n = num;
        arr = new int[n];
    }
    public void accept(){
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < n;){
            System.out.print("Enter element " + (i + 1) + ": ");
            int x = Integer.parseInt(in.nextLine());
            boolean valid = true;
            for(int j = 0; j < i; j++){
                if(arr[j] >= x){
                    valid = false;
                    break;
                }
            }
            if(valid)
                arr[i++] = x;
        }
    }
    public Mixer mix(Mixer a){
        Mixer t = new Mixer(this.n + a.n);
        int index = 0;
        int p = 0;
        int q = 0;
        for(int i = 0; i < t.n; i++){
            if(p < n && q < a.n){
                if(arr[p] < a.arr[q])
                    t.arr[index++] = arr[p++];
                else
                    t.arr[index++] = a.arr[q++];
            }
            else if(p < n){
                for(int k = p; k < n; k++)
                    t.arr[index++] = arr[k];
                break;
            }
            else if(q < a.n){
                for(int k = q; k < a.n; k++)
                    t.arr[index++] = a.arr[k];
                break;
            }
        }
        return t;
    }
    public void display(){
        for(int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Size of array 1: ");
        int size1 = Integer.parseInt(in.nextLine());
        Mixer x = new Mixer(size1);
        System.out.print("Size of array 2: ");
        int size2 = Integer.parseInt(in.nextLine());
        Mixer y = new Mixer(size2);
        System.out.println("Array 1 input");
        x.accept();
        System.out.println("Array 2 input");
        y.accept();
        Mixer z = new Mixer(size1 + size2);
        z = x.mix(y);
        System.out.println("Resultant array:");
        z.display();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory