0% found this document useful (0 votes)
7 views

Mca Java Lab Manual Lab 2nd Sem

The document is a lab manual for the Object-Oriented Programming with Java course (CSE22953) for MCA Semester 2. It outlines course objectives, outcomes, and a series of assignments covering various topics such as classes, method overloading, inheritance, and multithreading. Each assignment includes objectives, explanations, Java program implementations, expected outputs, and conclusions.

Uploaded by

student09hub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Mca Java Lab Manual Lab 2nd Sem

The document is a lab manual for the Object-Oriented Programming with Java course (CSE22953) for MCA Semester 2. It outlines course objectives, outcomes, and a series of assignments covering various topics such as classes, method overloading, inheritance, and multithreading. Each assignment includes objectives, explanations, Java program implementations, expected outputs, and conclusions.

Uploaded by

student09hub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

OBJECT ORIENTED

PROGRAMMING
WITH
JAVA
LAB MANUAL

MCA SEM 2

COURSE CODE: CSE22953


OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

Contents

Contents 1

1 Course Objectives 3

2 Course Outcomes 3

3 Catalogue Description 3

4 Assignment: Class and Constructor 4


4.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.2 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.3 Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.5 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

5 Assignment: Method Overloading 5


5.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.2 Program Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.3 Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.5 Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

6 Assignment: Inheritance and Method Overriding 6


6.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6.2 Program Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6.3 Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.5 Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

7 Assignment: Wrapper Classes and Arrays 7


7.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
7.2 Concepts Covered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.3 Java Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.5 Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

8 Assignment: Developing Interfaces - Multiple Inheritance and Extend-


ing Interfaces 9
8.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
8.2 Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
8.3 Code Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
8.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
8.5 Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
8.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

9 Assignment: Creating and Accessing Packages 10


9.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
9.2 Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
9.3 Code Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
9.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
9.5 Compilation and Execution . . . . . . . . . . . . . . . . . . . . . . . . . 11
9.6 Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
9.7 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

10 Assignment: Multithreaded Programming 12


10.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.2 Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.3 Code Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
10.5 Compilation and Execution . . . . . . . . . . . . . . . . . . . . . . . . . 13
10.6 Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
10.7 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

11 Assignment: Applet Programming 14


11.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
11.2 Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
11.3 Code Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
11.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
11.5 Creating an HTML File . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
11.6 Compilation and Execution . . . . . . . . . . . . . . . . . . . . . . . . . 15
11.7 Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
11.8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

12 Assignment: Designing an Applet for a Real-Time Web Page 15


12.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
12.2 Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
12.3 Code Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
12.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
12.5 Creating an HTML File . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
12.6 Compilation and Execution . . . . . . . . . . . . . . . . . . . . . . . . . 16
12.7 Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
12.8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

13 Assignment: Applying the Concept of Multi-Threading 17


13.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
13.2 Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
13.3 Code Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
13.4 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
13.5 Compilation and Execution . . . . . . . . . . . . . . . . . . . . . . . . . 18
13.6 Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
13.7 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

1 Course Objectives
1. To understand how to design, implement, test, debug, and document programs
that use basic data types and computation, simple I/O, conditional and control
structures, string handling, functions, and object-oriented approaches.

2 Course Outcomes
On completion of this course, the students will be able to:

CO1. Define classes, objects, members of a class, and the relationships among them
needed for solving specific problems.

CO2. Illustrate object-oriented modeling techniques like class and instance modeling.

CO3. Solve programs using standard design patterns.

CO4. Interpret the fundamentals of object-oriented programming in Java, including defin-


ing classes, invoking methods, and using class libraries.

CO5. Construct programming solutions with exception handling and multithreading con-
cepts.

CO6. Solve GUI programs with proper event-handling techniques.

3 Catalogue Description
This course investigates object-oriented methods, including object-oriented programming
methodologies and techniques. The current methodology is emphasized. The use of
object-oriented features such as encapsulation, information hiding, inheritance, and poly-
morphism is reinforced by class assignments and programming exercises. The importance
of multithreading and exception handling is introduced in this course.

