Combine Two Arrays into One ISC 2012 Theory

A class Combine contains an array of integers which combines two arrays into a single array including the duplicate elements if any, and sorts the combined array. Some of the members of the class are given below:

Class name: Combine
Data members/instance variables:
com[]: integer array
size: size of the array
Member functions/methods:
Combine(int num): parameterized constructor to assign size = num.
void inputArray(): to accept the array elements.
void sort(): sorts the elements of combined array in ascending order using the selection sort technique.
void mix(Combine a, Combine b): combines the parameterized object arrays and stores the result in the current object array along with duplicate elements if any.
void display(): displays the array elements.

Specify the class Combine giving details of the constructor, void inputArray(). void sort(), void mix(Combine, Combine) and void display(). Also define the main() function to create an object and call the methods accordingly to enable the task.

import java.io.*;
class Combine{
    int com[];
    int size;
    public Combine(int num){
        size = num;
        com = new int[size];
    }
    public void inputArray()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for(int i = 0; i < size; i++)
            com[i] = Integer.parseInt(br.readLine());
    }
    public void sort(){
        for(int i = 0; i < size; i++){
            int small = com[i];
            int pos = i;
            for(int j = i + 1; j < size; j++){ if(small > com[j]){
                    small = com[j];
                    pos = j;
                }
            }
            int temp = com[i];
            com[i] = small;
            com[pos] = temp;
        }
    }
    public void mix(Combine a, Combine b){
        int index = 0;
        for(int i = 0; i < a.size; i++)
            this.com[index++] = a.com[i];
        for(int i = 0; i < b.size; i++)
            this.com[index++] = b.com[i];
    }
    public void display(){
        for(int i = 0; i < size; i++)
            System.out.print(com[i] + "\t");
        System.out.println();
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("First array size: ");
        int size1 = Integer.parseInt(br.readLine());
        System.out.print("Second array size: ");
        int size2 = Integer.parseInt(br.readLine());
        Combine obj1 = new Combine(size1);
        Combine obj2 = new Combine(size2);
        Combine obj3 = new Combine(size1 + size2);
        System.out.println("Enter first array elements:");
        obj1.inputArray();
        System.out.println("Enter second array elements:");
        obj2.inputArray();
        System.out.println("First array elements:");
        obj1.display();
        System.out.println("Second array elements:");
        obj2.display();
        obj3.mix(obj1, obj2);
        obj3.sort();
        System.out.println("Combined array:");
        obj3.display();
    }
}

8 thoughts on “Combine Two Arrays into One ISC 2012 Theory

    • Sir,a program is given which states that:
      WAP in java to accept an integer number. Now create a new number by all the alternate digits of the original number.
      Sample Input:
      N=4532
      Sample Output:
      43

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.