icse-promo

Question

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:
Classname: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 nn):constructor to assign n=nn
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.

Share code with your friends

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

Code

				
					import java.util.Scanner;
public class Mixer
{
    int arr[];
    int n;
    Scanner sc=new Scanner(System.in);

    Mixer(int nn)
    {	
        n=nn;
        arr=new int[n];
    }

    void accept()
    {
        System.out.println("Enter "+n+" elements in ascending order");
        for(int i=0;i< n;i++)
        {
            arr[i]=sc.nextInt();
        }
    }

    Mixer mix(Mixer A)
    {
        Mixer B=new Mixer(n+A.n);		
        int x=0;
        for(int i=0;i< n;i++)
        {
            B.arr[x++]=arr[i];
        }		
        for(int j=0;j< A.n;j++)
        {
            B.arr[x++]=A.arr[j];
        }
        return B;
    }

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

    public static void main()
    {
        Scanner sc1=new Scanner(System.in);
        System.out.println("ENTER SIZE OF FIRST ARRAY");
        int sizeOfFirstArray=sc1.nextInt();
        Mixer P=new Mixer(sizeOfFirstArray);
        P.accept();
        System.out.println("ENTER SIZE OF SECOND ARRAY");
        int sizeOfSecondArray=sc1.nextInt();
        Mixer Q=new Mixer(sizeOfSecondArray);
        Q.accept();

        Mixer R=P.mix(Q);
        R.display();
    }
}




				
			

Coding Store

Leave a Reply

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