0% found this document useful (0 votes)
3 views16 pages

Java_Lab_Manual[1] (1)

The document is a Java lab manual for BCA Semester II, containing various programming exercises. It includes 12 programs covering topics such as data types, variable usage, string operations, constructors, inheritance, exception handling, and palindrome checking. Each program is accompanied by code examples demonstrating the concepts discussed.

Uploaded by

rahulbk182006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Java_Lab_Manual[1] (1)

The document is a Java lab manual for BCA Semester II, containing various programming exercises. It includes 12 programs covering topics such as data types, variable usage, string operations, constructors, inheritance, exception handling, and palindrome checking. Each program is accompanied by code examples demonstrating the concepts discussed.

Uploaded by

rahulbk182006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Aishwarya s

Izee business school


JAVA LAB MANUAL
(BCA SEM - II)

** Programs **

1) Java program to display “Hello World” and display the size of all the
data types.

2) Java program to implement the usage of static, local and global variables.

3) Java program to implement string operations string length, string


concatenate, substring.

4) Java program to find the maximum of three numbers.

5) Java program to check whether the number is odd or even.

6) Java program to implement default and parameterized constructors.

7) Java program to implement an array of objects.

8) Java program to implement Single Inheritance.

9) Java program to implement Multiple Inheritance using Interface.

10) Java program to demonstrate a division by zero exception.(p-11)

11) Java program to catch negative array size Exception. This exception is
caused when the array is initialized to negative values.(p-14)

12) Java program to check whether a number is palindrome or not.(p-17)

Aishwarya s
Izee business school
Program 1 - Java program to display “Hello World” and display the size of all
the data types.

public class HelloWorld {


public static void main(String[] args) {

// Display "Hello World"


System.out.println("Hello World");

// Display the size of all the data types


System.out.println("Size of byte: " + Byte.SIZE + " bits");
System.out.println("Size of short: " + Short.SIZE + " bits");
System.out.println("Size of int: " + Integer.SIZE + " bits");
System.out.println("Size of long: " + Long.SIZE + " bits");
System.out.println("Size of float: " + Float.SIZE + " bits");
System.out.println("Size of double: " + Double.SIZE + " bits");
System.out.println("Size of char: " + Character.SIZE + " bits");
System.out.println("Size of boolean: 1 bit (not defined by Java, it
depends on JVM implementation)");
}
}

Aishwarya s
Izee business school
Program 2 - Java program to implement the usage of static, local and global
variables.

public class VariableExample {

// Instance (Global) variable


int globalVariable = 10;

// Static variable
static int staticVariable = 20;

// Method demonstrating local variable


public void showVariables() {

// Local variable
int localVariable = 30;

System.out.println("Local Variable: " + localVariable);


System.out.println("Global Variable (Instance): " + globalVariable);
System.out.println("Static Variable: " + staticVariable);
}

public static void main(String[] args) {

// Accessing static variable directly using the class name


System.out.println("Static Variable accessed in main: " +
VariableExample.staticVariable);

// Creating an object of the class to access instance (global) variables


VariableExample obj = new VariableExample();

System.out.println("Global Variable accessed in main: " +


obj.globalVariable);

// Calling the method to demonstrate local, static, and global variables


obj.showVariables();

Aishwarya s
Izee business school
// Modifying and demonstrating the static variable usage
VariableExample.staticVariable = 50;

System.out.println("Modified Static Variable accessed in main: " +


VariableExample.staticVariable);
}
}

Program 3 - Java program to implement string operations string length, string


concatenate, substring.

public class StringOperations {


public static void main(String[] args) {

// Initialize two strings for operations


String str1 = "Hello";
String str2 = "World";

// 1. String Length
int length1 = str1.length(); // Length of str1
int length2 = str2.length(); // Length of str2
System.out.println("Length of str1: " + length1);
System.out.println("Length of str2: " + length2);
Aishwarya s
Izee business school
// 2. String Concatenation
String concatenatedString = str1 + " " + str2; // Concatenate str1 and str2
System.out.println("Concatenated String: " + concatenatedString);

// 3. Substring
String substring1 = str1.substring(1, 4); // Substring from index 1 to 3 of
str1
String substring2 = str2.substring(1); // Substring from index 1 to the
end of str2
System.out.println("Substring of str1 (1, 4): " + substring1);
System.out.println("Substring of str2 (1 to end): " + substring2);
}
}

Program 4 - Java program to find the maximum of three numbers.

import java.util.Scanner;

public class MaxOfThree {


public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);

// Read three numbers from the user


System.out.print("Enter first number: ");
Aishwarya s
Izee business school
int num1 = scanner.nextInt();

System.out.print("Enter second number: ");


int num2 = scanner.nextInt();

System.out.print("Enter third number: ");


int num3 = scanner.nextInt();

// Find the maximum number


int max = num1; // Assume num1 is the largest initially

if (num2 > max) {


max = num2; // Update max if num2 is larger
}

if (num3 > max) {


max = num3; // Update max if num3 is larger
}

// Display the maximum number


System.out.println("The maximum number is: " + max);

// Close the scanner


