Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

final assignment of oop 4

The document outlines a series of programming assignments related to Object Oriented Programming, focusing on interfaces, abstract classes, and their implementations in Java. Each assignment includes objectives, program code, discussions, and conclusions that emphasize the learning outcomes of abstraction and interface implementation. The assignments cover concepts such as defining interfaces, multiple interface implementation, and creating abstract classes with concrete methods.

Uploaded by

bikranshuj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

final assignment of oop 4

The document outlines a series of programming assignments related to Object Oriented Programming, focusing on interfaces, abstract classes, and their implementations in Java. Each assignment includes objectives, program code, discussions, and conclusions that emphasize the learning outcomes of abstraction and interface implementation. The assignments cover concepts such as defining interfaces, multiple interface implementation, and creating abstract classes with concrete methods.

Uploaded by

bikranshuj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Bachelor of Computer System and Information

Technology (BCS.IT)

Third Semester
September Intake
Assignment No 4

Object Oriented Programming


(CSC 2510)

MATRIC NO. :
:
STUDENTS NAME: Bikranshu Gupta
…………… :

LECTURER : Keshav Karn


1. Define an interface Shape with a method void draw(). Implement the Shape interface in a class
Circle and Rectangle that prints a message like “Drawing Circle “" and “Drawing Rectangle:,
respectively.
Objective: The objective of this question is to define an interface with an abstract method
and implement the interface in multiple classes.

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.

Conclusion: By completing the practical of abstraction, I learnt to define an interface with an


abstract method and implement the interface in multiple classes.

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.

Conclusion: By completing the practical of abstraction, I learnt to define multiple interface


implementation.

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.

5.Create an abstract class BankAccount with two abstract methods deposit(double


amount) and withdraw(double amount), and a concrete method displayBalance() to
display the balance. Then create two classes SavingsAccount and CheckingAccount that
extend BankAccount and implement the abstract methods. In the main method, test both
classes.

Program code.

// Abstract class BankAccount


abstract class BankAccount {
protected double balance;

public BankAccount() {
this.balance = 0.0; // Initialize balance to 0
}

// Abstract method for depositing money


public abstract void deposit(double amount);

// Abstract method for withdrawing money


public abstract void withdraw(double amount);

// Concrete method to display the balance


public void displayBalance() {
System.out.printf("Current Balance: $%.2f%n", balance);
}
}

// SavingsAccount class extending BankAccount


class SavingsAccount extends BankAccount {
private double interestRate;

public SavingsAccount(double interestRate) {


super(); // Call to the superclass constructor
this.interestRate = interestRate;
}

@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.");
}
}
}

// CheckingAccount class extending BankAccount


class CheckingAccount extends BankAccount {
private double overdraftLimit;

public CheckingAccount(double overdraftLimit) {


super(); // Call to the superclass constructor
this.overdraftLimit = overdraftLimit;
}

@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.");
}
}
}

// Main class to test the functionality


public class Saving {
public static void main(String[] args) {
// Create a Savings Account
SavingsAccount savings = new SavingsAccount(0.05);
savings.deposit(1000);
savings.withdraw(200);
savings.displayBalance();

// Create a Checking Account


CheckingAccount checking = new CheckingAccount(500);
checking.deposit(500);
checking.withdraw(600);
checking.displayBalance();

// Test overdraft in Checking Account


checking.withdraw(500);
checking.displayBalance();
}
}

Output:

You might also like