ISC 2012 Question 8

https://java4school.com/isc-2020-program

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 nn): parameterized constructor to assign size = nn
void inputarray(): accepts the array elements.
void sort(): sorts the elements of the 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 (int), 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.*;
class Combine
{
    static Scanner s=new Scanner(System.in);
    int com[];
    int size;
    Combine(int nn)
    {
        size=nn;
        com = new int[size];
    }

    void  inputArray()
    {
        System.out.println("Enter  numbers"+size+"times");
        for(int i=0;i<size;i++)
        {
            System.out.println("Enter a number");
            com[i]=s.nextInt();
        }
    }

    void Mix(Combine A,Combine B)
    {
        int l=A.com.length;int l1=B.com.length;
        for(int i=0;i<l+l1;i++)
        {
            if(i<l)
                com[i]=A.com[i];
            else
            {
                com[i]=B.com[i-l];
            }
        }
    }

    void sort()
    {

        for(int i=0;i<com.length;i++)
        {
            int c=-1;
            int tmp=com[i];
            for(int j=i+1;j<com.length;j++)
            {
                if(tmp>com[j])
                {
                    tmp=com[j];
                    c=j;
                }
            }
            if(c!=-1)
            {
                tmp=com[i];
                com[i]=com[c];
                com[c]=tmp;
            }
        }
    }

    void display()
    {
        for(int i=0;i<size;i++)
            System.out.print(com[i]+"|");
    }

    public static void main()
    {
        System.out.println("Enter size for first array");
        Combine A= new Combine(s.nextInt());
        A.inputArray();
        System.out.println("Enter size for second array");
        Combine B=new Combine(s.nextInt());
        B.inputArray();
        Combine o=new Combine(A.size+B.size);
        o.Mix(A,B);
        o.sort();
        o.display();
    }
}

https://www.youtube.com/watch?v=zEaeCL6Hb6s

LEAVE A REPLY

Please enter your comment!
Please enter your name here