0% found this document useful (0 votes)
18 views

Adobe Scan Dec 06, 2023

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Adobe Scan Dec 06, 2023

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

lass can

non-abstract methods.contain non


Al methodsabstract can'textend an
of an methods whereas an abstract dass
interface
Interfaces
implemented
provide a form of multiple
A class can
externd inheritance
must
any
be
abst intertace
ract . art
we
GKtA
methods and constants with no only one other class. number of
em 19. implementation, Aprotected parts, static implementation.
(Program 12.7) class Employee containsmethods, etc.
Int
Abstracterfaces interíaces
are limited can ta
classes can have to ptk.
employee's net salary. The details of the two employee details and another
classes are given below class partia
Class name
Employee
: Salary calcuiats te
Data mnembers
empNo
empName
stores the employee number
stores the employee name
empDesig stores the employee's
Member functions : desgination
Employee( ) default constructor
Employee(...) Parameterised
employee constructor assign vahues to
to
void display( ) number, name and desgination.
display the employee details
Class name
Data member
Salary
basic float variable to store the basic pay
Member functions
Salary(...) parameterized constructor to assign values to
data members.
void calculate(0 : calculate the employee's net salary according
to following rules :
DA =10% of basic
HRA = 15% of basic
Salary = basic+ DA + HRA
PF= 8% of Salary
Net salary = Salary - PF
Display the employee details and the Net salary.
Specify the class Employee giving details of the constructors and member function void disp
Using the concept of Inheritance specify the class Salary giving details of constructor
member function void calculate( ). The main function needs to be written.
Concept of Inheritance
589
Solution.
class Employee {
int empNo ;
String empName ;
String empDesig :
public Employee( ) {
empNo 0;

nublic Employee( int eno, String ename,


empNo eno ; String edesig ){
empName = ename ;
empDesig - edesig :

public void display( ) {


System.out.println("Employee No:"+ empNo) :
System.out.println("Employee Name :"+ empName):
System.out.printin("Employee Designation :"+ empDesig) :

class Salary extends Employee (


float basic;
public Salary(int eno, String enm, String edg, float bas) {
super(eno, enm, edg):
basic bas ;
}
public void calculate() {
float salary, netsalary:
float DA, HRA, PF:
DA basic 0.10f:
HRA basic * 0.15f;
PF - basic 0.08f ;
salary basic + DA + HRA ;
netsalary - salary - PF :
super.display() :
System.out.println("Net Salary : " + netsalary ) :

public class Test {


public static void main(Stringl] args) {
Salary s1 = new Salary(234, "Reva", "Mgr", 13000) ;
s1.calculate() :
Doid of 2]C
Specify
inheritance,
display( display(
overtime(
void ) double )functions:
Wages(....) Member
the wage rate hrs Data
Data below
givCn :
class Members display( )functions:
voidWorker(
....Member
) BasicName (Program
Wages
). Members
The
specify to
Worker
main( / compute 12.9)
instance
the /
giving instance
) thesuper A
unction class variables
details monthly
variables class
Wages
need of : Worker
wages
giving theovertime :
not Calculates
. with Calculates
(hours*rate) instance
Parameterised storesstores stores for has
nstructor() variables
instance
display
Parameterised to to
be
details the
the been
store store
ritten. the rate the worker.
other
amount variables defined
overall the the the
of the and per hours
structor( ),and details workers basic name
The to
wage constructor
returns hour
+ worked constructor detailsstore
void Basic of wage
pay of the
using both I
display(). Wages details the specificationsdetails
pay the of in Worke
the to the decimalsworker
double and the overtime assign to of
classes worker assign a
formula
displays worker.
of
values values both
amount Define
the
overtimel Usingthe
was a
conoept to to subdass
and ) the the
Concept of Inheritance 593
dass Worker
String Name:
double Basic;
Worter (String nm, double bas)
Name nm:
Basic - bas :

void display(){
System.out.printin("NAME : "+Name) :
System.out.printin("BASIC : "+Basic) :

public class Wages extends Worker


int hrs :
double rate :
double wage ;
Wages(String nm, double bas, int hr, double rt){
super(nm, bas) :
hrs hr :
rate -rt:

double overtime () {
return hrs "rate :

void display() {
super.display() :
wage overtime() +8asic ; :
System,out. printin("Hoursworked : "+hrs)
System.out.printtn("Rate: '+rate) :
System.out.pnnttn("Wage : mage) :

m 22 (Progran 12.10) A library isshesbwks u vghtulbe ub 4 chdr ge vn the cost prce of the bok ner
bwk can etuined fur days witheut ay rine tf the book is
day. As per the rules of the library, a
returned after 7 davs, a fine will also be huár ged jur the ekces duy > er the chart gien bel.
(ISC 2009)
COMPUTER SCIENCE-XIl (1SC)
600
lEMLz1, 2 v2 z2 be the oordinates of the two points, then the
plane is given by the
formula
V(12-x1)' +(y2- yl)?
distance between he
and the distane between the points in space is given by the
2-l)' +(y2 -y1l)' + (z2 -z1)?
formul,
poinS
the two
Saah the class D2Point giving the details of
distance2d(D2Point b). Using the concept of inheritance constructors
specify and
details ot the two constructors and function
wite the main function.
double the class D3P
function
distance3d(D3Point b). You Roit donble
do n¡tgvIng the
30. Adas Personal contains employee details andanother class Retire calculates
the need ts
Fund and Gratuity. The details of the two classes are given below :

Class name Persondl


employee Providey
s

Data Members :
Name stores the employee name
Pan stores the employee PAN number
basic_pay stores the employee basic salary (in decimals)
acc_no stores the employee bank account number
Member functions :
Personal( ...) parameterized constructor to assign value to data
members

void display( ) to display the employee details


Cass name Rete
Data Members :
Yrs stores the employee years of service
Pf stores the employee provident fund amount (in
decimals)
Grat stores the employee gratuity amount (in decimals)
Member functions :
Retire(...) parameterized constructor to assign value to data
members of both the classes.
void provident( ) calculates the PF as (2% of the basic pay) *years of
service.
void gratuity( ) calculates the gratuity as 12 months salary, if the years
of service is more than or equal to 10 years else the
gratuity amount is nil.
void display1() Displays the employee details along with the PF
(Provident Fund) and gratuity amount.
displayl.
functions void andthe
Specify the class Personal giving details of the constructor and member
of constructor,
details
Using the concept of inheritance, specify the class Retire giving The mainfunction
member functions void provident(), void gratuity() and the void display1().(ISC2010Specimen
need not be written.
Simple Data Structures
705
Poblem199. (Program 14.13) NICinstitute's resource manager has
like printer, storage media, etc. so that minimum decided to network the computer resources
resources and maximum sharing could be availed.
Accordinglypritners are linked to a centralized system and the printing jobs are done on a 'first cum
frst served' basis only. This is like the first person's
printing job will get done first and the next
person's job will be done as the next job in the list and so on. In order to avoid
is that no more than 20 printing jobs can be added. collision, the restriction
Define the class Printlob with the folloving details :
Class name
Printjob
Data members/instance variables
Jobl ] array of integers to hold the printing jobs
Newjob To add a new printing job into the array
Capacity The maximum capacity of the integer array
Front Topoint to the index of the front
Rear To point to the index of the last
Member functions:
Printjob() Constructor to initialize the data members
Capacity =20, Front = Rear =-land call the
function create Job()
void createJob( ) : To create an array to hold the printing jobs.
void addJob( ) : Adds the new printing job to the end of the last
printing job, if possible, otherwise displays the
message "Printjob is full", cannot add any more.
void removeJob( ) : Removes the printing job from the front if the
printing job is not empty, otherwise displays the
message "Printjob is empty".
(a) Specify the class Printjob giving details of the constructor and the function void addJob( ).
void create<ob() and void remove<ob() only. You do not need to write the main function.
(b) What is the common name of the entity described above ?
() State one of its applications. (ISC2008)

(a) import java.util.Scanner;


public class Printjob {
protected int job[ ]; /array to hold job
protected int front, rear ; /to point to first &last element in the job
protected int Capacity;
protected int Newjob;
public Printjob() {
Capacity - 20;
front =-1;
rear --1;
createJob():
private void createJob() {
job - new int[Capacity]:
706
COMPUTER SCIENCE-XII (1SC)
boolean isEmpty( ) {
return front -1;
}
public void addJob () {
int id 0;
System.out.printin("Enter
Scanner in = new
the job id of new print job:");:
try { Scanner(System.in):
id in.nextInt():

catch(Exception e) {
}
System.out.println(e):
Newjob - id;
if (rear -1)(
front rear 0;
job[rear] - id;
else if( rear+1 < job. length)
job[++rear] - id;
}
public void removeJob () {
int elem;
if( isEmpty( ) )
System.out.println("Printjob is empty");
else {
elem - job[front] :
if ( front = rear)
front = rear = -1;
else
front ++;
System.out.println("The print job removed is :" + elem) :

}
(0) The common name of the entity described is : Print Seroer.
Simple Data Structures 707
Proble.n21. (Program 14.14) Define a class Repeat which alloUs the user to add elements from one end (rear)
and remove clements from the other end (front) onty. (1SC2010)
The following details of the class Repeat are given below :
Class name Repeat
Data Menmbers / instance variables:
st[ ] an array to hold a maximum of 100 integer
elements
cap stores the capacity of the array
to point the index of the front
to point the index of the rear
Member functions :
Repeat(int m) constructor to initialize the data members cap = m,
f=-1r=-l and to create the integer array.
void pushvalue(int v) to add integers from the rear index if possible else
display the message("OVERFLOW")
int popvalue( ) to remove and return element from the front. If
array is empty then return-9999.
void disp() Displays the elements present in the list
Specify the class Repeat giving details of the constructor(int), member function void
pushvaluetint), int popvalue( )and void disp(). The main( )function need not be written.
Solution. import java.util.Scanner;
public class Repeat {
protected int str[ 1; /array to hold values
protected int f, r ; I/ to point to first &last element in the job
protected int cap;
public Repeat( int m) {
cap = m;
f= -1;
r= -1;
str = new int[cap]:

boolean isEmpty( )
return f == -1;

public void pushvalue()


int id 0;
System.out.println("Enter integer :");
Scanner in = new Scanner(System.in):
try {
id in.nextInt();
}
catch(Exception e) {
System.out.println(e):
COMPUTER SCIENCE-XIl (1SC)
708
if (r-1 ){
f 0; r = 0;
str[r] id;

else if( r + 1< str.length)


str[++r] id;
else
System.out. println("0VERFLOW!!"):
}
public int popvalue (0{
int elem;
if( isEmpty( ))
return -9999;
else { So
elem str[f] ;
if (f )
f=r-1;
else
f+;
return elem ;

public void display() {


if (isEmpty(0)
System.out.println("No Elements.");:
else {
for (int i f; i-r; it)
System.out. print(str[i]+" ");
System.out.println():
System.out.println("Total Elements :"(r-f+ 1) ):
}
}
}
Problem 22. (Program 14.15) Chain is an entity which can hold at the most 50 integers. enablesthe
Thechain withth
user to add and remove integers classChain
a 2010Specimenl
from both the ends i.e., front and rear. Definei (1SC
following details :
Class name Chain
DataMembers :
Data Members
elel the array to hold the integerelements.
cap stores the maximum capacity of the array.
front to point the index of the front.
rear to point the index of the rear.
Smplo Data Structures 709

Member funthons
Chainint max) constructor to initlalize the data cap max, front
rear - 0and to create the inteyer array.
vold puahfrontint v) to add integers from the front index if possible else
display the message("lull from Iront"),
int poptront() to remove the return elements from front. If array is
empty then return-999.
possible else
vold pushrear(int v) to add integers from the front Index lf
display the mesage("full from rear")
If the array
int poprear) to remove and return elements from rear.
Is empty then return-9.
the class Chain gving details of the constructor and member function vold pushfront(int),
Spify poprear( ). The main function need not be written.
Int popfront), vold pushreartlnt) and int

Solution. public class Chain (


protected int elel J: /array to hold values
job
protected int front, rear ; /to point to first &last element in the
protected int cap;
publicChain( int max)
cap - max;
front -1;
rear -1;
ele - new int[cap]:

boolean isEmpty( ) (
return front --1;

public void pushfront(int v)


if (front -1){
front 0; rear 0;
ele[front] V;

else if (front - 0)
System.out.println("Insertion from front end not possible."):
else // if ((rear-front + 1)< ele.length)
ele{ --front) v;

public void pushrear(int v) {


if ( rear 1){
front 0; rear 0;
ele[rear] v;

else if( rear + 1<ele.length)


cOMPUTER
SCIENCE-XII
(ISC)

System.out.println("oVERFLOW!!"):
-1;
-1:
ele[front): : =
rear
ele(rear]
rear)
rear
-rear) return
elemnt
;
-vele[++rear]
; popfront(){
int
public ++; :elemnt
frontreturn (){ = rear--
;
-999;
return
- return - front
-999;
isEmpty())
if( frontfront poprear)) - front
elemnt int isEmpty( elemnt
elemnt:
elemnt:
int ( ( else
if else else
{
if
{else int
else } public if(
}
}

710
Arrays And Strings
427
Iplement acass Fnyptiom nith the folloeing seratime
Class Name Encrvption
Data MembersTnstance Variablee
Meseage a string
EncMsg encrypted message
Methods
voidreadMessage) :reads the Message from user
String Rotl3(String s): encrypts the passed string using Rot13 logic
void Encrypt) invokes Rot13( ) to encrypt message and
store in EncMsg
void display) displays the encrypted message
Constructor method initialises data members
Method main) creates an object of Encryption and invokes
appropriate methods.
importjava.util.Scanner:
Solution class Encryption {
String Message :
String EncMsg :
Encryption() {
Message =
EncMsg=

void readMessage() {
Scanner in new Scanner(System.in) ;
:
System.out. println("Enter message to be encrypted :")
Message = in.nextLine() :

String Rot13(String s ) {
String emsg=;
for (int i - 0;i<s.length() ; i+) {
char c= s.charAt(i):
if (c >= 'a' && c< 'm)ct= 13 ;
C- 13 ;
else if (c >= n &&c< Z) c -
c+= 13;
else if (c>= 'A &8& c <= 'M)
-= 13 ;
else if (c >= 'A' && c c= Z) C
emsg = emsg + C

return emsg ;

void Encrypt() {
EncMsg Rot13(Message) :
428
The
output
}
produced
public
ssage
Bevtvany
Zrffntr Enter
Message
Original
Message EnterOptions enc.display() ; Encryption
enc.Encrypt()
Z enc.readMessage(): static
Buel:
message message
by
now now
Temina!
above void
enc
mes becomes
to Window
main(String[
; SCIENCE-XIl
COMPUTER
(|SC)
to program =
be be new
encrypted
:: encrypted
2: Encryption()
nal Bevtvany
is args) ]
:
: {
ge Zrffntr :
functionsDesign
10
(rogram
Subsets
are class a
that11.20)object.display() : ubic
girven object.recchange(0) : : Change
object.inputword()
System.out.println("Enter
Combination can static
The
below
= found be
C(n,k) Combination object void
: main(Sting)
in =
to k!(nk)! - giien a new
implement n!function Lharnged
Sting
Change()
set
ofn word args){
C
this elements. (n, "):
: :
k) :
formula. gis
The the newstr):
functionniunber
Some
of can of
the different
be
data computed
members (unordered)
from
and the K
formula
elenents -
member
:
COMPUTER SCIENCE-XIl (ISC)

546 Comblnatlon
/instance variables integer number
Data mCmbers
integer number Rocu

Memberfunctions : to initialize the data members n 0and k0 Cose.


Pro
members
Combination ()
to accept the value of the data pno
co
void read( ) return the factorial of number using recursion
int fact(int) technique
calculate the combination value
void compute( ) to show the result
void display( ) e..,..
giving details of theconstructor and member functionsoid
Specify the class Combination, display() with the main()function to create an ohiet
int fact(int), void compute() and void (ISC 2010 Speclmen)
member function according toenable the task.
call the
import java.util.Scanner ; 1. Wha
Solution.
class Combination {
int n,k ; 2. Wha
double Cmb ;
3.Wha
Combination() {
4. Wh
n =0;
k= 0;
Cmb = 0; 5. Wh

6, Is i
void read()
Scanner in = new Scanner(System.in) ; 7, W
n= in.nextInt() : 8. H
k= in.nextInt() :
9, G
int fact(int n) { 10,
if (n <= 1) return 1;
else return (n * fact(n -1) ):
void compute()
Cmb =(double)fact(n) / (double)fact(k) * fact (n - k) ):
void display() {
System.out.printin ("Combination of "*n+" -c- "+k+" is :"+ Cmb) :
public static void
main(String[] args ){
Combination object = new Combination() :
System.out.println("Entervalue of nand k:");
object.read() :
object.compute() :
object.display() :
SECTION - C
Answer any tw0 questions
Program/Algorithmshould be written in such a way that it pseudo codes
theEach
logic of the problem step wise. This can also achieved by using clearly depicts
(Flowcharts are not required)
The Programs to be written in Java,
The Algorithm must be written in general/standard foe

has heen defined to store the names and ranks of 50


11. Asnr ds Reond students. Define asub-cdass Rank
fnd thc hiçhest rank along with the name. The details of both classes
(101
Class ame Recoro
Data members /instance variables:
namel ] : to store the names of students
: to store the ranks of students
Member functions:
Record( ) : constructor to initialize data members
void readvalues( ) : to store the names and ranks
void display( ) displays the names and the corresponding ranks
Class. name Rank
Data members /instance variables :
index : integer to store the index of the topmost rank
Member functions :
Rank( ) constructor to invoke the base class constructor and to
initialize index =0
void highest () finds the index/location of the topmost rank and stores
it in index without sorting the array.
name
void display( ) displays the names and ranks along with the
having the topmost rank.

(xvii)
Specify the class Record giving details of the constructor(), votd readvalues( )and void
display ). Using the concept of inheritance, specify the class Rank giving details of
constructor( ), void highest()and void display).
NOT BE WRITTEN
THE MAIN() FUNCTION AND ALGORITHM NEED

Ans.
import java.util.Scanner ;
public class Record
{ String [ ]names ;
int [ ] rnk :
Record( ) {
names = new String 5] :
rnk = new int [5] :
}
void readvalues() {
Scanner kb = new Scanner(System.in) ;
for (int i =0;i<5; i+) {
System.out.println ("Name of student "+(i+1) :
names[i] =kb.next(0:
System.out.println("Rank of student "+(i+1)) :
rnk[i] = kb.nextInt() :

}
void display( ) {
for (int i = 0;i<5;i+) {
System.out.println ("Name "+ names[i] + " Rank :"+ rnk[i]) ;

}
class Rank extends Record
int index ;
Rank( ){
super( ) :
index = 0 ;

void highest( ){
int max= nk[o]: index 0;
for (int i =1 ;i<5; i+)
if (max < rnk[i])
index =i ;

}
void display( ) {
super.display( ):
System.out.println("Name of topmoster ranker is "+ names[index]) :
}

(xix)
12. Stack is a kind of data structure which can store elements wnth
the restriction that an element cou
be added or removed from the top only.
The details of the class Stack is given below :
Class nome Stack
Data Members / instance variables:
st[ ] the array to hold the names
size the maximum capaity of the string array
top the index of the topmost element of the stack
ctr to count the number of elements of the stack
Member functions:
Stack( ) default constructor
Stack(int cap) constructor to initialize size =cap and top -1
void pushname (String n) to push aname into the stack. If the stack is tull
display the message "OVERFLOw"
String popname( ) : removes a name from the top of the stack and
returns it. If the stack is empty, display the
message "UNDERFLOw"
Void display( ) display the elements of the stack.
(a) Specify the class Stack giving details of the constructor(int), void pushname(String n),
String popname( ) and void display ). 13. (4)
THE MAINOFUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
(b) Under what Principle does the aboe cntity uork? [21
Ans. public class Stack
{
String [] st ; W,
int size ;
The
int top ;
int ctr :
Stack( ) { b) Dis
size - 0 ; top = -1; ctr -0 ; I) Anst
Stack(int cap) {
size = cap ;
top = 1;
st =newString[cap]:
void pushname(String n) {
if (top -= st.length -1) {
System.out.println("0VERFLOW"):
else{
1/assetion: there is room for one more element
top++ ;
(x)
st[top] -n;
}

String popname( ){
String result
if (top -- -1) {
System.out. printin ("UNDERFLOW"):
else {
result st[top]:
top-- ;
}
return result :

void display( ){
System.out.println(st[top]+"<- top"):
for ( inti- top -1 ; i 0; i--){
System.out.println(st[i]) :

41
13. (a) Alinked list is formed from the objects of the class,
class Node
int info;
Node link;
Write an algorithm OR aMethod for deleting a node from a linked list.
The method declaration is given below :
void deletenode (Node start)
SECTION - C

Answer any two questions


Each Program/Algorithm should be written in such a way that it clearly depicts
the logic of the problem step wise. This can also be achieved by using pseudo codes.
(Flowcharts are not required)
The Programs must be written in Java.
The Algorithm must be written in general/standard form wherever required.

1. Link is an entity which can hold a maximum of 100integers. Link enables the user to add
elements from the rear end and remove integers from the front end of the entity.
Define a class Link with the following details :

Data members instance variables :


Ink[ ] entity to hold the integer elements
max : stores the maximum capacity of the entity
begin : to point to the index of the front end
end : to point to the index of the rear end
Member functions :
Link(int mm) : constructor to initialize max = mm, begin =0, end =0
void addlink(int v) : to add an element from the rear index if possible
otherwise display the message "OUT OF SIZE.."
int dellink( ) : to remove and return an elemernt from the front index,
if possible otherwise display the message "EMPTY.."
and return -99
void display( ) displays the elements of the entity
(a) Specify the class Link giving details of the constructor(int), void addlink(int),
int dellink( )and void display( ). [9]
THE MAIN FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
(6) What type of data structure is the above entity ? [1]

12. Asuper class Detail has been defined to store the details of a customer. Define a sub class
Bill to compute the monthly telephone charge of the customer as per the chart given
below : [10]
Number of calls Rate
1- 100 |Only rental charge
101 - 200 60 paisa per call + rental charge
201 -300 |80paisa per call + rental charge
Above 300 lrupee per call + rental charge
(vi)
The details ot vth the classes are given below:

Class name Detail


Data members / instance rariables:
name : to store the name of the customner
address : to store the address of the customer
telno : to store the phone number of the customer
rent : to store the monthly rental
charge
Member functions:
Detail(...) : parameterized constructor to assign values to data
members
void show( ) to display the details of the
customer
Cass name
Data members / instance variables :
n
to store the number of calls
amt
to store the amount to be paid by the
Member functions : customer
Bill(..) : parameterized constructor to assign values to data
members of both classes and to initialize amt = 0.0
void cal() calculates the monthly telephone charge as per the
chart given above
void show( ) displays the details of the customer and amount to be
paid
Specify the class Detail giving details of the constructor( ), and void show( ). Using the
Concept of inheritance, specify the class Bill giving details of the constructor( ), void cal)
and void show(0.
THE MAIN()FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.

3. (a) A linked list is formed from the objects of the class,


class node

int p
String n :

You might also like