410bco21 - Oop Java-Lab Manual
410bco21 - Oop Java-Lab Manual
LAB
B.Com., COMPUTER APPLICATION/BA. COMPUTER APPLICATION
Semester – IV
Lesson Writers
Editor
DirectorI/c
Prof. V.VENKATESWARLU
MA., M.P.S., M.S.W., M.Phil., Ph.D
SYLLABUS
2. Write a program to read Student Name, Reg.No, Marks and calculate Total, Percentage,
and Result. Display all the details of students .
7. Calculate area of the following shapes using method overloading. a.Rectangle b. Circle
c. Square
9. Java program for to display Serial Number from 1 to 5 by creating two Threads
10. Java program to demonstrate the following exception handlings a. Divided by Zero b.
Array Index Out of Bound c. Arithmetic Exception
CONTENTS
2. Write a program to read Student Name, Reg.No, Marks and 1.3 – 1.4
calculate Total, Percentage, and Result. Display all the details of
students .
3. Program to perform string operations 1.5 – 1.6
The objective of this lab is to master the OOP concepts with java programming and to learn
how to work with real time applications using java programming. These programs are widely
used in most real-time scenario. After the end of lab, students will be able know the complete
practical exposure on oop using java
STRUCTURE
CODE:
public class CommandLineExample {
public static void main(String[] args) {
// Check if any command-line arguments are provided
if (args.length == 0) {
System.out.println("No command-line arguments were provided.");
return;
}
OUTPUT:
Command-line arguments provided:
Argument 1: Hello (Length: 5)
Argument 2: World (Length: 5)
Argument 3: Java (Length: 4)
Argument 4: Programming (Length: 11)
EXPLANATION:
The program prints each word (argument) passed to it from the command line, along
with the position of the argument and its length.
For example, "Hello" is the first argument, and its length is 5 characters.
-Dr. G. NEELIMA
Centre for Distance Education 1.3 Acharya Nagarjuna University
CODE:
import java.util.Scanner;
scanner.close();
}
}
OOP with JAVA 1.4 OOP using JAVA Lab
OUTPUT:
Enter Student Name: John Doe
Enter Registration Number: 12345
Enter marks for Subject 1: 75
Enter marks for Subject 2: 85
Enter marks for Subject 3: 65
EXPLANATION:
-Dr. G. NEELIMA
Centre for Distance Education 1.5 Acharya Nagarjuna University
CODE:
import java.util.Scanner;
public class StringOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 1. Concatenation
String concatenatedString = str1 + " " + str2;
System.out.println("\nConcatenated String: " + concatenatedString);
// 4. Extracting substring
if (str1.length() >= 3) {
System.out.println("Substring of the first string (first 3 characters): " + str1.substring(0, 3));
} else {
System.out.println("First string is too short to extract a 3-character substring.");
}
if (str2.length() >= 3) {
System.out.println("Substring of the second string (first 3 characters): " + str2.substring(0, 3));
} else {
System.out.println("Second string is too short to extract a 3-character substring.");
}
} else {
System.out.println("The strings are not equal (case ignored).");
}
scanner.close();
}
}
OUTPUT:
Enter the first string: Hello
Enter the second string: World
Concatenated String: Hello World
Length of the first string: 5
Length of the second string: 5
First string in uppercase: HELLO
Second string in lowercase: world
Substring of the first string (first 3 characters): Hel
Substring of the second string (first 3 characters): Wor
The strings are not equal.
The strings are not equal (case ignored).
EXPLANATION:
Concatenated String: Combines the two input strings with a space in between.
Length: Displays the number of characters in each string.
Uppercase and Lowercase: Converts and displays the first string in all uppercase letters
and the second string in all lowercase letters.
Substring: Extracts and displays the first three characters of each string, if possible.
String Comparison: Checks whether the two strings are equal, both with and without
considering case sensitivity.
-Dr. G. NEELIMA
Centre for Distance Education 1.7 Acharya Nagarjuna University
CODE:
import java.util.Scanner;
scanner.close();
}
}
OUTPUT:
Enter the size of the matrix (N x N): 2
Enter the elements of the first matrix:
Element [0][0]: 1
Element [0][1]: 2
Element [1][0]: 3
Element [1][1]: 4
Enter the elements of the second matrix:
Element [0][0]: 5
Element [0][1]: 6
Element [1][0]: 7
Element [1][1]: 8
The resulting matrix after addition is:
68
10 12
EXPLANATION:
This program handles matrix addition by adding corresponding elements and is flexible for
any N×NN \times NN×N matrix size as defined by the user.
-Dr. G. NEELIMA
Centre for Distance Education 1.9 Acharya Nagarjuna University
CODE:
import java.util.Scanner;
scanner.close();
}
OUTPUT:
Enter the number of elements: 5
Enter the elements of the array:
Element 1: 64
Element 2: 34
Element 3: 25
Element 4: 12
Element 5: 22
Sorted array:
12 22 25 34 64
EXPLANATION:
The Bubble Sort algorithm sorts the array in ascending order.
After the sorting process, the smallest element moves to the front, and the largest element
moves to the end of the array.
-Dr. G. NEELIMA
Centre for Distance Education 1.11 Acharya Nagarjuna University
CODE:
class Student {
// Instance variables
String name;
int regNo;
double marks1;
double marks2;
double marks3;
// Default constructor
public Student() {
// Initializing with default values
name = "Unknown";
regNo = 0;
marks1 = 0.0;
marks2 = 0.0;
marks3 = 0.0;
}
// Parameterized constructor
public Student(String name, int regNo, double marks1, double marks2, double marks3) {
// Initializing with provided values
this.name = name;
this.regNo = regNo;
this.marks1 = marks1;
this.marks2 = marks2;
this.marks3 = marks3;
}
System.out.println();
OUTPUT:
Student 1 Details (Using Default Constructor):
Student Name: Unknown
Registration Number: 0
Marks 1: 0.0
Marks 2: 0.0
Marks 3: 0.0
Total Marks: 0.0
EXPLANATION:
Student 1:
Created using the default constructor, so the details are initialized to default values:
"Unknown" for the name, 0 for the registration number, and 0.0 for marks.
Student 2:
Created using the parameterized constructor with specific values provided during object
creation.
CODE:
public class AreaCalculator {
OUTPUT:
Area of the circle with radius 5.0 is: 78.53981633974483
Area of the rectangle with length 10.0 and breadth 5.0 is: 50.0
Area of the triangle with base 8.0 and height 6.0 is: 24.0
EXPLANATION:
The program calculates and displays the area for each shape using the appropriate
overloaded method.
Circle: With a radius of 5.0, the area is approximately 78.54.
Rectangle: With a length of 10.0 and a breadth of 5.0, the area is 50.0.
Triangle: With a base of 8.0 and a height of 6.0, the area is 24.0.
CODE:
// Base class
class Animal {
String name;
OUTPUT:
Buddy is eating.
Buddy is sleeping.
Buddy is walking.
Buddy is barking.
OOP with JAVA 1.16 OOP using JAVA Lab
EXPLANATION:
The Dog class inherits the eat() and sleep() methods from the Animal class, and the walk()
method from the Mammal class.
When the dog object calls these methods, it performs actions appropriate to each method,
using the name "Buddy".
CODE:
class SerialNumberPrinter implements Runnable {
private String threadName;
@Override
public void run() {
// Printing numbers from 1 to 5
for (int i = 1; i<= 5; i++) {
System.out.println(threadName + ": " + i);
try {
// Introducing a small delay to simulate real-time thread execution
Thread.sleep(100); // 100 milliseconds delay
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
}
}
}
}
OUTPUT:
Thread 1: 1
Thread 2: 1
Thread 1: 2
Thread 2: 2
Thread 1: 3
Thread 2: 3
Thread 1: 4
Thread 2: 4
Thread 1: 5
Thread 2: 5
Both threads have finished execution.
EXPLANATION:
The two threads execute concurrently, each printing numbers from 1 to 5.
Due to the small delay introduced by Thread.sleep(100), the threads alternate their output,
but the exact sequence may vary depending on the thread scheduler.
CODE:
public class ExceptionHandlingDemo {
public static void main(String[] args) {
// 1. Demonstrating Divide by Zero (ArithmeticException)
try {
int a = 10;
int b = 0;
int result = a / b;
System.out.println("Result of division: " + result);
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e.getMessage());
}
OUTPUT:
Exception caught: / by zero
Exception caught: Index 10 out of bounds for length 5
Exception caught: For input string: "NaN"
EXPLANATION:
o / by zero: This message indicates that the program tried to divide by zero, which is not
allowed.
o Index 10 out of bounds for length 5: This message indicates that the program tried to
access an array element at an index that doesn't exist.
o For input string: "NaN": This message indicates that the program tried to parse a non-
numeric string as an integer, which is invalid.