3
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

4 Assignment: Class and Constructor


4.1 Objective
To understand the concept of classes and constructors in Java by implementing a simple
program that demonstrates object creation and initialization.

4.2 Theory
In Java, a class is a blueprint for creating objects. It defines fields (variables) and
methods (functions) that describe the behavior of the object.
A constructor is a special method that initializes an object when it is created. It has
the same name as the class and does not have a return type.
There are three types of constructors in Java:
• Default Constructor: No parameters, assigns default values.
• Parameterized Constructor: Accepts arguments to initialize variables.
• Copy Constructor: Copies values from another object.

4.3 Java Program


1 // Java program to demonstrate Class and Constructor
2 class Student {
3 String name ;
4 int age ;
5
6 // Default Constructor
7 Student () {
8 name = " Unknown " ;
9 age = 0;
10 }
11
12 // Parameterized Constructor
13 Student ( String n , int a ) {
14 name = n ;
15 age = a ;
16 }
17
18 // Method to display student details
19 void display () {
20 System . out . println ( " Name : " + name + " , Age : " + age ) ;
21 }
22
23 public static void main ( String [] args ) {
24 // Creating objects using both constructors
25 Student s1 = new Student () ;
26 Student s2 = new Student ( " John " , 22) ;
27
28 // Displaying student details
29 s1 . display () ;
30 s2 . display () ;
31 }
32 }

4
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

4.4 Explanation
• The Student class has two constructors: a default constructor and a parameterized
constructor.

• The default constructor initializes name to ”Unknown” and age to 0.

• The parameterized constructor assigns values based on arguments.

• In the main method, two objects are created: one using the default constructor and
another using the parameterized constructor.

• The display() method prints the student details.

4.5 Output
Name: Unknown, Age: 0
Name: John, Age: 22

5 Assignment: Method Overloading


5.1 Objective
The objective of this assignment is to understand the concept of method overloading in
Java. Method overloading allows multiple methods in a class to have the same name but
different parameter lists.

5.2 Program Explanation


In this program, we define a class Calculator that demonstrates method overloading.
The class contains multiple methods named add() that take different numbers and types
of parameters.

5.3 Java Program


1 class Calculator {
2

3 // Method to add two integers


4 int add ( int a , int b ) {
5 return a + b ;
6 }
7
8 // Method to add three integers
9 int add ( int a , int b , int c ) {
10 return a + b + c ;
11 }
12
13 // Method to add two double numbers
14 double add ( double a , double b ) {
15 return a + b ;
16 }
17

5
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

18 public static void main ( String [] args ) {


19 Calculator calc = new Calculator () ;
20
21 System . out . println ( " Sum of 2 and 3: " + calc . add (2 , 3) ) ;
22 System . out . println ( " Sum of 1 , 4 and 6: " + calc . add (1 , 4 , 6) ) ;
23 System . out . println ( " Sum of 2.5 and 3.5: " + calc . add (2.5 , 3.5) )
;
24 }
25 }

Listing 1: Java Program for Method Overloading

5.4 Explanation
• The Calculator class contains three add() methods.
• The first add() method takes two integers as parameters.
• The second add() method takes three integers as parameters.
• The third add() method takes two double values as parameters.
• The compiler determines which add() method to call based on the number and
type of arguments passed.

5.5 Expected Output


Sum of 2 and 3: 5
Sum of 1, 4 and 6: 11
Sum of 2.5 and 3.5: 6.0

6 Assignment: Inheritance and Method Overriding


6.1 Objective
The objective of this assignment is to understand the concept of inheritance and method
overriding in Java.
• Inheritance allows a class (subclass) to acquire properties and behaviors of another
class (superclass).
• Method Overriding allows a subclass to provide a specific implementation of a
method that is already defined in its superclass.

6.2 Program Explanation


In this program:
• We define a superclass Animal with a method makeSound().
• The subclass Dog extends Animal and overrides the makeSound() method.
• The overridden method in Dog provides a specific implementation for the makeSound()
method.

