Java Lab Manual
Java Lab Manual
LAB REPORT
(ACADEMIC YEAR 2023-2026)
SUBMITTED BY
CLASS: B
SEMESTER: 3rd
P a g e 1 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
Sr. Title
No.
1 a. WAP to find fibonacci upto given number using
for loop.
b. WAP to print prime numbers using while loop.
c. WAP whether a given string is palindrome or
not?
d. WAP to perform arithmetic operations(menu
driven).
2 a. WAP to sort the elements of array in ascending
order.
b. WAP for calculating Matrix multiplication
operation.
c. WAP for sorting given list of names in ascending
order.
3 a. WAP to demonstrate the working of banking-
system where we deposit and withdraw amount
from our account.
b. WAP using class and object for calculating area
of circle, rectangle, triangle using menu driven.
c. WAP to create a room class, the attributes of this
class is roomno, roomtype, roomarea, and ac-
machine. In this class the member functions are
setdata and displaydata.
4 Given the classes EmployeeDetails, DepartmentDetails, and Salary,
implement a multilevel inheritance structure in Java:
P a g e 2 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
1. School: This base class holds basic information about a school, such as
schoolName and schoolId, and includes a printDetails() method to
display these details.
6 a. Write a Java program with two variables: an int variable a and a String
variable b. Set default values in the default constructor and print the values in the
constructor.
b. Write a Java program with three constructors: the first is a default constructor
that will print an introduction, the second is a parameterized constructor with
a String parameter name to print the name, and the third is a
parameterized constructor with a String parameter school and an int
parameter roll to print the school name and roll number.
7 a. WAP to illustrate use of abstract class that has abstract and non-
abstract methods.
b. WAP to illustrate use of interface.
8 WAP for null pointer exception and illustrate finally block and
throws keyword.
9 a. WAP to create a text file.
b. WAP to write text in text file.
c. WAP to read text from text file
10 Write a java program for calculator operation using AWT controls
P a g e 3 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
1.
A - WAP to find fibonacci upto given number using for loop
import [Link];
int a = 0, b = 1, next;
[Link]("Fibonacci series: " + a + " " + b);
OUTPUT :-
import [Link];
P a g e 4 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
int num = 2;
while (num <= n) {
boolean isPrime = true;
int i = 2;
while (i <= [Link](num)) {
if (num % i == 0) {
isPrime = false;
break;
}
i++;
}
if (isPrime) {
[Link](num + " ");
}
num++;
}
}
}
OUTPUT :-
import [Link];
P a g e 5 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
if ([Link](reversed)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
}
}
OUTPUT :-
import [Link];
do {
[Link]("\n--- Menu ---");
[Link]("1. Addition");
[Link]("2. Subtraction");
[Link]("3. Multiplication");
[Link]("4. Division");
[Link]("5. Exit");
P a g e 6 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
switch (choice) {
case 1:
[Link]("Result: " + (num1 + num2));
break;
case 2:
[Link]("Result: " + (num1 - num2));
break;
case 3:
[Link]("Result: " + (num1 * num2));
break;
case 4:
if (num2 != 0) {
[Link]("Result: " + (num1 / num2));
} else {
[Link]("Division by zero is not
allowed.");
}
break;
case 5:
[Link]("Exiting program.");
break;
default:
[Link]("Invalid choice! Please try again.");
}
} while (choice != 5);
}
P a g e 7 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
2.
A - WAP to sort the elements of array in ascending order.
import [Link];
P a g e 8 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
import [Link];
P a g e 9 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
if (cols1 != rows2) {
[Link]("Matrix multiplication is not possible
(columns of first matrix != rows of second matrix).");
return;
}
// Matrix multiplication
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
P a g e 10 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
import [Link];
import [Link];
P a g e 11 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
3.
A - WAP to demonstrate the working of banking- system where
we deposit and withdraw amount from our account.
import [Link];
class BankAccount {
private String accountHolderName;
private double balance;
P a g e 12 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
[Link] = initialBalance;
}
P a g e 13 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
int choice;
do {
[Link]("\n--- Banking Menu ---");
[Link]("1. Deposit");
[Link]("2. Withdraw");
[Link]("3. Display Balance");
[Link]("4. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter amount to deposit: ");
double depositAmount = [Link]();
[Link](depositAmount);
break;
case 2:
[Link]("Enter amount to withdraw: ");
double withdrawAmount = [Link]();
[Link](withdrawAmount);
break;
case 3:
[Link]();
break;
case 4:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice, please try again.");
}
} while (choice != 4);
}
}
OUTPUT :-
P a g e 14 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
import [Link];
P a g e 15 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
class AreaCalculator {
public double calculateCircleArea(double radius) {
return [Link] * radius * radius;
}
int choice;
do {
[Link]("\n--- Area Calculator Menu ---");
[Link]("1. Circle");
[Link]("2. Rectangle");
[Link]("3. Triangle");
[Link]("4. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter radius of the circle: ");
double radius = [Link]();
[Link]("Area of Circle: " +
[Link](radius));
break;
case 2:
P a g e 16 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
P a g e 17 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
class Room {
private int roomNo;
private String roomType;
private double roomArea;
private boolean acMachine;
P a g e 18 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
[Link] = roomNo;
[Link] = roomType;
[Link] = roomArea;
[Link] = acMachine;
}
OUTPUT :-
4.
A – Given the classes EmployeeDetails, DepartmentDetails, and
Salary, implement a multilevel inheritance structure in Java:
P a g e 19 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
class EmployeeDetails {
private String employeeName;
private int employeeId;
P a g e 20 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
[Link] = departmentId;
}
P a g e 21 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
5.
A - Consider a multilevel inheritance structure in Java with the
following classes:
1. School: This base class holds basic information about a school,
such as schoolName and schoolId, and includes a printDetails()
method to display these details.
P a g e 22 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
class School {
private String schoolName;
private String schoolId;
@Override
public void printDetails() {
[Link](); // Call the parent class's printDetails
method
[Link]("Student Name: " + studentName);
[Link]("Student ID: " + studentId);
}
P a g e 23 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
@Override
public void printDetails() {
[Link](); // Call the parent class's printDetails
method
[Link]("Graduation Year: " + graduationYear);
}
}
OUTPUT :-
P a g e 24 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
6.
A - Write a Java program with two variables: an int variable a
and a String variable b. Set default values in the default
constructor and print the values in the constructor.
class DefaultConstructorDemo {
private int a;
private String b;
// Default constructor
public DefaultConstructorDemo() {
// Set default values
a = 0;
b = "Default String";
OUTPUT :-
P a g e 25 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
class ConstructorDemo {
// Default constructor
public ConstructorDemo() {
[Link]("Welcome to Constructor Demo!");
}
P a g e 26 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
7.
A - WAP to illustrate use of abstract class that has abstract and
non-abstract methods.
// Abstract class
abstract class Shape {
// Abstract method
abstract void calculateArea();
// Non-abstract method
void displayShapeName(String shapeName) {
[Link]("The shape is: " + shapeName);
}
}
P a g e 27 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
void calculateArea() {
double area = [Link] * radius * radius;
[Link]("Area of the Circle: " + area);
}
}
OUTPUT :-
// Interface definition
interface Animal {
void makeSound(); // Abstract method
P a g e 28 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
8.
A - WAP for null pointer exception and illustrate finally
block and throws keyword.
try {
// Attempting to call a method on a null object
[Link]([Link]()); // This will throw
NullPointerException
} catch (NullPointerException e) {
// Catching the NullPointerException
P a g e 29 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
import [Link].*;
P a g e 30 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
OUTPUT :-
9.
A - WAP to create a text file.
import [Link];
import [Link];
try {
// Checking if the file already exists
if ([Link]()) {
[Link]("File created: " +
[Link]());
} else {
[Link]("File already exists.");
}
} catch (IOException e) {
[Link]("An error occurred.");
[Link]();
}
}
}
OUTPUT :-
P a g e 31 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
import [Link];
import [Link];
try {
// Creating a FileWriter object to write text to the file
FileWriter writer = new FileWriter(fileName);
OUTPUT :-
import [Link];
import [Link];
P a g e 32 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
import [Link];
try {
// Creating a Scanner object to read the file
Scanner reader = new Scanner(file);
OUTPUT :-
10.
A - Write a java program for calculator operation
using AWT controls
import [Link].*;
import [Link].*;
P a g e 33 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
public CalculatorAWT() {
// Creating frame
frame = new Frame("AWT Calculator");
// Creating TextField
textField = new TextField();
[Link](30, 40, 280, 30);
[Link](textField);
P a g e 34 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentText += "+";
[Link](currentText);
}
});
[Link](addButton);
P a g e 35 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
// Equal button
equalButton = new Button("=");
[Link](100, 280, 60, 40);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Evaluating the expression manually
String result = evaluateExpression(currentText);
[Link](result);
currentText = result; // Store the result for further
operations
} catch (Exception ex) {
[Link]("Error");
currentText = "";
}
}
});
[Link](equalButton);
// Clear button
clearButton = new Button("C");
[Link](170, 280, 60, 40);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentText = "";
[Link](currentText);
}
});
[Link](clearButton);
// Frame settings
[Link](350, 400);
[Link](null);
[Link](true);
P a g e 36 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
[Link](0);
}
});
}
P a g e 37 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
return [Link](result);
} catch (Exception e) {
return "Error"; // Return "Error" in case of invalid
expression
}
}
11.
A - WAP to demonstrate LinkedList and it's methods
.
import [Link];
P a g e 38 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
// Accessing elements
String firstElement = [Link]();
String lastElement = [Link]();
[Link]("First element: " + firstElement);
[Link]("Last element: " + lastElement);
OUTPUT :-
P a g e 39 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
import [Link];
P a g e 40 | 41
IFT2308 -JAVA PROGRAMMING LAB
Name: ANGELEENA MARIA ROY Enrollment No.: -A71004923010
[Link]();
[Link]("After clearing the HashSet: " + set);
}
}
OUTPUT :-
P a g e 41 | 41