BCA-RRIMS JAVA LAB MAUNAL 2025-2026 SEMESTER 2 SEP SCHEME JAVA LAB MANUAL Program 1. Java program to display “Hello World” and display the size of all the data types package sem22024lab; public class HelloWorld { public static void main (String[] args) { // TODO Auto-generated method stub System.out.println("Hello World"); } } OUTPUT: Hello World BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 2 - Java program to implement the usage of static, local and global variables. package sem22024lab; public class VariableExample { // Global variable (Instance variable) int globalVar = 10; // Static variable static int staticVar = 20; void demonstrateVariables() { // Local variable int localVar = 30; System.out.println("Local Variable: " + localVar); System.out.println("Global Variable: " + globalVar); System.out.println("Static Variable: " + staticVar); } public static void main(String[] args) { // Creating an object to access the instance variable VariableExample obj = new VariableExample(); obj.demonstrateVariables(); // Accessing the static variable without an object System.out.println("Accessing Static Variable without object: " +VariableExample.staticVar); } } OUTPUT: Local Variable: 30 Global Variable: 10 Static Variable: 20 Accessing Static Variable without object: 20 BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 3 - Java program to implement string operations string length, string concatenate, substring package sem22024lab; public class StringOperations { public static void main(String[] args) { // String declaration String str1 = "Hello"; String str2 = "World"; // String length System.out.println("Length of str1: " + str1.length()); // String concatenation String concatenatedStr = str1 + " " + str2; System.out.println("Concatenated String: " + concatenatedStr); // Substring String substring = concatenatedStr.substring(6, 11); System.out.println("Substring: " + substring); } } OUTPUT: Length of str1: 5 Concatenated String: Hello World Substring: World BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 4 - Java program to find the maximum of three numbers package sem22024lab; import java.util.Scanner; public class MaxOfThreeNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Taking three numbers as input System.out.print("Enter first number: "); int num1 = scanner.nextInt(); System.out.print("Enter second number: "); int num2 = scanner.nextInt(); System.out.print("Enter third number: "); int num3 = scanner.nextInt(); // Finding the maximum number int max = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3?num2 : num3); System.out.println("Maximum number is: " + max); scanner.close(); } } OUTPUT: Enter first number: 24 Enter second number: 12 Enter third number: 10 Maximum number is: 24 BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 5 - Java program to check whether the number is odd or even. package sem22024lab; import java.util.Scanner; public class OddEvenCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Taking input from the user System.out.print("Enter a number: "); int num = scanner.nextInt(); // Checking if the number is odd or even if (num % 2 == 0) { System.out.println(num + " is Even."); } else { System.out.println(num + " is Odd."); } scanner.close(); } } OUTPUT: Enter a number: 45 45 is Odd. Enter a number: 12 12 is Even. BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 6 - Java program to implement default and parameterized constructors package sem22024lab; public class ConstructorExample { int number; String text; // Default Constructor ConstructorExample() { this.number = 0; this.text = "Default"; System.out.println("Default Constructor Called"); } // Parameterized Constructor ConstructorExample(int number, String text) { this.number = number; this.text = text; System.out.println("Parameterized Constructor Called"); } void display() { System.out.println("Number: " + number + ", Text: " + text); } public static void main(String [] args) { // Creating object using Default Constructor ConstructorExample obj1 = new ConstructorExample(); obj1.display(); // Creating object using Parameterized Constructor ConstructorExample obj2 = new ConstructorExample(42, "Hello"); obj2.display(); }} OUTPUT: Default Constructor Called Number: 0, Text: Default Parameterized Constructor Called Number: 42, Text: Hello BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 7 - Java program to implement an array of objects. package sem22024lab; public class Student { String name; int age; // Constructor Student(String name, int age) { this.name = name; this.age = age; } void display() { System.out.println("Name: " + name + ", Age: " + age); } public static void main(String[] args) { // Creating an array of Student objects Student[] students = new Student[3]; // Initializing objects in the array students[0] = new Student("Alice", 20); students[1] = new Student("Bob", 22); students[2] = new Student("Charlie", 21); // Displaying student details for (Student student : students) { student.display(); } } } OUTPUT: Name: Alice, Age: 20 Name: Bob, Age: 22 Name: Charlie, Age: 21 BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 8 - Java program to implement Single Inheritance package sem22024lab; class Parent { void display() { System.out.println("This is the Parent class"); } } class Child extends Parent { void show() { System.out.println("This is the Child class"); } public static void main(String[] args) { Child obj = new Child(); obj.display(); // Calling method from Parent class obj.show(); // Calling method from Child class } } OUTPUT: This is the Parent class This is the Child class BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 9 – Java program to implement Multiple Inheritance using Interface package sem22024lab; interface A { void methodA(); } interface B { void methodB(); } // Class implementing multiple interfaces class C implements A, B { public void methodA() { System.out.println("Method A from Interface A"); } public void methodB() { System.out.println("Method B from Interface B"); } } public class MultipleInheritanceExample { public static void main(String[] args) { C obj = new C(); obj.methodA(); obj.methodB(); } } OUTPUT: Method A from Interface A Method B from Interface B BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 10 - Java program to demonstrate a division by zero exception package sem22024lab; public class DivisionByZeroExample { public static void main(String[] args) { try { int numerator = 10; int denominator = 0; int result = numerator / denominator; // This will throw an exception System.out.println("Result: " + result); } catch (ArithmeticException e) { System.out.println("Error: Division by zero is not allowed."); } } } OUTPUT: Error: Division by zero is not allowed. BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 11 - Java program to add two integers and two float numbers. When no arguments are supplied give a default value to calculate the sum. Use method overloading package sem22024lab; class Addition { // Method to add two integers int add(int a, int b) { return a + b;} // Method to add two float numbers float add(float a, float b) { return a + b;} // Method with no arguments, using default values int add() { int defaultA = 5; int defaultB = 10; return defaultA + defaultB; } } public class MethodOverloadingExample { public static void main(String[] args) { Addition addition = new Addition(); // Calling overloaded methods System.out.println("Sum of two integers: " + addition.add(3, 7)); System.out.println("Sum of two floats: " + addition.add(4.5f, 2.3f)); System.out.println("Sum with default values: " + addition.add()); } } OUTPUT: Sum of two integers: 10 Sum of two floats: 6.8 Sum with default values: 15 BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 12 - Java program that demonstrates run-time polymorphism. package sem22024lab; class Animal { void makeSound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Dog barks"); } } class Cat extends Animal { @Override void makeSound() { System.out.println("Cat meows"); } } public class RuntimePolymorphismExample { public static void main(String[] args) { Animal myAnimal; // Reference of type Animal myAnimal = new Dog(); myAnimal.makeSound(); // Calls Dog's makeSound method myAnimal = new Cat(); myAnimal.makeSound(); // Calls Cat's makeSound method } } OUTPUT: Dog barks Cat meows BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 13 - Java program to catch negative array size Exception. This exception is caused when the array is initialized to negative values package sem22024lab; public class NegativeArraySizeExceptionExample { public static void main(String[] args) { try { int size = -5; // Negative size int[] arr = new int[size]; // This will throw NegativeArraySizeException } catch (NegativeArraySizeException e) { System.out.println("Error: Array size cannot be negative."); } } } OUTPUT: Error: Array size cannot be negative. BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 14 - Java program to handle null pointer exception and use the “finally” method to display a message to the user. package sem22024lab; public class NullPointerExceptionExample { public static void main(String[] args) { try { String str = null; // Null reference System.out.println(str.length()); // This will throw NullPointerException } catch (NullPointerException e) { System.out.println("Error: Attempted to access a null reference."); } finally { System.out.println("Execution completed. This message is fromthe finallyblock."); } } OUTPUT: Error: Attempted to access a null reference. Execution completed. This message is from the finally block BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 15 - Java program to import user-defined packages package sem22024lab; public class MyClass { public void displayMessage() { System.out.println("Hello from MyClass in mypackage!"); } } package samplecheckpkg; import sem22024lab.MyClass; public class PackageExample { public static void main(String[] args) { MyClass obj = new MyClass(); obj.displayMessage(); } } OUTPUT: Hello from MyClass in mypackage! BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 16 - Java program to check whether a number is palindrome or not import java.util.Scanner; public class PalindromeChecker { public static boolean isPalindrome(int number) { int original = number; int reverse = 0; while (number > 0) { int digit = number % 10; reverse = reverse * 10 + digit; number /= 10; } return original == reverse; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int num = scanner.nextInt(); scanner.close(); if (isPalindrome(num)) { System.out.println(num + " is a palindrome."); } else { System.out.println(num + " is not a palindrome."); } } } OUTPUT: Enter a number: 121 121 is a palindrome. Enter a number: 435 435 is not a palindrome BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 17 - Java program to find the factorial of a list of numbers package sem22024lab; import java.util.Scanner; public class FactorialList { // Method to calculate factorial public static long factorial(int num) { long fact = 1; for (int i = 1; i <= num; i++) { fact *= i; } return fact; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of elements: "); int n = scanner.nextInt(); int[] numbers = new int[n]; System.out.println("Enter the numbers:"); for (int i = 0; i < n; i++) { numbers[i] = scanner.nextInt(); } System.out.println("Factorials:"); for (int num : numbers) { if (num < 0) { System.out.println("Factorial is not defined for negative numbers: " + num); } else { System.out.println("Factorial of " + num + " is " + factorial(num)); } } scanner.close(); } } BCA-RRIMS JAVA LAB MAUNAL 2025-2026 OUTPUT: Enter the number of elements: 5 Enter the numbers: 2 4 5 6 2 Factorials: Factorial of 2 is 2 Factorial of 4 is 24 Factorial of 5 is 120 Factorial of 6 is 720 Factorial of 2 is 2 BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 18 - Java program to display all prime numbers between two limits. package sem22024lab; import java.util.Scanner; public class PrimeNumbersInRange { // Method to check if a number is prime public static boolean isPrime(int num) { if (num < 2) { return false;} for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { return false;} } return true;} public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the lower limit: "); int lower = scanner.nextInt(); System.out.print("Enter the upper limit: "); int upper = scanner.nextInt(); System.out.println("Prime numbers between " + lower + " and " + upper +":"); for (int i = lower; i <= upper; i++) { if (isPrime(i)) { System.out.print(i + " ");} } System.out.println(); scanner.close();} } OUTPUT: Enter the lower limit: 10 Enter the upper limit: 50 Prime numbers between 10 and 50: 11 13 17 19 23 29 31 37 41 43 47 BCA-RRIMS JAVA LAB MAUNAL 2025-2026 Program 19 - Java program to create a thread using Runnable Interface. package sem22024lab; public class RunnableExample implements Runnable { @Override public void run() { System.out.println("Thread is running."); } public static void main(String[] args) { // Create a new thread using Runnable interface RunnableExample runnable = new RunnableExample(); Thread thread = new Thread(runnable); thread.start(); System.out.println("Main thread is running."); } } OUTPUT: Main thread is running. Thread is running.