6
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

6.3 Java Program


1 class Animal {
2 // Method to be overridden
3 void makeSound () {
4 System . out . println ( " Animal makes a sound " ) ;
5 }
6 }
7
8 class Dog extends Animal {
9 // Overriding the method in subclass
10 @Override
11 void makeSound () {
12 System . out . println ( " Dog barks " ) ;
13 }
14
15 public static void main ( String [] args ) {
16 Animal myAnimal = new Animal () ;
17 myAnimal . makeSound () ; // Calls method from Animal class
18
19 Dog myDog = new Dog () ;
20 myDog . makeSound () ; // Calls overridden method from Dog class
21 }
22 }

Listing 2: Java Program for Inheritance and Overriding

6.4 Explanation
• The Animal class contains a method makeSound() that prints a generic sound
message.

• The Dog class extends Animal and overrides the makeSound() method to provide a
specific implementation.

• The @Override annotation ensures that the method properly overrides a method
in the superclass.

• When we create an object of Dog, the overridden method in the subclass is executed.

6.5 Expected Output


Animal makes a sound
Dog barks

7 Assignment: Wrapper Classes and Arrays


7.1 Objective
This assignment aims to provide hands-on experience with Java wrapper classes and
arrays. The student will learn how to use wrapper classes for primitive data types and
manipulate arrays efficiently.

7
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

7.2 Concepts Covered


• Wrapper classes: Integer, Double, Character, etc.

• Autoboxing and Unboxing

• Array declaration, initialization, and traversal

• Performing operations on arrays using wrapper classes

7.3 Java Implementation


Problem Statement: Write a Java program that demonstrates the use of wrapper
classes for converting primitive data types into objects and vice versa. Also, perform
basic operations on an array of integers using wrapper classes.
1 public class WrapperArrayDemo {
2 public static void main ( String [] args ) {
3 // Using Wrapper classes
4 Integer num = Integer . valueOf (10) ; // Boxing
5 int primitiveNum = num ; // Unboxing
6
7 System . out . println ( " Wrapper Object : " + num ) ;
8 System . out . println ( " Unboxed Value : " + primitiveNum ) ;
9
10 // Array of Wrapper class
11 Integer [] numbers = {5 , 10 , 15 , 20 , 25};
12
13 // Sum of array elements using Wrapper class
14 int sum = 0;
15 for ( Integer n : numbers ) {
16 sum += n ;
17 }
18
19 System . out . println ( " Sum of Array Elements : " + sum ) ;
20 }
21 }

Listing 3: Demonstration of Wrapper Classes and Arrays

7.4 Explanation
• The program demonstrates boxing and unboxing using the Integer wrapper class.

• An array of Integer objects is created and initialized.

• The sum of the array elements is calculated using an enhanced for loop.

7.5 Expected Output


Wrapper Object: 10
Unboxed Value: 10
Sum of Array Elements: 75

8
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

7.6 Conclusion
This program highlights the use of wrapper classes in Java and their integration with
arrays. Wrapper classes help in utilizing object-oriented features for primitive data types,
making operations more flexible.

8 Assignment: Developing Interfaces - Multiple In-


heritance and Extending Interfaces
8.1 Objective
This assignment demonstrates how to implement multiple inheritance in Java using in-
terfaces and how to extend interfaces for better modularity and reusability.

8.2 Concept
In Java, multiple inheritance is achieved using interfaces. A class can implement multiple
interfaces, and an interface can extend another interface. This allows developers to create
a structured and reusable design without the ambiguity of multiple inheritance found in
other languages.

8.3 Code Implementation


1 // Interface 1
2 interface FirstInterface {
3 void methodOne () ;
4 }
5

