Anda di halaman 1dari 9

SECTION – B

Answer any two questions.

Each program should be written in such a way that it clearly depicts the logic of the problem.
This can be achieved by using mnemonic names and comments in the program.
(Flowcharts and Algorithms are not required.)

The programs must be written in Java.

Question 7

A class Composite contains a two dimensional array of order [m x n]. The maximum value [10]
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:

: Composite
Class name

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.
Question 8

Design a class Sort which enables a word to be arranged in alphabetical order. The details of [10]
the members of the class are given below :

Class name : Sort

Data members/instance variables:

Str : stores a word

Len : to store the length of the word

Methods/Member functions:

Sort( ) : default constructor

void readword( ) : to accept the word

void arrange ( ) : to arrange the word in alphabetical order using any


standard sorting technique.
void display( ) : displays the original word along with the sorted word

Specify the class Sort giving details of the constructor, void readword( ), void arrange( )
and void display( ). Define the main( ) function to create an object and call the functions
accordingly to enable the task
Question 9

A Special number is a number in which the sum of the factorial of its digits is equal to the [10]
number.
Example: 145 ( 1! + 4! + 5! = 145 ). Thus, 145 is a Special number.
Design a class Special to check if the given number is a Special number or not. Some of the
members of the class are given below:

Class name : Special

Data member/instance variable:

n : integer to store number

Member functions/methods:

Special( ) : default constructor

void read( ) : to accept the number


int factorial(int x) : return the factorial of a number using recursive
technique
boolean isSpecial( ) : checks for the special number by invoking the
function factorial( ) and returns true if Special,
otherwise returns false
void display( ) : displays the result with an appropriate message

Specify the class Special, giving details of the Constructor, void read( ), int factorial(int),
boolean isSpecial( ) and void display( ). Define the main() function to create an object and
call the member function according to enable the task.
Solution 7

import java.util.Scanner;
class Composite
{
int m;
int n;
int arr[][];

Composite(int mm,int nn)


{
m=mm;
n=nn;
arr=new int[m][n];
}

int isComposite(int p)
{
int i,count=0,num=p;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
count++;
}
}
if(count>2)
return 1;
else
return 0;
}

void fill()
{
int row,column,numbers=0,check=0;
for(column=0;column<n;column++)
{
for(row=0;row<m;row++)
{
check=0;
while(check!=1)
{
check=isComposite(numbers);
numbers++;
}
numbers--;
arr[row][column]=numbers;
numbers++;
}
}
}

void display()
{
int dispr,dispc;
for(dispr=0;dispr<m;dispr++)
{
for(dispc=0;dispc<n;dispc++)
{
System.out.print(arr[dispr][dispc]+ " ");
}
System.out.println();
}
}

public static void main()


{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows=in.nextInt();
System.out.println("Enter the number of columns");
int columns=in.nextInt();
if(rows<=20&&columns<=20)
{
Composite obj=new Composite(rows,columns);
obj.fill();
obj.display();
}
else
{
System.out.println("Out of range");
}

}
}

Question 8

import java.util.Scanner;
class Sort
{
String str,sorted;
int len;

Sort()
{
str="";
len=0;
sorted="";
}

void readword()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter word");
str=in.next();
len=str.length();
}

void arrange()
{
char letter=' ';
int i,pos,charnum=0;
for(i=65;i<=90;i++)
{
for(pos=0;pos<len;pos++)
{
letter=str.charAt(pos);
charnum=(int)letter;
if((charnum==i)||(charnum==32+i))
{
sorted=sorted+letter;
}
}
}
}

void display()
{
System.out.println("Original word : "+str);
System.out.println("Sorted word : "+sorted);
}

public static void main()


{
Sort srt=new Sort();
srt.readword();
srt.arrange();
srt.display();
}
}

Question 9

import java.util.Scanner;
class Special
{
int n,fact;

Special()
{
n=0;
fact=0;
}

void read()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number");
n=in.nextInt();
}

int factorial(int x)
{
if(x==1)
return 1;
else
fact=factorial(x-1)*x;
return fact;
}

boolean isSpecial()
{
int rem,sum=0,ncopy=n;
while(ncopy>0)
{
rem=ncopy%10;
sum=sum+factorial(rem);
ncopy=ncopy/10;
}
if(sum==n)
return true;
else
return false;
}

void display()
{
boolean check=isSpecial();
if(check==true)
System.out.println("Special number");
else
System.out.println("Not special number");
}

public static void main()


{
Special spc=new Special();
spc.read();
spc.display();
}
}