Software Development Tools Laboratory Laboratory Manual Third Year - Information Technology

advertisement
Laboratory Manual
Software Development Tools Laboratory
Third Year - Information Technology
Teaching Scheme
Examination Scheme
Theory : ——
Term Work: 50 Marks
Practical : 04 Hrs/Week
Practical : 0 Marks
Oral : 50 Marks
Prepared By
Prof. Patil P. M.,
Prof. Bhagwat K. S.
Department of Information Technology
Vidya Pratishthan’s College of Engineering
Baramati – 413133, Dist- Pune (M.S.)
INDIA
December 2012
Table of Contents
1 Introduction to Java and its features.
1.1 Problem Statement . . . . . . . . . . .
1.2 Pre Lab . . . . . . . . . . . . . . . . .
1.3 Hardware and Software Requirement .
1.4 Theory . . . . . . . . . . . . . . . . . .
1.5 Procedure . . . . . . . . . . . . . . . .
1.6 Post Lab . . . . . . . . . . . . . . . . .
1.7 Viva Questions . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
1
1
1
3
4
4
2 developing classes,its objects, methods,constructor
2.1 Problem Statement . . . . . . . . . . . . . . . . . . .
2.2 Pre Lab . . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Hardware and Software Requirement . . . . . . . . .
2.4 Theory . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4.1 Introducing Classes and Objects: . . . . . . .
2.4.2 Overloading Methods: . . . . . . . . . . . . .
2.4.3 Constructor: . . . . . . . . . . . . . . . . . .
2.4.4 ”this” Keyword: . . . . . . . . . . . . . . . .
2.4.5 Overloading Constructor: . . . . . . . . . . .
2.5 Procedure . . . . . . . . . . . . . . . . . . . . . . . .
2.6 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . .
2.7 Viva Questions . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6
6
6
6
6
6
8
9
9
10
10
10
10
3 Inheritance
3.1 Problem Statement . . . . . . . . . . . .
3.2 Pre Lab . . . . . . . . . . . . . . . . . .
3.3 Hardware and Software Requirement . .
3.4 Theory . . . . . . . . . . . . . . . . . . .
3.4.1 Inheritance: . . . . . . . . . . . .
3.4.2 Types of Inheritance: . . . . . . .
3.4.3 Access specifiers in Inheritance:
3.4.4 Inheritance using ”super”: . . .
3.4.5 Method overriding: . . . . . . .
3.4.6 Using final with inheritance: . .
3.5 Procedure . . . . . . . . . . . . . . . . .
3.6 Post Lab . . . . . . . . . . . . . . . . . .
3.7 Viva Questions . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
11
11
11
11
11
11
12
12
12
13
13
14
15
15
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
i
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
TABLE OF CONTENTS
4 Packages and Interface
4.1 Problem Statement . . . . . . . . . .
4.2 Pre Lab . . . . . . . . . . . . . . . .
4.3 Hardware and Software Requirement
4.4 Theory . . . . . . . . . . . . . . . . .
4.4.1 Abstract classes: . . . . . . .
4.4.2 Interface: . . . . . . . . . . .
4.4.3 Packages: . . . . . . . . . . .
4.5 Procedure . . . . . . . . . . . . . . .
4.6 Post Lab . . . . . . . . . . . . . . . .
4.7 Viva Questions . . . . . . . . . . . .
TABLE OF CONTENTS
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
17
17
17
17
17
17
18
18
19
20
20
. . . .
. . . .
. . . .
. . . .
. . . .
block:
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
22
22
22
22
22
22
23
23
24
24
24
25
25
25
25
6 Multithreading
6.1 Problem Statement . . . . . . . . . . . . . . . . . . . .
6.2 Pre Lab . . . . . . . . . . . . . . . . . . . . . . . . . .
6.3 Hardware and Software Requirement . . . . . . . . . .
6.4 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.4.1 Multithreaded Programming: . . . . . . . . . .
6.4.2 The ”Thread” class and ”Runnable” interface:
6.4.3 Main Thread: . . . . . . . . . . . . . . . . . . .
6.4.4 Creating a thread: . . . . . . . . . . . . . . . .
6.4.5 Extending ”Thread”: . . . . . . . . . . . . . . .
6.4.6 Creating multiple threads:- . . . . . . . . . . .
6.4.7 Using isAlive() and join(): . . . . . . . . . . . .
6.5 Procedure . . . . . . . . . . . . . . . . . . . . . . . . .
6.6 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . .
6.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
27
27
27
27
27
27
28
28
28
30
30
30
30
31
31
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
32
32
32
32
32
32
33
33
34
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5 Exception Handling
5.1 Problem Statement . . . . . . . . . . . . . . .
5.2 Pre Lab . . . . . . . . . . . . . . . . . . . . .
5.3 Hardware and Software Requirement . . . . .
5.4 Theory . . . . . . . . . . . . . . . . . . . . . .
5.4.1 Exception Handling: . . . . . . . . . .
5.4.2 General form of an exception handling
5.4.3 multiple catch clauses: . . . . . . . . .
5.4.4 Nested try statements: . . . . . . . .
5.4.5 throw k/w: . . . . . . . . . . . . . . .
5.4.6 throws k/w: . . . . . . . . . . . . . . .
5.4.7 finally clause: . . . . . . . . . . . . . .
5.5 Procedure . . . . . . . . . . . . . . . . . . . .
5.6 Post Lab . . . . . . . . . . . . . . . . . . . . .
5.7 Viva Questions . . . . . . . . . . . . . . . . .
7 I/O
7.1
7.2
7.3
7.4
classes in java
Problem Statement . . . . . . . . . .
Pre Lab . . . . . . . . . . . . . . . .
Hardware and Software Requirement
Theory . . . . . . . . . . . . . . . . .
7.4.1 Writing console output: . . .
7.4.2 The PrintWriter class:- . . .
7.4.3 Reading and Writing Files: .
7.4.4 Writing File: . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
ii
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
TABLE OF CONTENTS
7.5
7.6
7.7
TABLE OF CONTENTS
Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
34
35
35
8 Applet in java
8.1 Problem Statement . . . . . . . . . . . . . . .
8.2 Pre Lab . . . . . . . . . . . . . . . . . . . . .
8.3 Hardware and Software Requirement . . . . .
8.4 Theory . . . . . . . . . . . . . . . . . . . . . .
8.4.1 Applet: . . . . . . . . . . . . . . . . .
8.4.2 An Applet Skeleton:- . . . . . . . . . .
8.4.3 Applet Initialization and Termination:
8.4.4 more closely at these methods: . . . .
8.4.5 Simple Applet Display Method: . . .
8.4.6 Requesting Repainting: . . . . . . . .
8.5 Procedure . . . . . . . . . . . . . . . . . . . .
8.6 Post Lab . . . . . . . . . . . . . . . . . . . . .
8.7 Viva Questions . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
36
36
36
36
36
36
38
39
39
39
40
41
41
42
9 Abstract Window Toolkit
9.1 Problem Statement . . . . . . . . . . .
9.2 Pre Lab . . . . . . . . . . . . . . . . .
9.3 Hardware and Software Requirement .
9.4 Theory . . . . . . . . . . . . . . . . . .
9.4.1 Working with Frame Windows:
9.4.2 Menu Bars and Menus: . . . .
9.5 Procedure . . . . . . . . . . . . . . . .
9.6 Post Lab . . . . . . . . . . . . . . . . .
9.7 Viva Questions . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
43
43
43
43
43
43
45
47
48
48
10 Event Handling
10.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.2 Pre Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.3 Hardware and Software Requirement . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.4 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.4.1 Event Handling: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.4.2 Definition of ’Event’ and description of the roles of ’Sources’ and ’listeners’:
10.4.3 Event Classes: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.4.4 Event Classes: Main Event classes in java.awt.event . . . . . . . . . . . . . .
10.4.5 Sources of events: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.4.6 Event Listener Interfaces: . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.5 Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.6 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
49
49
49
49
49
49
50
51
52
52
53
53
54
54
.
.
.
.
.
.
.
.
.
.
.
.
.
.
55
55
55
55
55
55
56
56
11 java
11.1
11.2
11.3
11.4
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
DB connectivity-JDBC
Problem Statement . . . . . . . . . . . . . .
Pre Lab . . . . . . . . . . . . . . . . . . . .
Hardware and Software Requirement . . . .
Theory . . . . . . . . . . . . . . . . . . . . .
11.4.1 What is JDBC? . . . . . . . . . . . .
11.4.2 JDBC has four driver of connection:
11.4.3 The Structure of JDBC: . . . . . . .
iii
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
TABLE OF CONTENTS
TABLE OF CONTENTS
11.5 Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.6 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
References
57
57
57
58
iv
List of Tables
v
List of Figures
1.1
1.2
source code is compiled to get byte codes which are executed at runtime. . . . . . . . .
first simple Example.java source code . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
4
2.1
2.2
2.3
2.4
2.5
2.6
SimplePoint class . . .
SimplePoint Object . .
SimpleRectangle class .
SimpleRectangle object
Point class . . . . . . .
Point object . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
7
7
7
8
8
9
3.1
3.2
3.3
3.4
Syntax of Inheritance
Method overriding . .
Defining Final method
Defining Final class . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
12
13
14
14
4.1
4.2
Interface Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
package example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18
21
5.1
5.2
Exception Handling Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
throws syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
25
6.1
Thread class methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
8.1
8.2
Simple Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simple Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
38
11.1 JDBC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
57
.
.
.
.
vi
Assignment 1
Introduction to Java and its
features.
1.1
Problem Statement
Write a java program to sort the ten numbers using buble sort.
1.2
Pre Lab
Knowledge of programming in C . Knowledge of object oriented concepts of C++ and programming in
C++.
1.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
1.4
Theory
Introduction to Java:- Java was developed by five inventors from Sun Microsystems of USA in 1991.These
inventors are James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike Sheridan.
This language was initially called ”OAK” (name of tree) but was renamed ”Java” in 1995.Java
is simple and powerful language. It is an object oriented language. Also it is platform independent
language, because it can run under any O.S. Such as Linux, win98, win2000, winXP etc. Features of
Java:-
1
1.4. THEORY
Introduction to Java and its features.
1. Simple: In java no need of header files, pointer arithmetic, operator overloading, virtual base class.
2. Object Oriented: Java is object oriented language, but dose not support multiple inheritance.
3. Distributed and Dynamic: Java allows to create application on network hence it is distributed,
also capacity to dynamically linking in new class libraries, methods and object.
4. Robust: Java dose not allow pointer and concept of reference variable.
5. Secure: Java security has 3 primary components 1 class loader 2 byte code verifier 3 security
manager
6. Portable: Platform independent
7. Architectural natural: Java generate byte code which can be executed by any processor.
8. Interpreted: Java interpreter can execute java byte code directly on any m/c.Interpreter use byte
code hence interpreter is faster
9. Multithreading: Java handles multiple tasks simultaneously.
10. High performance: Java gives high performance due to use of intermediate Byte Code.
figure 1.1 shows the Compile Time and Runtime Environment of Java :
A first simple program:
Let’s start by compiling and running the short simple program shown in figure 1.2
Compiling the java program:
To compile the program use following command.
C :> javac Example.java
where javac is a compiler
To run the program, you must use the java interpreter, called java.
C :> java Example
When the program is run, the following output is displayed:
This is a simple java program.
Array data type: array is the collection of similar type of elements. there are two types of array.
a) one dimensional array:syntax for declaration of one dimensional array is as follows
type varname[];
e.g Array declaration: int a[];
allocation of memory: a=new int[40];
Lab Manual - Software Development Tools Laboratory
2
VPCOE, Baramati
1.5. PROCEDURE
Introduction to Java and its features.
Figure 1.1: source code is compiled to get byte codes which are executed at runtime.
combining the above two statements in a single statement
int a[]=new int[40];
b) two dimensional array declaration:following is the syntax
type varname[][]=new varname[40][40];
1.5
Procedure
create an int array we want to sort using bubble sort algorithm
int intArray[] = new int[];
print array before sorting using bubble sort algorithm
sort an array using bubble sort algorithm
In bubble sort, we basically traverse the array from first to arraylength − 1 position and compare
the element with the next one. Element is swapped with the next element if the next element is greater.
Bubble sort steps are as follows.
1. Compare array[0] and array[1]
2. If array[0] > array [1] swap it.
Lab Manual - Software Development Tools Laboratory
3
VPCOE, Baramati
1.6. POST LAB
Introduction to Java and its features.
Figure 1.2: first simple Example.java source code
3. Compare array[1] and array[2]
4. If array[1] > array[2] swap it. ...
5. Compare array[n-1] and array[n]
6. if [n-1] > array[n] then swap it.
After this step we will have largest element at the last index.
Repeat the same steps for array[1] to array[n-1]
1.6
Post Lab
Hence from this assignment you can learn how to compile and execute simple java program from command prompt and using IDE.
1.7
Viva Questions
1. Who was developed ”JAVA” Language?
2. What is mean by platform neutral (or platform Independent) language?
3. Why we would use JAVA Language?
4. What are the Features of JAVA?
5. Why java is secure language?
6. Why interpreter is faster in Java?
7. What is the use of ”static” k/w in public static void main (String args[]) function?
Lab Manual - Software Development Tools Laboratory
4
VPCOE, Baramati
1.7. VIVA QUESTIONS
Introduction to Java and its features.
8. Why we use String args[] as parameter of main method?
9. How to declare array and allocate memory for array elements in Java? Give with example?
10. What is the difference between Java and C++?
Lab Manual - Software Development Tools Laboratory
5
VPCOE, Baramati
Assignment 2
developing classes,its objects,
methods,constructor
2.1
Problem Statement
Write a program to construct a class department and creates its objects also implement constructor
overloading in it.
2.2
Pre Lab
Knowledge of programming in C . Knowledge of object oriented concepts of C++.
2.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
2.4
2.4.1
Theory
Introducing Classes and Objects:
Following figure 2.1 shows the code for a class called SimplePoint that represents a point in 2D space:
This segment of code declares a class– a new data type really– called SimplePoint. The SimplePoint
class contains two integer member variables, x and y. The public keyword preceding the declaration for
x and y means that any other class can freely access these two members.
6
2.4. THEORY
developing classes,its objects, methods,constructor
Figure 2.1: SimplePoint class
You create an object from a class such as SimplePoint by instantiating the class. When you create
a new SimplePoint object (we show in figure 2.2 you how shortly), space is allocated for the object and
its members x and y. In addition, the x and y members inside the object are initialized to 0 because of
the assignment statements in the declarations of these two members.
Figure 2.2: SimplePoint Object
Now, here is in figure 2.3 contain a class, SimpleRectangle ,That represents a rectangle in 2D space:
Figure 2.3: SimpleRectangle class
This segment of code declares a class (another data type)–SimpleRectangle– that contains two integer
members, width and height. SimpleRectangle also contains a third member, origin, whose data type is
SimplePoint. Notice that the class name SimplePoint is used in a variable declaration as the variable’s
type. You can use the name of a class anywhere you can use the name of a primitive type. As
with SimplePoint, when you create a new SimpleRectangle object as shown in figure 2.4, space is
allocated for the object and its members, and the members are initialized according to their declarations.
Interestingly, the initialization for the origin member creates a SimplePoint object with this code: new
SimplePoint() as illustrated here:
This figure 2.4 shows the difference between primitive types and reference types. Both width and
Lab Manual - Software Development Tools Laboratory
7
VPCOE, Baramati
2.4. THEORY
developing classes,its objects, methods,constructor
Figure 2.4: SimpleRectangle object
height are integers and are fully contained within SimpleRectangle. On the other hand, origin simply
references a SimplePoint object somewhere else.
The SimplePoint and SimpleRectangle classes as shown are simplistic implementations for these
classes. Both should provide a mechanism for initializing their members to values other than 0. Additionally, SimpleRectangle could provide a method for computing its area, and because SimpleRectangle
creates a SimplePoint when it’s created, the class should provide for the clean up of the SimplePoint
when SimpleRectangle gets cleaned up. So, here is in figure 2.5contain a new version of SimplePoint,
called Point, that contains a constructor which you can use to initialize a new Point to a value other
than (0,0):
Figure 2.5: Point class
Now, when you create a Point, you can provide initial values for it like this:
new Point(44, 78)
The values 44 and 78 are passed into the constructor and subsequently assigned to the x and y members
of the new Point object as shown in figure 2.6 here:
2.4.2
Overloading Methods:
In Java it is possible to define two or more methods within the same class that share the same name,
as long as their parameter declarations are different. When this is the case, the methods are said to be
Lab Manual - Software Development Tools Laboratory
8
VPCOE, Baramati
2.4. THEORY
developing classes,its objects, methods,constructor
Figure 2.6: Point object
overloaded, and the process is referred to as method overloading. Method overloading is one of the ways
that Java implements polymorphism. If you have never used a language that allows the overloading of
methods, then the concept may seem strange at first. But as you will see, method overloading is one of
Java’s most exciting and useful features.
When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide
to determine which version of the overloaded method to actually call. Thus, overloaded methods must
differ in the type and/or number of their parameters. While overloaded methods may have different
return types, the return type alone is insufficient to distinguish two versions of a method. When
Java encounters a call to an overloaded method, it simply executes the version of the method whose
parameters match the arguments used in the call.
2.4.3
Constructor:
-Methods declared having the same name as that of class name, This type of declaration are called as
constructor.
-Once defined, the constructor is automatically called immediately after the object is created, before
the new operator completes.
-Constructors look a little strange because they have no return type, not even void.
2.4.4
”this” Keyword:
-Sometimes a ”method” will need to refer to the ”object” that invoked it or call it. To allow this, java
defines the ”this” keyword.
-”this” is always a reference to the object on which the method was invoked or called.
-You can use ”this” anywhere a reference to an object of the current class type is permitted.
Lab Manual - Software Development Tools Laboratory
9
VPCOE, Baramati
2.5. PROCEDURE
2.4.5
developing classes,its objects, methods,constructor
Overloading Constructor:
-In addition to overloading normal methods, you can also overload constructor methods.
-In overloading constructor, class name and method name both are same, only parameter passes are
different. That type of overloading is called as overloading constructor.
2.5
Procedure
1. create department class
2. create two or more constructors having parameters are different.
3. create two or more salary methods to calculate salary of teaching and non teaching staff.
4. use this keyword in any one method to create reference of current class
5. create another constructoroverloadingdemo class and define main method in it.
6. create different objects of department class in main method.Differentiate constructors by passing
different arguments
7. call the all methods through objects.
2.6
Post Lab
Hence from this assignment you can learn how to create class, define methods, object creation and
constructor overloading.
2.7
Viva Questions
1. What is mean by constructor? and How this constructor get call?
2. What is the use of ”this” keyword?
3. In java how dynamically allocated memory for object is released or how such objects are destroyed
for later reallocation?
4. What is the use of Garbage Collector in java?
5. What is mean by Method Overloading?
6. What is mean by Constructor Overloading?
7. What is the default Data Type of all members of a class in Java?
Lab Manual - Software Development Tools Laboratory
10
VPCOE, Baramati
Assignment 3
Inheritance
3.1
Problem Statement
Write a program to inherit a class ”Department” from ”Company” class and its members using extends
keyword.
3.2
Pre Lab
Knowledge of programming in C . Knowledge of object oriented concepts of C++ and programming in
C++.
3.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
3.4
3.4.1
Theory
Inheritance:
One of the strengths of object-oriented programming is inheritance. This feature allows you to create a
new class that is based on an old class.
Def: super class having all methods and variables is accessed by its subclass are called ”Inheritance”.
11
3.4. THEORY
3.4.2
Inheritance
Types of Inheritance:
There are five types of inheritance: a) Single b)multilevel c)hierarchical d) hybrid e) Multiple inheritance.
Note: Multiple inheritance is not possible in java -Keyword ”extends” used to create a subclass of
superclass. Syntax:
Figure 3.1: Syntax of Inheritance
3.4.3
Access specifiers in Inheritance:
a) public: ’public’ members of superclass is accessible to subclass.
b) private: ’private’ members of superclass is not accessible to subclass.
c) protected: ’protected’ members of superclass is accessible to subclass.
3.4.4
Inheritance using ”super”:
-Private data members basically not accessible to subclass due to encapsulation.
-Whenever a subclass needs to refer to its nearer superclass, it can do so by use of the k/w ”super” to
access private data also.
”super” has two general forms:
A) Using ”super” to calls the ”superclass constructor”. A subclass can call a constructor method defined
by its superclass by use of the following form of super.
Syntax:
Super(parameter-list);
B) The 2nd is used to access members of the superclass that has been hidden by a members of a
subclass. The 2nd form of super acts somewhat like this, except that it always refers to the superclass
of the subclass in which it is used.
Syntax:
Super.member
Here, member can be either a method or an instance variable.This 2nd form of super is most applicable
Lab Manual - Software Development Tools Laboratory
12
VPCOE, Baramati
3.4. THEORY
Inheritance
to situations in which ’member names’ of a subclass hide members by the same name in the superclass.
3.4.5
Method overriding:
If a method in subclass having same name and type as that method of superclass then it is called as
’overridden’ of method of subclass, And that superclass method makes hidden by the subclass.
If you wish to access the superclass version of an overridden function. You can do so by using super.
Figure 3.2: Method overriding
3.4.6
Using final with inheritance:
There are three uses of final keyword:
1) 1st it can be used to create the equivalent of a ’named constant’ this use was described in previously
2) The other two uses of final apply to inheritance is as follows:
A) Using final to prevent overriding:
-To disallow a method from being overridden, specify final as a modifier at the start of its declaration.
-Methods declared as ”final” cannot be overridden
Lab Manual - Software Development Tools Laboratory
13
VPCOE, Baramati
3.5. PROCEDURE
Inheritance
syntax:
Figure 3.3: Defining Final method
Because of method declared as final in superclass Test, it cannot override in its child class. When
you trying to do this then compile time error is occurred.
B) Using final prevent inheritance:
-Sometimes you will want to prevent a class from being inherited. To do this, precede the class declaration with final.
-Declaring class as final implicitly declares all of its method as final.
Syntax:
Figure 3.4: Defining Final class
3.5
Procedure
1. first create parent class as Company
2. then inherit class Departments from company
3. also create two or more child classes of department.(i.e.Manufacturing, Storedept, Saledept)
4. also use super in child classes
Lab Manual - Software Development Tools Laboratory
14
VPCOE, Baramati
3.6. POST LAB
3.6
Inheritance
Post Lab
hence from this assignment you learn creation of Inheritance in java. Also use of super, final and abstract
classes.
3.7
Viva Questions
1. What is the use of ”static” k/w in java?
2. What are the restrictions when methods declared as static?
3. At Which time static block get executed?
4. How you call static methods from outside its class?
5. How to declare constant in java?
6. What is mean by nested classes?
7. What is the scope of Nested(inner) class in their outer class ?
8. Which k/w is used to create Inheritance in java?
9. Which are the access specifiers in inheritance?
10. Which are the two uses of ”super” in Inheritance?
11. What is the syntax of ”super” to call superclass constructor?
12. What is the basic difference of ”this” and ”super” keyword?
13. What is mean by method overriding?
14. How to access overridden function in child class?
15. Which keyword is used in Inheritance to refer nearer class member?
16. Which keyword is used to prevent method overriding in Inheritance?
17. How to prevent Inheritance?
18. What is mean by abstract class?
19. Which keyword is used to declare abstract class and method?
20. What are the restrictions of abstract class?
Lab Manual - Software Development Tools Laboratory
15
VPCOE, Baramati
3.7. VIVA QUESTIONS
Inheritance
21. Why abstract class contain no any private method?
22. Why abstract class contain no any abstract constructor?
23. Why method overriding is prevented in abstract class?
24. How you create multiple inheritance in java?
25. What are the members of interface class?
26. The constants in interface as always by default , and .. .
27. The methods in an interface class are always by default and but these can never be .
28. Can we create object of interface class?
29. How we can declare interface class?
Lab Manual - Software Development Tools Laboratory
16
VPCOE, Baramati
Assignment 4
Packages and Interface
4.1
Problem Statement
a) Write a program to create nested packages ”Company and Department” using package keyword those
can be used in ”Employee” class.
b) Write a program to create interface ”Conversions” that can be implemented by ”Convert” class.
4.2
Pre Lab
Knowledge of programming in C .
Knowledge of object oriented concepts of C++.
4.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
4.4
Theory
4.4.1
Abstract classes:
- An abstract class is a class in which one or more methods declared but not defined, that means the
bodies of the method are neglected. Such methods are called as ”abstract methods”.
- To declare class abstract simply use the ”abstract” k/w in front of the class k/w in class declaration.
- A abstract class is superclass that only defines a generalize form which will be shared by all of its
subclass.
17
4.4. THEORY
Packages and Interface
Restrictions:
-Abstract methods cannot be private because private methods cannot be inherited.
-An object of abstract class cannot be instantiated using new k/w, whereas you can declare variable of
a type abstract class.
-There is no need to define all methods of abstract class within its subclass.
-Also you cannot declare abstract constructor or abstract Static methods.
-Method overriding is not possible or prevented.
-Any subclass must implement all ”abstract class methods” declared in superclass.
4.4.2
Interface:
- We already know that multiple inheritances are not permitted in java.
- But in real life application we need to inherit methods and properties from several distinct classes.
- To support the concept of multiple inheritances java provide the alternative approach known as interface.
- An interface is a collection of constant or abstract methods or both
- Interface does not define what method dose ( i.e no method body)
- The constant in interface as always by default public static and final.
- The methods in an interface are always by default public and abstract, these can never be static.
- We cannot create object of interface.
Figure 4.1: Interface Syntax
- In implement inherited class contain abstract method body is defined, that method always defined
as public.
4.4.3
Packages:
-Packages are container for classes.
-You can define classes inside a package that are not accessible by code outside that package.
Defining a package:
General form of package statement:
Lab Manual - Software Development Tools Laboratory
18
VPCOE, Baramati
4.5. PROCEDURE
Packages and Interface
Syntax:
package pkg;
Here, pkg is the name of package
For e.g. the following statement creates a package called MyPackage.
e.g. package MyPackage;
Multileveled package statements:
You can create a hierarchy of packages. To do so, simply separate each package name from the one
above it by use of a period.
The general form of multileveled package statement is shown as follows.
package pkg1[.pkg2[.pkg3]];
- You cannot rename a package without renaming the directory in which the classes are stored.
e.g mypack :is directory(package name)
pack1.java :is the class file inside the mypack package
Note: main function i.e. public static void main(String args[]) is not present into the package classes
files.
In figure 4.2 first e.g class pack1 is one of the class in package(i.e. mypack). In this class pack1
contain no any main function is present.
4.5
Procedure
A)for implementing assignment on package.
1. first create Company package and define classes inside it.
2. then after words create another nested package Department inside the Company package then
define classes inside it.
3. Also create Employee class outside to the packages
4. compile all the classes.
5. import or use the packages and their classes and its members in Employee class.
Lab Manual - Software Development Tools Laboratory
19
VPCOE, Baramati
4.6. POST LAB
Packages and Interface
B)for implementing assignment on interface.
1. Create interface Conversions
2. Declare abstract methods mltoliter() and litertoml().
3. Create Convert class and implements Conversions interface in it.
4. define the abstract methods of Conversion interface in it
5. Create another InterfaceDemo class
6. create object of Convert class
7. Create reference variable of Conversions interface.
8. assign object to reference variable.
9. through this reference variable call the methods.
4.6
Post Lab
Hence from this assignment you learn creation of packages and How to create multiple inheritance by
using interface.
4.7
Viva Questions
1. What is mean by packages?
2. How we define package?
3. How we can create multileveled (or Nested) packages?
4. What is the use of interface?
5. What type of methods are present in interface class?
6. What is the access specifiers of methods that when we implement in implementation class and
that are abstract in interface class?
Lab Manual - Software Development Tools Laboratory
20
VPCOE, Baramati
4.7. VIVA QUESTIONS
Packages and Interface
Figure 4.2: package example
Lab Manual - Software Development Tools Laboratory
21
VPCOE, Baramati
Assignment 5
Exception Handling
5.1
Problem Statement
Write a program to handle exception (e. g. Arithmetic Exception, Array Index out of bound) in java
using try-catch block.
5.2
Pre Lab
Knowledge of programming in C. Knowledge of object oriented concepts of C++.
5.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
5.4
5.4.1
Theory
Exception Handling:
-An Exception is an ”abnormal condition” that arises( or occurred) in a code sequence at run time.
-In other words, an exception is a run time error.
-In computer language like ”c” that do not support exception handing.
-Java exception handling is managed via 5 keywords: try, catch, throw, throws, and finally.
-Briefly, here is how they work:
a) try : program statements that you want to monitor for exception are contained within try block.
b) catch: if an exceptions occurs within the try block, it is thrown. Your code can catch this exception
22
5.4. THEORY
Exception Handling
(using catch) and handle it in some rational manner.
c) throw: system generated exceptions are automatically thrown by the java un time system. To
manually throw an exception, use the keyword throw.
d) throws: any exception that is thrown out of a method must be specified as such by a throws clause.
e) finally: any code that absolutely must be executed before a method returns is put in a finally block.
5.4.2
General form of an exception handling block:
Figure 5.1: Exception Handling Block
- In figure 5.1, ”Exception Type” is the type of exception that has occurred.
- Each try block must have at least one catch block present.
- Multiple catch blocks with one try are possible.
5.4.3
multiple catch clauses:
-In some cases, more than one exception could be occurred by a single piece of code.
-To handle this type of situation, you can use two or more ”catch” clauses, each catching a different
type of exception.
-When an exception is thrown, each catch statement is inspected in order, and the 1st one whose type
matches that of the exception is executed.
-After one catch statement executes, the other are bypassed, and execution continues after the try/catch
Lab Manual - Software Development Tools Laboratory
23
VPCOE, Baramati
5.4. THEORY
Exception Handling
block.
5.4.4
Nested try statements:
- The try statement can be nested.
- i.e., a try statement can be inside the block of another try.
- If an inner try statement does not have a catch handler for a particular exception, the next try
statements catch handlers are inspected for a match.
- This continuous until one of the catch statement succeeds, or until the entire nested try statements
are exhausted.
- If o catch statement matches, then the java run time system will handle the exception.
5.4.5
throw k/w:
- So far you have only been catching exceptions that are thrown by the java run time system.
- However, it is possible for your program to throw an exception explicitly, using the throw statement.
- General syntax of throw is as follows:
throw Exceptionobject;
- The flow of execution stops immediately after the ”throw” statement; any subsequent statements
are not executed.
- The nearest enclosing try block is inspected to see if it has a catch statement that matches the type
of the exception. If it does find a match, control is transferred to that statement. if not then the next
enclosing try statement is inspected, and so on. If no matching catch is found, then the default exception
handler halts the program and print the stack trace.
5.4.6
throws k/w:
-If a method is capable of causing an exception that it does not handle, it must specify this behavior so
that callers of the method can guard themselves against that exception.
-You do this by including a ”throws” clause in the methods declaration.
-A throws clause list the types of exceptions that a method might throw.
-This is necessary for all exceptions except those of type Error or RuntimeException.
Syntax: General syntax of a method declaration that includes a ”throws” clause is shown in figure 5.2:
in figure 5.2, ’Exception list’ is a comma separated list of the exceptions that a method can throw.
Lab Manual - Software Development Tools Laboratory
24
VPCOE, Baramati
5.5. PROCEDURE
Exception Handling
Figure 5.2: throws syntax
5.4.7
finally clause:
-Finally creates block of code that will be executed after a try catch block has completed and before
the code following the try/catch block.
-The finally block will execute, whether or not an exception is thrown.
-If an exception is thrown, the finally block will execute even if no ”catch” statement matches the exception.
Uses of finally:
- This can be useful for closing file handles and freeing up any other resources that might have been
allocated at beginning of method.
- ”finally” clause optional. However each try statement requires at least one catch or a finally clause.
5.5
Procedure
1. declare a class Exceptiondemo.
2. inside the main method write try with multiple catch block.
3. place all the code in try block that contain ArithmeticException and ArryIndexOutOfBoundsException.
4. Handle both the Exception with the help of two catch block respectively.
5.6
Post Lab
Hence from this assignment you learn exception handling by using try, catch, throw, throws, and finally
keywords.
5.7
Viva Questions
1. What is mean by Exception?
Lab Manual - Software Development Tools Laboratory
25
VPCOE, Baramati
5.7. VIVA QUESTIONS
Exception Handling
2. In java how Exception handling is managed?
3. What is the use of try and catch block?
4. What is the use of ”throw” clause in Exception handling?
5. What is the use of ”throws” clause in Exception handling?
6. What are the uses of finally block?
7. At which time finally block get executed?
Lab Manual - Software Development Tools Laboratory
26
VPCOE, Baramati
Assignment 6
Multithreading
6.1
Problem Statement
Write a program to create a Bouncing Ball as example of use of Multithreading.
6.2
Pre Lab
Knowledge of programming in C .
Knowledge of object oriented concepts of C++.
Knowledge of Multithreaded programming
Knowledge of Thread class, Runnable interface and its different methods.
6.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
6.4
6.4.1
Theory
Multithreaded Programming:
Definition: ”multithreaded program” contain two or more parts (i.e threads) that can run concurrently
(or same time) are called as multithreaded programming.
27
6.4. THEORY
6.4.2
Multithreading
The ”Thread” class and ”Runnable” interface:
Thread class defines several methods that help to manage threads as shown in figure 6.1.
Figure 6.1: Thread class methods
-The reminder of this assignment explains how to use ”Thread class ” and ”Runnable interface” to
create and manage threads.
6.4.3
Main Thread:
-If any java program starts up, one thread running immediately called it as main thread.
-It is important for two reasons:
i) Main thread is a thread on which child threads are created.
ii) Often it must be the last thread to finish execution because it performs various shutdown actions.
-Although the main thread is created automatically when your program is started,
-It can be controlled through a ”Thread class” object. To do so, you must obtain a reference to it by
calling the method ”currentThread()”. Which is the public static member of Thread.
-Its general Syntax is shown here:
Static Thread.currentThread()
This method (i.e currentThread()) returns a ”reference” to the thread in which it is called. Once
you have a reference to the ”main thread” you can control it just like any other thread.
6.4.4
Creating a thread:
-In the most common method you create a ’thread’ by instantiating an object of type ”Thread” class.
-In java there are defines two ways to create the Threads:
Lab Manual - Software Development Tools Laboratory
28
VPCOE, Baramati
6.4. THEORY
Multithreading
I) You can implement the ”Runnable interface”.
II) You can extend the ”Thread” class, itself.
I) Implementing Runnable:
- The easiest way to create a ’thread’ is to create a class that implements Runnable interface. Runnable
abstracts the unit of executable code.
- You can construct a thread on any object that implements Runnable
- To ’implement Runnable’, a class need only implement a single method called ”run”. which is declared
like this.
Syntax: public void run()
- Inside run(), you will define the code that constitutes the new thread.
- It is important to understand that ’run()’ can call other methods, use other classes, and declare variables, just like the main thread can.
- The only difference is that ”run()” establishes the entry point for another, concurrent thread of execution within your program.
- This ’thread’ will end when ’run()’ returns.
- After you create a class that implements ’Runnable’ interface, you will instantiate an object of type
”Thread” from within that class.
- ”Thread” defines several constructors. The one that we will use is shown here:
Thread (Runnable threadob, String threadName) // constructor
- In this constructor, ’threadob’ is an instance of a class that implements the Runnable interface.
This defines where execution of the thread will start. The name of the new thread is specified by
’threadName’.
- After the new thread is created, it will not start running until you call its ’start()’ method, which is
declared within Thread class.
- In essence, ”start()” executes call to ’run()’.
- The start method syntax is as follows:
void start(); //run() calls the start() method.
Lab Manual - Software Development Tools Laboratory
29
VPCOE, Baramati
6.5. PROCEDURE
6.4.5
Multithreading
Extending ”Thread”:
- First create a new class that extends ”Thread” class, and then to create an instance of that class.
- The extending class must override the ”run()” method, which is the entry point of the new thread.
- Run() method must also call by using ”start()” method to start execution of the new thread.
6.4.6
Creating multiple threads:-
-So far, we have been seen only two threads: the main thread and child thread.
-However, your program can create many threads as it needs.
6.4.7
Using isAlive() and join():
- There are, 2 ways exist to determine whether a thread has finished.
- (I) you can call ”isAlive()” on the ”Thread”. This method is defined by ’Thread’, and its general
syntax is as follows:
final Boolean isAlive()
- True: the isAlive method returns ’true’ if the thread upon which it is called is still running.
- False: otherwise it returns ’false’
- (II)while isAlive() is occasionally useful the method that you will more commonly use to wait for a
thread to finish is called ”join()”
Syntax:
final void join() throws InterruptedException
6.5
Procedure
1. Create child window in applet by extending Frame class.
2. Then use Graphics class that present in AWT to fill Oval in frame and
3. then animate them by changing coordinates.
Lab Manual - Software Development Tools Laboratory
30
VPCOE, Baramati
6.6. POST LAB
6.6
Multithreading
Post Lab
Hence from this assignment you learn how to create multiple child threads in Main thread by extending
Thread class and implementing Runnable interface.
6.7
Viva Questions
1. What is mean by Multithreaded Programming?
2. Which are the methods defined by ”Thread” class to manage thread?
3. What is the use of isAlive() and join() method of Thread class in multithreading?
4. Why main thread is important?
5. How we can get reference of main thread so that we can control it?
6. Which are the two methods for creation of child threads?
7. What is the use of run() method and who is calling this run() method?
Lab Manual - Software Development Tools Laboratory
31
VPCOE, Baramati
Assignment 7
I/O classes in java
7.1
Problem Statement
Write a java program to read from txt file and write to console using I/O classes in java.
7.2
Pre Lab
Knowledge of programming in C .
Knowledge of object oriented concepts of C++.
Knowledge of I/O classes in JAVA.
7.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
7.4
7.4.1
Theory
Writing console output:
-Console output is most easily accomplished with print() and println(), described earlier, which are used
in most of the examples.
-These ’methods’ are defined by the class ’PrintStream’ (which is the type of the object referenced by
System.out).
-PrintStream class also implements the low level method ”write()”. Thus, ’write()’ can be used to
write to the console. -You will not often use write() to perform console output, because ”print()” and
32
7.4. THEORY
I/O classes in java
”println()” are substantially easier to use.
7.4.2
The PrintWriter class:-
- Although using ’System.out’ to write to the console is still permissible under java.
- For real world programs, the recommended method of writing to the console when using java is through
a PrintWriter stream.
- ’PrintWriter’ is one of the character based classes.
- PrintWriter defines several constructors. The one we will use is shown here:
PrintWriter(OutputStream outputstream, Boolean flushonnewline)
- Here , flushonnewline controls whether java flushes the output stream every time a println() method
is called. If flushonnewline is true, flushing automatically takes place. If false, flushing is not automatic.
- ’PrintWriter’ supports the print() and println() methods for all types including object. Thus you can
use these methods in the same way as they have been used with System.out.
- To write to the console by using a ”printWriter’, specify ’System.out’ for the o/p stream and flush the
stream after each newline.
For e.g, this line of code creates a ”PrintWriter” that is connected to console output.
PrintWriter pw=new PrintWriter(System.out,true);
7.4.3
Reading and Writing Files:
- Java provides a number of classes and methods that allows you to read and write files.
- In java, all files are ’byte oriented’, and java provides methods to read and write bytes from and to a
file.
- In java there are two stream classes are used.
1) FileInputStream
2) FileOutputStream
- Both these classes create byte streams linked to files.
- To open a file, you simply create an object of one of these classes, passing the name of the file as an
argument to the constructor.
- While both classes support additional, overridden constructors, the following are the forms that we
will be using.
Lab Manual - Software Development Tools Laboratory
33
VPCOE, Baramati
7.5. PROCEDURE
I/O classes in java
FileInputStream(String fileName)throws FileNotFoundException
FileOutputStream(String fileName)throws FileNotFoundException
- Here ,filename- indicates the name of file that you want to open.
- When you create an input stream, if the file does not exist, then FileNotFoundException is thrown.
- For OutputStreams, if the file can not be created, then FileNotFoundException is thrown.
- Note: in earlier versions of java, FileOutputStream() throw an IOException when an output file could
not be created. This was changed by Java2.
- When you are done with a file , you should close it by calling ’close()’. It is defined by both classes(i.e
FileInputStream and FileOutputStream), as shown here
void close() throws IOException //syntax of closing file.
- To read from a file , you can use a method read() that is defined within FileInputStream.
- Syntax:
Int read() throws IOException
- Each time that it is called , it reads a single(1) byte from the file and returns the byte as an integer
value.
- read() returns -1 when the end of file is occurred. It can throw an IOException.
- The following program uses read() to i/p and display the content of the text file, the name of which
is specified as a command line argument. you can use this same approach whenever you use command
line arguments.
7.4.4
Writing File:
- To write to a file, you will use the ’write()’ method defined by FileOutputStream class.
- Its simplest syntax is shown here:
void write(int byteval)throws IOException
- This method writes the byte specified by byteval to the file. Although byteval is declared as an integer.
Only the low order eight bits are written to the file.
- If an error occurs during writing, an IOException is thrown.
7.5
Procedure
1. create a one text file that to be we are reading.
Lab Manual - Software Development Tools Laboratory
34
VPCOE, Baramati
7.6. POST LAB
I/O classes in java
2. create a class
3. in main method declare variable of type FileInputStream and FileOutputStream ;
4. for opening i/p file create object such as fin=new FileInputStream(args[0])and pass file name that
you want to read.
5. for opening another o/p file to write data inside it ,create object such as fout=new FileOutputStream(args[1]);
6. read source file by using fin.read() method and
7. copy data in destination file by using fout.write();
7.6
Post Lab
Hence from this assignment you learn use of JAVA I/O classes to create new file and Read data from
that file.
7.7
Viva Questions
1. What is the use of BufferedReader and DataInputStream class in java?
2. What is the use of FileInputStream class?
3. What is the use of FileOutputStream class?
4. How you open a file that you want to read or write?
Lab Manual - Software Development Tools Laboratory
35
VPCOE, Baramati
Assignment 8
Applet in java
8.1
Problem Statement
Write a program to construct Applet containing control item such that we can change its state.
8.2
Pre Lab
Knowledge of programming in C.
Knowledge of object oriented concepts of C++.
8.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
8.4
Theory
8.4.1
Applet:
-Applets are small application that are accessed on an internet server, transported over the internet,
automatically installed, and run as part of a web document.
-Applets are differ from applications in several key areas
-Let’s start with the simple applet shown in figure 8.1:
-In above program first statement imports the ’awt’ (Abstract Window Toolkit) package containing
all classes.
-Applets interact with the user through the AWT, not through the console based I/O classes.
36
8.4. THEORY
Applet in java
Figure 8.1: Simple Applet
-The AWT contains support for a ”windows-Based”, graphical interface.
-The second statement imports the ”applet” package, which contains the class Applet.
-Every ’applet’ that you create must be a childclass of ”Applet”.
-After that we create ’SimpleApplet’ class, and that class is subclass of ’Applet’ class. This class must
be declared as ’public’, because it will be accessed by code that is outside the program.
-Inside ’SimpleApplet’, ’paint()’ is declared. This method is defined by the AWT and must be overridden
by the applet. -’paint()’ is called each time that the ’applet’ must ’redisplay’ its output. ”paint()” is also
called when the applet begins execution. -The ’paint()’ method has one parameter of type ’Graphics’.
This parameter contains the graphics context, which describes the graphics environment in which the
applet is running. This context is used whenever o/p to the applet is required. -Inside the ’paint()’,
’drawString()’ method is called, which is a member of the ’Graphics’ class. -This method outputs a
string beginning at the specified x, y location. It has following general syntax:
void drawString(String msg, int x, int y)
-In a java window, the upper left corner is location 0,0. The call to drawString() in the applet causes
the msg ”A simple applet” to be displayed beginning at location 20,20.
-NOTE: Applet does not have a main() method. Unlike java programs, applets do not start execution
at main(). Instead, an applet start execution when the name of its class is passed to an ’applet viewer’
or to a network browser.
-There are 2 ways in which you can run an applet :
1) Executing the applet within a java-compatible web browser(e).
2) Using an ”applet viewer”, such as the standard SDK tool, ’appletviewer’. An ”applet viewer” executes
your applet in window.
Each of these method described below: - To execute an applet in a ’web browser’, you need to write a
Lab Manual - Software Development Tools Laboratory
37
VPCOE, Baramati
8.4. THEORY
Applet in java
short HTML text file that contains the appropriate applet tag. figure 8.2 is the HTML file that executes
SimpleApplet.
Figure 8.2: Simple Applet
After you create this file, you can execute your browser and then load this file, which causes simple
applet to be executed.
To execute ”SimpleApplet”, with an ”appletviewer ” you may also execute the HTML file shown earlier.
For e.g, if the preceding HTML file is called ”RunApp.html”, then the following command line will run
’SimpleApplet’:
C : appletviewer RunApp.html
or use following command:
C : javac SimpleApplet.java
C : appletviewer SimpleApplet.java
After execution of above cmd. The window produced by ’SimpleApplet’, as displayed by the ”appletviewer” is shown in the following illustration.
8.4.2
An Applet Skeleton:-
- All but the most trivial applets override a set of methods that provides the basic mechanism by which
the ’browser’ or ’applet viewer’ interfaces to the applet and controls its execution. 4 of these methods
i)init() ii)start() iii) stop(), and iv)display() are defined by the applet. Another, paint(), is defined by
the AWT component class.
- Default implementations for all of these methods are provided. Applets do not need to override those
methods they do not use.
Lab Manual - Software Development Tools Laboratory
38
VPCOE, Baramati
8.4. THEORY
8.4.3
Applet in java
Applet Initialization and Termination:
-When an applet started, the AWT calls the following methods, in this sequence: 1) init() 2) start 3)
paint()
-When an applet is terminated, the following sequence of methods calls takes place: 1) stop() 2) destroy()
All should execute each time as applet program is loaded hence called as life cycle of applet.
8.4.4
more closely at these methods:
1)init(): the init() method is the 1st method to be called. This is where you should ”initialize variables”.
2)start(): this method called 2nd time, after init().it is also called to ”restart an applet” after it has
been stopped.
3)paint(): paint() method called when an applets window must be restored. Also paint() method redisplay contents of window. paint() is also called when the applet begins execution. The paint() method
has one parameter of type Graphics. Parameter will contain the graphics context, which describes the
graphics environment in which the applet running.
4)stop(): method is called when the applet is stopped. or it suspends execution of applet.
5) destroy(): this method is called when the environment determines that your applet needs to be
removed completely from memory.
8.4.5
Simple Applet Display Method:
-As we have mentioned, applets are displayed in a window and they use the AWT to perform i/p and
o/p.
-Although we will examine the methods, procedures and techniques necessary to fully handle the ’AWT’
window environment in subsequent chapters. A few are described here, because we will use them to
write ’sample applet’.
-In previous program, to output a string to an applet , use drawstring() method, which is a member(method) of the Graphics class. Typically, it is called from within either paint().
void drawString(String msg, int x, int y)
- Here, msg is the string to be o/p beginning at x,y. In a java window, the upper-left corner is
location 0,0.
- The drawstring() method will not recognize newline characters. If you want to start line of text on
another line, you must do so manually, specifying the precise x,y location where you want the line to start.
Lab Manual - Software Development Tools Laboratory
39
VPCOE, Baramati
8.4. THEORY
Applet in java
Methods:
1) setBackground(): to set the background color of an applet’s window, use setBackground() method.
2) setForeground(): to set the foreground color of an applets window, use setForeground() method.
These methods are defined by Component,
Syntax:
void setBackground(Color.newColor)
void setForground(Color.newColor)
- A good place to set the foreground and background colors is in the ’init()’ method. Of course, you
can change these colors as often as necessary during the execution of your applet.
The default Foreground color is ”Black”
The default Background color is ”light gray”.
- You can obtain the current settings for the B.G and F.G. colors by calling getBackground() and getForeground(), respectively.
- They are also defined by Component and are shown here:
Color getBackground()
Color getForeground()
8.4.6
Requesting Repainting:
- As a general rule, an applet writes to its window only when its update() or paint() method is called
by the AWT.
- This raises an interesting question:
How can the applet itself cause its window to be changed when its information changes ?
For e.g. If an applet is displaying a moving banner, what mechanism dose the applet use to update the
window each time this banner scrolls ?
- The applet is that it must quickly return control to the ”AWT run time system”.
- It cannot create a loop inside paint() that repeatedly scrolls the banner, for e.g. this would prevent
control from passing back to the AWT.
- Whenever your applet needs to update the information displayed in its window, it simply calls ”repaint()”
- The repaint() method is defined by the AWT. It causes the AWT run-time system to execute a call to
your applets update() method, which in its default implementation, calls paint().
Lab Manual - Software Development Tools Laboratory
40
VPCOE, Baramati
8.5. PROCEDURE
Applet in java
- Thus, for another part of your applet to o/p to its window, simply store the o/p and Then call ”repaint()”. The AWT will then execute a call to paint(), which can display the stored information.
- For e.g. if part of your applet needs to o/p a string, it can store this string in a String variable and
then call repaint().
- Inside ’paint()’ you will output the string using drawString().
- The repaint() method has 4 forms:
1)The simplest version of repaint() is shown here:
void repaint() //entire window to be repainted
2)The following version shows a ”region” that will be repainted:
void repaint(int left, int top, int width, int height)
void repaint(long maxDelay)
void repaint(long maxDelay, int x, int y, int width, int height)
8.5
Procedure
Applet for changing the font of text.
1. create the Fchange class that extends from Applet class and it implements ActionListener, ItemListener interface
2. declare the variables of each control class type.
3. Define the life cycle methods of Applet
4. in init() method do the initialization by creating object using new keyword.
5. also in init() method set the Layout of each control.
6. do the registry of applet with the ActionListener and ItemListener interface
7. by using paint() method change the font of a msg
8. also define the abstract methods of listener such as actionPerformed() and itemStateChanged() to
handle the events.
8.6
Post Lab
Hence from this assignment you learn how to execute Applet application in Browser as well as how to
execute by using appletviewer.
Lab Manual - Software Development Tools Laboratory
41
VPCOE, Baramati
8.7. VIVA QUESTIONS
8.7
Applet in java
Viva Questions
1. Where is applet executed?
2. What is the life cycle of Applet?
3. Which methods get executed sequentially when applet application is loaded?
4. What is the use of init() method in Applet?
5. What is the use of paint() method in Applet?
6. What is the use of repaint() method?
7. What is the use of appletviewer in applet?
Lab Manual - Software Development Tools Laboratory
42
VPCOE, Baramati
Assignment 9
Abstract Window Toolkit
9.1
Problem Statement
Write a program to construct GUI of notepad using an Abstract Window Toolkit
9.2
Pre Lab
Knowledge of programming in C .
Knowledge of object oriented concepts of C++.
Knowledge of Core JAVA.
Knowledge about GUI of editor.
Knowledge of AWT programming.
9.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
9.4
9.4.1
Theory
Working with Frame Windows:
After the applet, the type of window you will most often create is derived from Frame. You will use it
to create child windows within applets, and top-level or child windows for applications. As mentioned,
it creates a standard-style window.
Frame supports these two constructors:
43
9.4. THEORY
Abstract Window Toolkit
Frame( )
Frame(String title)
The first form creates a standard window that does not contain a title. The second form creates a
window with the title specified by title. Notice that you cannot specify the dimensions of the window.
Instead, you must set the size of the window after it has been created. There are several methods you
will use when working with Frame windows. They are examined here.
Setting the Window’s Dimensions
The setSize( ) method is used to set the dimensions of the window. Its signature is shown here:
void setSize(int newWidth, int newHeight)
void setSize(Dimension newSize)
The new size of the window is specified by newWidth and newHeight, or by the width and height
fields of the Dimension object passed in newSize. The dimensions are specified in terms of pixels. The
getSize( ) method is used to obtain the current size of a window. Its signature is shown here:
Dimension getSize( )
This method returns the current size of the window contained within the width and height fields of
a Dimension object.
Hiding and Showing a Window:
After a frame window has been created, it will not be visible until you call setVisible( ). Itssignature
is shown here:
void setVisible(boolean visibleFlag)
The component is visible if the argument to this method is true. Otherwise, it is hidden.
Setting a Window’s Title:
You can change the title in a frame window using setTitle( ), which has this general form:
Lab Manual - Software Development Tools Laboratory
44
VPCOE, Baramati
9.4. THEORY
Abstract Window Toolkit
void setTitle(String newTitle)
Here, newTitle is the new title for the window.
Closing a Frame Window:
When using a frame window, your program must remove that window from the screen when it is
closed, by calling setVisible(false). To intercept a window-close event, you must implement the windowClosing( ) method of the indowListener interface. Inside windowClosing( ), you must remove the
window from the screen. The example in the next section illustrates this technique.
9.4.2
Menu Bars and Menus:
A top-level window can have a menu bar associated with it. A menu bar displays a list oftop-level menu
choices. Each choice is associated with a drop-down menu. This concept is implemented in Java by
the following classes: MenuBar, Menu, and MenuItem. In general, a menu bar contains one or more
Menu objects. Each Menu object contains a list of MenuItem objects. Each MenuItem object represents something that can be selected by the user. Since Menu is a subclass of MenuItem, a hierarchy of
nested submenus can be created. It is also possible to include checkable menu items. These are menu
options of type CheckboxMenuItem and will have a check mark next to them when they are selected. To
create a menu bar, first create an instance of MenuBar. This class only defines the default constructor.
Next, create instances of Menu that will define the selections displayed on the bar. Following are the
constructors for
Menu:
Menu( )
Menu(String optionName)
Menu(String optionName, boolean removable)
Here, optionName specifies the name of the menu selection. If removable is true, the pop-up menu
can be removed and allowed to float free. Otherwise, it will remain attached to the menu bar. (Removable menus are implementation-dependent.) The first form creates an empty menu.
Individual menu items are of type MenuItem. It defines these constructors:
Lab Manual - Software Development Tools Laboratory
45
VPCOE, Baramati
9.4. THEORY
Abstract Window Toolkit
MenuItem( )
MenuItem(String itemName)
MenuItem(String itemName, MenuShortcut keyAccel)
Here, itemName is the name shown in the menu, and keyAccel is the menu shortcut for this item.
You can disable or enable a menu item by using the setEnabled( ) method. Its form is shown here:
void setEnabled(boolean enabledFlag)
If the argument enabledFlag is true, the menu item is enabled. If false, the menu item is disabled.
You can determine an item’s status by calling isEnabled( ). This method is shown here:
boolean isEnabled( )
isEnabled( ) returns true if the menu item on which it is called is enabled. Otherwise, it returns
false.You can change the name of a menu item by calling setLabel( ). You can retrieve the current name
by using getLabel( ). These methods are as follows:
void setLabel(String newName)
String getLabel( )
Here, newName becomes the new name of the invoking menu item. getLabel( ) returns the current
name.You can create a checkable menu item by using a subclass of MenuItem called CheckboxMenuItem. It has these constructors:
CheckboxMenuItem( )
CheckboxMenuItem(String itemName)
CheckboxMenuItem(String itemName, boolean on)
Here, itemName is the name shown in the menu. Checkable items operate as toggles. Each time
one is selected, its state changes. In the first two forms, the checkable entry is unchecked. In the third
form, if on is true, the checkable entry is initially checked. Otherwise, it is cleared. You can obtain the
status of a checkable item by calling getState( ). You can set it to a known state by using setState( ).
These methods are shown here:
Lab Manual - Software Development Tools Laboratory
46
VPCOE, Baramati
9.5. PROCEDURE
Abstract Window Toolkit
boolean getState( )
void setState(boolean checked)
If the item is checked, getState( ) returns true. Otherwise, it returns false. To check an item, pass
true to setState( ). To clear an item, pass false.
Once you have created a menu item, you must add the item to a Menu object by using add( ), which
has the following general form:
MenuItem add(MenuItem item)
Here, item is the item being added. Items are added to a menu in the order in which the calls to
add( ) take place. The item is returned.Once you have added all items to a Menu object, you can add
that object to the menu bar by using this version of add( ) defined by MenuBar:
Menu add(Menu menu)
Here, menu is the menu being added. The menu is returned. Menus only generate events when an
item of type MenuItem or CheckboxMenuItem is selected. They do not generate events when a menu
bar is accessed to display a dropdown menu, for example. Each time a menu item is selected, an ActionEvent object is generated. Each time a check box menu item is checked or unchecked, an ItemEvent
object is generated. Thus, you must implement the ActionListener and ItemListener interfaces in order
to handle these menu events. The getItem( ) method of ItemEvent returns a reference to the item that
generated this event. The general form of this method is shown here:
Object getItem( )
9.5
Procedure
1. create Menuframe by extending from Frame class
2. create menubar by using MenuBar class
3. create the menu items such as File, Edit by using Menu class
4. create menuitems such as cut, copy, paste, new, save etc by using MenuItem class
5. create checkable menu items by using CkeckboxMenuItem class.
Lab Manual - Software Development Tools Laboratory
47
VPCOE, Baramati
9.6. POST LAB
Abstract Window Toolkit
6. create an object to handle action and item event.
7. register it to recieve those events.
8. create an object of MyWindowAdapter class to handle window events
9. register it to recieve those events
10. implement the ActionListener, and ItemListener interface to handle diffrent events generated by
menu items.
9.6
Post Lab
Hence from this assignment you learn how to create editor by using AWT classes.
9.7
Viva Questions
1. Which class is present at the top of the AWT hierarchy?
2. What is the use of Frame class?
3. When we select Menu Item from the Menu then which type of event is occurred?
4. When we select CheckoxMenuItem from Menu then which type of event is occurred?
Lab Manual - Software Development Tools Laboratory
48
VPCOE, Baramati
Assignment 10
Event Handling
10.1
Problem Statement
Write a program to extend calculator application to demonstrate event handling in java.
10.2
Pre Lab
Knowledge of programming in C .
Knowledge of object oriented concepts of C++.
Knowledge of Event classes .
Knowledge of interface classes and its abstract methods.
10.3
Hardware and Software Requirement
Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.
10.4
Theory
10.4.1
Event Handling:
- As we have seen that applets are event driven programs. Thus, event handling is at the core of
successful applet programming.
- Most events to which your applet will respond are generated by the user. These events are passed to
your applet in a verity of ways, with the specific methods depending upon the actual event.
- There are several type of events: 1)The most commonly ’handled events’ are those generated by the
49
10.4. THEORY
Event Handling
mouse, the keyboard, and various controls, such as push button.
- Events are supported by the
java.awt.event package
- The theory starts with an overview of java’s event handling mechanisms. It then examines the main
event classes and interfaces, and develops several examples that illustrate the fundamentals of event
processing.
10.4.2
Definition of ’Event’ and description of the roles of ’Sources’ and
’listeners’:
- A) Events: In the delegation model, an event is an object that describes a state change in a source.
- It can be generated as a consequence of a person interacting with the elements in a graphical user
interface GUI. E.g. some of the activities that cause events to be generated are pressing a button,
entering a character via the keyboard, selecting an item in a list, and clicking the mouse. Many other
user operations could also be cited as examples.
- Events may also occur that are not directly caused by interactions with a user interface for e.g. an
event may be generated when a timer expires, a counter exceeds a value, a s/w or h/w failure occurs,
or an operation is completed.
- You are free to define events that are appropriate for your application.
- B) Event Sources: a source is an object that generates an event. This occurs when the internal state
of that object changes in some way. sources may generate more than one type of event.
- A sources must register listener in order for the listeners to receive notification about a specific type
of event.
- Each type of event has its own registration method
Syntax:
public void addTypeListener(TypeListener el) //add listener method is provided by ”source”.
- Here, Type is the name of the event and el is a object(reference) to the event listener.
- For e.g. the method that registers a ’keyboard event’ listener is called ”addKeyListener()”. llly
- The method that registers a mouse motion listener is called ”addMouseMotionListener()”.
- When an event occurs, all registered listeners are notified and receive a copy of the event object. This
is known as multicasting the event.
- Some sources may allow only one listener to register
Syntax:
public void addTypeListener(TypeListener el)throws java.util.TooManyListenersException
- A source must also provide a method that allows a listener to unregister an interest in a specific type
of event.
Lab Manual - Software Development Tools Laboratory
50
VPCOE, Baramati
10.4. THEORY
Event Handling
Syntax:
Public void removeTypeListener(TypeListener el)
Here, Type:is the name of event
El:is a reference to the event listener.
For e.g: To remove a ’keyboard listener ’, you would call ”rmoveKeyListener()”
- The methods that add or remove listeners are provided by the ”source” that generates events.
- For e.g: the component class provides methods to add and remove keyboard and mouse event listener.
- C) EventListeners: A listener is an object that is notified when an event occurs. It has two major
requirements.
1)It must have been registered with one or more sources to receive notifications about specific types of
events.
2)It must implement methods to receive and process these notification
- The methods that receive and process events are defined in a set of interfaces found in java.awt.event
- For e.g. the ’MouseMotionListener” interface defines two methods to receive notifications when the
mouse is dragged or moved.
- Any object may receive and process one or both of these events if it provides an implementation of
this interfaces.
10.4.3
Event Classes:
- In java’s event handling mechanism uses the classes that represent events.
- At the root of the java event class hierarchy is ’EventObject’, which is in java.util.EventObject.
- It is the superclass of all events. Its one constructor is shown here:
EventObject(Object src)
Here, src is the object that generates this event.
- EventObject contains two methods:
1) getSource() //returns the source of event
2) toString() //returns the string equivalent of the event.
General syntax of getSource()
- the class ”AWTEvent”, defined within the ’java.awt’ package
EventObject -superclass of all AWT based events
Lab Manual - Software Development Tools Laboratory
51
VPCOE, Baramati
10.4. THEORY
Event Handling
AWTEvent -subclass
- all the classes that we are going to study, these all are subclasses of ”AWTEvent”.
To summarize:
EventObject: is a superclass of all events.
AWTEvent: is a superclass of all AWT events.
- The package ”java.awt.event” defines several type of events that are generated by various user interface
elements.
- Following table list out the most important of these event classes and provides a brief description of
when they are generated.
- The most commonly used constructors and methods in each class are described in the following section.
10.4.4
Event Classes: Main Event classes in java.awt.event
1) ActionEvent:
2) AdjustmentEvent
3) ComponentEvent
4) ContainerEvent
5) FocusEvent
6) InputEvent
7) ItemEvent
8) KeyEvent
9) MouseEvent
10) MouseWheelEvent
11) TextEvent
12) WindowEvent
10.4.5
Sources of events:
- Following are some of the user interface components that can generate the events described in the
previous section.
- In addition to these GUI elements, other components such as an applet, can generate events.
- For e.g. you receive ”Key” and ”Mouse” events from an applet.
- Event Source
1) Button
2) Checkbox
Lab Manual - Software Development Tools Laboratory
52
VPCOE, Baramati
10.5. PROCEDURE
Event Handling
3) Choice
4) List
5) MenuItem
6) Scrollbar
7) TextComponents
8) Window
10.4.6
Event Listener Interfaces:
- The event model has two parts:
A) Sources B) Listeners
- Listeners: are created by implementing one or more of the interfaces defined by the ”java.awt.event”
package.
- Source: when an event occurs, the event source call the appropriate method defined by the listener
and provides an ”event object” as its argument.
- Following are commonly used ’Listener Interfaces’:
1) KeyListener interface
Methods: void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
2) MouseListener interface
Method: void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePresseed(MouseEvent me)
void mouseReleased(MouseEvent me)
3) MouseMotionListener
4) MouseWheelListener.
10.5
Procedure
1. create the Frame window by using Frame class
Lab Manual - Software Development Tools Laboratory
53
VPCOE, Baramati
10.6. POST LAB
Event Handling
2. choose the layout manager
3. design the GUI of Caluculator by using the controls
4. implements the Actionlistener interface having abstract methods to handle the events that to be
generated by button sorces.
5. write the appropriate code for appropriate button event.
10.6
Post Lab
Hence from this assignment you learn how to handle the different type of events by using Event Handling
concept of JAVA.
10.7
Viva Questions
1. Which are the abstract methods present in KeyListener interface class to handle the keyboard
events?
2. Which are the abstract methods present in MouseListener interface class to handle the mouse
events?
3. Which are the abstract methods present in ActionListener interface class to handle the events?
4. Which are the abstract methods present in ItemListener interface class to handle the events?
5. What is mean by Event?
6. How events are generated?
7. Which package we import for handling the various type of events?
8. Which are the Event Source classes present in java.awt.event.* package?
Lab Manual - Software Development Tools Laboratory
54
VPCOE, Baramati
Assignment 11
java DB connectivity-JDBC
11.1
Problem Statement
Write a program to access Student database using JDBC connectivity with Access or Oracle and perform
insert, update,delete, search operation on it .
11.2
Pre Lab
Knowledge of programming in C .
Knowledge of object oriented concepts of C++.
Knowledge of SQL.
Knowledge of Oracle, SQL server, MS Access Databases.
11.3
Hardware and Software Requirement
Specify Hardware and Software Requirement.
11.4
Theory
11.4.1
What is JDBC?
- Working with leaders in the database field, Sun developed a single API for database access-JDBC.
As part of this process, they kept three main goals in mind:
I) JDBC should be a SQL-level API.
55
11.4. THEORY
java DB connectivity-JDBC
II) JDBC should capitalize on the experience of existing database APIs.
III)JDBC should be simple.
- A SQL-level API means that JDBC allows you to construct SQL statements and embed them inside
Java API calls.
- In short, you are basically using SQL. But JDBC lets you smoothly translate between the world of
the database and the world of the Java application. Your results from the database, for instance, are
returned as Java objects, and access problems get thrown as exceptions.
- Sun drew upon the successful aspects of one such API, Open DataBase Connectivity (ODBC). ODBC
was developed to create a single standard for database access in the Windows environment.
11.4.2
JDBC has four driver of connection:
1) JavaPure Driver
2) Java net API Driver
3) JDBC ODBC driver.
4) Third party driver
11.4.3
The Structure of JDBC:
- JDBC accomplishes its goals through a set of Java interfaces, each implemented differently by individual
vendors. The set of classes that implement the JDBC interfaces for a particular database engine is called
a JDBC driver. In building a database application, you do not have to think about the implementation
of these underlying classes at all; the whole point of JDBC is to hide the specifics of each database and
let you worry about just your application. Figure 11.1 illustrates the JDBC architecture. - If you think
about a database query for any database engine, it requires you to connect to the database, issue your
SELECT statement, and process the result set.
-In Example, you have the full code listing for a simple SELECT application from the Imaginary
JDBC Driver for. This application is a single class that gets all of the rows from a table in an MS Access
database located on my drive.
-First, it connects to the database by getting a database connection under my user id, from the JDBC
DriverManager class. It uses that database connection to create a Statement object that performs the
SELECT query.
-A ResultSet object then provides the application with the key and val fields from the test table.
Lab Manual - Software Development Tools Laboratory
56
VPCOE, Baramati
11.5. PROCEDURE
java DB connectivity-JDBC
Figure 11.1: JDBC Architecture
11.5
Procedure
1. first load the driver by using Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”); statement.
2. make connection with the database server by using Drivermanager.getConnection(”jdbc:odbc:ksb”,”scott”,”tiger”);
3. display the menu Insert,Update,Delete,Search operation
4. take keyboard through input
5. perform the operation on database by using PreparedStatement class
11.6
Post Lab
Hence from this assignment you learn JDBC concept of JAVA to create connectivity to any Database
by using specific Driver. Also performing different operations on that.
11.7
Viva Questions
1. What is mean by JDBC driver?
2. Which are the drivers in JDBC for Connection?
3. What is the use of following method of Class in JDBC? Class.forName();
Lab Manual - Software Development Tools Laboratory
57
VPCOE, Baramati
References
[1] Herbert Schildt, ”The Complete Reference JAVA2”, Tata McGraw Hill Publication
58
Download