6 // Interface 2
7 interface SecondInterface {
8 void methodTwo () ;
9 }
10
11 // Extending Interface
12 interface Extend edInte rface extends FirstInterface , SecondInterface {
13 void methodThree () ;
14 }
15
16 // Implementing multiple interfaces
17 class Implem entin gClass implements Exten dedInt erface {
18 public void methodOne () {
19 System . out . println ( " Method One from FirstInterface " ) ;
20 }
21
22 public void methodTwo () {
23 System . out . println ( " Method Two from SecondInterface " ) ;
24 }
25
26 public void methodThree () {
27 System . out . println ( " Method Three from Ex tended Interf ace " ) ;
28 }
29

9
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

30 public static void main ( String [] args ) {


31 Impl ementi ngClas s obj = new Imp lement ingCla ss () ;
32 obj . methodOne () ;
33 obj . methodTwo () ;
34 obj . methodThree () ;
35 }
36 }

8.4 Explanation
• The program defines two interfaces: FirstInterface and SecondInterface.

• The ExtendedInterface extends both FirstInterface and SecondInterface,


demonstrating interface extension.

• The ImplementingClass implements ExtendedInterface, thus inheriting methods


from both interfaces.

• In the main method, an object of ImplementingClass is created to call all the


implemented methods.

8.5 Expected Output


Method One from FirstInterface
Method Two from SecondInterface
Method Three from ExtendedInterface

8.6 Conclusion
This assignment illustrates how Java supports multiple inheritance using interfaces and
how an interface can extend other interfaces for better modularity.

9 Assignment: Creating and Accessing Packages


9.1 Objective
This assignment demonstrates how to create, define, and use packages in Java to organize
classes and improve code modularity.

9.2 Concept
A package in Java is a namespace that organizes a set of related classes and interfaces.
Using packages helps avoid name conflicts and allows controlled access to classes. Java
provides built-in packages, and developers can create custom packages as needed.

10
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

9.3 Code Implementation


Step 1: Create a Package and Define a Class
1 // Save this file as MyPackageClass . java inside a folder named "
mypackage "
2 package mypackage ; // Defining a package
3
4 public class MyPackageClass {
5 public void displayMessage () {
6 System . out . println ( " Hello from MyPackageClass in mypackage ! " ) ;
7 }
8 }

Step 2: Access the Package in Another Java File


1 // Save this file outside " mypackage " folder as Main . java
2 import mypackage . MyPackageClass ; // Importing the package
3
4 public class Main {
5 public static void main ( String [] args ) {
6 MyPackageClass obj = new MyPackageClass () ; // Creating object
of class in package
7 obj . displayMessage () ; // Calling the method
8 }
9 }

9.4 Explanation
• The first file MyPackageClass.java is stored inside a folder named mypackage,
defining a package using the package keyword.

• The Main.java file, located outside the package folder, imports mypackage.MyPackageClass
using the import statement.

• The displayMessage() method from MyPackageClass is invoked in Main class.

9.5 Compilation and Execution


# Step 1: Compile the package class
javac -d . MyPackageClass.java

# Step 2: Compile the Main class


javac Main.java

# Step 3: Run the Main class


java Main

9.6 Expected Output


Hello from MyPackageClass in mypackage!

11
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

9.7 Conclusion
This assignment demonstrates how Java allows code organization using packages. It
shows how to create a package, define classes inside it, and access those classes in another
Java file.

10 Assignment: Multithreaded Programming


10.1 Objective
This assignment demonstrates how to create and manage multiple threads in Java using
the Thread class and the Runnable interface.

10.2 Concept
• Multithreading allows concurrent execution of two or more parts of a program to
maximize CPU utilization.

• Java provides two ways to create threads:

1. By extending the Thread class.


2. By implementing the Runnable interface.

10.3 Code Implementation


Method 1: Extending the Thread class
1 // File : ThreadExample1 . java
2 class MyThread extends Thread {
3 public void run () {
4 for ( int i = 1; i <= 5; i ++) {
5 System . out . println ( " Thread running : " + i ) ;
6 try {
7 Thread . sleep (500) ;
8 } catch ( I n t e r r u p t e d E x c e p t i o n e ) {
9 System . out . println ( e ) ;
10 }
11 }
12 }
13 }
14

