Savitribai Phule Pune University
WORKBOOK
Programming in Java - CS 359
T. Y. B. Sc. (Computer Science)
SEMESTER V
(From Academic Year 2021)
Student Name:
College:
Roll No: Exam Seat No:
Year: Division:
Table of Contents
Sr No Contents
1 Introduction
2 Assignment Completion Sheet
Section I - Object Oriented Programming using Java
3 Java Tools and IDE, Simple Java Programs
4 Array of Objects and Packages
5 Inheritance and Interfaces
6 Exception and File Handling
7 GUI Designing, Event Handling
Assignment Completion Sheet
Sr. No Assignment Name Marks Signature
(Out of 5)
1 Java Tools and IDE, Simple java programs
2 Array of Objects and Packages
3 Inheritance and Interfaces
4 Exception And File Handling
5 GUI Designing, Event Handling
Total out of 25
Total out of 05 (Quiz /Viva)
Total out of 30
Total (Out of 15)
This is to certify that Mr/Ms _
University Exam Seat Number has successfully completed the course work
for CS 359 - Programming in Java and has scored Marks out of 15.
Instructor Head
Internal Examiner External Examiner
CS – 359
Object Oriented
Programming
Using Java
Lab Assignments
Set A
a) Using javap, view the methods of the following classes from the lang package:
java.lang.Object , java.lang.String and java.util.Scanner. and also Compile sample
program 8. Type the following command and view the bytecodes. javap -c
MyClass.
b) Write a program to calculate perimeter and area of rectangle.
(hint : area = length * breadth , perimeter=2*(length+breadth))
c) Write a menu driven program to perform the following operations
i. Calculate the volume of cylinder. (hint : Volume: π × r² × h)
ii. Find the factorial of given number.
iii. Check the number is Armstrong or not.
iv. Exit
d) Write a program to accept the array element and display in reverse order.
Set B
a) Write a java program to display the system date and time in various formats
shown below
Current date is : 31/08/2021 Current date is : 08-31-2021
Current date is : Tuesday August 31 2021
Current date and time is : Fri August 31 15:25:59 IST 2021
Current date and time is : 31/08/21 15:25:59 PM +0530
Current time is : 15:25:59
Current week of year is : 35
Current week of month : 5
Current day of the year is : 243
Note: Use java.util.Date and java.text.SimpleDateFormat class
b) Define a class MyNumber having one private int data member. Write a default
constructor to initialize it to 0 and another constructor to initialize it to a value
(Use this). Write methods isNegative, isPositive, isZero, isOdd, isEven. Create
an object in main. Use command line arguments to pass a value to the object
(Hint : convert string argument to integer) and perform the above tests. Provide javadoc
comments for all constructors and methods and generate the html help file.
c) Write a menu driven program to perform the following operations
on multidimensional array ie matrix :
i. Addition
ii. Multiplication
iii. Transpose of any matrix.
iv. Exit
Set C
a) Write a program to accept n names of country and display them in descending
order.
b) Write a menu driven program to perform the following operations on 2D array:
i. Sum of diagonal elements
ii. Sum of upper diagonal elements
iii. Sum of lower diagonal elements
iv. Exit
c) Write a program to display the 1 to 15 tables.
(1*1=1 2 * 1 = 2……. 15 * 1 = 15
1*2=2 2*2=4 15 * 2 = 30
1*3=3 2*3=6 15 * 3 = 45
… … …..
1 * 10 = 10 2 * 10 = 20 15 * 10 = 150)
Assignment Evaluation
0: Not Done 1: Incomplete 2:Late Complete
3: Needs Improvement 4: Complete 5: Well Done
Practical In-charge
Assignment 2: Array of Objects and Packages
Set A
a) Create an employee class(id,name,deptname,salary). Define a default and
parameterized constructor. Use ‘this’ keyword to initialize instance variables.
Keep a count of objects created. Create objects using parameterized constructor
and display the object count after each object is created.(Use static member and
method). Also display the contents of each object.
b) Define Student class(roll_no, name, percentage) to create n objects of the
Student class. Accept details from the user for each object. Define a static
method “sortStudent” which sorts the array on the basis of percentage.
c) Write a java program to accept 5 numbers using command line arguments sort
and display them.Write a java program that take input as a person name in the
format of first, middle and last name and then print it in the form last, first and
middle name, where in the middle name first character is capital letter
Set B
a) Write a Java program to create a Package “SY” which has a class SYMarks
(members – ComputerTotal, MathsTotal, and ElectronicsTotal). Create another
package TY which has a class TYMarks (members – Theory, Practicals). Create n
objects of Student class (having rollNumber, name, SYMarks and TYMarks). Add
the marks of SY and TY computer subjects and calculate the Grade (‘A’ for >= 70,
‘B’ for >= 60 ‘C’ for >= 50 , Pass Class for > =40 else ‘FAIL’) and display the
result of the student in proper format.
b) Define a class CricketPlayer (name,no_of_innings,no_of_times_notout, totatruns,
bat_avg). Create an array of n player objects .Calculate the batting average for each
player using static method avg(). Define a static sort method which sorts the array
on the basis of average. Display the player details in sorted order.
Set C
a) Write a package for String operation which has two classes Con and Comp. Con
class has to concatenates two strings and comp class compares two strings. Also
display proper message on execution.
b) Create four member variables for Customer class. Assign public, private,
protected and default access modifiers respectively to these variables. Try to
access these variables from other classes (Same package and Different package)
Assignment Evaluation
0: Not Done 1: Incomplete 2:Late Complete
3: Needs Improvement 4: Complete 5: Well Done
Practical In-charge
Assignment 3: Inheritance and Interfaces
Lab Assignments
Set A
a) Write a program for multilevel inheritance such that country is inherited from
continent. State is inherited from country. Display the place, state, country
and continent.
b) Define an abstract class Staff with protected members id and name. Define
a parameterized constructor. Define one subclass OfficeStaff with member
department. Create n objects of OfficeStaff and display all details.
c) Define an interface “Operation” which has methods area(),volume().Define a
constant PI having a value 3.142.Create a class cylinder which implements
this interface (members – radius, height) Create one object and calculate the
area and volume.
d) Write a program to find the cube of given number using function interface.
Set B
a) Create an abstract class “order” having members id,description.Create two
subclasses “Purchase Order” and “Sales Order” having members customer
name and Vendor name respectively.Define methods accept and display in all
cases. Create 3 objects each of Purchase Order and Sales Order and accept and
display details.
b) Write a program to using marker interface create a class product(product_id,
product_name, product_cost, product_quantity) define a default and
parameterized constructor. Create objects of class product and display the
contents of each object and Also display the object count.
Set C
a) Create an interface Department containing attributes deptName and deptHead. It also
has abstract methods for printing the attributes. Create a class hostel containing
hostelName, hostelLocation and numberOfRooms. The class contains method
printing the attributes. Then write Student class extending the Hostel class and
implementing the Department interface. This class contains attributes studentName,
regNo, electiveSubject and avgMarks. Write suitable printData method for this class.
Also, implement the abstract methods of the Department interface. Write a driver
class to test the Student class. The program should be menu driven containing the
options:
i. Admit new student
ii. Migrate a student
iii. Display details of a student
For the third option, a search is to be made on the basis of the entered registration
Number.
Assignment Evaluation
0: Not Done 1: Incomplete 2:Late Complete
3: Needs Improvement 4: Complete 5: Well Done
Practical In-charge
Assignment 4 : Exception And File Handling
Set A
a) Define a class patient (patient_name, patient_age,
patient_oxy_level,patient_HRCT_report). Create an object of patient. Handle
appropriate exception while patient oxygen level less than 95% and HRCT scan
report greater than 10, then throw user defined Exception “Patient is Covid
Positive(+) and Need to Hospitalized” otherwise display its information.
b) Write a program to read a text file “sample.txt” and display the contents of a file in
reverse order and also original contents change the case (display in upper case).
c) Accept the names of two files and copy the contents of the first to the second.
First file having Book name and Author name in file. Second file having the contents
of First file and also add the comment ‘end of file’ at the end.
Set B
a) Write a program to read book information (bookid, bookname, bookprice, bookqty)
in file “book.dat”. Write a menu driven program to perform the following operations
using Random access file:
i. Search for a specific book by name.
ii. Display all book and total cost
b) Define class EmailId with members ,username and password. Define default and
parameterized constructors. Accept values from the command line Throw user
defined exceptions – “InvalidUsernameException” or “InvalidPasswordException”
if the username and password are invalid.
c) Define a class MyDate (day, month, year) with methods to accept and display
a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception
“InvalidDateException” if the date is invalid.
Examples of invalid dates : 03 15 2019, 31 6 2000, 29 2 2021
Set C
a) Write a menu driven program to perform the following operations on a set of integers
as shown in the following figure. A load operation should generate 10 random
integers (2 digit) and display the number on screen. The save operation should save
the number to a file “number.txt”. The short menu provides various operations and
the result is displayed on the screen.
b) Write a java program to accept Employee name from the user and check whether it is
valid or not. If it is not valid then throw user defined Exception “Name is Invalid”
otherwise display it.(Name should contain only characters
Assignment Evaluation
0: Not Done 1: Incomplete 2:Late Complete
3: Needs Improvement 4: Complete 5: Well Done
Practical In-charg
Assignment 5: GUI Designing, Event Handling
Lab Assignments
Set A
a) Write a java program that works as a simple calculator. Use a grid layout to
arrange buttons for the digits and for the +, -, *, % operations. Add a text field to
display the result.
Simple Calculator
1 2 3 +
4 5 6 -
7 8 9 *
0 .
= /
b) Design a screen to handle the Mouse Events such as MOUSE_MOVED and
MOUSE_CLICK and display the position of the Mouse_Click in a
TextField.
Set B
a) Create the following GUI screen using appropriate layout managers. Accept the
name, class , hobbies of the user and apply the changes and display the selected
options in a text box.
Your Habbies
Arial
Name : Class : Hobbies:
b) Write a Java program to design a screen using Awt that will take a user name and
password. If the user name and password are not same, raise an Exception with
appropriate message. User can have 3 login chances only. Use clear button to
clear the TextFields.
Page 69 of 70
Set C
a) Write a java program to create the following GUI for user registration form
Co-WIN Registration
AdharCard No. :
Birth Year :
Mobile No. :
Age Group : 18 & above 45 &
above Select Hospital :
Vaccines : Covishield, Covaxin Sputnik V.
Time Slot : Morning Afternoon Evening
Submit
If above conditions are met, display “Registration Successful” otherwise “Registration
Failed” after the user clicks the Submit button.
b) Write a program to display the following menus and sub-menus.
Additional Programs for practice
a) Write Java program to design three text boxes and two buttons using swing . Enter
different strings in first and second textbox. On clicking the First command
button, concatenation of two strings should be displayed in third text box and on
clicking second command button , reverse of string should display in third text
box.
b) Create an Applet which displays a message in the center of the screen. The message
indicates the events taking place on the applet window. Handle events like mouse
click, mouse moved, mouse dragged, mouse pressed, and key pressed. The message
should update each time an event occurs. The message should give details of the
event such as which mouse button was pressed, which key is pressed etc. (Hint: Use
Page 70 of 70
repaint(), KeyListener, MouseListener, MouseEvent method getButton, KeyEvent
methods getKeyChar)
c) Write a java program to create the following GUI for user registration form.
Perform the following validations: i. Password should be minimum 6 characters
containing atleast one uppercase letter, one digit and one symbol. ii. Confirm
password and password fields should match. iii. The Captcha should generate two
random 2 digit numbers and accept the sum from the user. If above conditions are
met, display “Registration Successful” otherwise “Registration Failed” after the
user clicks the Submit button.
d) Write a menu driven program to perform the following operations on a set of
integers. The Load operation should generate 50 random integers (2 digits) and
display the numbers on the screen. The save operation should save the numbers to a
file “numbers.txt”. The Compute menu provides various operations and the result is
displayed in a message box. The Search operation accepts a number from the user in
an input dialog and displays the search result in a message dialog. The sort operation
sorts the numbers and displays the sorted data on the screen.
File Comput Operatio
e n
Load Sum Searc
Sav Average h Sort
e Maximu ASC
Exit m DESC
Minimu
Numbers :
Assignment Evaluation
0: Not Done 1: Incomplete 2:Late Complete
3: Needs Improvement 4: Complete 5: Well Done
Practical In-charge