OBJECT OREIENTED
PROGRAM
LAB SHEET-6
1.PROBELM STATEMENT
1.Create an interface Drawable with an abstract method
draw().
2.Create classes Circle and Rectangle that implement
the Drawable interface and provide their own
implementations of the draw() method.
3.Test the classes in the main method.
CODE:
interface Drawable {
void draw();
}
class Circle implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a Circle");
}
}
class Rectangle implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a Rectangle");
}
}
public class Main {
public static void main(String[] args) {
Drawable circle = new Circle();
Drawable rectangle = new Rectangle();
circle.draw();
rectangle.draw();
}
}
OUTPUT:
Drawing a Circle
Drawing a Rectangle
2.PROBELM STATEMENT
1. Create an interface Vehicle with:
2. An abstract method start().
3. A default method stop().
4. A static method honk().
5. Create a class Car that implements the Vehicle
interface and provides an implementation for the start()
method.
6. Test the start(), stop(), and honk() methods in the
main method
CODE:
interface Vehicle {
void start();
default void stop() {
System.out.println("The vehicle has stopped.");
}
static void honk() {
System.out.println("The vehicle is honking: Beep!
Beep!");
}
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("The car is starting.");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.start();
myCar.stop();
Vehicle.honk();
}
}
OUTPUT:
The car is starting.
The vehicle has stopped.
The vehicle is honking: Beep! Beep!
3.PROBELM STATEMENT
1. Create a package com.example.math with a class
Calculator that has methods to add, subtract, multiply,
and divide two numbers.
2. Create another package com.example.main with a
class Main that uses the Calculator class to perform
calculations.
3. Test the Calculator class in the main method
CODE:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public double divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Division by zero
is not allowed");
}
return (double) a / b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int a = 10, b = 5;
System.out.println("Addition: " + calculator.add(a,
b));
System.out.println("Subtraction: " +
calculator.subtract(a, b));
System.out.println("Multiplication: " +
calculator.multiply(a, b));
try {
System.out.println("Division: " +
calculator.divide(a, b));
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
}
}
OUTPUT:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
4.PROBELM STATEMENT
1. Create two interfaces Flyable and Swimmable with
methods fly() and swim(), respectively.
2. Create a class Duck that implements both interfaces
and provides implementations for the fly() and swim()
methods.
3. Test the Duck class in the main method
Code:
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
class Duck implements Flyable, Swimmable {
@Override
public void fly() {
System.out.println("The duck is flying gracefully in
the sky.");
}
@Override
public void swim() {
System.out.println("The duck is swimming
smoothly in the water.");
}
}
public class Mains {
public static void main(String[] args) {
Duck duck = new Duck();
duck.fly();
duck.swim();
}
}
OUTPUT:
The duck is flying gracefully in the sky.
The duck is swimming smoothly in the water.