3 Sem CE251 Prac List 21 22
3 Sem CE251 Prac List 21 22
3 Sem CE251 Prac List 21 22
: 2021-22
3. Given two non-negative int values, return true if they have the same first digit, such 1 1
as with 72 and 75.
firstDigit(7, 71) → true
firstDigit(6, 17) → false
firstDigit(31, 311) → true
4. The problem is to write a program that will grade multiple-choice tests. Assume there are eight 2 1
students and ten questions, and the answers are stored in a two-dimensional array. Each row
records a student’s answers to the questions, as shown in the following array.
Students’ Answers to the Questions:
0123456789
Student 0 A B A C C D E E A D
Student 1 D B A B C A E E A D
Student 2 E D D A C B E E A D
Student 3 C B A E D C E E A D
Student 4 A B D C C D E E A D
Student 5 B B E C C D E E A D
Student 6 B B A C C D E E A D
Student 7 E B E C C D E E A D
The key is stored in a one-dimensional array:
Key to the Questions:
0123456789
Key D B D C C D A E A D
Your program grades the test and displays the result. It compares each student’s answers
with the key, counts the number of correct answers, and displays it.
5. We have triangle made of blocks. The topmost row has 1 block, the next row down 2 1
has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops
or multiplication) the total number of blocks in such a triangle with the given number
of rows.
triangle(0) → 0
triangle(1) → 1
triangle(2) → 3
PART-II (10 Hours) Hrs. CO
Object Oriented Programming: Classes, Methods, Inheritance
1. Design a class named Cylinder containing following attributes and behavior. 2 1,2
One double data field named radius. The default value is 1.
One double data field named height. The default value is 1.
A no-argument constructor that creates a default Cylinder.
A Single argument constructor that creates a Cylinder with the specified radius.
Two argument constructor that creates a Cylinder with the specified radius and
height.
A method named getArea() that returns area of the Cylinder.
Create a class TestCylinder and test and display result.
2. Design a class named Account that contains:
A private int data field named id for the account (default 0).
A private double data field named balance for the account (default 500₹).
A private double data field named annualInterestRate that stores the
current interest rate (default 7%). Assume all accounts have the same
interest rate.
A private Date data field named dateCreated that stores the date when the
1. Create an abstract class GeometricObject as the superclass for Circle and 2 1,2
Rectangle. GeometricObject models common features of geometric objects. Both
Circle and Rectangle contain the getArea() and getPerimeter() methods for
computing the area and perimeter of a circle and a rectangle. Since you can
compute areas and perimeters for all geometric objects, so define the getArea() and
getPerimeter() methods in the GeometricObject class. Give implementation in the
specific type of geometric object. Create TestGeometricObject class to display area
and perimeter of Rectangle and Triangle, compare area of both and display results.
Design of all classes are given in the following UML diagram.
4
Write a program that raises two exceptions. Specify two ‘catch’ clauses for the two 2
exceptions. Each ‘catch’ block handles a different type of exception. For example the
3.
exception could be ‘ArithmeticException’ and ‘ArrayIndexOutOfBoundsException’.
Display a message in the ‘finally’ block.
CO
PART-V (12 hours) Hrs.
File Handling & Streams
1. 4
WAP to show how to create a file with different mode and methods of File class to 2
find path, directory etc.
2. When to use Character Stream over Byte Stream? When to use Byte Stream over Character 4
2
Stream? Give example.
3. 4
Write a program to transfer data from one file to another file so that if the destination 2
file does not exist, it is created.
4. 4
WAP to show use of character and byte stream. 2
5. Write a program to enter any 15 numbers from the user and store only even numbers in a file 4
2
named “Even.txt”. And display the contents of this file on the console.
(BufferedReader / BufferedWriter).
6. 4
WAP to demonstrate methods of wrapper class. 2
CO
PART-VI (10 hours) Hrs.
Multithreading
1. 3
Write a program to create thread which display “Hello World” message. 2
A. by extending Thread class
B. by using Runnable interface.
2. Generate 15 random numbers from 1 to 100 and store it in an int array. Write a program to 3
2
display the numbers stored at odd indexes by thread1 and display numbers stored at even
indexes by thread2.
3. 3
Write a program to increment the value of one variable by one and display it after one 2
second using thread using sleep() method.
4. 3
Write a program to create three threads ‘FIRST’, ‘SECOND’, ‘THIRD’. Set the 2
priority of the ‘FIRST’ thread to 3, the ‘SECOND’ thread to 5(default) and the
‘THIRD’ thread to 7.
5. Write a program to solve producer-consumer problem using thread 3
2
Synchronization.
CO
PART-VII (6 hours) Hrs.
Collection Framework and Generic
1. Create a generic method for sorting an array of Comparable objects. 5,6
2
2. Write a program that counts the occurrences of words in a text and displays the words 5.6
and their occurrences in alphabetical order of the words. Using Map and Set Classes. 2
3. Personal Loan Eligibility Criteria for Salaried Applicant is as follows: 5,6
2
Eligible Age Group - 21 years to 60 years
Citizenship – Indian
Create a class AccountHolder to store above given information entered by a user. Create 5
objects of AccountHolder class and store them in an ArrayList. Display names of account
holders , who are eligible to get a loan based on given criteria.