Odd Even ISC Specimen 2023 Theory

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 num): parameterized constructor to initialize the data member m = num
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.

import java.util.Scanner;
class OddEven{
    int a[];
    int m;
    public OddEven(int num){
        m = num;
        a = new int[m];
    }
    public void fillArray(){
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < a.length; i++)
            a[i] = Integer.parseInt(in.nextLine());
    }
    public OddEven arrange(OddEven p, OddEven q){
        int index = 0;
        for(int i = 0; i < p.m; i++){
            if(p.a[i] % 2 == 1)
                this.a[index++] = p.a[i];
        }
        for(int i = 0; i < q.m; i++){
            if(q.a[i] % 2 == 1)
                this.a[index++] = q.a[i];
        }
        for(int i = 0; i < p.m; i++){
            if(p.a[i] % 2 == 0)
                this.a[index++] = p.a[i];
        }
        for(int i = 0; i < q.m; i++){
            if(q.a[i] % 2 == 0)
                this.a[index++] = q.a[i];
        }
        return this;
    }
    public void display(){
        for(int i = 0; i < a.length; i++)
            System.out.print(a[i] + " ");
        System.out.println();
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Size of array 1: ");
        int s1 = Integer.parseInt(in.nextLine());
        OddEven obj1 = new OddEven(s1);
        System.out.println("Enter array 1 elements:");
        obj1.fillArray();
        System.out.print("Size of array 2: ");
        int s2 = Integer.parseInt(in.nextLine());
        OddEven obj2 = new OddEven(s2);
        System.out.println("Enter array 2 elements:");
        obj2.fillArray();
        OddEven obj3 = new OddEven(s1 + s2);
        obj3 = obj3.arrange(obj1, obj2);
        System.out.print("Array 1 = ");
        obj1.display();
        System.out.print("Array 2 = ");
        obj2.display();
        System.out.print("Arranged array: ");
        obj3.display();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory