Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Question 6.

[10]
Design a class NumDude to check if a given number is a
Dudeney number or not. (A Dudency number is a positive
integer that is a perfect cube, such that the sum of its digits
isequal to the cube root of the number.)
Example: 5832 = (5 +8 +3 +2) = (18 - 5832
Some of the members of the class are given below:
Class name :Unique
Data memberlinstance variable:
num
: to store a positive integer
number
Methods/Member
NumDude( )
functions:
: default constructor to
the data member with initialise
initialvalue legal
SECTION B
Answer 6.
import java.util.Scanner;
publicclass NumDude
intnum;
public NumDude()
num-0;
public void input()

Scanner in =new 5canner(System.in);


System.out.print("Enter the number:");
num =in.nextInt):

public int sumDigits(int x)

if (x==0)
return 0;
else
return x%10 +sumDigits(x/10):

public void isDude)

int sdig=sumDigits(num);
if ((sdig*sdig*sdig) = num)
System.out.println("Dudency number "):
else
System.out.println("Not a Dudency number ");

public static void main(String args[) |


NumDudeob=new NumDude();
ob.input():
ob.isDude();
Each program should be written in such aa way that it
derty depicts the logic of the problem steyeise
This can be achieved by using comments in the
and mnemonic names or pseudo codes for algorithms,program
programs musttbe eritten in Java and| the
algorithms musm
be toriten in general / standard form, wherever required
specified.
(Flowcharts are not required)
Question 9.
Adouble ended queue is a linear data structure ohic
enables the user to add and remove integers from either ends
Le, from front or rear.
The details for the class de Queue are given below:
Class name :deQueue
Data members/instance variables:
;array to hold integer elements
lim maximum capacity of thedequeue
front : to point the index of the front end
rear
tepoint the index of the rear end
Methods/Member functions:
deQueuet int 1) : constructor to initialise lim = 1,
front-0 and rear =0
oid addFrontint v) :to add integers in the
dequeue
at the front end if possible
otherwise display the message
"OVERFLOW
FRONT"
FROM
Solved Papers 363
to add ntegers in the dequeue Methods/Member functions:
at the rear end if poss1ble, Supply...) parameterised constructor to
otherwise, displav the message Assign vulues of the data members
OVERFLOW FROM of both theclasses
REAR"
double calculation ) :returns the d1fterene betueen the
removes and returns the integers amountofdemand (rate xdemand)
from the front end of the dequeue and the amount produced (rate x
if any, clse returns - 999 produced)
ntpepRear) :remvoes and returns the integers oid displav ) :to display the details of the product
from the rear end of the dequeue if and the difterence in amount of
any, else returns - 999 demand and amount of supply by
vdshow() : displays the elements of the invoking the method calculation )
dequeue Assume that the super class Demand has been defned.
of inheritance. spccify the class
i Specifu the class deQueue giving details of the Using the conceptdetails
function void addFront(int) and int popFront( ) Supply givng the of the constructor(...), double
Assume that the other functions hate been defned. calculation() and void display{).
The main0 function and algorithm need NOT be The super class, main function and algorithm need
written. |4| NOT be writen.
ij) Diferentiate between a stack and a queue. |1| Question 11.
Question 10. IS) i) Alinked list is formed from the objects of the class
giten belou: 12}
super class Demand has been defined to store the details
A class Node
dthe demands for a product. Define a subclass Supply
ahuch contains the production and supply details of the doubel sal;
products. Node nert;
The details of the members of both the classes are gien
belou Write an Algorithm OR a Method to add a node at
Class nane :Demand the end of the an existing lnked list. The method
Data memnbers/instance variables: declaration is as follows:
:string to store the product ID void addNote(Node ptr, double ss)
prame :string to store the product name (ii) Ansuer the following questions from the diagram ofa
integer to store the quantity Binary Tree givn below:
pdemand
demanded for the product
Methods/Member functions:
parameterised constructor to
Demand...) assign talues of the data members
Dod display ) to display the details of the product
Class name :Supply
Data members/instance variables: (a) Write the pre-order traversal of the aboe tree
rroduced integer to store the quantity of the structure. [1]
productproduced (b) Name the parent of the nodes D andB. (1|
prate to store the cost per unit of the (c) State the level of nodes E anf F when the root is
prdouct in decimal at level O. (1]
Solved Papers369
Psenstr to
publcvoidpopFront() Only one pointer is Two pointers are used
publicvoddisplay) used for performing perform operations
(front-)&& (rear-=-)
operations
Systemout.prntln("Original sentence Empty condition is Empty condition is

sort(this):
System.out,println("Deque is empty"); checked using checked using
Top--1 Front=-1 ||
Systemout println( "Sorted sentence else if(front-rear)
Front=Rear+l
Full condition is
publicsatic void main(String args|]) Fullcondition is
System.out.println("The deleted element 1s : checked using checked using
Rear--Max-1
SortAlpha ob-new SortAIpha(); Qarrlfront|D: Top-Max-1
tront-1; There are no variants There are three types
ob.acceptsent(): of variants ie circular
available for stack
rear-1; queue, double-ended
ob d1splay0:
queue and priority
else if(tront-(size-1) queue
Can be considered as
SECTION C Can be considered as a
System.out.println("The deleted element is " vertical collection visual
a horizontal collection
Answer 9.
Qarrlfront|D; visual
() front-0: Used to solve the
Used to solve the
class deQueue
recursive type problems problem having
else sequential processing
int Qarrl): Answer 10.
int lim; class supply extends demand
System.out.printIn("The deleted element is :".
int front;
Qarrlfront|)D.
int rear; int pproduced,
Iront=front+1;
public deQueue(int ) double prate;
public supply(int pd, double pr, String prodid,
lim=l; String pn, int pdm)
front=-1; (i) Difference between stack and queue:
rear-1; Pproduced=pd;
Stack Queue prate-pr;
Qarrl]=new Qarr[l];
The stack is based on super(prodid.pn, pdm):
The queue is based on
LIFO(Last In First Out) FIFO(First In First Out)
public vod addFront(int v) principle principle. public double calculation ()
Insertion Operation is Insertion Operation
if(front-0 && ear=lim-1) | (front-rear-l called Push Operation is called Enqueue return ((prate*super.pdemand) (prate*
System.out.println("Overflow from tront Operation pproduced));
else if(front-1) && (rear=1) Deletion Operation is Deletion Operation
Ifront-rear-0; called Pop Operation is called Dequeue public void display )
Qarr[front]=v; Operation
Push and Pop Operation Enqueue and Dequeue
takes place from one Operation takes place
System.out.println("Product ID", super.pid);
else if (front--0) end of the stack from a different end of
System.out.println("Product Name :" super.
pname);
| front=lim-1; the queue
Qar[front|=v; Ihe most accessible The insertion end is System.out.println("Product demand" super.
pdemand);
element is called Top called Rear End and the
else
and the least
is called the accessible deletion end is called
System.out.println("Product
Pproduced):
Produced
the stack Bottom of the Front End.

front=front-1; Simple Implementation Complex


System.out.printin("Product Rate:".prate),
double diff- calculation( );
Qarr[front}=v; implementation in System.out.printin("Demand -Supply ", diff);
Comparison to stack
: checks Wner
void isDude( )
number is a Dudeney nunber
Dudeney number
function
Methods/Member fun
by invokingandthedisplays ShortAlpha )
sumDigitst ) and displays the
the
result with an appropriate
message.
NumDude giving details of the constructor void acceptsent( )
Specifythe class void is
(0), void inputl )) intsumDigits(int)an and object and call vo1d short( SortAlpha F
Dude(). Define a main() function to create
the functions accordingly to enable the task.
Question 7. [10]
Aclass Trans is defined to find the transpose of a square void displayl )
matrix. A transpose ofa matrix is obtained by interchanging
the elements of the rows and columns.
Example: Ifsize of the matrix =3, then Specify the class SortA
ORIGINAL TRANSPOSE ), void acceptsent )
11 5 7 11 8 1 display( ). Define a ma
callthe functions accordt
8 13 5 13 6
1 6 20 7 9 20 Answer
Some of the members of the class are given below: Each program shoulo
Class name : Trans clearly depicts the
Data members/instance variables: This can be achieved b
arrl ][] and mnemonic names O
: to store integers in the matrix prograns must be writte
: integer to store the size of the be written in general / s
matrix
Methods/Member functions: (Flowchar
Trans(int mm) : parameterised constructor to
initialise the data member m = Question 9.
mm
Adouble ended queue
enables the user to add aI
void fillarray( ) : to enter integer elements in the i.e., from front or rear.
matrix
The details for the class d
void transpose( ) : to create the transpose of the Class
given matrix name
void display( ) : Data members/instance
displays the original matrix
and the transposed matrix Qrrl]
by invoking the method lim
transpose( ) front
Specify the class Trans giving details of the constructor rear :t
). void fillarray( ), void transpose( )and void
). Define a main (0 function to create an object anddisplay
call the Methods/Member functa
functions accordingly to enable the task. deQueue(int1)
Question 8. [10]
Solved Papers 367
public void input()

Scanner in =new Scanner(System.in): arr[i]li]=s.nextInt():


