Combine Arrays ISC Computer Science 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 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.util.Scanner;
class Combine{
    int com[];
    int size;
    public Combine(int num){
        size = num;
        com = new int[size];
    }
    public void inputArray(){
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < size; i++)
            com[i] = Integer.parseInt(in.nextLine());
    }
    public void sort(){
        for(int i = 0; i < size - 1; 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] = com[pos];
            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] + " ");
        System.out.println();
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Size of array 1: ");
        int s1 = Integer.parseInt(in.nextLine());
        System.out.print("Size of array 2: ");
        int s2 = Integer.parseInt(in.nextLine());
        Combine x = new Combine(s1);
        Combine y = new Combine(s2);
        Combine z = new Combine(s1 + s2);
        System.out.println("Enter array 1 elements:");
        x.inputArray();
        System.out.println("Enter array 2 elements:");
        y.inputArray();
        z.mix(x, y);
        z.sort();
        System.out.println("Combined and sorted list:");
        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