Вы находитесь на странице: 1из 17

BCSCCS407R01

JAVA PROGRAMMING LABORATORY


























School of Computing
Shanmugha Arts, Science, Technology and Research Academy [SASTRA]
Thanjavur 613 401









List of Experiments


1. Polymorphism in java.
2. Inheritance in Java.
3. Interface in Java.
4. Packages in java.
5. Multiple Threads in JAVA.
6. Exceptions in JAVA.
7. Applet using Labels, Text Fields and Buttons.
8. Applet using Checkbox, Option Buttons, Choice, List and Scrollbar.
9. Standalone GUI interface using Frames.
10. Applet for demonstrating font features.
11. Applet using Panels and Layout manager.
12. Applet for demonstrating graphics features using brush styles, colors.
13. Library Management using JDBC.


Additional Exercises

1. Program using Adapter Classes.
2. Programs that illustrate the use of files and streams.
3. Programs to draw various shapes using applets
a) Cylinder b) cube c) Square inside a circle d) circle inside a squar
4. Understand and handle mouse events and keyboard events.
5. Demonstrate RMI (Remote Method Invocation) in Java.
6. Programs using Swings Concepts.


1. Polymorphism in Java


Objective: To demonstrate the concept of polymorphism in Java.

Pre Lab: To demonstrate compile-time Polymorphism

Implement a Java program to define a class called Mobilephone with Model
Number, Manufacture Name, Cost, Size, Color, MemoryCapacity,
CameraPixelSize, etc.,. Include various types of constructors such as Default
constructor, Parameterized constructor with two arguments, three arguments and
four arguments. Create objects to invoke various types of constructors and
demonstrate polymorphism.

Algorithm :

Create a base class called Shape.
Use this class to store 2 double type values that could be used to compute
the area of figures.
Derive two specific classes called Triangle and Rectangle from the base
class Shape.
Using Run time polymorphism, calculate the area of Triangle and
Rectangle.


Sample I/O :

Enter the sides : 4 8

Area of rectangle = 32

Area of triangle = 16










2. Inheritance in Java

Objective: To demonstrate the concept of inheritance in Java

Pre Lab : Define a class named Vehicle with data members - name, model
number, price, service station name; add constructor and display() method.
Define another class Bike that extends the class Vehicle with data member
discount rate and methods constructor, display() ,discount() to compute discount.
Use object of the extended class to compute the discount and display the details
of the vehicle, inherited from its parent.

Algorithm :

The diagram given in the next page represents a hierarchical classification of
department details of an educational institution. Implement the same with
appropriate classes.


Sample I/O :

Enter student name : Joe
Enter reg. No. : 117003000
Enter address : aaa, bbb, ccc
Enter year of joining : 2013

Enter marks : 78
87
82


Grade = A















































Person

Category
Student

name
regno
address
yearOfJoinning

getStudentDetails()
display()



Staff

name
id
dateOfJoining

getdata()



Marks

m1
m2
m3
getMarkDetails()
Teaching

qualification
skills
areaofinterest

getPersonalDetails()
display()







NonTeaching

certification
yearsOfExperience
expertise

inputdata()
showdata()
Result
tot
grade
computeGrade()

3. Interface in Java

Objective: To implement the concept of interface in Java.

PreLab : Create two interfaces inter1 and inter2 with the signatures meth1()
and meth2() respectively. Create Class Test and implement the two interfaces and
override the signatures. Also write your own methods in Class Test. Use the
instance of the Class Test and call the methods.

Algorithm :

Create two interfaces Plumber and Electrician with methods unplugDrain()
and changeBulb() respectively.
Create a class named Human.
Derive three classes named HandyPerson, IndustrialPlumber and
NobodySpecial from human.
HandyPerson makes use of both Plumber and Electrician interfaces
whereas IndustrialPlumber implements only the interface Plumber.





Sample Output :

objHandyPerson : bulb changed, drain unplugged
objIndustrialPlumber : drain unplugged

4. Packages in Java

Objective: To implement the concept of packages in Java.

PreLab : Define a package named in that contains only one class named InValue.
This class contains three methods named stringReader(), intReader() and
fractReader() to read a string, integer, and fractional number respectively. Import
the in package in the code InValueDemo.java that contains only one class named
InValueDemo. This class contains the main() method that creates an object for the
class InValue and then invokes the stringReader(), intReader() and fractReader()
methods.


Algorithm :

Create two packages having the same class name in each package.
For example, create STUDENT class in two packages named P1 and P2
with data members such as name, register number, course, department,
year of study.
Create methods with same name in both the classes to accept data, display
data, with different implementation.
Create instance of class STUDENT from the above two packages.
Invoke the methods using those objects (of both classes named
STUDENT)

Sample input / output :

Enter student name : XXX
Enter reg. No. : 3456
Enter course : cse
Year of study : 2010

XXX 3456 cse 2010 from p1 package

5. Program to create multiple threads in JAVA.


Objective: To implement the concept of threads in JAVA.

PreLab : Write a simple program to create a thread using Thread class. Call
the start() method which gives a call to run().

Algorithm :

