0% found this document useful (0 votes)
14 views11 pages

Class 12 Computer Science Lab Assignments

The document outlines various programming assignments for a Class 12 Computer Science lab, focusing on concepts such as 2D arrays, recursion, inheritance, string manipulation, stacks, queues, linked lists, exception handling, and class-based programming. Each assignment includes specific tasks such as matrix operations, class definitions, and algorithm implementations. The document serves as a comprehensive guide for students to develop their programming skills through practical exercises.

Uploaded by

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

Class 12 Computer Science Lab Assignments

The document outlines various programming assignments for a Class 12 Computer Science lab, focusing on concepts such as 2D arrays, recursion, inheritance, string manipulation, stacks, queues, linked lists, exception handling, and class-based programming. Each assignment includes specific tasks such as matrix operations, class definitions, and algorithm implementations. The document serves as a comprehensive guide for students to develop their programming skills through practical exercises.

Uploaded by

Akakya Roy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ST.

LAWRENCE HIGH SCHOOL, 27 BALLYGUNGE CIRCULAR ROAD, KOLKATA-700019

CLASS 12 LAB ASSIGNMENT: COMPUTER SCIENCE

2D ARRAY
Write a program to declare a square matrix A[][] of order M M where ‘M’ is the
number of rows and the number of columns, such that M must be greater than 2 and less
than 10. Accept the value of M as user input. Display an appropriate message for an
invalid input. Allow the user to input integers into this matrix. Perform the following
tasks:
(a) Display the original matrix.
(b) Check if the given matrix is said to be symmetric or not. A square matrix is said to be
symmetric, if the element of the i“l row and j 1 column is equal to the element of the
jt' row and ith column.
(c) Find the sum of the elements of left diagonal and the sum of the elements of
right diagonal of the matrix and display them.

2. Write a program to declare a square matrix A[][] of order M M, where M is the


number of rows and columns, such that M must be greater than 2 and less than 10.
Accept the value of M as user input. Display an appropriate message for an invalid
input. Allow the user to input integers into this matrix. Perform the following tasks:
a. Display the original matrix.
b. Check if the matrix is a magic square. A magic square is a square matrix in which
the sum of every row, every column, and both diagonals are equal.
c. Find the frequency of a given element in the matrix. Prompt the user to input
an element and count how many times it appears in the matrix.
RECURSION
Some of the data members and member functions are given below:
Class name: LCM
Data members /instance variables:
n1: to store an integer number, n2: to store an integer number, large: integer to store the
largest from nl, n2
sm: integer to store the smallest from n1, n2
1: to store LCM of two numbers
Methods/Member functions:
LCM(): default constructor to initialize data members with legal initial values
void accept(): to accept n1 and n2
int getLCM(): returns the LCM of n1 and n2 using the recursive technique
void display(): to print the numbers n1, n2 and LCM
Specify the class LCM giving details of the constructor, void accept(), int getLCM()
and void display(). Define a main() function to create an object and call the member
functions accordingly to enable the task.
4. Design a class NumDude to check if a given number is a Dudeney number or not. A
Dudeney number is a positive integer that is a perfect cube, such that the sum of its
digits is equal to the cube root of the number.
Example: 5832 = (5 * 8 3 + 2)3 = 183 = 5832
Some of the members of the class are given below:
Class name: NumDude
Data member/instance variable: num: to store a positive integer number
Methods/Member functions:
NumDude(): default constructor to initialize the data member with legal initial value.
void input(): to accept a positive integer.
int sumDigits(int x): returns the sum of the digits of number ‘x’ using recursive
technique.
void isDude(): checks whether the given number is a Dudeney number by invoking the
function sumDigits() and display the result with an appropriate message.
Specify the class NumDude giving details of the constructor(), void input(), int
sumDigits(int) and void isDude(). Define a main() function to create an object and
call the functions accordingly to enable the task.
5. Design a class Revno which reverses an integer number.
Example: 94765 becomes 56749 on reversing the digits of the number.
Some of the members of the class are given below:
Class name: Revno
Data member/instance variable: num: to store the integer
Member functions/methods:
Revno(): default constructor
void inputnum(): to accept the number
int reverse(int nn): returns the reverse of a number by using recursive technique
void display(): displays the original number along with its reverse by invoking
the method reverse()
Specify the class Revno, giving details of the constructor, void inputnum(), int
reverse(int) and void display(). Define the main() function to create an object and call
the functions accordingly to enable the task.
INHERITANCE
6. A super class Demand has been defined to store the details of the demands for a
product. Define a subclass Supply which contains the production and supply details of
the products.
The details of the members of both the classes are given below:
Class name: Demand
Data members/instance variables:
pid: string to store the product ID
pname: string to store the product name
pdemand: integer to store the quantity demanded for the product
Methods/Member functions:
Demand(...): parameterized constructor to assign values to the data members
void display(): to display the details of the product
Class name: Supply
Data members/instance variables:
pproduced: integer to store the quantity of the product produced
prate: to store the cost per unit of the product in decimal
Methods/Member functions:
Supply(...): parameterized constructor to assign values to the data members of both the
classes
double calculation(): returns the difference between the amount of demand (rate
demand) and the amount produced (rate x produced)
void display(): to display the details of the product and the difference in amount of
demand and amount of supply by invoking the method calculation()
Assume that the super class Demand has been defined. Using the concept of inheritance,
specify the class Supply giving the details of the constructor(...), double calculation() and
void display().
The super class, main function and algorithm need not be written.
A library issues books on rental basis at a 2% charge on the cost price of the book per
day. As per the rules of the library, a book can be retained for 7 days without any fine.
If the book is returned after 7 days, a fine will also be charged for the excess days as
per the chart given below:
Number of excess days Fine per day (Rs.)