scanner.close();
}
}

Aishwarya s
Izee business school
Program 5 - Java program to check whether the number is odd or even.

import java.util.Scanner;

public class OddEvenCheck {


public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);

// Read a number from the user


System.out.print("Enter a number: ");
int num = scanner.nextInt();

// Check if the number is even or odd


if (num % 2 == 0) {
System.out.println(num + " is an even number.");
} else {
System.out.println(num + " is an odd number.");
}

// Close the scanner


scanner.close();
}
}

Aishwarya s
Izee business school
Program 6 - Java program to implement default and parameterized
constructors.

public class ConstructorExample {

// Default Constructor
public ConstructorExample() {
System.out.println("This is the default constructor.");
}

// Parameterized Constructor
public ConstructorExample(String message, int number) {
System.out.println("This is the parameterized constructor.");
System.out.println("Message: " + message);
System.out.println("Number: " + number);
}

public static void main(String[] args) {


// Creating an object using the default constructor
ConstructorExample obj1 = new ConstructorExample();

// Creating an object using the parameterized constructor


ConstructorExample obj2 = new ConstructorExample("Hello, Java!",42);
}
}

Aishwarya s
Izee business school
Program 7 - Java program to implement an array of objects.

class Student {
// Instance variables
String name;
int age;

// Constructor to initialize student details


public Student(String name, int age) {
this.name = name;
this.age = age;
}

// Method to display student details


public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}

public class ArrayOfObjects {


public static void main(String[] args) {
// Create an array of Student objects
Student[] students = new Student[3]; // Array to hold 3 Student objects

// Initialize the array with Student objects


students[0] = new Student("Alice", 20);
students[1] = new Student("Bob", 22);
students[2] = new Student("Charlie", 21);

// Display the details of all students


for (int i = 0; i < students.length; i++) {
students[i].display();
}
}
}

Aishwarya s
Izee business school
Program 8 - Java program to implement Single Inheritance

// Parent class
class Animal {
// Instance variable of the parent class
String name;

// Constructor of the parent class


public Animal(String name) {
this.name = name;
}

// Method of the parent class


public void eat() {
System.out.println(name + " is eating.");
}
}

// Child class that inherits from Animal


class Dog extends Animal {
// Constructor of the child class
public Dog(String name) {
// Call the constructor of the parent class
super(name);
}

// Method of the child class


public void bark() {
System.out.println(name + " is barking.");
Aishwarya s
Izee business school
}
}

public class SingleInheritance {


public static void main(String[] args) {
// Create an object of the Dog class (child class)
Dog dog = new Dog("Buddy");

// Call methods from both parent and child classes


dog.eat(); // Inherited from Animal class
dog.bark(); // Defined in Dog class
}
}

Program 9 - Java program to implement Multiple Inheritance using Interface.

// Interface 1
interface Animal {
void eat(); // Method to eat
}

// Interface 2
interface Bird {
void fly(); // Method to fly
}
Aishwarya s
Izee business school
// Class that implements both Animal and Bird interfaces
class Eagle implements Animal, Bird {

// Implementing the eat method from Animal interface


public void eat() {
System.out.println("Eagle is eating.");
}

// Implementing the fly method from Bird interface


public void fly() {
System.out.println("Eagle is flying.");
}
}

public class MultipleInheritanceUsingInterface {


public static void main(String[] args) {
// Create an object of the Eagle class
Eagle eagle = new Eagle();

// Calling methods from both interfaces


eagle.eat(); // From Animal interface
eagle.fly(); // From Bird interface
}
}

Aishwarya s
Izee business school
Program 10 - Java program to demonstrate a division by zero exception. (p-
11)

public class DivisionByZero {


public static void main(String[] args) {
int numerator = 10;
int denominator = 0;

try {
// Attempting division by zero
int result = numerator / denominator;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
// Catch the exception and display a message
System.out.println("Error: Division by zero is not allowed.");
}

System.out.println("Program continues after the exception handling.");


}
}

Aishwarya s
Izee business school
Program 11 - Java program to catch negative array size Exception. This
exception is caused when the array is initialized to negative values.(p-14)

public class NegativeArraySizeExceptionExample {


public static void main(String[] args) {
try {
// Trying to create an array with a negative size
int size = -5;
int[] arr = new int[size]; // This will throw
NegativeArraySizeException
System.out.println("Array created with size: " + size);
} catch (NegativeArraySizeException e) {
// Catching the NegativeArraySizeException
System.out.println("Error: Cannot create an array with a negative
size.");
}

System.out.println("Program continues after exception handling.");


}
}

Aishwarya s
Izee business school
Program 12 - Java program to check whether a number is palindrome or not

import java.util.Scanner;

public class PalindromeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number: ");
int a = sc.nextInt();
int original_value = a;
int rev = 0;
int digit;

while (a != 0) {
digit = a % 10;
rev = rev * 10 + digit;
a = a / 10;
}

if (rev == original_value) {
System.out.println("The given number is a Palindrome.");
} else {
System.out.println("The given number is not a Palindrome.");
}

sc.close();
}
}

Aishwarya s
Izee business school

You might also like