IT251 Java Practical List
IT251 Java Practical List
IT251 Java Practical List
Implement Object Oriented programming concept using basic syntaxes of control Structures, strings, and
CO1 function for developing skills of logic building activity.
Use of a variety of basic control structures including selection and repetition; classes and objects in a
CO2 tiered architecture (user interface, controller, and application logic layers)
Demonstrates how to achieve reusability using inheritance, interfaces, and packages and describes faster
CO3 application development that can be achieved.
Demonstrate understanding and use of different exception handling mechanisms and concepts of
CO4 multithreading for robust faster and efficient application development.
Identify and describe common abstract user interface components to design GUI in Java using Swing
CO5 along with a response to events.
Identify, Design & develop complex Graphical user interfaces using principal Java Swing classes based
CO6 on MVC architecture.
PART-I CO Hours
Data Types, Variables, Control Statements, Operators
3 Hours
1. 1
1. Why do computers use binary numbers? Which one is the correct option?
• It makes the computer faster and more efficient than other number bases.
• At the time when computers were invented, humans only knew about the
numbers 0 and 1.
• Because there are 10 types of people: Those who understand binary, and those
who don't.
• It is easier to build electronic devices reliably if they only have to distinguish
between two electric states.
• It is impossible to build a computer that uses a different number base than
binary.
2. Write an algorithm for baking muffins. Assume that you have a large
number of hungry friends, so you'll want to produce several batches of
muffins! (Many solutions can be possible).
Answer: Make the muffins batter:
Mix the dry ingredients.
Cream the butter and sugar.
Beat in the eggs.
Stir in the dry ingredients.
Bake the muffins:
Set the oven for the appropriate temperature.
Set the timer.
Place the muffins into the oven.
Allow the muffins to bake.
Add frosting and sprinkles:
Mix the ingredients for the frosting.
Spread frosting and sprinkles onto the muffins
3. What is the difference between the file MyProgram.java and the file
MyProgram.class?
• A .java file contains code written in the Java language, and a .class file contains
code written in the C++ language.
• A .java file is a much larger binary file and a .class file is a smaller compressed
version.
• The .class file is for object-oriented programming and the .java file is for
procedural programming.
• MyProgram.java is a source code file typed by the programmer, and
MyProgram.class is a compiled executable class file that is run by the computer.
• The programmer writes the .class file first, and the .java file is generated
automatically later.
2. Write a program that will print your initials to standard output in letters that are nine
lines tall. Each big letter should be made up of a bunch of *’s. For example, if your
initials were “DJE”.
3. Write a program that asks the user’s name, and then greets the user by name. Before outputting 1
the user’s name, convert it to upper case letters. For example, if the user’s name is Jane, then
the program should respond “Hello, Jane, Good Morning!”.
4. Implements a simple program that will compute the amount of interest that is earned on 45,000 1
invested at an interest rate of 0.07 for one year. The interest and the value of the investment after
one year are printed to standard output.
5. If you have N Apples, then you have N/12 dozen apples, with N%12 apples left over. (This is 1
essentially the definition of the / and % operators for integers.) Write a program that asks the
user how many apples she has and then tells the user how many dozen apples she has and how
many extra apples are left over.
A gross of apples is equal to 144 apples. Extend your program so that it will tell the user how
many gross, how many dozen, and how many lefts over apples she has. For example, if the user
says that she has 1342 apples, then your program would respond with
Your number of apples is 9 gross, 3 dozen, and since 1342 is equal to 9*144 + 3*12 + 10.
6. This program reads a sequence of positive integers input by the user, and it will print out the 1
average of those integers. The user is prompted to enter one integer at a time. The user must
enter a 0 to mark the end of the data. (The zero is not counted as part of the data to be averaged.)
The program does not check whether the user’s input is positive, so it will actually work for
both positive and negative input values.
PART-II CO Hours
String & Arrays
3 Hours
1. Assume on your phone keypad, the alphabets are mapped to digits as follows: 1
ABC (2), DEF (3), GHI (4), JKL (5), MNO (6), PQRS (7), TUV (8), WXYZ (9).
Write a program named KeyPad, which prompts user for a String (case
insensitive), and converts to a sequence of keypad digits. Use (a) a nested -if, (b) a
switch-case-default.
2. Write a program called LetterExchange that prompts user for a string consisting 1
of mix-case letters only. You program shall compute the text; and print the output
in uppercase. For examples, exchanges 'A' and 'Z', 'B' and 'Y', 'C' and 'X', and so
on.
3. Write a program named CheckBinary shall prompt user for a binary string; and 1
decide if the input string is a valid binary string. The binary number system uses
2 symbols, 0 and 1.
4. Write a program which prompts user for the number of students in a class (a non- 1
negative integer), and saves it in an int variable called totalStudents. It then
prompts user for the grade of each of the students (integer between 0 to 100) and
saves them in an int array called marks. The program shall then compute and print
the average (in double rounded to 2 decimal places) and minimum/maximum (in
int).
5. Write a program which takes an array of int and an int value; and returns true if 1
the array contains the given int. (hint: use contains method)
For example: Array = 1,3,4,5,7,8,9,12
int value = 9
Method should return true
PART-III CO Hours
Object Oriented Programming : Classes, Methods,
Constructors
4 Hours
1. /* The Video class holds information about a single television programme recorded and it 1,3
is used in a video shop system. It holds the video details.
*/
public class VideoTape
{ private String title; // the title of the programme
private String classification; // classification of the programme (comedy, drama, action, or
romance,horror)
private int time; // the running time of the programme in minutes
// Create a new video tape with a given title, classification, and time.
// Return the time of this video tape as a string in the following format: 2:06.
………….
// Set a new classification for this video tape.
………….
/* Print the details of the video tape to the output terminal in the following format:
* Patrick Jane (action) 3;45
*/
2. Write a program to create a Classroom class, the attributes of this class is roomno, 1,3
roomtype, roomarea and ACmachine. Define different types of constructor to display the
room details.
3. Create a class called Salesman that includes three pieces of information as instance 1,3
variables—a first name (type String), a last name (type String) and a monthly salary
(double). Your class should have a constructor that initializes the three instance variables.
Provide a set and a get method for each instance variable. If the monthly salary is not
positive, set it to 0.0. Write a test application named SalesmanTest that demonstrates class
Employee’s capabilities. Create two Salesman objects and display each object’s yearly
salary. Then give each salesman a 10% raise and dis- play each salesman’s yearly salary
again.
4. Design a class called BookID to represent a unique BookID. The BookID consists of 10 1,3
digits divided into 4 parts. For example, the XYZ 01 345 represents the
following information:
The first part: The first digit “XYZ” signifies the book name
The second part: "01" identifies the publisher code.
The third part: "345" is the title number for the book.
The class should have a constructor and methods to set and get the BookID as a string.
5. Create a class called DataOfPatient to store the below information. The member methods 1,3
should include methods to enter information and display the patient's information. Create
class Date to have the date (year, month and day as its fields) and a method to display the
date.
A City Hospital wants to create a database regarding its day to day patients. The
information to store includes,
- Name of the patient
- Age of the patient
- Disease
- Date of admission
- Date of discharge
CO Hours
PART-IV
Inheritance, Interface , Package
4
Hours
1. Consider the following class hierarchy where Class Car is the supper class and the classes 3,4
VintageCar and ElectricCar are two subclasses derived from Car.
4. Create a class named 'Shape' with a method to print "This is This is shape". Then create two 3
other classes named 'Rectangle', 'Circle' inheriting the Shape class, both having a method to
print "This is rectangular shape" and "This is circular shape" respectively. Create a subclass
'Square' of 'Rectangle' having a method to print "Square is a rectangle". Now call the method
of 'Shape' and 'Rectangle' class by the object of 'Square' class.
5. Write a java that implements an interface AdvancedArithmetic which contains a method 3,4
signature int divisor_sum(int n). You need to write a class called MyCalculator which
implements the interface.
divisorSum function just takes an integer as input and return the sum of all its divisors. For
example, divisors of 6 are 1, 2, 3 and 6, so divisor_sum should return 12. The value of n will
be at most 1000.
6. Assume you want to capture shapes, which can be either circles (with a radius and a color) 3
or rectangles (with a length, width, and color). You also want to be able to create signs (to
post in the campus center, for example), each of which has a shape (for the background of
the sign) and the text (a String) to put on the sign.
Create classes and interfaces for circles, rectangles, shapes, and signs. Write
a program that illustrates the significance of interface default method.
7. Write a java program which creates a package University and Institute. Placed respective 3
classes in both the packages and Package classes can access details of each other’s’. (hint:
Access Specifier can be used)
PART-V CO Hours
Exception Handling
3 Hours
1. Write a java program which takes two integers x & y as input, you have to compute x/y. If
x and y are not integers or if y is zero, exception will occur and you have to report it. 3
2. A piece of Java code is given below. You have to complete the code by writing down the
3,5
handlers for exceptions thrown by the code. The exceptions the code may throw along
with the handler message are listed below:
MyException: This is a user defined Exception which you need to create. It takes a
parameter param. When an exception of this class is encountered, the handler should print
"MyException[param]", here param is the parameter passed to the exception class.
Exceptions other than mentioned above: Any other exception except the above ones
fall in this category. Print "Exception encountered".
Example: For an exception of MyException class if the parameter value is 5, the message
will look like
MyException[5].
3. Write a java program to generate user defined exception using “throw” and “throws”
3,5
keyword.
Also Write a java that differentiate checked and unchecked exceptions. (Mention at least
two checked and two unchecked exception in program).
PART-VI CO Hours
File Handling
4 Hours
1. Write a program that will count the number of lines in each file that is specified on the 2
command line. Assume that the files are text files. Note that multiple files can be
specified, as in "java LineCounts file1.txt file2.txt file3.txt". Write each file name, along
with the number of lines in that file, to standard output. If an error occurs while trying to
read from one of the files, you should print an error message for that file, but you should
still process all the remaining files.
2. Write an example that counts the number of times a particular character, such as e, 2
appears in a file. The character can be specified at the command line. You can use
xanadu.txt as the input file.
3. Write a Java Program to Search for a given word in a File. Also show use of Wrapper 2
Class with an example.
4. Write a program to copy data from one file to another file. If the destination file 2
does not exist, it is created automatically.
5. Write a program to show use of character and byte stream. Also show use of 2
BufferedReader /BufferedWriter to read console input and write them into a file.
PART-VII CO Hours
Multithreading
5 Hours
1. Write a program to create thread which display “Hello World” message. 2,3,5
A. by extending Thread class
B. by using Runnable interface.
2. Write a program which takes N and number of threads as an argument. Program should 2,3,5
distribute the task of summation of N numbers amongst number of threads and final result
to be displayed on the console.
3. Write a java program that implements a multi-thread application that has three threads. 2,3,5
First thread generates random integer every 1 second and if the value is even, second
thread computes the square of the number and prints. If the value is
Odd, the third thread will print the value of cube of the number.
4. Write a program to increment the value of one variable by one and display it after one 2
second using thread using sleep() method.
5. Write a program to create three threads ‘FIRST’, ‘SECOND’, ‘THIRD’. Set the priority 2,3,5
of the ‘FIRST’ thread to 3, the ‘SECOND’ thread to 5(default) and the ‘THIRD’ thread to
7.
6. Write a program to solve producer-consumer problem using thread 2
synchronization.
PART-VIII CO Hours
Applet and Event Handling
6 Hours
1. Write a program to create an applet having width 300 pixels and height is 100 pixels. 2,4
Display “Hello world” message into applet screen using applet life cycle methods.
2. WAP to create the different component (like button, labels, checkbox etc), container, 2,4
panel with appropriate example.
3. WAP that demonstrate use of graphics class in applet like drawing line, rectangle, 2,4
ellipses,circle etc.
4. WAP to show the concept of layout managers. 2,4
5. Write a program to display mouse pointer position in status bar of applet screen when 2
mouse left click is occurred. When mouse enter in to area of applet screen, background
color of applet should change with Blue color. And when mouse exit from applet screen
it should set to default background color (white). If click is right click then appropriate
message should display into status bar of applet.
6. Provide radio buttons in the frame. The text in the TextField should display the label of 2
the selected radio buttons. Write a program that displays the text with user defined font
in a TextField.
7. Write a program to implement the ‘MouseListener’ interface. Implement the five methods 2,4
defined by the interface.
8. Write a program using mouse adapter classes and handle only two mouse events. For 2
example, ‘mouse click’ and ‘mouse drag’ events.
9. Create an applet with a menu bar consisting of menus ‘File’, ‘Edit’ and ‘Help’. The menu 2
‘File’ should have menu items such as ‘New’, ‘Open’, ‘Save’ and ‘Exit’. The menu ‘Edit’
should have menu items such as ‘Cut’, ‘Copy’ and ‘Paste’. The menu items ‘Find’ of
‘Help’ should have a sub menu having items ‘Search by Name’ and ‘Search by Extension’.
Include a pop-up menu also with items ‘Open’ and ‘Save’.
PART-IX CO Hours
Collection Framework
2 Hours
1. Design a Custom Stack using ArrayList class, which implements following functionalties of
stack.
1,3 5
Beyond Syllabus: Lambda Expression, JJS, New features in Java9/10 (REPL, Functional
Programming), JDBC,