10
Above 10 5.00
A super class Library has been defined. Define a subclass Compute to calculate the
fine and the total amount. The details of the members of both the classes are given
below:
Class name: Library
Data members /instance variables:
name: to store the name of the book
author: to store the author of the book
p: to store the price of the book (in decimals)
Methods/Member functions:
Library(...): parameterized constructor to assign values to the data members
void show(): displays the book details
Class name: Compute
Data members/instance variables:
d: number of days taken in returning the book
I: to store the fine (in decimals)
Methods/Member functions:
Compute(...): parameterized constructor to assign values to the data members of both the
classes
void fine(): calculates the fine for the excess days as given in the table above
void show(): displays the book details along with the number of days, fine and the
total amount to be paid. Total amount is (2% of price of book * total no. of days) +
fine
Assume that the super class Library has been defined. Using the concept of
inheritance, specify the class Compute giving the details of the constructor, void
fine() and void show() functions.
The super class, main() function and algorithm need not be written.
8. A super class EmpSal has been defined to store the details of an employee. Define a
subclass Overtime to compute the total salary of the employee, after adding the
overtime amount based on the following criteria.
If hours are more than 40, then 1 5000 are added to salary as an overtime amount
If hours are between 30 and 40 (both inclusive), then 1 3000 are added to salary as an
overtime amount
If hours are less than 30, then the salary remains unchanged
The details of the members of both the classes are given below.
Class name: EmpSal
Data members/instance variables:
empnum: to store the name of the employee
empcode: integer to store the employee code
salary: to store the salary of the employee in decimal

Methods/Member functions:
EmpSal(...): parameterized constructor to assign values to data members
void show(): to display the details of the employee
Class name: Overtime
Data members/instance variables:
hours: integer to store overtime in
hours totsal: to store the total salary in
decimal Methods/Member functions:
Overtime(...): parameterized constructor to assign values to data members of both the
classes.
void calsal(): calculates the total salary by adding the overtime amount to salary as per
the criteria given above
void show(): to display the employee details along with the total salary (salary + overtime
amount)
Assume that the super class EmpSal has been defined. Using the concept of
inheritance, specify the class Overtime giving the details of the constructor(...), void
calsal() and void show().
The super class, main() function and algorithm need not be written.
STRING MANIPULATION
9. Write a program to accept a sentence which may be terminated by either ‘.’, “
or !’
only. The words may be separated by a single blank space and should be case-
sensitive. Perform the following tasks:
(a) Determine if the accepted sentence is a Pangram or not. A Pangram is a sentence
that contains every letter of the alphabet at least once. Example: “The quick brown fox
jumps over the lazy dog.”
(b) Display the first occurring longest and shortest word in the accepted sentence.

