icse-promo

Question

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:
Classname: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():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( 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.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;

public class Combine
{

    int com[]=new int[100];
    int size;
    Scanner sc=new Scanner(System.in);

    Combine(int nn)
    { 
        size=nn;
        com=new int[size];
    }

    void inputarray()
    {    
        System.out.println("\nEnter "+size+" elements");
        for(int i=0;i< size;i++)
        {
            com[i]=sc.nextInt();
        }
    }

    void sort()
    {
        int t;
        for(int i=0;i< size;i++)
        { 

            for(int j=i;j< size;j++)
            {    
                if(com[j]< com[i])
                {
                    t=com[j];
                    com[j]=com[i];
                    com[i]=t;

                }
            }

        }
    }

    void mix(Combine A,Combine B)
    {
        int x=0,y=0,z=0;
        while(x< A.size)
        {
            com[z++]=A.com[x++];
        }
        while(y< B.size)
        {
            com[z++]=B.com[y++];
        }
    }

    void display()
    {

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

    public static void main()
    {
        Scanner sc1=new Scanner(System.in);
        System.out.println("\n enter size of first array");
        int a=sc1.nextInt();
        Combine P=new Combine(a);
        P.inputarray();
        System.out.println("\n Enter size of Second array");
        int b=sc1.nextInt();
        Combine Q=new Combine(b);
        Q.inputarray();

        Combine R=new Combine(a+b);
        R.mix(P,Q);
        R.sort();
        System.out.println("Array 1:");
        P.display();
        System.out.println();
        System.out.println("Array 2:");
        Q.display();
        System.out.println();
        System.out.println("Combined Array:");
        R.display();
    }
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *