PRASHNIKAप्रश्निका
Back to the paper

Design a class OddEven to arrange two single dimensional arrays into one single dimensional array…

Computer Science202310 marksProgram
Design a class OddEven to arrange two single dimensional arrays into one single dimensional array, such that the odd numbers from both the arrays are at the beginning followed by the even numbers. Example: Array 1: { 2, 13, 6, 19, 26, 11, 4 } Array 2: { 7, 22, 4, 17, 12, 45 } Arranged Array = { 13, 19 11, 7, 17, 45, 2, 6, 26, 4, 22, 4, 12 } Some of the members of the class are given below: Class name : OddEven Data members/instance variables: a[ ] : to store integers in the array m : integer to store the size of the array Methods / Member functions: OddEven(int mm) : parameterised constructor to initialize the data member m=mm void fillarray( ) : to enter integer elements in the array OddEven arrange(OddEven P, OddEven Q ) : stores the odd numbers from both the parameterized object arrays followed by the even numbers from both the arrays and returns the object with the arranged array void display( ) : displays the elements of the arranged array Specify the class OddEven giving details of the constructor( ), void fillarray( ), OddEven arrange(OddEven, OddEven) and void display( ). Define a main( ) function to create objects and call the functions accordingly to enable the task.

Answer

Answer

AI
import java.util.Scanner;

class OddEven
{
    int a[];
    int m;

    OddEven(int mm)
    {
        m = mm;
        a = new int[m];
    }

    void fillarray()
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < m; i++)
        {
            System.out.print("Enter element " + (i + 1) + ": ");
            a[i] = sc.nextInt();
        }
    }

    OddEven arrange(OddEven P, OddEven Q)
    {
        OddEven R = new OddEven(P.m + Q.m);
        int k = 0;
        for (int i = 0; i < P.m; i++)
            if (P.a[i] % 2 != 0)
                R.a[k++] = P.a[i];
        for (int i = 0; i < Q.m; i++)
            if (Q.a[i] % 2 != 0)
                R.a[k++] = Q.a[i];
        for (int i = 0; i < P.m; i++)
            if (P.a[i] % 2 == 0)
                R.a[k++] = P.a[i];
        for (int i = 0; i < Q.m; i++)
            if (Q.a[i] % 2 == 0)
                R.a[k++] = Q.a[i];
        return R;
    }

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

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter size of array 1: ");
        int m1 = sc.nextInt();
        OddEven o1 = new OddEven(m1);
        o1.fillarray();

        System.out.print("Enter size of array 2: ");
        int m2 = sc.nextInt();
        OddEven o2 = new OddEven(m2);
        o2.fillarray();

        OddEven o3 = new OddEven(0);
        OddEven result = o3.arrange(o1, o2);
        result.display();
    }
}
Explanation: arrange() first copies all odd elements of P in order, then all odd elements of Q, then all even elements of P, then all even elements of Q, into a new OddEven object R sized m of P plus m of Q, and returns R. Tested (run for real) with Array1={2,13,6,19,26,11,4} and Array2={7,22,4,17,12,45): the program printed "13 19 11 7 17 45 2 6 26 4 22 4 12", which exactly matches the Arranged Array given in the question.
Arrays, Strings

From ISC 2023 Specimen Computer Science Paper 1, question 7.