Create three threads Fact, Sum, and MultiplicaionTable.
Fact thread should produce factorial values.
Sum thread should produce sum of natural numbers
FiveTable thread class should produce table.
From the main class apply different methods using Thread class and call
the above said classes.(E.g.: priority(),sleep(),suspend(),resume(),isAlive()
and join() methods)

Sample input/output :

Fact thread : 120
Sum thread : 20
FiveTable thread : 5 10 15 20 25
Fact thread : 24
.
.
.
.



6. Exceptions in JAVA

Objective: To create a program to demonstrate user-defined exceptions in
JAVA.

PreLab : Create a java code that will raise an ArithmeticException (e.g. a
divide-by-zero error) inside a try block. Catch the exception with catch block
and display the message.

Algorithm :

Create a class Exception1 that extends Exception class
Create another class ExceptionTest that has a method that throws exception
of the above type
From main, create an object of ExceptionTest and invoke the method that
throws the exception.
Define catch block to handle the exception thrown.
Also, add code to catch and throw the exception again, catch and handle it.


Sample input/output :

Exception1 thrown

Exception1 caught
.
.
.

7. Simple Applet using Labels, Text Field and Buttons
(Calculator)


Objective: To create an Applet in JAVA, that shows the manipulation of
Labels, Text Field and Buttons (Calculator application)

PreLab : Create a Login screen using AWT package.

Algorithm :

Implement a java Applet application to design a calculator.
Include the options for Backspace, CE, C, MC, MR, = and sqrt
Add buttons for basic keys such as +, -, *, /


Sample output :





8. Simple Applet using Option Buttons, Choice, List and
Scrollbars

Objective: To design a Student Response Form with java Applet application.

PreLab : Create a simple form simulating the online mobile recharging,
adding the relevant controls.

Algorithm :

Create a Student Response Form, which include details such as Name,
register number, sex, degree, branch, year of study date of birth, address
(include e-mail id), Hobby and Extra-Curricular activities.
For getting these details, use appropriate GUI interface elements such as
Label, Textfield, Textarea, List/Choice, radio Button, Checkbox Button
etc.,
Add appropriate event handling mechanism to these controls.
The event handler should validate the keyed in input. For example, Name
field cannot be blank, register number should be 9 digits, etc.
Combine all these collected details in a simple English Sentence form and
display it in a Textarea control.

Sample output :

Display the student response form

9. Stand alone GUI interface using Frames
(Text Editor with menus)

Objective: To create a standalone graphical user interface using frames.

PreLab : Create a simple frame by extending the Frame class in an Applet
window. Call the appropriate methods to set the size of the frame.

Algorithm :

Create a java application using frames to create a simple notepad editor,
which includes options for File Open, File Save, Cut, Copy, Paste, Find
and Replace option using dialog box.
File menu consists of submenus like New, Open, Save and Quit.
Edit menu consists of Cut, Copy, and Paste, Find and Replace as
submenus.

Sample output :






10. Applet demonstrating font features


Objective: Create a java applet program that demonstrates the font features.

PreLab : Create a class that inherits Font class and assign values to the pre-
defined variables of the class. Assign these values to the object of the Font
class and observe the result.

Algorithm :

1. Creating text using different font types.
2. Apply the various font sizes and styles.
3. Manage text output using Font Metrics
4. Use methods like getFamily() getAvailableFontFamilyNames(), Font(),
setFont(), getAllFonts(), getFont(), bytesWidth(..), charWidth(..) etc.


Sample output :





11. Applet using Panels and Layout Managers

Objective: To demonstrate panels and the various layout managers using
Applet.

PreLab : Create an Applet extending the Panel class. Use methods that loads
and displays images, plays audio clips

Algorithm :

Create an Applet for layout demo. Add various controls like textbox,
checkbox. Add some meaningful text on the screen
Apply the various layouts like Flow Layout, Border Layout, Insets, Grid
Layout and Card Layout.

Sample Output :







12. Applet demonstrating graphics features using brush
styles, colors

Objective: To demonstrate the various graphics features available in JAVA by
taking paintbrush application as an example.

PreLab :

Algorithm :

Implement a java program to design paintbrush application using menus.
Extend Applet class that contains various methods from Graphics class for
drawing lines, drawing and filling circle, rectangle, oval, etc
Menu includes various options to select circle, ellipse, square, and
rectangle.
Include facility for selecting brush style and color for the selected object.
Implement this with popup menu as well.

Sample output :



13. Library Management using JDBC

Objective: To understand database connectivity, operation and manipulation
using JDBC, with the help of a library management system.

PreLab : Connect to a database using a Connection object and display the
data available in the tables.

Algorithm :

I. Basic steps to use a database in Java

Load the JDBC driver
Specify the name and location of the database being used
Connect to the database with a Connection object
Execute a SQL query using a Statement object
Get the results in a ResultSet object
Finish by closing the ResultSet, Statement and Connection object

II. Database manipulation

Create a frame and controls like textbox for entering text and for
displaying the details, list for displaying the available list of books, grid for
searching, etc.
Create tables like Books, Journals, Users, etc
Enter some book name in text field and display its details, if the record is
available.
Enter the details of new books received in the library and add this new
record to the database.
Create windows for issue and return of books and update the database
accordingly.
Delete the record details of a book, if it was lost.

Sample Output :