STACKS
10. A stack is a kind of data structure which can store elements with the restriction that
an element can be added or removed from the top end only.

The details of the class Stack are given below:


Class name: Stack
Data members/instance variables:
cha[]: array to hold the characters
size: stores the maximum capacity of the stack
top: to point the index of the topmost element of the stack
Member functions/methods:
Stack(int mm): constructor to initialize the data member size = mm, top = -1 and
create the character array
void pushpchar(char v): to add characters from the top end if possible else display the
message “Stack full”
char pop char(): to remove and return characters from the top end, if any, else returns ‘$
void display(): to display elements of the stack
Specify the class Stack, giving the details of void push_char(char) and char
pop_char(). Assume that the other functions have been defined.
The main() function and algorithm need not be written.

QUEUE
11. Queue is an entity which can hold a maximum of 100 integers. The queue enables
the user to add integers from the rear and remove integers from the front.
Define a class Queue with the following details:
Class name: Queue
Data members/instance variables:
Que[]: array to hold the integer elements
size: stores the size of the array
front: to point the index of the front
rear: to point the index of the rear
Member functions:
Queue(int mm): constructor to initialize the data size = mm, front = 0, rear = 0
void addele(int v): to add integer from the rear if possible else display the message
“Overflow”
int delete(): returns elements from front if present, otherwise displays the message
“Underflow” and returns -9999
void display(): displays the array elements
Specify the class Queue giving details of only the functions void addele(int) and int
delete(). Assume that the other functions have been defined.
The main() function and algorithm need not be written.
12. A double-ended queue is a linear data structure which enables the user to add
and remove integers from either ends i.e. from front or rear.
The details of the class DeQueue are given below:
Class name: DeQueue
Data members/instance variables:
qrr[]: array to hold integer elements
lim: maximum capacity of the dequeue
front: to point the index of the front end
rear: to point the index of the rear end
Methods/Member functions:
DeQueue(int l): constructor to initialize lim - 1, front = 0 and rear - 0
void addFront(int v): to add integers in the dequeue at the front end if possible, otherwise
display the message “OVERFLOW FROM FRONT”
void addRear(int v): to add integers in the dequeue at the rear end if possible, otherwise
display the message “OVERFLOW FROM REAR”
int popFront(): removes and returns the integers from the front end of the dequeue if any,
else returns -999
int popRear(): removes and returns the integers from the rear end of the dequeue if any,
else retufns -999
void show(): displays the elements of the dequeue
Specify the class DeQueue giving details of the functions void addFront(int) and int
popFront(). Assume that the other functions have been defined.
SINGLE LINKED LIST
13.A linked list is formed from the objects of the class Node. The class structure of the
Node is given below:
class Node

int n;
Node
link;

Write a pogram to search for a number from an existing linked list.


The method declaration is as follows: void FindNode( Node str, int b )
14. A linked list is formed from the objects of the
class class Node

int info;
Node
link;

Write a program for deleting a node from a linked list. The method declaration is given
below: void deleteNode (Node start)
15. A linked list is formed from the objects of the
class class Node

int number;
Node nextNode;

Write a program to add a node at the end of an existing linked list The method
declaration is as follows: void add node(Node start, int num)

EXCEPTION HANDLING AND FILE HANDLING


16. Write a Java program to simulate a simple division calculator with the
following requirements:
Input: Accept two integers (numerator and denominator) from the user.
Exceptions to Handle:
ArithmeticException: If the denominator is 0.
InputMismatchException: If the user enters non-integer values.
Output: Print the result of numerator / denominator if inputs are valid.
Catch exceptions and display appropriate messages:
”Error: Division by zero!” for ArithmeticException.
”Error: Invalid input! Please enter integers." for InputMismatchException.
Finally Block: Print "Operation completed.” regardless of success/failure.

17. WAP to input a string from text file using default delimiter to
StringTokenizer constructor. Display the frequency of vowels in each token.
Sample Input: Java Virtual Machine
Sample Output: Number of vowels in Java : 2
Number of vowels in Vinual: 3
Number of vowels in Machine: 3