15 public class ThreadExample1 {


16 public static void main ( String [] args ) {
17 MyThread t1 = new MyThread () ;
18 t1 . start () ; // Start the thread
19 }
20 }

Method 2: Implementing the Runnable interface


1 // File : ThreadExample2 . java
2 class MyRunnable implements Runnable {
3 public void run () {
4 for ( int i = 1; i <= 5; i ++) {

12
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

5 System . out . println ( " Runnable thread running : " + i ) ;


6 try {
7 Thread . sleep (500) ;
8 } catch ( I n t e r r u p t e d E x c e p t i o n e ) {
9 System . out . println ( e ) ;
10 }
11 }
12 }
13 }
14
15 public class ThreadExample2 {
16 public static void main ( String [] args ) {
17 Thread t1 = new Thread ( new MyRunnable () ) ;
18 t1 . start () ; // Start the thread
19 }
20 }

10.4 Explanation
• Method 1: The class MyThread extends the Thread class and overrides the run()
method.

• Method 2: The class MyRunnable implements the Runnable interface and provides
its own run() method.

• The start() method is used to begin execution of a new thread.

• Thread.sleep(500); pauses execution for 500 milliseconds.

10.5 Compilation and Execution


To compile and execute the program:

1. javac ThreadExample1.java
2. java ThreadExample1

1. javac ThreadExample2.java
2. java ThreadExample2

10.6 Expected Output


Thread running: 1
Thread running: 2
...
Runnable thread running: 1
Runnable thread running: 2
...

10.7 Conclusion
This assignment demonstrates how Java uses multithreading to execute multiple threads
concurrently, improving efficiency and performance.

13
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

11 Assignment: Applet Programming


11.1 Objective
This assignment demonstrates how to create and run a simple Java Applet using the
Applet class and the paint() method.

11.2 Concept
• Applets are Java programs that run within a web browser or an applet viewer.

• Java applets extend the Applet class and override the paint() method to perform
graphical operations.

• Applets do not have a main() method like standalone Java applications.

11.3 Code Implementation


1 // File : SimpleApplet . java
2 import java . applet . Applet ;
3 import java . awt . Graphics ;
4
5 // Extend the Applet class
6 public class SimpleApplet extends Applet {
7 // Override paint () method
8 public void paint ( Graphics g ) {
9 g . drawString ( " Hello , this is a Java Applet ! " , 50 , 50) ;
10 }
11 }

11.4 Explanation
• The class SimpleApplet extends Applet.

• The paint() method is overridden to display a message using drawString().

• The parameters (50, 50) specify the x and y coordinates for drawing the string.

11.5 Creating an HTML File


To run the applet, create an HTML file:
1 <! - - File : SimpleApplet . html -->
2 < html >
3 < body >
4 < applet code = " SimpleApplet . class " width = " 400 " height = " 200 " > </
applet >
5 </ body >
6 </ html >

14
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

11.6 Compilation and Execution


To compile and run the applet:

1. javac SimpleApplet.java
2. appletviewer SimpleApplet.html

11.7 Expected Output


A window displaying the text:

Hello, this is a Java Applet!

11.8 Conclusion
This assignment introduces the basics of applet programming in Java, illustrating how
to create and display simple graphics using the paint() method.

12 Assignment: Designing an Applet for a Real-Time


Web Page
12.1 Objective
This assignment demonstrates how to integrate a Java applet into a real-time web page
by displaying dynamic content.

12.2 Concept
• Applets can be embedded in web pages using the <applet> tag or JavaScript.

• They allow dynamic content updates, such as clocks, stock prices, or interactive UI
elements.

• The repaint() method is used to refresh the applet at regular intervals.

12.3 Code Implementation


