1. OOPs concepts in Java – 1 a.
Write a program to create a class and implement a default,
overloaded and copy Constructor
public class Person {
private String name;
private int age;
// Default Constructor
public Person() {
[Link] = "Unknown";
[Link] = 0;
}
// Overloaded Constructor
public Person(String name, int age) {
[Link] = name;
[Link] = age;
}
// Copy Constructor
public Person(Person otherPerson) {
[Link] = [Link];
[Link] = [Link];
}
// Getter methods
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void displayInfo() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
public static void main(String[] args) {
// Using Default Constructor
Person person1 = new Person();
[Link]("Default Constructor:");
[Link]();
[Link]();
// Using Overloaded Constructor
Person person2 = new Person("John Doe", 25);
[Link]("Overloaded Constructor:");
[Link]();
[Link]();
// Using Copy Constructor
Person person3 = new Person(person2);
[Link]("Copy Constructor:");
[Link]();
}
}
#output : Default Constructor: Overloaded Constructor: Copy Constructor:
Name: Unknown Name: John Doe Name: John Doe
Age: 0 Age: 25 Age: 25
b. Write a program to create a class and implement the concepts of Method Overloading
#Output:
public class MethodOverloadingExample {
// Method with two integer parameters
public int add(int a, int b) {
return a + b;
}
// Method with three integer parameters
public int add(int a, int b, int c) {
return a + b + c;
}
// Method with two double parameters
public double add(double a, double b) {
return a + b;
}
// Method with a String parameter
public String concatenate(String str1, String str2) {
return str1 + str2;
}
public static void main(String[] args) {
MethodOverloadingExample example = new MethodOverloadingExample();
// Call methods with different parameter combinations
int sum1 = [Link](5, 10);
int sum2 = [Link](5, 10, 15);
double sum3 = [Link](3.5, 7.5);
String result = [Link]("Hello, ", "World!");
// Display the results
[Link]("Sum 1: " + sum1);
[Link]("Sum 2: " + sum2);
[Link]("Sum 3: " + sum3);
[Link]("Concatenated String: " + result);
}}
#Output:
Sum 1: 15
Sum 2: 30
Sum 3: 11.0
Concatenated String: Hello, World!
[Link] a program to create a class and implement the concepts of Static methods
#Program:
public class MathOperations {
// Static method to calculate the sum of two numbers
public static int add(int a, int b) {
return a + b;
}
// Static method to calculate the difference of two numbers
public static int subtract(int a, int b) {
return a - b;
}
// Static method to calculate the product of two numbers
public static int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
// Calling static methods without creating an instance
int sumResult = [Link](5, 3);
int differenceResult = [Link](8, 4);
int productResult = [Link](2, 6);
// Displaying results
[Link]("Sum: " + sumResult);
[Link]("Difference: " + differenceResult);
[Link]("Product: " + productResult);
} }
#Output :
Sum: 8
Difference: 4
Product: 12
Prac 2.a Write a program to implement the concepts of Inheritance and Method overriding
#Program:
// Base class
class Animal {
String name;
// Constructor
public Animal(String name) {
[Link] = name;
}
// Method to display information about the animal
public void displayInfo() {
[Link]("I am an animal. My name is " + name);
}
}
// Derived class (subclass) inheriting from Animal
class Dog extends Animal {
String breed;
// Constructor
public Dog(String name, String breed) {
// Call the constructor of the base class (Animal)
super(name);
[Link] = breed;
}
// Override the displayInfo method from the base class
@Override
public void displayInfo() {
[Link]("I am a dog. My name is " + name + " and my breed is " + breed);
}
// New method specific to the Dog class
public void bark() {
[Link]("Woof! Woof!");
} }
public class InheritanceExample {
public static void main(String[] args) {
// Create an instance of the base class (Animal)
Animal animal = new Animal("Generic Animal");
[Link]();
[Link]();
// Create an instance of the derived class (Dog)
Dog dog = new Dog("Buddy", "Golden Retriever");
[Link]();
[Link]();
} }
#Program:
I am an animal. My name is Generic Animal
I am a dog. My name is Buddy and my breed is Golden Retriever
Woof! Woof!
2.b Write a program to implement the concepts of Abstract classes and methods
#Program :
abstract class Animal {
// Abstract method (no implementation body)
abstract void makeSound();
// Regular method (with implementation body)
void eat() {
[Link]("Animal is eating.");
} }
// Concrete subclass (extends Animal and implements makeSound())
class Dog extends Animal {
@Override
void makeSound() {
[Link]("Woof!");
} }
// Concrete subclass (extends Animal and implements makeSound())
class Cat extends Animal {
@Override
void makeSound() {
[Link]("Meow!");
} }
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
[Link](); // Output: Woof!
[Link](); // Output: Animal is eating.
[Link](); // Output: Meow!
[Link](); // Output: Animal is eating.
} }
#Program :
Woof!
Animal is eating.
Meow!
Animal is eating.
2.c Write a program to implement the concept of interfaces.
#Program :
interface Shape {
// Interface methods (no implementation)
double getArea();
void draw();
}
class Circle implements Shape {
private double radius;
public Circle(double radius) {
[Link] = radius;
}
@Override
public double getArea() {
return [Link] * radius * radius;
}
@Override
public void draw() {
[Link]("Drawing a circle with radius " + radius);
} }
class Rectangle implements Shape {
private double width, height;
public Rectangle(double width, double height) {
[Link] = width;
[Link] = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public void draw() {
[Link]("Drawing a rectangle with width " + width + " and height " + height);
} }
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
printShapeInfo(circle);
printShapeInfo(rectangle);
}
static void printShapeInfo(Shape shape) {
[Link]("Shape area: " + [Link]());
[Link]();
} }
#Output :
Shape area : 78.53981633974483
Drawing a circle with radius 5.0
Shape area : 24.0
Drawing a ectangle with width 4.0 and height 6.0
Prac 3.a Write a program to raise built-in exceptions and raise them as per the requirements
public class CustomExceptionExample {
// Method that throws a built-in exception based on a condition
public static void validateAge(int age) throws IllegalArgumentException {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (age < 18) {
throw new IllegalArgumentException("Age must be 18 or older");
}
// Perform other operations if the age is valid
[Link]("Age is valid: " + age);
}
// Method that throws a built-in exception based on a condition
public static void validateDivide(int numerator, int denominator) throws ArithmeticException
{
if (denominator == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
int result = numerator / denominator;
[Link]("Result of division: " + result);
}
public static void main(String[] args) {
try {
// Example 1: Validating age
validateAge(25);
// Example 2: Performing division
validateDivide(10, 2);
// Example 3: Attempting to divide by zero (should raise an exception)
validateDivide(5, 0); // This line will not be executed due to the exception
} catch (IllegalArgumentException e) {
[Link]("IllegalArgumentException caught: " + [Link]());
} catch (ArithmeticException e) {
[Link]("ArithmeticException caught: " + [Link]());
} } }
#Output :
Age is valid: 25
Result of division: 5
ArithmeticException caught: Cannot divide by zero
3.b Write a program to define user defined exceptions and raise them as per the
requirements
#Program :
// Custom exception class
class CustomValidationException extends Exception {
// Constructor that takes a custom message
public CustomValidationException(String message) {
super(message);
} }
public class CustomExceptionExample {
// Method that throws a custom exception based on a condition
public static void validateAge(int age) throws CustomValidationException {
if (age < 0) {
throw new CustomValidationException("Age cannot be negative");
}
if (age < 18) {
throw new CustomValidationException("Age must be 18 or older");
}
// Perform other operations if the age is valid
[Link]("Age is valid: " + age);
}
public static void main(String[] args) {
try {
// Example 1: Validating age
validateAge(25);
// Example 2: Attempting to validate with negative age (should raise a custom exception)
validateAge(-5); // This line will not be executed due to the exception
} catch (CustomValidationException e) {
[Link]("CustomValidationException caught: " + [Link]());
} } }
#Output :
Age is valid: 25
CustomValidationException caught: Age cannot be negative.