Java
Java
Faculty Name:
Arti Panwar Student Name: Piyush Saini
Roll No.: 21BC8003
Course: BCA
Semester: V
Batch: 2023-2024
INDEX
S. Name of the Exercise Date Signatu Remark
N. re & s
Date
1 Simple java standalone programs, arrays 12-7-23
OUTPUT
1. Method Overloading:
Code:
public class Calculator {
public int add (int a, int b) {
return a + b;
}
OUTPUT:
2. Constructor Overloading:
public class Student {
private String name;
private int age;
public Student () {
name = "Unknown";
age = 0;
}
student1.displayInfo();
student2.displayInfo();
student3.displayInfo();
}
}
OUTPUT:
3. Recursion:
public class Factorial {
public static int calculate Factorial (int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * calculate Factorial (n - 1);
}
}
OUTPUT:
Program Code:
// Superclass
class Animal {
String name;
void eat () {
System.out.println(name + " is eating.");
}
}
// Subclass 1
class Dog extends Animal {
Dog (String name) {
super(name);
}
void bark () {
System.out.println(name + " is barking.");
}
}
// Subclass 2
class Cat extends Animal {
Cat (String name) {
super(name);
}
void meow () {
System.out.println(name + " is meowing.");
}
}
dog.eat ();
dog.bark();
cat.eat();
cat.meow();
}
}
OUTPUT:
Parent(int number) {
this.number = number;
}
void display() {
System.out.println("Number from Parent: " + number);
}
}
@Override
void display() {
super.display(); // Calls the display method of the superclass
System.out.println("Number from Child: " + number);
}
}
class Parent {
final void finalMethod() {
// Some code here
}
}
MyClass(String data) {
this.data = data;
}
@Override
public String toString() {
return "MyClass[data=" + data + "]";
}
}
System.out.println(obj1.toString());
System.out.println(obj2.toString());
OUTPUT:
OUPUT
8Exception handling- Dealing with errors give output for the same
try {
// Scenario 2: Array Index Out of Bounds Exception
int[] numbers = {1, 2, 3};
int value = numbers[4];
System.out.println("Value: " + value); // This line will not be
reached
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("ArrayIndexOutOfBoundsException: " +
e.getMessage());
}
Output:
p
try {
// Scenario 2: Attempt to access a null reference
String str = null;
int length = getStringLength(str);
System.out.println("String Length: " + length); // This line will
not be reached
} catch (NullPointerException e) {
System.err.println("NullPointerException: " + e.getMessage());
}
try {
// Scenario 3: Custom exception handling
int number = -5;
if (number < 0) {
throw new IllegalArgumentException("Number cannot be
negative.");
}
} catch (IllegalArgumentException e) {
System.err.println("IllegalArgumentException: " +
e.getMessage());
}
}
Output:
Program Code:
class MyThread extends Thread {
private int count;
// Starting threads
thread1.start();
thread2.start();
// Interrupting a thread
thread1.interrupt();
try {
// Waiting for threads to finish
thread1.join();
thread2.join();
} catch (InterruptedException e) {
System.err.println("Main thread interrupted");
}