1.
Write a JAVA program to Implement string operations using
Stringclass, StringBuffer class & toString method.
Program:
public class StringOperations {
public static void main(String[] args) {
// Using String class
String str1 = "Hello";
String str2 = "World";
// Concatenation
String str3 = str1 + " " + str2;
[Link]("Concatenated String: " + str3);
// Length
[Link]("Length of str1: " + [Link]());
// Substring
[Link]("Substring of str1: " + [Link](1, 4));
// Character at a position
[Link]("Character at position 2 in str2: " + [Link](2));
// Uppercase
[Link]("Uppercase str1: " + [Link]());
// Replace
[Link]("Replace 'l' with 'p' in str1: " + [Link]('l', 'p'));
// Using StringBuffer class
StringBuffer sb = new StringBuffer("Hello");
// Append
[Link](" World");
[Link]("Appended StringBuffer: " + [Link]());
// Insert
[Link](5, ",");
[Link]("Inserted StringBuffer: " + [Link]());
// Reverse
[Link]();
[Link]("Reversed StringBuffer: " + [Link]());
// Delete
[Link](0, 6);
[Link]("Deleted StringBuffer: " + [Link]());
// Replace
[Link](0, [Link](), "Goodbye World");
[Link]("Replaced StringBuffer: " + [Link]());
// Using toString method
String strFromBuffer = [Link]();
[Link]("String from StringBuffer using toString: " + strFromBuffer);
}
}
Output:
Concatenated String: Hello World
Length of str1: 5
Substring of str1: ell
Character at position 2 in str2: r
Uppercase str1: HELLO
Replace 'l' with 'p' in str1: Heppo
Appended StringBuffer: Hello World
Inserted StringBuffer: Hello, World
Reversed StringBuffer: dlroW ,olleH
Deleted StringBuffer: ,olleH
Replaced StringBuffer: Goodbye World
String from StringBuffer using toString: Goodbye World
2. Write a JAVA program to Implement of constructor, constructor
overloading,
Program:
public class ConstructorExample {
// Instance variables
private String name;
private int age;
// Default constructor
public ConstructorExample() {
[Link] = "Unknown";
[Link] = 0;
[Link]("Default constructor called");
}
// Parameterized constructor with one parameter
public ConstructorExample(String name) {
[Link] = name;
[Link] = 0;
[Link]("Constructor with name called");
}
// Parameterized constructor with two parameters
public ConstructorExample(String name, int age) {
[Link] = name;
[Link] = age;
[Link]("Constructor with name and age called");
}
// Method to display the details
public void display() {
[Link]("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
// Using default constructor
ConstructorExample person1 = new ConstructorExample();
[Link]();
// Using constructor with one parameter
ConstructorExample person2 = new ConstructorExample("Alice");
[Link]();
// Using constructor with two parameters
ConstructorExample person3 = new ConstructorExample("Bob", 25);
[Link]();
}
}
Output:
Default constructor called
Name: Unknown, Age: 0
Constructor with name called
Name: Alice, Age: 0
Constructor with name and age called
Name: Bob, Age: 25
3. Write a JAVA program to Implement Method overloading, Method
overriding.
Program:
// Base class
class Animal {
// Method to be overridden
public void sound() {
[Link]("Animal makes a sound");
}
}
// Derived class
class Dog extends Animal {
// Overriding the sound method
@Override
public void sound() {
[Link]("Dog barks");
}
}
// Class demonstrating method overloading
class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Overloaded method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Overloaded method to add two double values
public double add(double a, double b) {
return a + b;
}
}
public class MethodOverloadingOverriding {
public static void main(String[] args) {
// Demonstrating method overloading
Calculator calculator = new Calculator();
[Link]("Sum of 2 and 3: " + [Link](2, 3));
[Link]("Sum of 2, 3 and 4: " + [Link](2, 3, 4));
[Link]("Sum of 2.5 and 3.5: " + [Link](2.5, 3.5));
// Demonstrating method overriding
Animal animal = new Animal();
[Link](); // Calls the method from Animal class
Dog dog = new Dog();
[Link](); // Calls the overridden method from Dog class
// Demonstrating polymorphism with method overriding
Animal polymorphicDog = new Dog();
[Link](); // Calls the overridden method from Dog class
}
}
Output:
Sum of 2 and 3: 5
Sum of 2, 3 and 4: 9
Sum of 2.5 and 3.5: 6.0
Animal makes a sound
Dog barks
Dog barks
4. Write a JAVA program to Implement Static Keyword, Final keyword, Super
keyword, this keyword.
Program:
// Parent class demonstrating final and super keywords
class Parent {
// final variable
final int finalVar = 100;
// final method
final void displayFinal() {
[Link]("This is a final method in Parent class.");
}
// method to be overridden
void show() {
[Link]("Parent class show() method.");
}
}
// Child class inheriting Parent class
class Child extends Parent {
// instance variable
int var;
// Constructor using 'this' keyword
Child(int var) {
[Link] = var;
}
// Overriding show method
@Override
void show() {
[Link](); // using super keyword to call parent class method
[Link]("Child class show() method.");
}
// method to display variable using this keyword
void display() {
[Link]("Value of var: " + [Link]);
}
}
// Class demonstrating static keyword
class StaticDemo {
// static variable
static int staticVar;
// static method
static void staticMethod() {
[Link]("This is a static method.");
}
// static block
static {
staticVar = 10;
[Link]("Static block executed. Value of staticVar: " + staticVar);
}
}
// Main class
public class KeywordDemo {
public static void main(String[] args) {
// Demonstrating static keyword
[Link]();
[Link]("Accessing static variable: " + [Link]);
// Demonstrating final keyword
Parent parent = new Parent();
[Link]();
// [Link] = 200; // This will cause a compilation error because finalVar is final
// Demonstrating super and this keywords
Child child = new Child(50);
[Link]();
[Link]();
}
}
Output:
Static block executed. Value of staticVar: 10
This is a static method.
Accessing static variable: 10
This is a final method in Parent class.
Parent class show() method.
Child class show() method.
Value of var: 50
5. Write a JAVA program to Implement Try-catch block - Throw and Throws.
Program:
public class ExceptionHandlingDemo {
public static void main(String[] args) {
// Using try-catch block
try {
int result = divide(10, 0); // This will cause an ArithmeticException
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Caught an ArithmeticException: " + [Link]());
}
// Using throw keyword
try {
validateAge(15); // This will throw an exception
} catch (IllegalArgumentException e) {
[Link]("Caught an IllegalArgumentException: " + [Link]());
}
// Using throws keyword
try {
checkNumber(-5); // This will throw an exception
} catch (Exception e) {
[Link]("Caught an Exception: " + [Link]());
}
}
// Method using throws keyword
public static void checkNumber(int number) throws Exception {
if (number < 0) {
throw new Exception("Number must be non-negative.");
}
[Link]("Number is valid: " + number);
}
// Method using throw keyword
public static void validateAge(int age) {
if (age < 18) {
throw new IllegalArgumentException("Age must be 18 or older.");
}
[Link]("Age is valid: " + age);
}
// Method using try-catch block
public static int divide(int a, int b) {
return a / b;
}
}
Output:
Caught an ArithmeticException: / by zero
Caught an IllegalArgumentException: Age must be 18 or older.
Caught an Exception: Number must be non-negative.
6. Write a JAVA program to Implement Final vs Finally vs Finalize.
Program:
class FinalDemo {
// final variable
final int finalVar = 10;
// final method
final void display() {
[Link]("This is a final method.");
}
}
// Class that cannot be inherited because it is declared final
final class FinalClass {
void show() {
[Link]("This is a final class.");
}
}
class FinalizeDemo {
// Override finalize method
@Override
protected void finalize() throws Throwable {
[Link]("Finalize method called.");
[Link]();
}
}
public class FinalFinallyFinalizeDemo {
public static void main(String[] args) {
// Demonstrating final
FinalDemo finalDemo = new FinalDemo();
[Link]("Final variable value: " + [Link]);
[Link]();
// Demonstrating finally
try {
int result = 10 / 0; // This will cause an ArithmeticException
} catch (ArithmeticException e) {
[Link]("Caught an ArithmeticException: " + [Link]());
} finally {
[Link]("Finally block executed.");
}
// Demonstrating finalize
FinalizeDemo finalizeDemo = new FinalizeDemo();
finalizeDemo = null; // Eligible for garbage collection
[Link](); // Requesting JVM to call garbage collector
[Link]("Main method completed.");
}
}
Output:
Final variable value: 10
This is a final method.
Caught an ArithmeticException: / by zero
Finally block executed.
Main method completed.
Finalize method called.