JavaPrac 1-3
JavaPrac 1-3
JavaPrac 1-3
// Constructor
public Animal(String name) {
this.name = name;
}
// Method to display information about the animal
public void displayInfo() {
System.out.println("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);
this.breed = breed;
}
// Override the displayInfo method from the base class
@Override
public void displayInfo() {
System.out.println("I am a dog. My name is " + name + " and my breed is " + breed);
}
// New method specific to the Dog class
public void bark() {
System.out.println("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");
animal.displayInfo();
System.out.println();
// Create an instance of the derived class (Dog)
Dog dog = new Dog("Buddy", "Golden Retriever");
dog.displayInfo();
dog.bark();
} }
#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() {
System.out.println("Animal is eating.");
} }
// Concrete subclass (extends Animal and implements makeSound())
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Woof!");
} }
// Concrete subclass (extends Animal and implements makeSound())
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Meow!");
} }
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
dog.makeSound(); // Output: Woof!
dog.eat(); // Output: Animal is eating.
cat.makeSound(); // Output: Meow!
cat.eat(); // 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) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
} }
class Rectangle implements Shape {
private double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public void draw() {
System.out.println("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) {
System.out.println("Shape area: " + shape.getArea());
shape.draw();
} }
#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
System.out.println("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;
System.out.println("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) {
System.out.println("IllegalArgumentException caught: " + e.getMessage());
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: " + e.getMessage());
} } }
#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
System.out.println("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) {
System.out.println("CustomValidationException caught: " + e.getMessage());
} } }
#Output :
Age is valid: 25
CustomValidationException caught: Age cannot be negative.