Define a class with following specifications.
Class Name: JoinArray
Member Variables:
size – To store the number of elements.
ar[] – Array to store elements/numbers.
Member Methods:
JoinArray(int n) – Parameterized constructor to initialize member variables.
void getElements() – Accept the elements/numbers using Scanner class.
void doJoin(JoinArray A,JoinArray B) –
To join two arrays into one array.
void displayArray() –
To print the new joined array.
Invoke all the above methods in main() function using object of the class.
Java
import java.util.*;
public class JoinArray
{
int ar[];
int size;
JoinArray(int n)
{
size=n;
ar=new int[n];
}
public void getElements()
{
Scanner sc=new Scanner(System.in);
for(int i=0;i<size;i++)
{
ar[i]=sc.nextInt();
}
}
public void doJoin(JoinArray A,JoinArray B)
{
int pos=0;
for(int i=0;i<A.size;i++,pos++)
{
ar[pos]=A.ar[i];
}
for(int i=0;i<B.size;i++,pos++)
{
ar[pos]=B.ar[i];
}
}
public void displayArray()
{
System.out.println("Joined array:-");
for(int i=0;i<size;i++)
{
System.out.println(ar[i]);
}
}
public static void main(String args[])
{
int n=0;
Scanner sc=new Scanner(System.in);
System.out.print("Number of elements for first array: ");
n=sc.nextInt();
JoinArray ob1=new JoinArray(n);
System.out.print("Number of elements for second array: ");
n=sc.nextInt();
JoinArray ob2=new JoinArray(n);
System.out.println("Enter first array elements:-");
ob1.getElements();
System.out.println("Enter second array elements:-");
ob2.getElements();
JoinArray ob3=new JoinArray(ob1.size+ob2.size);
ob3.doJoin(ob1,ob2);
ob3.displayArray();
}
}Java