java Lab. Experiments and Manual
java Lab. Experiments and Manual
Java Programming
MCA III Semester
List of Experiments
2|Page
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-1
Objective: Write a program to demonstrate the scope of variables, emphasizing
the three types: local variable, instance variable, and static variable.
Output:
Instance Variable: 10
Static Variable: 20
Local Variable: 30
3|Page
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-2
Objective: Write a program to Demonstrate the Concept of CLASS in JAVA and
how can we create an object of the class.
// Step 4: Create the main class to demonstrate class and object creation
public class Main {
public static void main(String[] args) {
// Step 5: Create an object of the Student class
Student student1 = new Student();
Output:
Details of Student 1:
Name: John Doe
Roll Number: 101
Marks: 95.5
Details of Student 2:
Name: Jane Doe
Roll Number: 102
Marks: 89.0
5|Page
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-3
Objective: Write a Java program to demonstrate the concept of constructors and
constructor overloading.
// Step 1: Create a class named Book
class Book {
// Step 2: Declare class attributes
String title;
String author;
int pageCount;
Output:
7|Page
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-4
Objective: Write a Java program to demonstrate the concept of methods and
method overloading.
Output:
9|Page
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment No. 5
Objective: Write a Java program to demonstrate the concept of inheritance and
the use of the super keyword.
// Step 1: Create a superclass named Vehicle
class Vehicle {
// Step 2: Declare superclass attributes
String brand;
int year;
10 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
// Step 9: Create the main class to demonstrate inheritance and super keyword
public class Main {
public static void main(String[] args) {
// Step 10: Create an object of the Car class
Car myCar = new Car("Toyota", 2022, "Camry");
11 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-6
Objective: Write a Java program to demonstrate the concept of polymorphism
and method overriding.
// Step 7: Create the main class to demonstrate polymorphism and method overriding
public class Main {
public static void main(String[] args) {
// Step 8: Create an array of Shape objects
Shape[] shapes = new Shape[3];
shapes[0] = new Circle();
shapes[1] = new Square();
shapes[2] = new Shape(); // Creating an object of the superclass
Output:
Drawing a circle
Drawing a square
Drawing a generic shape
.
13 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-7
Objective: Write a Java program to demonstrate the concept of interfaces and
achieving multiple inheritance.
// Step 1: Create a Drawable interface
interface Drawable {
// Step 2: Declare an abstract draw method
void draw();
}
// Step 8: Create the main class to demonstrate multiple inheritance through interfaces
public class Main {
public static void main(String[] args) {
// Step 9: Create an object of the Shape class
Shape myShape = new Shape();
Output:
Drawing a shape
Resizing the shape by 50%
15 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-8
Objective: Write a Java program to demonstrate the implementation of
exception handling and showcase the use of the finally block.
import java.util.Scanner;
public class ExceptionHandlingDemo {
try {
System.out.print("Enter the numerator: ");
int numerator = scanner.nextInt();
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
} finally {
System.out.println("Finally block executed - closing resources.");
scanner.close();
}
}
private static int performDivision(int numerator, int denominator) {
if (denominator == 0) {
throw new ArithmeticException("Denominator cannot be zero.");
}
return numerator / denominator;
}
}
Sample Output:
Enter the numerator: 10
Enter the denominator: 0
Error: Cannot divide by zero.
Finally block executed - closing resources.
16 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-9
Objective: Develop a Java program to illustrate the concept of multithreading,
showcasing the concurrent execution of multiple threads.
@Override
public void run() {
synchronized (this) {
for (int i = 1; i <= 5; i++) {
System.out.println("Executing Task " + taskName + ": Step " + i);
try {
Thread.sleep(500); // Simulate some work being done
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
try {
// Use join to ensure sequential execution after both threads complete
thread1.join();
thread2.join();
} catch (InterruptedException e) {
17 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
e.printStackTrace();
}
18 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-10
Objective: Create a Java program to demonstrate the use of String and various
string manipulation techniques. The objective is to illustrate common operations
on strings, including concatenation, substring extraction, length determination,
and the use of string methods for manipulation and analysis.
// Concatenation
String resultConcatenation = str1 + str2;
String resultConcatMethod = str1.concat(str2);
// Substring Extraction
String substring = resultConcatenation.substring(7, 12);
System.out.println("Substring Extraction: " + substring);
// String Length
int length = resultConcatenation.length();
System.out.println("String Length: " + length);
// String Methods
System.out.println("Uppercase: " + resultConcatenation.toUpperCase());
System.out.println("Lowercase: " + resultConcatenation.toLowerCase());
System.out.println("Character at index 4: " + resultConcatenation.charAt(4));
System.out.println("Index of 'o': " + resultConcatenation.indexOf('o'));
System.out.println("Replace 'Hello' with 'Greetings': " + resultConcatenation.replace("Hello",
"Greetings"));
System.out.println("Trim leading and trailing spaces: " + " Trim ".trim());
}
}
Output:
Concatenation using + operator: Hello, World!
Concatenation using concat method: Hello, World!
Substring Extraction: World
19 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
String Length: 13
Uppercase: HELLO, WORLD!
Lowercase: hello, world!
Character at index 4: o
Index of 'o': 4
Replace 'Hello' with 'Greetings': Greetings, World!
Trim leading and trailing spaces: Trim
20 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-11
Objective: Create a Java program to demonstrate the concept of packages,
showcasing how to organize and manage classes within a package structure. The
objective is to illustrate the benefits of using packages for modularizing code,
preventing naming conflicts, and enhancing code maintainability.
// File: MathOperations.java
package com.example;
21 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-12
Objective: Develop a Java program to explore Input/Output (I/O) operations,
including the basics of streams, byte and character streams, and the usage of
predefined streams. The objective is to illustrate fundamental concepts related to
handling input and output in Java.
import java.io.*;
public class IOOperationsDemo {
public static void main(String[] args) {
// Standard Input using System.in
System.out.print("Enter a line of text: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
String userInput = reader.readLine();
System.out.println("You entered: " + userInput);
} catch (IOException e) {
e.printStackTrace();
}
int data;
while ((data = inputFile.read()) != -1) {
outputFile.write(data);
}
inputFile.close();
outputFile.close();
} catch (IOException e) {
e.printStackTrace();
}
22 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
int charData;
while ((charData = fileReader.read()) != -1) {
fileWriter.write(charData);
}
fileReader.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Enter a line of text: Hello, I/O Operations!
You entered: Hello, I/O Operations!
23 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-13
Objective: Create a Java program to demonstrate inter-thread communication
using the wait(), notify(), and notifyAll() methods..
class Message {
private String content;
@Override
public void run() {
synchronized (message) {
for (int i = 1; i <= 5; i++) {
String newContent = "Message " + i;
message.setContent(newContent);
System.out.println("Produced: " + newContent);
try {
Thread.sleep(1000); // Simulate some work being done
message.notify(); // Notify the consumer thread
message.wait(); // Release the lock and wait for the consumer to consume
} catch (InterruptedException e) {
e.printStackTrace();
}
24 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
}
}
}
}
@Override
public void run() {
synchronized (message) {
for (int i = 1; i <= 5; i++) {
try {
message.wait(); // Wait for the producer to produce
System.out.println("Consumed: " + message.getContent());
Thread.sleep(1000); // Simulate some work being done
message.notify(); // Notify the producer thread
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
producerThread.start();
consumerThread.start();
}
}
Output:
Produced: Message 1
25 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Consumed: Message 1
Produced: Message 2
Consumed: Message 2
Produced: Message 3
Consumed: Message 3
Produced: Message 4
Consumed: Message 4
Produced: Message 5
Consumed: Message 5
26 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-14
Objective: Develop a Java program to demonstrate the Delegation Event Model,
focusing on event classes and how they are used for handling user interactions.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// Create a JButton
JButton button = new JButton("Click Me");
27 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
Experiment-15
Objective: Create a basic Task Management System where users can add tasks,
view tasks, and mark tasks as completed.
public class Task {
private String description;
private boolean completed;
@Override
public String toString() {
return "Task: " + description + " | Completed: " + completed;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public TaskManager() {
this.tasks = new ArrayList<>();
}
28 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
while (true) {
System.out.println("\nTask Management System");
System.out.println("1. Add Task");
System.out.println("2. View Tasks");
System.out.println("3. Mark Task as Completed");
System.out.println("4. Exit");
System.out.print("Choose an option (1-4): ");
29 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901
Lab Manual: Java Programming
switch (choice) {
case 1:
System.out.print("Enter task description: ");
scanner.nextLine(); // Consume the newline character
String description = scanner.nextLine();
taskManager.addTask(description);
break;
case 2:
taskManager.viewTasks();
break;
case 3:
System.out.print("Enter the index of the task to mark as completed: ");
int index = scanner.nextInt();
taskManager.markTaskAsCompleted(index);
break;
case 4:
System.out.println("Exiting Task Management System. Goodbye!");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please choose a valid option.");
}
}
}
}
30 | P a g e
Department of Computer Applications,
Mewar University, NH-48, Gangrar, Chittorgarh (Rajasthan) 312901