System.out.print("Enter the number: "): System.out.print(" ");
num = in.nextInt();

nublic int sumDigits(int x)


publicvoid transpose()
if (x-0) for(int i-0; i< m; itt)
return 0;
else
for(int j -0; j< m; j++)
return x%10 +sumDigits(x/10);
arrT[i][i]-arrlj]lil;
public void isDude()

int sdig=sumDigits(num);
if ((sdig*sdig*sdig) num) public void display()
System.out.printin("Dudency number ");
else int i,j;
System.out.printin("Not aDudency number "): System.out.println("Before Transpose:");
for(i-0; i <m; it+)
public static void main(String args[]) |
NumDudeob=new NumDude(); for(j-0; j <m; jt+)
ob.input):
ob.isDude(); System.out.print(arr[ili]" ");

System.out.println(" "):
Answer 7.
transpose();
import java.util.Scanner; System.out.println("After Transpose:");
public class Trans for(i -0; i<m; itt)

int arr[|[; for(j -0; j <m; j+)


int arrT|]:
staticint m; System.out.print(arrT|i]li]" "):
public Trans(int mm)
System.out.println( ");
m=mm;
arr=new int[m][m);
arrT=new int[m][m]; public static void main(String args|)

public void fillarray) Scanners =new Scanner(System.in):


System.out.println("Enter number of rows/cols :");
int i, j. m=s.nextlnt();
System.out.println("Enter elements :"); Trans ob-new Trans(m);
Scanner s =new Scanner(System.in); ob.fillarray ():
for(i =0; i <m; itt) ob.display(0:

tor(j=0; j<m;j++)

You might also like