Wish you all a happy 2016 . In this section the Java topics from Class XII will be covered . So questions based on concepts of inheritance , string , binary tree will be framed in this section . Here goes an example of a Group C section .
Question 11
In a computer game, a vertical column and a pile of rings are displayed. The objective of the game is
to pile up rings on the column till it is full. It can hold 10 rings at the most. Once the column is full,
the rings have to be removed from the top till the column is empty and the game is over. Define the
class RingGame with the following details:
Class Name : RingGame
Data members
ring[] : array to hold rings (integer)
max : integer to hold maximum capacity
of
ring array
upper : integer to point to the uppermost
element
Member functions
RingGame(int m) : constructor to initialize, max = m &
upper to -1.
void jump-in(int) : adds a ring to the top of the
column, if
possible, otherwise, displays a
message “Column full. Start
removing rings”.
void jump-out() : removes the ring from the top, if
column is not empty, otherwise
outputs a message,
“Congratulations the game is over “.
Specify the class RingGame giving details of the constructor and functions void jump-in(int) and void
jump-out(). Also define the main function to create an object and call methods accordingly to enable
the task.
[ 10 ]
import java.util.*;
import java.lang.*;
import java.io.*;
class RingGame
{
int max;
int[] ring;
int upper;
RingGame(int m)
{
max = m;
ring = new int[m];
upper=-1;
}
void jumpin(int i)
{
if (upper==max-1) // -1 because the indices are 0 1 2 for 3 elements . upper indicates the // index so it starts from 0 . index==max will throw array out of bounds // exception .
{
System.out.println("Column full ! Start removing rings.");
}
else
{
ring[++upper] = i; // pre incremetal operator ++upper used so that the first element is // inserted at index 0 .
}
}
public void jumpout()
{
if (upper == -1) // index of an array usually starts from 0 so -1 index indicates there is no // other element in this array.
{
System.out.println("Congratulations ! the game is over .");
}
else
{
ring[upper]=ring[upper--]; // element last inserted is popped out
}
}
public static void main (String[] args)
{
RingGame obj = new RingGame(3);
obj.jumpin(5);
obj.jumpin(2);
obj.jumpin(1);
obj.jumpin(7); // will not be inserted
obj.jumpout();
obj.jumpout();
obj.jumpout();
obj.jumpout(); // game is over
}
}
Question 12.
A class Author contains details of the author and another class BookList contains details of the books
written by him. The details of the 2 classes are given below:
Class name : Author
Data members
authorno : stores the author’s number
name : stores the author’s name
Member functions
Author() : default constructot
Author(…..) : parameterized constructor to assign
values to author number and name.
void show() : to display the author’s details.
Class name : Booklist
Data members
bookno : long type variable to store the book
number.
bookname : stores the book name
price : float variable to store price
edition : integer type variable to store the
edition number
Member functions
Booklist(…..) : parameterized constructor to assign
values to data members of both the
classes.
void show() : to display all the details.
6
Specify the class Author giving details of the constructors and member function void show(). Using
the concept of inheritance, specify the class Booklist giving details of the constructor and the
member function void show(). Also define the main function to create an object and call methods
accordingly to enable the task.
[ 10 ]
import java.util.*;
import java.lang.*;
import java.io.*;
class Author
{
int authorno;
String name;
public Author()
{
// If you donot define this constructor , null value will be assigned to name and author //number will be 0 by default.
}
public Author(int num, String name)
{
authorno = num;
this.name = name;
}
public void show()
{
System.out.println("The Author's Details are as follows");
System.out.println("Author Name - " + name + " & Author Number is " + authorno);
}
}
class BookList extends Author
{
long bookno;
String bookname;
float price;
int edition;
public BookList(long no, String name, float price, int ed)
{
bookno = no;
bookname = name;
this.price=price;
edition = ed;
}
public void show()
{
System.out.println("The Details of the Book is as follows");
System.out.println("Book No. - " + bookno + " Book Name : " + bookname + " Price is " + price + " INR Edition No. " + edition);
}
public static void main(String[] args)
{
Author a; // a is used as a reference here
a= new Author(1,"Dan Brown");
a.show(); // displays the method of the parent class
a = new BookList(13324, "Digital Fortress", 599, 1); // a is referenced to the child class BookList
a.show(); // displays the method of the child class
}
}
Question 13.
a) A Linked List is formed from the objects of the class,
class Node
{
intnum;
Node next;
}
Write an algorithm of a method for inserting a node in the end of a list. The method
declaration is given below:
voidinsertnode(Node start) [ 4 ]
Sol.
void insertnode(Node start) {
if (start == null)
return;
else {
start.next = null;
if (head == null) {
head = start;
tail = start;
} else {
tail.next = start;
tail = start;
}
}
}
b) State the complexity of the following algorithms:
i) Linear Search
ii) Binary search
iii) Selection sort
[ 3 ]
Sol.
i) O(n)
ii) O(log n)
iii) O(n2)
c) List the nodes in the tree given below using :
i) Preorder Traversal
ii) Postorder Traversal
iii) Inorder Traversal
Sol.
Preorder Traversal - FGIJKLHPMON
Postorder Traversal - IKLJGONMPHF
Inorder Traversal - IGKJLFPHOMN
(Note : If you like to save time it is best to attempt the last 2 questions in this sample paper. The inheritence question hardly has anything to code and the last question will take less than 5 min to answer provided you know the linked list algorithm)
Question 11
In a computer game, a vertical column and a pile of rings are displayed. The objective of the game is
to pile up rings on the column till it is full. It can hold 10 rings at the most. Once the column is full,
the rings have to be removed from the top till the column is empty and the game is over. Define the
class RingGame with the following details:
Class Name : RingGame
Data members
ring[] : array to hold rings (integer)
max : integer to hold maximum capacity
of
ring array
upper : integer to point to the uppermost
element
Member functions
RingGame(int m) : constructor to initialize, max = m &
upper to -1.
void jump-in(int) : adds a ring to the top of the
column, if
possible, otherwise, displays a
message “Column full. Start
removing rings”.
void jump-out() : removes the ring from the top, if
column is not empty, otherwise
outputs a message,
“Congratulations the game is over “.
Specify the class RingGame giving details of the constructor and functions void jump-in(int) and void
jump-out(). Also define the main function to create an object and call methods accordingly to enable
the task.
[ 10 ]
import java.util.*;
import java.lang.*;
import java.io.*;
class RingGame
{
int max;
int[] ring;
int upper;
RingGame(int m)
{
max = m;
ring = new int[m];
upper=-1;
}
void jumpin(int i)
{
if (upper==max-1) // -1 because the indices are 0 1 2 for 3 elements . upper indicates the // index so it starts from 0 . index==max will throw array out of bounds // exception .
{
System.out.println("Column full ! Start removing rings.");
}
else
{
ring[++upper] = i; // pre incremetal operator ++upper used so that the first element is // inserted at index 0 .
}
}
public void jumpout()
{
if (upper == -1) // index of an array usually starts from 0 so -1 index indicates there is no // other element in this array.
{
System.out.println("Congratulations ! the game is over .");
}
else
{
ring[upper]=ring[upper--]; // element last inserted is popped out
}
}
public static void main (String[] args)
{
RingGame obj = new RingGame(3);
obj.jumpin(5);
obj.jumpin(2);
obj.jumpin(1);
obj.jumpin(7); // will not be inserted
obj.jumpout();
obj.jumpout();
obj.jumpout();
obj.jumpout(); // game is over
}
}
Question 12.
A class Author contains details of the author and another class BookList contains details of the books
written by him. The details of the 2 classes are given below:
Class name : Author
Data members
authorno : stores the author’s number
name : stores the author’s name
Member functions
Author() : default constructot
Author(…..) : parameterized constructor to assign
values to author number and name.
void show() : to display the author’s details.
Class name : Booklist
Data members
bookno : long type variable to store the book
number.
bookname : stores the book name
price : float variable to store price
edition : integer type variable to store the
edition number
Member functions
Booklist(…..) : parameterized constructor to assign
values to data members of both the
classes.
void show() : to display all the details.
6
Specify the class Author giving details of the constructors and member function void show(). Using
the concept of inheritance, specify the class Booklist giving details of the constructor and the
member function void show(). Also define the main function to create an object and call methods
accordingly to enable the task.
[ 10 ]
import java.util.*;
import java.lang.*;
import java.io.*;
class Author
{
int authorno;
String name;
public Author()
{
// If you donot define this constructor , null value will be assigned to name and author //number will be 0 by default.
}
public Author(int num, String name)
{
authorno = num;
this.name = name;
}
public void show()
{
System.out.println("The Author's Details are as follows");
System.out.println("Author Name - " + name + " & Author Number is " + authorno);
}
}
class BookList extends Author
{
long bookno;
String bookname;
float price;
int edition;
public BookList(long no, String name, float price, int ed)
{
bookno = no;
bookname = name;
this.price=price;
edition = ed;
}
public void show()
{
System.out.println("The Details of the Book is as follows");
System.out.println("Book No. - " + bookno + " Book Name : " + bookname + " Price is " + price + " INR Edition No. " + edition);
}
public static void main(String[] args)
{
Author a; // a is used as a reference here
a= new Author(1,"Dan Brown");
a.show(); // displays the method of the parent class
a = new BookList(13324, "Digital Fortress", 599, 1); // a is referenced to the child class BookList
a.show(); // displays the method of the child class
}
}
Output is as follows
=================
The Author's Details are as follows
Author Name - Dan Brown & Author Number is 1
The Details of the Book is as follows
Book No. - 13324 Book Name : Digital Fortress Price is 599 INR Edition No. 1
Question 13.
a) A Linked List is formed from the objects of the class,
class Node
{
intnum;
Node next;
}
Write an algorithm of a method for inserting a node in the end of a list. The method
declaration is given below:
voidinsertnode(Node start) [ 4 ]
Sol.
void insertnode(Node start) {
if (start == null)
return;
else {
start.next = null;
if (head == null) {
head = start;
tail = start;
} else {
tail.next = start;
tail = start;
}
}
}
b) State the complexity of the following algorithms:
i) Linear Search
ii) Binary search
iii) Selection sort
[ 3 ]
Sol.
i) O(n)
ii) O(log n)
iii) O(n2)
c) List the nodes in the tree given below using :
i) Preorder Traversal
ii) Postorder Traversal
iii) Inorder Traversal
Sol.
Preorder Traversal - FGIJKLHPMON
Postorder Traversal - IKLJGONMPHF
Inorder Traversal - IGKJLFPHOMN
(Note : If you like to save time it is best to attempt the last 2 questions in this sample paper. The inheritence question hardly has anything to code and the last question will take less than 5 min to answer provided you know the linked list algorithm)
Preorder Traversal - FGIJKLHPMONNHL 23 coins
ReplyDeletePostorder Traversal - IKLJGONMPHF