1 // File : Dig it al Cl oc kA pp le t . java
2 import java . applet . Applet ;
3 import java . awt . Graphics ;
4 import java . util . Date ;
5
6 public class D ig it al Cl oc kA pp le t extends Applet implements Runnable {
7 Thread t ;
8 String time ;
9

10 public void init () {


11 t = new Thread ( this ) ;
12 t . start () ;
13 }
14

15
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

15 public void run () {


16 while ( true ) {
17 time = new Date () . toString () ;
18 repaint () ;
19 try {
20 Thread . sleep (1000) ;
21 } catch ( I n t e r r u p t e d E x c e p t i o n e ) {
22 e . printStackTrace () ;
23 }
24 }
25 }
26
27 public void paint ( Graphics g ) {
28 g . drawString ( " Current Time : " + time , 50 , 50) ;
29 }
30 }

12.4 Explanation
• The class DigitalClockApplet extends Applet and implements Runnable for multi-
threading.
• The run() method continuously updates the time every second.
• The paint() method displays the real-time clock on the webpage.

12.5 Creating an HTML File


To embed the applet in a web page, create the following HTML file:
1 <! - - File : D ig it al Cl oc kA pp le t . html -->
2 < html >
3 < body >
4 <h2 > Real - time Digital Clock </ h2 >
5 < applet code = " D ig it al Cl oc kA pp le t . class " width = " 400 " height = " 100
" > </ applet >
6 </ body >
7 </ html >

12.6 Compilation and Execution


To compile and run the applet:
1. javac DigitalClockApplet.java
2. appletviewer DigitalClockApplet.html

12.7 Expected Output


A web page displaying a real-time digital clock that updates every second.

12.8 Conclusion
This assignment illustrates how Java Applets can be used for real-time web applications,
such as displaying dynamic content on a webpage.

16
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

13 Assignment: Applying the Concept of Multi-Threading


13.1 Objective
This assignment demonstrates the implementation of multi-threading in Java to execute
multiple tasks simultaneously.

13.2 Concept
• Multi-threading allows concurrent execution of two or more parts of a program for
maximum utilization of CPU.

• It is implemented using the Thread class or the Runnable interface.

• The start() method begins execution of a thread.

• The sleep() method pauses thread execution for a specific time.

13.3 Code Implementation


1 // File : M u l t i T h r e a d i n g E x a m p l e . java
2 class Task1 extends Thread {
3 public void run () {
4 for ( int i = 1; i <= 5; i ++) {
5 System . out . println ( " Task 1 - Count : " + i ) ;
6 try {
7 Thread . sleep (1000) ;
8 } catch ( I n t e r r u p t e d E x c e p t i o n e ) {
9 e . printStackTrace () ;
10 }
11 }
12 }
13 }
14
15 class Task2 extends Thread {
16 public void run () {
17 for ( int i = 1; i <= 5; i ++) {
18 System . out . println ( " Task 2 - Count : " + i ) ;
19 try {
20 Thread . sleep (1200) ;
21 } catch ( I n t e r r u p t e d E x c e p t i o n e ) {
22 e . printStackTrace () ;
23 }
24 }
25 }
26 }
27

28 public class M u l t i T h r e a d i n g E x a m p l e {
29 public static void main ( String [] args ) {
30 Task1 t1 = new Task1 () ;
31 Task2 t2 = new Task2 () ;
32
33 t1 . start () ;
34 t2 . start () ;
35 }

17
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953

36 }

13.4 Explanation
• The Task1 and Task2 classes extend Thread and override the run() method.

• Both threads print numbers from 1 to 5 with different sleep durations.

• The start() method initiates each thread to run concurrently.

13.5 Compilation and Execution


To compile and run the program:

1. javac MultiThreadingExample.java
2. java MultiThreadingExample

13.6 Expected Output


Task 1 - Count: 1
Task 2 - Count: 1
Task 1 - Count: 2
Task 2 - Count: 2
...

Threads execute in parallel, with varying sleep durations.

13.7 Conclusion
This assignment demonstrates how Java multi-threading enables parallel execution of
tasks, improving efficiency in applications.

18

You might also like