final assignment of oop 4
final assignment of oop 4
Technology (BCS.IT)
Third Semester
September Intake
Assignment No 4
MATRIC NO. :
:
STUDENTS NAME: Bikranshu Gupta
…………… :
Program code:
interface Shape
{
void draw();
}
class Circle implements Shape
{
@Override
public void draw() {
System.out.println("Drawing Circle");
}
}
class Rectangle implements Shape {
@Override
public void draw()
{
System.out.println("Drawing Rectangle");
}
}
class Shape
{
public static void main(String[] args)
{
Shape circle = new Circle();
Shape rectangle = new Rectangle();
circle.draw();
rectangle.draw();
}
}
Output:
Discussion: The practice to define an interface with an abstract method and implement the
interface in multiple classes was understood and performed successfully.
2. Create two interfaces, Shape and Colorable. Shape should have a method void draw(),
and Colorable should have a method void color(). Create a class ColoredCircle that
implements both interfaces. Implement the methods in the class to print appropriate
messages when calling draw() and color().
Objective:
The objective of this question is to understand multiple interface implementation.
Project Code:
interface Shape
{
void draw();
}
interface Colorable
{
void color();
}
class ColoredCircle implements Shape, Colorable
{
@Override
public void draw()
{
System.out.println("Drawing a Circle");
}
@Override
public void color() {
System.out.println("Coloring the Circle");
}
}
class Demo {
public static void main(String[] args)
{
ColoredCircle coloredCircle = new ColoredCircle();
coloredCircle.draw();
coloredCircle.color();
}
}
Output:
Discussion:
The pratice to understand multiple interface implementation is performed and understand
successfully.
3. Create an abstract class Animal with an abstract method sound(). Then create two
classes Dog and Cat that extend the Animal class. Each subclass should implement the
sound() method to print "Bark" for Dog and "Meow" for Cat.
Objective: The objective of this question is to use of abstract classes and methods to define
a common interface for related classes.
Program Code:
abstract class Animal
{
abstract void sound();
}
class Dog extends Animal
{
@Override
void sound()
{
System.out.println("Bark");
}
}
class Cat extends Animal
{
@Override
void sound()
{
System.out.println("Meow");
}
}
class Sounddemo
{
public static void main(String[] args)
{
Animal dog = new Dog();
dog.sound();
Animal cat = new Cat();
cat.sound();
}
}
Output:
Discussion:
The Pratice to use of abstract classes and methods to define a common interface for related
classes is performed and understand successfully.
Conclusion: By completing the project of abstraction, I learnt to use of abstract classes and
methods to define a common interface for related classes.
4. Create an abstract class Shape with an abstract method area(), and a concrete method
description() that prints "This is a shape". Then, create two classes Circle and
Rectangle
that extend Shape and implement the area() method to return the area of the respective
shapes.
Objective: The objectuve is to use of an abstract class Shape with an abstract method
area() and a concrete method description(). The classes Circle and Rectangle
extend Shape and implement the area() method to calculate and return the area of a circle
and a rectangle, respectively.
Program code:
abstract class Shape
{
public abstract double area();
public void description()
{
System.out.println("This is a shape.");
}
}
class Circle extends Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
@Override
public double area()
{
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape
{
private double length;
private double width;
public Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
@Override
public double area()
{
return length * width;
}
}
public class AREA
{
public static void main(String[] args) {
Shape circle = new Circle(5.0);
circle.description();
System.out.println("Area of the circle: " + circle.area());
Shape rectangle = new Rectangle(4.0, 6.0);
rectangle.description();
System.out.println("Area of the rectangle: " + rectangle.area());
}
}
Output:
Discussion:
The Pratice to use of an abstract class Shape with an abstract method area() and a
concrete method description(). The classes Circle and Rectangle extend Shape
and implement the area() method to calculate and return the area of a circle and a
rectangle, respectively is learned and performed successfully.
Conclusion: By completing the project. I learned to use an abstract class Shape with an
abstract method area() and a concrete method description(). The classes Circle
and Rectangle extend Shape and implement the area() method to calculate and return
the area of a circle and a rectangle, respectively.
Program code.
public BankAccount() {
this.balance = 0.0; // Initialize balance to 0
}
@Override
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.printf("Deposited $%.2f to Savings Account.%n", amount);
} else {
System.out.println("Deposit amount must be positive.");
}
}
@Override
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.printf("Withdrew $%.2f from Savings Account.%n", amount);
} else {
System.out.println("Insufficient funds or invalid amount.");
}
}
}
@Override
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.printf("Deposited $%.2f to Checking Account.%n", amount);
} else {
System.out.println("Deposit amount must be positive.");
}
}
@Override
public void withdraw(double amount) {
if (amount > 0 && amount <= (balance + overdraftLimit)) {
balance -= amount;
System.out.printf("Withdrew $%.2f from Checking Account.%n", amount);
} else {
System.out.println("Insufficient funds or invalid amount.");
}
}
}
Output: