Code is copied!
A class Composite contains a two-dimensional array of order [m x n]. The maximum
values 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.
[Composite numbers are those which have more than two factors.]
The details of the members of the class are given below:
Class name :Composite
Data members/instance variables:
arr[ ] [ ] :integer array to store 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 ) : to return 1 if the 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( ) :to display 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 all the functions accordingly to enable the task.
Solution:
,,,
import java.util.*;
class Composite
{
int arr[][],m,n;
Composite(int mm,int nn)
{
m=mm;
n=nn;
arr=new int[m][n];
}
int isComposite(int p)
{
int count=0;
for(int i=1;i<=p;i++)
{
if(p%i==0)
{
count++;
}
}
if(count>2){
return 1;
}
else{
return 0;
}
}
void fill()
{
boolean check=false;int num=2;
for(int i=0;i< m;i++)
{
for(int j=0;j< n;j++)
{
while(!check)
{
int compos=isComposite(num);
if(compos==1)
check=true;
else
num+=1;
}
arr[j][i]=num;
check=false;
num+=1;
}
}
}
void display()
{
for(int i=0;i< m;i++)
{
for(int j=0;j< n;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public static void main(String []args)
{
Composite ob=new Composite(20,20);
ob.fill();
ob.display();
}}