‹ Back to the paper
A class JoinArray contains an array of integer elements. Form a new array which will contain…
A class JoinArray contains an array of integer elements. Form a new array which will contain elements of the second array from last index to first index followed by elements of the first array from last index to first index.
Example:
Array1
| 9 | 7 | 6 | 4 |
Array2
| 8 | 3 | 2 | 1 |
Resultant Array
| 1 | 2 | 3 | 8 | 4 | 6 | 7 | 9 |
The details of the members of the class are given below:
Class name : JoinArray
Data members/instance variables:
arr[ ] : integer array
size : integer to store the size of the array
Member functions/methods:
JoinArray(int s) : parameterised constructor to initialise size = s
void accept( ) : to input the elements of the array
JoinArray attach(JoinArray P, JoinArray Q) : to return the resultant array after combining arrays of objects P and Q as per the description given above
void display( ) : to display the array elements
Specify the class JoinArray giving details of the constructor(int), void accept( ), JoinArray attach(JoinArray, JoinArray) and void display( ). Define the main( ) function to create objects and call the functions accordingly to enable the task.
Answer
Answer
AIimport java.util.Scanner;
class JoinArray
{
int arr[];
int size;
JoinArray(int s)
{
size = s;
arr = new int[size];
}
void accept()
{
Scanner sc = new Scanner(System.in);
for (int i = 0; i < size; i++)
{
System.out.print("Enter element: ");
arr[i] = sc.nextInt();
}
}
JoinArray attach(JoinArray P, JoinArray Q)
{
JoinArray R = new JoinArray(P.size + Q.size);
int k = 0;
for (int i = Q.size - 1; i >= 0; i--)
R.arr[k++] = Q.arr[i];
for (int i = P.size - 1; i >= 0; i--)
R.arr[k++] = P.arr[i];
return R;
}
void display()
{
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of Array1: ");
int s1 = sc.nextInt();
JoinArray P = new JoinArray(s1);
P.accept();
System.out.print("Enter size of Array2: ");
int s2 = sc.nextInt();
JoinArray Q = new JoinArray(s2);
Q.accept();
JoinArray R = P.attach(P, Q);
System.out.print("Resultant Array: ");
R.display();
}
}Explanation: attach(P,Q) creates a new JoinArray R of size P.size+Q.size, first copies Q's elements from its last index down to its first index, then copies P's elements from its last index down to its first index, into R. Tested: for Array1={9,7,6,4} and Array2={8,3,2,1}, the program correctly outputs the resultant array 1 2 3 8 4 6 7 9, matching the example in the question.From ISC 2025 Improvement Computer Science Paper 1, question 7.