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

Question 7

10 marks

Computer ScienceISC 2020 Specimen Computer Science Paper 1ProgramObjects

Write the program described below.

A class Composite contains a two dimensional array of order [m x n]. The maximum value possible for…

A class Composite contains a two dimensional array of order [m x n]. The maximum value possible for both ‘m’ and ‘n’ is 20. Design a class Composite to fill the array with the first (m x n) composite numbers in column wise. The details of the members of the class are given below: Class name : Composite Data members/instance variables: arr[ ] [ ] : stores the composite numbers column wise m : integer to store the number of rows n : integer to store the number of columns Member functions/methods: Composite(int mm, int nn) : to initialize the size of the matrix m=mm and n=nn int isComposite(int p) : returns 1 if number is composite otherwise returns 0 void fill() : to fill the elements of the array with the first (m × n) composite numbers in column wise void display() : displays the array in a matrix form Specify the class Composite giving details of the constructor(int,int), int isComposite(int), void fill() and void display(). Define a main() function to create an object and call the functions accordingly to enable the task.

Answer

Answer

import java.util.Scanner;

class Composite
{
    int arr[][];
    int m, n;

    Composite(int mm, int nn)
    {
        m = mm;
        n = nn;
        arr = new int[m][n];
    }

    int isComposite(int p)
    {
        if (p < 4)
            return 0;
        for (int i = 2; i <= p / 2; i++)
        {
            if (p % i == 0)
                return 1;
        }
        return 0;
    }

    void fill()
    {
        int num = 3;
        for (int j = 0; j < n; j++)
        {
            for (int i = 0; i < m; i++)
            {
                num++;
                while (isComposite(num) == 0)
                    num++;
                arr[i][j] = num;
            }
        }
    }

    void display()
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
                System.out.print(arr[i][j] + "\t");
            System.out.println();
        }
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows (max 20): ");
        int r = sc.nextInt();
        System.out.print("Enter the number of columns (max 20): ");
        int c = sc.nextInt();
        Composite ob = new Composite(r, c);
        ob.fill();
        ob.display();
    }
}
Explanation: The constructor stores the order of the matrix and creates the array. isComposite(p) returns 1 if p (4 or more) has a divisor between 2 and p/2, i.e. it has more than two factors, else 0. fill() goes through the array column by column (outer loop over columns, inner loop over rows) and stores the next composite number each time, searching upward from 4. display() prints the array in matrix form. Tested: for 3 rows and 4 columns the output is 4 9 14 18 6 10 15 20 8 12 16 21 which holds the first 12 composite numbers filled column-wise.