Practical Assignment 3
Practical Assignment 3
// Concatenation
String str3 = str1 + " " + str2;
System.out.println("Concatenated String: " + str3);
// Length
System.out.println("Length of str1: " + str1.length());
// Substring
System.out.println("Substring of str1: " + str1.substring(1, 4));
// Character at a position
System.out.println("Character at position 2 in str2: " + str2.charAt(2));
// Uppercase
System.out.println("Uppercase str1: " + str1.toUpperCase());
// Replace
System.out.println("Replace 'l' with 'p' in str1: " + str1.replace('l', 'p'));
// Append
sb.append(" World");
System.out.println("Appended StringBuffer: " + sb.toString());
// Insert
sb.insert(5, ",");
System.out.println("Inserted StringBuffer: " + sb.toString());
// Reverse
sb.reverse();
System.out.println("Reversed StringBuffer: " + sb.toString());
// Delete
sb.delete(0, 6);
System.out.println("Deleted StringBuffer: " + sb.toString());
// Replace
sb.replace(0, sb.length(), "Goodbye World");
System.out.println("Replaced StringBuffer: " + sb.toString());
// Instance variables
private String name;
private int age;
// Default constructor
public ConstructorExample() {
this.name = "Unknown";
this.age = 0;
System.out.println("Default constructor called");
}
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() {
System.out.println("Animal makes a sound");
}
}
// Derived class
class Dog extends Animal {
// Overriding the sound method
@Override
public void sound() {
System.out.println("Dog barks");
}
}
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:
// final method
final void displayFinal() {
System.out.println("This is a final method in Parent class.");
}
// method to be overridden
void show() {
System.out.println("Parent class show() method.");
}
}
// static method
static void staticMethod() {
System.out.println("This is a static method.");
}
// static block
static {
staticVar = 10;
System.out.println("Static block executed. Value of staticVar: " + staticVar);
}
}
// Main class
public class KeywordDemo {
public static void main(String[] args) {
// Demonstrating static keyword
StaticDemo.staticMethod();
System.out.println("Accessing static variable: " + StaticDemo.staticVar);
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:
// final method
final void display() {
System.out.println("This is a final method.");
}
}
class FinalizeDemo {
// Override finalize method
@Override
protected void finalize() throws Throwable {
System.out.println("Finalize method called.");
super.finalize();
}
}
// Demonstrating finally
try {
int result = 10 / 0; // This will cause an ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Caught an ArithmeticException: " + e.getMessage());
} finally {
System.out.println("Finally block executed.");
}
// Demonstrating finalize
FinalizeDemo finalizeDemo = new FinalizeDemo();
finalizeDemo = null; // Eligible for garbage collection
System.gc(); // Requesting JVM to call garbage collector
Output:
Final variable value: 10
This is a final method.
Caught an ArithmeticException: / by zero
Finally block executed.
Main method completed.
Finalize method called.