Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 7

10 marks

Computer ScienceISC 2025 Specimen Computer Science Paper 1ProgramArrays, Strings

Write the program described below.

A class Mixarray contains an array of integer elements along with its capacity (More than or equal…

A class Mixarray contains an array of integer elements along with its capacity (More than or equal to 3). Using the following description, form a new array of integers which will contain only the first 3 elements of the two different arrays one after another. Example: Array1: { 78, 90, 100, 45, 67 } Array2: {10, 67, 200, 90 } Resultant Array: { 78, 90, 100, 10, 67, 200} The details of the members of the class are given below: Class name : Mixarray Data members/instance variables: arr[] : integer array cap : integer to store the capacity of the array Member functions/methods: Mixarray (int mm ) : to initialize the capacity of the array cap=mm void input( ) : to accept the elements of the array Mixarray mix(Mixarray P, Mixarray Q) : returns the resultant array having the first 3 elements of the array of objects P and Q void display( ) : to display the array with an appropriate message. Specify the class Mixarraygiving details of the constructor(int), void input( ), Mixarray mix(Mixarray,Mixarray) and void display( ). Define a main( ) function to create objects and call all the functions accordingly to enable the task.

Answer

Answer

Official answer key
import java.util.Scanner;

class Mixarray
{
    int arr[];
    int cap;

    Mixarray(int mm)
    {
        cap = mm;
        arr = new int[cap];
    }

    void input()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter " + cap + " elements:");
        for (int i = 0; i < cap; i++)
            arr[i] = sc.nextInt();
    }

    Mixarray mix(Mixarray P, Mixarray Q)
    {
        Mixarray res = new Mixarray(6);
        int k = 0;
        for (int i = 0; i < 3; i++)
            res.arr[k++] = P.arr[i];
        for (int i = 0; i < 3; i++)
            res.arr[k++] = Q.arr[i];
        return res;
    }

    void display()
    {
        System.out.println("Resultant array:");
        for (int i = 0; i < cap; 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 capacity of first array (>=3): ");
        int c1 = sc.nextInt();
        Mixarray P = new Mixarray(c1);
        P.input();

        System.out.print("Enter capacity of second array (>=3): ");
        int c2 = sc.nextInt();
        Mixarray Q = new Mixarray(c2);
        Q.input();

        Mixarray R = new Mixarray(6);
        Mixarray res = R.mix(P, Q);
        res.display();
    }
}
Explanation: mix(P,Q) creates a new Mixarray object of capacity 6, copies the first three elements of P into it followed by the first three elements of Q, and returns it. Tested with Array1={78,90,100,45,67} and Array2={10,67,200,90}: the program correctly outputs the resultant array 78 90 100 10 67 200 (verified by running the code), matching the example in the question.