0% found this document useful (0 votes)
3 views20 pages

java ass 5

The document contains multiple Java code examples demonstrating various programming concepts such as interfaces, inheritance, abstract classes, and data structures like stacks and queues. It includes implementations for bank accounts, resizable shapes, and employee management, showcasing methods for depositing, withdrawing, resizing, and displaying information. Each section concludes with a main class that tests the functionality of the implemented classes.

Uploaded by

street28gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

java ass 5

The document contains multiple Java code examples demonstrating various programming concepts such as interfaces, inheritance, abstract classes, and data structures like stacks and queues. It includes implementations for bank accounts, resizable shapes, and employee management, showcasing methods for depositing, withdrawing, resizing, and displaying information. Each section concludes with a main class that tests the functionality of the implemented classes.

Uploaded by

street28gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

NAME- RIYA DESHMANKAR

ROLL NO.-2300290100212
SECTION-C

1.)
interface BankAccount {
void deposit(double amount);
void withdraw(double amount);
}

class SavingsAccount implements BankAccount {


private double balance;
private final double interestRate = 0.05;

public void deposit(double amount) {


balance += amount + (amount * interestRate);
System.out.println("Deposited with interest. New Balance: " +
balance);
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrew: " + amount + ". Remaining
Balance: " + balance);
} else {
System.out.println("Insufficient balance.");
}
}
}

class CurrentAccount implements BankAccount {


private double balance;

public void deposit(double amount) {


balance += amount;
System.out.println("Deposited. New Balance: " + balance);
}

public void withdraw(double amount) {


if (amount <= balance) {
balance -= amount;
System.out.println("Withdrew: " + amount + ". Remaining
Balance: " + balance);
} else {
System.out.println("Insufficient balance.");
}
}
}

public class BankTest {


public static void main(String[] args) {
System.out.println("Name: Riya Deshmankar\nRoll No:
2300290100212");

BankAccount sa = new SavingsAccount();


sa.deposit(1000);
sa.withdraw(200);

BankAccount ca = new CurrentAccount();


ca.deposit(1000);
ca.withdraw(200);
}
}Output

2.)
// Define the Resizable interface
interface Resizable {
void resize(double factor);
}

// Implementing the Resizable interface in Circle class


class Circle implements Resizable {
private double radius;

public Circle(double radius) {


this.radius = radius;
}

@Override
public void resize(double factor) {
radius *= factor;
}

public void display() {


System.out.println("Circle with radius: " + radius);
}
}

// Implementing the Resizable interface in Rectangle class


class Rectangle implements Resizable {
private double width, height;

public Rectangle(double width, double height) {


this.width = width;
this.height = height;
}
@Override
public void resize(double factor) {
width *= factor;
height *= factor;
}

public void display() {


System.out.println("Rectangle with width: " + width + ", height: " +
height);
}
}

// Main class to demonstrate resizing


public class ResizableShapesDemo
{
public static void main(String[] args) {
// Circle resizing
Circle circle = new Circle(5);
System.out.println("Original Circle:");
circle.display();
circle.resize(2);
System.out.println("Resized Circle:");
circle.display();

// Rectangle resizing
Rectangle rectangle = new Rectangle(4, 3);
System.out.println("\nOriginal Rectangle:");
rectangle.display();
rectangle.resize(1.5);
System.out.println("Resized Rectangle:");
rectangle.display();
}
}
Output
3.)
// Defining the Worker interface
interface Worker {
void work();
}

// Defining the Eater interface


interface Eater {
void eat();
}
// Defining the Sleeper interface
interface Sleeper {
void sleep();
}

// Implementing all three interfaces in the Human class


class Human implements Worker, Eater, Sleeper {
@Override
public void work() {
System.out.println("Human is working.");
}

@Override
public void eat() {
System.out.println("Human is eating.");
}
@Override
public void sleep() {
System.out.println("Human is sleeping.");
}
}
// Main class to demonstrate multiple inheritance through interfaces
public class WorkerEaterSleeperDemo {
public static void main(String[] args) {
Human human = new Human();

// Demonstrating multiple inheritance


System.out.println("Human performing actions:");
human.work();
human.eat();
human.sleep();
}
}
Output
4.)
import java.util.*;

class Stack {
private LinkedList<Integer> stack = new LinkedList<>();

public void push(int data) {


stack.addFirst(data);
}

public int pop() {


return stack.removeFirst();
}

public int peek() {


return stack.getFirst();
}

public boolean isEmpty() {


return stack.isEmpty();
}
}

public class StackDemo {


public static void main(String[] args) {
System.out.println("Name: Riya Deshmankar\nRoll No:
2300290100212");

Stack s = new Stack();


s.push(10);
s.push(20);
System.out.println("Top: " + s.peek());
s.pop();
System.out.println("Top after pop: " + s.peek());
}
}
Output
7.)
import java.util.*;

class Queue {
private LinkedList<Integer> queue = new LinkedList<>();

public void enqueue(int data) {


queue.addLast(data);
}

public int dequeue() {


return queue.removeFirst();
}

public int peek() {


return queue.getFirst();
}

public boolean isEmpty() {


return queue.isEmpty();
}
}

public class QueueDemo {


public static void main(String[] args) {
System.out.println("Name: Riya Deshmankar\nRoll No:
2300290100212");

Queue q = new Queue();


q.enqueue(1);
q.enqueue(2);
System.out.println("Front: " + q.peek());
q.dequeue();
System.out.println("Front after dequeue: " + q.peek());
}
}
Output
6.)
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}

class Triangle extends Shape {


private double side1, side2, side3;

public Triangle(double side1, double side2, double side3) {


this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

@Override
double calculateArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
double calculatePerimeter() {
return side1 + side2 + side3;
}

public String triangleType() {


if (side1 == side2 && side2 == side3) {
return "Equilateral";
} else if (side1 == side2 || side2 == side3 || side1 == side3) {
return "Isosceles";
} else {
return "Scalene";
}
}
}

public class ShapeDemo {


public static void main(String[] args) {
Triangle triangle = new Triangle(5, 5, 8);
System.out.println("Area of triangle: " + triangle.calculateArea());
System.out.println("Perimeter of triangle: " +
triangle.calculatePerimeter());
System.out.println("Triangle type: " + triangle.triangleType());
}
}
Output

7.)
abstract class Employee {
String name;
int age;
double salary;

public Employee(String name, int age, double salary) {


this.name = name;
this.age = age;
this.salary = salary;
}
void work() {
System.out.println(name + " is working.");
}

void rest() {
System.out.println(name + " is resting.");
}

abstract void displayDetails();


}

class Manager extends Employee {


String department;

public Manager(String name, int age, double salary, String


department) {
super(name, age, salary);
this.department = department;
}
@Override
void displayDetails() {
System.out.println("Manager: " + name + ", Age: " + age + ", Salary:
" + salary + ", Department: " + department);
}
}

class Worker extends Employee {


int hoursWorked;

public Worker(String name, int age, double salary, int hoursWorked) {


super(name, age, salary);
this.hoursWorked = hoursWorked;
}

@Override
void displayDetails() {
System.out.println("Worker: " + name + ", Age: " + age + ", Salary: "
+ salary + ", Hours Worked: " + hoursWorked);
}
}
public class EmployeeDemo {
public static void main(String[] args) {
Manager manager = new Manager("Alice", 40, 80000, "HR");
Worker worker = new Worker("Bob", 25, 30000, 40);

manager.displayDetails();
manager.work();
manager.rest();

worker.displayDetails();
worker.work();
worker.rest();
}
}
Output

You might also like