RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 1
Aim : Write a program to print the hello world using java .
public class HelloWorld
{
public static void main(String[] args)
{
[Link]("Hello, World!");
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 2
Aim : write a program to take input using scanner class.
import [Link];
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Enter your age: ");
int age = [Link]();
[Link]("Enter your height in meters: ");
double height = [Link]();
[Link]("Hello " + name + "! You are " + age + " years old and " + height + "
meters tall.");
[Link]();
}
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 3
AIM : Write a program demonstrate widening and narrowing type casting using
java .
public class TypeCastingExample {
public static void main(String[] args) {
// Widening Type Casting
// Smaller data type → Larger data type
int intValue = 100;
double doubleValue = intValue; // Automatic casting
[Link]("Widening Type Casting:");
[Link]("Integer value: " + intValue);
[Link]("Converted to Double: " + doubleValue);
// Narrowing Type Casting
// Larger data type → Smaller data type
double d = 99.99;
int i = (int) d; // Manual casting
[Link]("\nNarrowing Type Casting:");
[Link]("Double value: " + d);
[Link]("Converted to Integer: " + i);
}
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 4
Aim : Write a program perform arithmetic operator in java
import [Link];
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Taking input from user
[Link]("Enter first number: ");
double num1 = [Link]();
[Link]("Enter second number: ");
double num2 = [Link]();
// Performing arithmetic operations
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
double quotient = num1 / num2;
double remainder = num1 % num2;
// Displaying results
[Link]("\n--- Arithmetic Operations ---");
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
[Link]("Addition: " + sum);
[Link]("Subtraction: " + difference);
[Link]("Multiplication: " + product);
[Link]("Division: " + quotient);
[Link]("Modulus: " + remainder);
[Link]();
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 5
AIM : Write a program perform relational operations in java
import [Link];
public class RelationalOperations {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
// Performing relational operations
[Link]("\n--- Relational Operations ---");
[Link]("num1 == num2 : " + (num1 == num2));
[Link]("num1 != num2 : " + (num1 != num2));
[Link]("num1 > num2 : " + (num1 > num2));
[Link]("num1 < num2 : " + (num1 < num2));
[Link]("num1 >= num2 : " + (num1 >= num2));
[Link]("num1 <= num2 : " + (num1 <= num2));
[Link]();
}
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 6
AIM : Write a program to demonstrate logical And , or , not operators in java
import [Link];
public class LogicalOperatorsDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Taking input from user
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
// Conditions
boolean condition1 = (num1 > 0);
boolean condition2 = (num2 > 0);
[Link]("\n--- Logical Operators Demonstration ---");
// Logical AND (&&)
[Link]("Logical AND (num1 > 0 && num2 > 0): "
+ (condition1 && condition2));
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
// Logical OR (||)
[Link]("Logical OR (num1 > 0 || num2 > 0): "
+ (condition1 || condition2));
// Logical NOT (!)
[Link]("Logical NOT (!(num1 > 0)): "
+ (!condition1));
[Link]();
}
}
OUTPUT :
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 7
AIM : Write a program demonstrate bitwise operations in java
public class BitwiseOperatorsDemo {
public static void main(String[] args) {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
[Link]("Value of a = " + a);
[Link]("Value of b = " + b);
// Bitwise AND
[Link]("\nBitwise AND (a & b) : " + (a & b));
// Bitwise OR
[Link]("Bitwise OR (a | b) : " + (a | b));
// Bitwise XOR
[Link]("Bitwise XOR (a ^ b) : " + (a ^ b));
// Bitwise NOT
[Link]("Bitwise NOT (~a) : " + (~a));
// Left Shift
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
[Link]("Left Shift (a << 1) : " + (a << 1));
// Right Shift
[Link]("Right Shift (a >> 1) : " + (a >> 1));
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 8
AIM : Write a program to find the largest number among three values in java
import [Link];
public class LargestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Taking input from user
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
[Link]("Enter third number: ");
int num3 = [Link]();
int largest;
// Finding largest number using if-else
if (num1 >= num2 && num1 >= num3) {
largest = num1;
}
else if (num2 >= num1 && num2 >= num3) {
largest = num2;
}
else {
largest = num3;
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
// Display result
[Link]("\nLargest number is: " + largest);
[Link]();
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 9
AIM : Write a program to write simple if statement in java
import [Link];
public class SimpleIfExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Taking input from user
[Link]("Enter a number: ");
int number = [Link]();
// Simple if statement
if (number > 0) {
[Link]("The number is positive.");
}
[Link]();
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 10
AIM : Write a program to use if-else decision making in java
import [Link];
public class IfElseExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Taking input from user
[Link]("Enter a number: ");
int number = [Link]();
// if-else decision making
if (number % 2 == 0) {
[Link]("The number is Even.");
} else {
[Link]("The number is Odd.");
}
[Link]();
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 11
AIM : Write a java program to demonstrate nested if-else.
public class NestedIfElseDemo {
public static void main(String[] args) {
int number = 15;
if (number > 0) {
[Link]("Number is positive.");
if (number % 2 == 0) {
[Link]("Number is even.");
} else {
[Link]("Number is odd.");
}
} else if (number < 0) {
[Link]("Number is negative.");
} else {
[Link]("Number is zero.");
}
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 12
AIM : Write a java program to demonstrate switch statement .
public class SwitchDemo {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
Break;
case 6:
[Link]("Saturday");
break;
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
case 7:
[Link]("Sunday");
break;
default:
[Link]("Invalid day number");
}
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 13
AIM : Write a java program to demonstrate while loop .
public class WhileLoopDemo {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link]("Value of i: " + i);
i++; // Increment i
}
[Link]("Loop finished.");
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 14
AIM : Write a java program to demonstrate do while-loop .
public class DoWhileDemo {
public static void main(String[] args) {
int i = 1;
do {
[Link]("Value of i: " + i);
i++; // Increment i
}
while (i <= 5);
[Link]("Loop finished.");
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 15
AIM : Write a java program to demonstrate for loop .
public class ForLoopDemo {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link]("Value of i: " + i);
}
[Link]("Loop finished.");
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 16
AIM : Write a java program to demonstrate nested loops .
public class NestedLoopDemo {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) { // Outer loop
for (int j = 1; j <= 4; j++) { // Inner loop
[Link]("i = " + i + ", j = " + j);
}
[Link](); // Print blank line after each outer loop iteration
}
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 17
AIM : Write a java program to demonstrate multiplication table .
import [Link];
public class MultiplicationTable {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
[Link]("Multiplication Table of " + num + ":");
for (int i = 1; i <= 10; i++) {
[Link](num + " x " + i + " = " + (num * i));
}
[Link]();
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 18
AIM : Develop a java program that demonstrate the use of abstract
class .
abstract class Animal {
// Abstract method (no body)
abstract void sound();
// Normal method
void sleep() {
[Link]("Animal is sleeping");
}
}
class Dog extends Animal {
// Providing implementation of abstract method
void sound() {
[Link]("Dog barks");
}
}
public class AbstractClassDemo {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); // Calls implemented method
[Link](); // Calls normal method from abstract class
}
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 19
AIM : WPA To examine the significance of the this keyword in
resolving naming conflicts between instance variables and local
variables .
class Student {
int rollNo; // Instance variable
String name; // Instance variable
// Constructor with local variables having same names
Student(int rollNo, String name) {
// Using 'this' to refer to instance variables
[Link] = rollNo;
[Link] = name;
}
void display() {
[Link]("Roll No: " + rollNo);
[Link]("Name: " + name);
}
}
public class ThisKeywordDemo {
public static void main(String[] args) {
Student s1 = new Student(101, "Rahul");
[Link]();
}
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 20
AIM : WPA To implement conditional control statement using if-else and logical
operators to control the flow of execution based on given conditions.
import [Link];
public class ConditionalControlDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your age: ");
int age = [Link]();
[Link]("Do you have a driving license? (true/false): ");
boolean hasLicense = [Link]();
// Using if-else with logical AND (&&) operator
if (age >= 18 && hasLicense == true) {
[Link]("You are eligible to drive.");
}
else if (age >= 18 && hasLicense == false) {
[Link]("You are eligible by age but must obtain a license.");
}
else {
[Link]("You are not eligible to drive.");
}
[Link]();
}
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 21
AIM : Write a program to find the give number is prime or not .
import [Link];
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
boolean isPrime = true; // Assume number is prime
if (num <= 1) {
isPrime = false; // 0 and 1 are not prime
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break; // Exit loop if divisible
}
}
}
if (isPrime) {
[Link](num + " is a prime number.");
} else {
[Link](num + " is not a prime number.");
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
[Link]();
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 22
AIM : WPA Develop program to demonstrate the use of exception handling
using predefined exception classes.
import [Link];
public class ExceptionHandlingDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter numerator: ");
int numerator = [Link]();
[Link]("Enter denominator: ");
int denominator = [Link]();
// This may throw ArithmeticException if denominator is 0
int result = numerator / denominator;
[Link]("Result: " + result);
// Accessing array element
int[] arr = {1, 2, 3};
[Link]("Enter index to access (0-2): ");
int index = [Link]();
// This may throw ArrayIndexOutOfBoundsException
[Link]("Element at index " + index + " is " + arr[index]);
}
catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed!");
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Error: Invalid array index!");
}
catch (Exception e) {
[Link]("Error: " + [Link]());
}
finally {
[Link]("Program execution finished.");
[Link]();
}
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 23
AIM : WPA Develop a program to handle multiple exceptions using multiple try
blocks and multiple catch blocks .
import [Link];
public class MultipleExceptionsDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// First try block: ArithmeticException
try {
[Link]("Enter numerator: ");
int numerator = [Link]();
[Link]("Enter denominator: ");
int denominator = [Link]();
int result = numerator / denominator; // May throw ArithmeticException
[Link]("Result: " + result);
}
catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero!");
}
// Second try block: ArrayIndexOutOfBoundsException
try {
int[] arr = {10, 20, 30};
[Link]("Enter array index to access (0-2): ");
int index = [Link]();
[Link]("Element at index " + index + " is " + arr[index]);
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Error: Invalid array index!");
}
// Third try block: InputMismatchException
try {
[Link]("Enter an integer value: ");
int value = [Link](); // May throw InputMismatchException if input is not an integer
[Link]("You entered: " + value);
}
catch ([Link] e) {
[Link]("Error: Invalid input! Please enter an integer.");
}
finally {
[Link]("Program execution finished.");
[Link]();
}
}
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 24
AIM : WPA a program to find the number is odd or even .
import [Link];
public class OddEvenCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
// Check if number is even or odd
if (num % 2 == 0) {
[Link](num + " is an even number.");
} else {
[Link](num + " is an odd number.");
}
[Link]();
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 25
AIM : WPA a program to demonstrate use of throw , throws , and
finally keyword .
import [Link];
class CustomException {
// Method that declares it may throw an exception using 'throws'
static void checkAge(int age) throws Exception {
if (age < 18) {
// Using 'throw' to explicitly throw an exception
throw new Exception("Age must be 18 or above to vote.");
}
else {
[Link]("You are eligible to vote.");
}
}
}
public class ThrowThrowsFinallyDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter your age: ");
int age = [Link]();
// Call method that may throw exception
[Link](age);
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
}
catch (Exception e) {
[Link]("Exception caught: " + [Link]());
}
finally {
// finally block always executes
[Link]("Execution of try-catch-finally block is complete.");
[Link]();
}
}
}
OUTPUT
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
PRACTICAL 26
AIM : WAP To implement interfaces in java and demonstrate multiple
inheritance to achieve abstraction and loose coupling .
interface Engine {
void startEngine();
}
interface MusicSystem {
void playMusic();
}
// A class implementing multiple interfaces (demonstrates multiple inheritance)
class Car implements Engine, MusicSystem {
private String carName;
Car(String name) {
[Link] = name;
}
// Implementing Engine interface method
public void startEngine() {
[Link](carName + "'s engine has started.");
}
// Implementing MusicSystem interface method
public void playMusic() {
[Link](carName + " is playing music.");
}
}
RMS COLLAGE OF BCA
Enroll no : 24020391025110012 SUBJECT CODE : BCA23MJ401
// Demonstrates loose coupling using interface reference
public class InterfaceDemo {
public static void main(String[] args) {
// Using interface reference (Engine) to refer to Car object
Engine myCarEngine = new Car("Tesla");
[Link](); // Can only access Engine methods
// Using interface reference (MusicSystem) to refer to Car object
MusicSystem myCarMusic = new Car("Tesla");
[Link](); // Can only access MusicSystem methods
// Using concrete class reference
Car myCar = new Car("Tesla");
[Link]();
[Link]();
}
}
OUTPUT