Java_Lab_Manual[1] (1)
Java_Lab_Manual[1] (1)
** 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.
11) Java program to catch negative array size Exception. This exception is
caused when the array is initialized to negative values.(p-14)
Aishwarya s
Izee business school
Program 1 - Java program to display “Hello World” and display the size of all
the data types.
Aishwarya s
Izee business school
Program 2 - Java program to implement the usage of static, local and global
variables.
// Static variable
static int staticVariable = 20;
// Local variable
int localVariable = 30;
Aishwarya s
Izee business school
// Modifying and demonstrating the static variable usage
VariableExample.staticVariable = 50;
// 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);
}
}
import java.util.Scanner;
Aishwarya s
Izee business school
Program 5 - Java program to check whether the number is odd or even.
import java.util.Scanner;
Aishwarya s
Izee business school
Program 6 - Java program to implement default and parameterized
constructors.
// 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);
}
Aishwarya s
Izee business school
Program 7 - Java program to implement an array of objects.
class Student {
// Instance variables
String name;
int age;
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;
// 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 {
Aishwarya s
Izee business school
Program 10 - Java program to demonstrate a division by zero exception. (p-
11)
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.");
}
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)
Aishwarya s
Izee business school
Program 12 - Java program to check whether a number is palindrome or not
import java.util.Scanner;
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