Experiment No.
1
class Student {
int rollNo;
String name;
void display() {
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.rollNo = 1;
s1.name = "Mitesh";
Student s2 = new Student();
s2.rollNo = 2;
s2.name = "Rahul";
System.out.println("Student 1 Details:");
s1.display();
System.out.println("\nStudent 2 Details:");
s2.display();
}
Output :-
Student 1 Details:
Roll No: 1
Name: Mitesh
Student 2 Details:
Roll No: 2
Name: Rahul
Experiment No. 2
Program 1: Method Overloading
class MathOperation {
int add(int a, int b) {
return a + b;
int add(int a, int b, int c) {
return a + b + c;
double add(double a, double b) {
return a + b;
public class Main {
public static void main(String[] args) {
MathOperation obj = new MathOperation();
System.out.println("Sum of 2 integers: " + obj.add(10, 20));
System.out.println("Sum of 3 integers: " + obj.add(10, 20, 30));
System.out.println("Sum of 2 doubles: " + obj.add(5.5, 6.5));
Sum of 2 integers: 30
Sum of 3 integers: 60
Sum of 2 doubles: 12.0
Program 2: Constructor Overloading
class Student {
int rollNo;
String name;
Student() {
rollNo = 0;
name = "Unknown";
Student(int r, String n) {
rollNo = r;
name = n;
void display() {
System.out.println("Roll No: " + rollNo + ", Name: " + name);
public class Main2 {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student(1, "Mitesh");
Student s3 = new Student(2, "Rahul");
s1.display();
s2.display();
s3.display();
}
}
Roll No: 0, Name: Unknown
Roll No: 1, Name: Mitesh
Roll No: 2, Name: Rahul
Experiment No. 3
Single Inheritance
class Parent {
void showParent() {
System.out.println("This is Parent class");
}
}
class Child extends Parent {
void showChild() {
System.out.println("This is Child class");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Child c = new Child();
c.showParent();
c.showChild();
}
}
Output:
This is Parent class
This is Child class
2. Multilevel Inheritance
class GrandParent {
void showGrandParent() {
System.out.println("This is GrandParent class");
}
}
class Parent extends GrandParent {
void showParent() {
System.out.println("This is Parent class");
}
}
class Child extends Parent {
void showChild() {
System.out.println("This is Child class");
}
}
public class MultilevelInheritance {
public static void main(String[] args) {
Child c = new Child();
c.showGrandParent();
c.showParent();
c.showChild();
}
}
Output:
This is GrandParent class
This is Parent class
This is Child class
Hierarchical Inheritance
class Parent {
void showParent() {
System.out.println("This is Parent class");
}
}
class Child1 extends Parent {
void showChild1() {
System.out.println("This is Child1 class");
}
}
class Child2 extends Parent {
void showChild2() {
System.out.println("This is Child2 class");
}
}
public class HierarchicalInheritance {
public static void main(String[] args) {
Child1 c1 = new Child1();
Child2 c2 = new Child2();
c1.showParent();
c1.showChild1();
c2.showParent();
c2.showChild2();
}
}
Output:
This is Parent class
This is Child1 class
This is Parent class
This is Child2 class
Hybrid Inheritance (via Interfaces)
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method from Interface A");
}
public void methodB() {
System.out.println("Method from Interface B");
}
}
public class HybridInheritance {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}
Output:
Method from Interface A
Method from Interface B
Program: Exception Handling in Java
public class ExceptionHandlingDemo {
public static void main(String[] args) {
try {
int a = 10 / 0;
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception: Division by zero not
allowed.");
}
try {
int arr[] = new int[5];
arr[6] = 50;
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Exception: Index out of range.");
}
try {
int num = Integer.parseInt("abc");
}
catch (NumberFormatException e) {
System.out.println("Number Format Exception: Invalid
conversion.");
}
finally {
System.out.println("Finally block always executes.");
}
System.out.println("Program continues normally...");
}
}
Output:
Arithmetic Exception: Division by zero not allowed.
Array Exception: Index out of range.
Number Format Exception: Invalid conversion.
Finally block always executes.
Program continues normally...
Experiment No. 7
<!DOCTYPE html>
<html>
<head>
<title>Email Validation</title>
<script>
function validateEmail() {
let email = document.getElementById("email").value;
if (email.indexOf("@") === -1 || email.indexOf(".") === -1) {
alert("Invalid Email: Missing '@' or '.' character.\nPlease re-enter.");
document.getElementById("email").focus();
return false;
} else {
alert("Valid Email Entered: " + email);
return true;
</script>
</head>
<body>
<h2>Email Validation Example</h2>
<form onsubmit="return validateEmail()">
Enter Email: <input type="text" id="email" />
<input type="submit" value="Check" />
</form>
</body>
</html>
Experiment No. 1
Change Background Color Automatically
<!DOCTYPE html>
<html>
<head>
<title>Auto Background Color Change</title>
<script>
let colors = ["lightblue", "lightgreen", "lightpink",
"lightyellow", "lavender", "orange"];
let index = 0;
function changeColor() {
document.body.style.backgroundColor = colors[index];
index = (index + 1) % colors.length;
}
window.onload = function() {
setInterval(changeColor, 5000);
}
</script>
</head>
<body>
<h2>Background color will change every 5 seconds</h2>
</body>
</html>
Output
Change Background Color