CLASS & OBJECTS BASED PROGRAM


[Link] are two strings, input string and a mask string that remove all the characters of the
mask string from the original string.
Example:
INPUT:
ORIGINAL STRING: communication
MASK STRING: mont
OUTPUT: cuicai

A class StringOp is defined as follows to perform above operation.

Some of the members of the class are given below:


Class name: StringOp
Data members/instance variables:
str: to store the original string
msk: to store the mask string
nstr: to store the resultant string
Methods/Member functions:
StringOp(): default constructor to initialize the data members with legal initial values

void accept(): to accept the original string str and the mask string msk in lowercase
void form(): to form the new string nstr after removal of characters present in mask string
from the original string.
void display(): to display the original string nstr and the newly formed string nstr

Specify the class StringOp giving details of the constructor, void accept(), void form()
and void display(). Define a main() function to create an object and call all the
functions accordingly to enable the task.

19. Hamming numbers are positive integers whose prime factors include 2, 3 and 5 only.

Example:
n = 6 is a hamming number as 6 = 2 > 3. So, its prime factors are limited to 2, 3.
n = 14 is not a hamming number as 14 = 2 x 7. It has 7 as one of its prime factors.

Design a program to accept any positive integer number and check if it is a Hamming
number or not. Display the result with an appropriate message. The program should also
generate efror message if a negative number is entered.

20. A disarium number is a number in which the sum of the digits to the power of
their respective position is equal to the number itself.

Example: 135 = 11 + 32 + 53
Hence, 135 is a disarium number.
Design a class Disarium to check if a given number is a disarium number or not. Some of
the members of the class are given below:

Class name: Disarium


Data members/instance variables: int num: stores the number , int size: stores the size
of the number
Methods/Member functions:
Disarium(int nn): parameterized constructor to initialize the data members n = nn and
size - 0
void countDigit(): counts the total number of digits and assigns it to size
int sumofDigits(int n, int p): returns the sum of the digits of the number (n) to the power
of their respective positions (p) using recursive technique
void check(): checks whether the number is a disarium number and displays the result
with an appropriate message.

Specify the class Disarium giving the details of the constructor(), void countDigit(), int
sumofDigits(int, int) and void check(). Define the main() function to create an object and
call the functions accordingly to enable the task.

MIXED-TOPIC
21. A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as
well as an Adam number.
Prime number: A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 ... etc.
Adam number: The Square of a number and the square of its reverse are reverse to each
other. Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)" = 961 which is reverse of 169
thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all
Prime-Adam integers that are in the range between m and n (both inclusive) and
output them along with the frequency.
22. Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
only. The words are to be separated by a single blank space and are in UPPER CASE.
Perform the following tasks:
i. Check for the validity of the accepted sentence only for the terminating character.
ii. Arrange the words in ascending order of their length. If two or mofe words
have the same length, then sort them alphabetically.
iii. Display the original sentence along with the converted sentence.

23. A Circular Prime is a prime number that remains prime under cyclic shifts of its
digits. When the leftmost digit is removed and replaced at the end of the remaining
string of digits, the generated number is still prime. The process is repeated until the
original number is reached again.
A number is said to be prime if it has only two factors 1 and itself.
Example:
131
311
113
Hence, 131 is a circular prime.
Accept a positive number N and check whether it is a circular prime or not. The new
numbers formed after the shifting of the digits should also be displayed.

24.A company manufactures packing cartons in four sizes, i.e, cartons to accommodate 6
boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of
boxes to be packed (N) by the user (maximum up to 1000 boxes) and display the
break-up of the cartons used in descending order of capacity (i.e. preference should be
given to the highest capacity available, and if boxes left are less than 6, an extra canon
of capacity 6 should be used.)

25. A Goldbach number is a positive even integer that can be expressed as the sum of
two odd primes.
Note: All even integer numbers greater than 4 are Goldbach numbers.
Example:
6=3 + 3
10 = 3 + 7
10 = 5 + 5
Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3 and
7, 5 and 5.
Write a program to accept an even integer 'N' where N > 9 and N < 50. Find all the odd
prime pairs whose sum is equal to the number 'N'.

You might also like