Java Programming Lab Manual - Dec 2016-Apr2017
Java Programming Lab Manual - Dec 2016-Apr2017
School of Computing
Shanmugha Arts, Science, Technology and
Research Academy [SASTRA]
Thanjavur – 613 401
List of Experiments
Additional Exercises
Create a Point class that has two data members x and y of double type.
Write a default constructor which initializes its data members to zero.
Write a parameterized constructor which takes two parameters of
double type and assigns them to its data members. Write another
parameterized constructor that takes one Point object as parameter and
copies the values of the passed object’s data member to the calling
object’s data members. Write a find_distance method that takes two
double parameters representing the x, y values of a point and finds the
distance between the calling object and the passed parameters and
returns the distance as double value. Overload the find_distance
method, that takes single Point object parameter and computes the
distance between both the points and returns the distance as a double
value. Write a display method to print the point in the format of “(x,
y)”. In main method create three point objects p1, p2 and p3. Initialize
p1 with (3.25, 7.89), p2 with (5.37, 18.12) and p3 with p2. Find
distance between p1 and (7.9, 16.25) using first find_distance method
and between p1 and p3 using second find_distance method.
2. Inheritance in Java
Pre-Lab Exercise:
Create an abstract class called Shape2d that has two data members of
type double and two abstract methods area() and display(). Create two
derived classes Rectangle and Triangle. In both classes define the
method area() to compute the area of that shape and return it as a
double value and define the method display() to display the value of
the data members with appropriate caption and the calculated area.
Create a class called Shape2dDemo and provide main method to
instantiate the objects of Rectangle and Triangle for demonstration of
the above classes.
Lab Exercise:
Create a class called Account that has the protected data members
accnumber of int type, balance of double, and constructors for
initialization.
Create a class called Customer. The data members of the class are
cust_id of int type, name, and address of string type, and objects of
SBAccount, and FDAccount. Provide parameterized constructor with
cust_id, name and address as parameters for initializing. Provide a
method called createAccount(int type). Based on the value of type
(SB, or FD) create a new account of given type. Provide a method
transaction(int type) {type may be withdraw, deposit or calc_interest
on SBAccount, or closing of FDAccount} to perform the requested
transaction on the requested Account object.
Pre-Lab Exercise:
Lab Exercise:
Pre-Lab Exercise:
In the main() method get input for two operands and one operator from
the user and check if the first operand is within the range 10,000 to
50,000 and second operand is within the range 500 to 5000 and the
operator is one of the characters ‘+’, ‘-‘, ‘*’ or ‘/’. If the operand is not
within the range then create and throw an OperationFailedException
with BadOperandException as its root cause. If the operator is not one
of the listed characters create and throw an OperationFailedException
with BadOperatorException as its root cause. If exception is thrown,
print the exception and its root cause; otherwise print the result of the
evaluated expression.
Lab Exercise:
Pre-Lab Exercise:
Lab Exercise:
Create a class called queue which has an array of integers, and front
and rear position values to represent the front and rear of the queue,
and two Boolean flags full and empty. Implement two synchronized
methods get and put to retrieve next item to consume from the queue
and to store the newly produced item into the queue. Set the flags
appropriately according to the state of the queue. Create one object of
the queue and two threads namely producer thread and consumer
thread and share the queue for both the threads. Producer thread should
produce items and place it in the queue and the consumer will
consume the items one by one from the queue. If the queue is full the
producer has to wait until one of the items is consumed by the
consumer. Similarly if the queue is empty the consumer has to wait
until the producer places at least one item into the queue.
6. ArrayList
Pre-Lab Exercise:
Lab Exercise:
Create a Point class that has two data members x and y of double type.
Write a default constructor which initializes its data members to zero.
Write a parameterized constructor which takes two parameters of
double type and assigns them to its data members. Write another
parameterized constructor that takes one Point object as parameter and
copies the values of the passed object’s data member to the calling
object’s data members. Write a find_distance method that takes two
double parameters representing the x, y values of a point and finds the
distance between the calling object and the passed parameters and
returns the distance as double value. Overload the find_distance
method, that takes single Point object parameter and computes the
distance between both the points and returns the distance as a double
value. Write a display method to print the point in the format of “(x,
y)”. In main method create an ArrayList <Point> objects. Find
distance between every pair of points and display it.
7. Set, Comparable interface and Comparator class
Pre-Lab Exercise:
Lab Exercise:
Create a Point class that implements Comparable Interface and has two
data members x and y of double type. Write a default constructor
which initializes its data members to zero. Write a parameterized
constructor which takes two parameters of double type and assigns
them to its data members. Write another parameterized constructor that
takes one Point object as parameter and copies the values of the passed
object’s data member to the calling object’s data members. Write a
find_distance method that takes two double parameters representing
the x, y values of a point and finds the distance between the calling
object and the passed parameters and returns the distance as double
value. Overload the find_distance method, that takes single Point
object parameter and computes the distance between both the points
and returns the distance as a double value. Define the compareTo
method that returns difference between the distances of the two points
from origin. Write a display method to print the point in the format of
“(x, y)”. In main method create a HashSet <Point> objects. Create and
add 10 Point objects in the HashSet. Display the points in the
ascending order of the points based on the distance from the origin to
that point. Also find distance between every pair of points and display
it.
8. Simple Applet using AWT controls
Pre-Lab Exercise:
Lab Exercise:
Write a JAVA Applet program that displays the current water tank
level and buttons like start, stop, suspend and resume. When clicking
on the start button the water level in the tank should be increased by
some fixed quantity for every second. When suspend button is pressed
the filling process should be suspended temporarily and once resume
button is pressed the filling of water should be resume from the point
left. When stop button is pressed the filling process should be
terminated. Also it should be able to restart again the filling process
from beginning by pressing the start button again. The applet should
check for all possible error conditions and should display the error
message in the status bar. For example if stop button is pressed
without starting the filling process, if the suspend button is pressed
without starting the process, if resume is pressed without suspend, if
water overflows.
GUI
9. Frame using AWT
Pre-Lab Exercise:
GUI:
Lab Exercise:
GUI:
10. Layout Managers
Pre-Lab:
Lab Exercise:
GUI:
11. File I/O
Pre-Lab Exercise:
Lab Exercise:
Pre-Lab:
Lab Exercise:
Using JDBC connect the database and design the following GUI for
manipulation of database with buttons for adding, deleting, updating
and browsing through students’ records. Initially display the first
record in the student table. When the user presses Clear button display
a blank form. When the user after entering register number presses
Search button, display the corresponding student’s detail in the form.
When the user presses Add button, display a blank form. After
entering all the details the user should press the Update button to insert
the record into the student table. When the user presses Delete button
the current student record that is displayed in the form should be
deleted from the student table. Implement the button << for moving to
the first record, < for moving to the previous record, > for moving to
the next record and >> for moving to the last record. If any student
information in the form is updated during search or browse operations,
upon clicking Update button the corresponding record should be
updated with the current information.
GUI: