Mca Java Lab Manual Lab 2nd Sem
Mca Java Lab Manual Lab 2nd Sem
PROGRAMMING
WITH
JAVA
LAB MANUAL
MCA SEM 2
Contents
Contents 1
1 Course Objectives 3
2 Course Outcomes 3
3 Catalogue Description 3
1
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953
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.
CO5. Construct programming solutions with exception handling and multithreading con-
cepts.
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.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
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953
4.4 Explanation
• The Student class has two constructors: a default constructor and a parameterized
constructor.
• In the main method, two objects are created: one using the default constructor and
another using the parameterized constructor.
4.5 Output
Name: Unknown, Age: 0
Name: John, Age: 22
5
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953
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.
6
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953
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.
7
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953
7.4 Explanation
• The program demonstrates boxing and unboxing using the Integer wrapper class.
• The sum of the array elements is calculated using an enhanced for loop.
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.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.
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
8.4 Explanation
• The program defines two interfaces: FirstInterface and SecondInterface.
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.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.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.
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.2 Concept
• Multithreading allows concurrent execution of two or more parts of a program to
maximize CPU utilization.
12
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953
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.
1. javac ThreadExample1.java
2. java ThreadExample1
1. javac ThreadExample2.java
2. java ThreadExample2
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.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.
11.4 Explanation
• The class SimpleApplet extends Applet.
• The parameters (50, 50) specify the x and y coordinates for drawing the string.
14
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953
1. javac SimpleApplet.java
2. appletviewer SimpleApplet.html
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.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.
15
OBJECT ORIENTED PROGRAMMING WITH JAVA - LAB MANUAL
CSE22953
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.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.2 Concept
• Multi-threading allows concurrent execution of two or more parts of a program for
maximum utilization of CPU.
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.
1. javac MultiThreadingExample.java
2. java MultiThreadingExample
13.7 Conclusion
This assignment demonstrates how Java multi-threading enables parallel execution of
tasks, improving efficiency in applications.
18