Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views9 pages

B1 078 Mane Java Assignment 5

Java assignment , content related to java language
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)
6 views9 pages

B1 078 Mane Java Assignment 5

Java assignment , content related to java language
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/ 9

Name Prathamesh Nathaji Mane

UID no. & Branch 2022200078 / EXTC-B

Experiment No. 5

AIM: Implementing the program for given problem statements using the concepts
of Abstract Classes & Interfaces

Program 1

PROBLEM ● Create a Shape class which is abstract with data color and abstract
STATEMENT: method area() and derive two classes Rectangle and Circle derived
from shape, override area() method and complete the classes.
● Create a class called Diagram which contains an array of shape
class. Each Diagram can be a composition of rectangle or circle
shapes. Diagram class has method called totalArea() which
computes the totalArea() of both shapes.
● Add setters, getters and constructors wherever required. Design a
Tester class with main() to create different diagram objects and print
the total areas and details of each diagram.

PROGRAM: import java.util.Scanner;

public class Expt_5 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("=======================================
===========");
System.out.println("Enter the number of shapes:");
int numberOfShapes = sc.nextInt();

Diagram diagram = new Diagram(numberOfShapes);

System.out.println("=======================================
===========");
System.out.println("Shapes in Diagram:");
diagram.displayShapes();
System.out.println("=======================================
===========");
System.out.println("Total Area of Diagram: " + diagram.totalArea());

System.out.println("=======================================
===========");
}
}

abstract class Shape {


String color;

Shape(String color) {this.color = color;}

abstract double area(); //creating the abstract class

public String getColor() {return color;}


public void setColor(String color) {this.color = color;}
}

class Rectangle extends Shape {


int length, breadth;

Rectangle(String color, int length, int breadth) {


super(color);
this.length = length;
this.breadth = breadth;
}

double area() {return length * breadth;}

//getter setter methods for rectangle


public int getLength() {return length;}
public void setLength(int length) {this.length = length;}

public int getBreadth() {return breadth;}


public void setBreadth(int breadth) {this.breadth = breadth;}

public String toString() {


return "Rectangle [color=" + color + ", length=" + length + ",
breadth=" + breadth + "]";
}
}

class Circle extends Shape {


int radius;

Circle(String color, int radius) {


super(color);
this.radius = radius;
}

double area() {return (22 / 7.0) * radius * radius;}


//getter setter methods for rectangle
public int getRadius() {return radius;}
public void setRadius(int radius) {this.radius = radius;}

public String toString() {


return "Circle [color=" + color + ", radius=" + radius + "]";
}
}

class Diagram {
Shape[] shapes; //creating an array of type Shape
Scanner sc = new Scanner(System.in); //scanner class

Diagram(int numberOfShapes) {
shapes = new Shape[numberOfShapes]; //setting the size of array as
number of shapes given by user

for (int i = 0; i < numberOfShapes; i++) {

System.out.println("=======================================
===========");
System.out.println("Enter the type of shape (Rectangle/Circle):");
String shapeType = sc.next();

if (shapeType.equalsIgnoreCase("Rectangle")) { //if the given shape


is rectangle then this loop is excuted
System.out.println("Enter color, length, and breadth of the
Rectangle:");
String color = sc.next();
int length = sc.nextInt();
int breadth = sc.nextInt();
shapes[i] = new Rectangle(color, length, breadth);
} else if (shapeType.equalsIgnoreCase("Circle")) { //if the given
shape is circle then this loop is excuted
System.out.println("Enter color and radius of the Circle:");
String color = sc.next();
int radius = sc.nextInt();
shapes[i] = new Circle(color, radius);
} else { //if the given shape is not matched with above shapes then
this loop is excuted
System.out.println("Invalid shape type. Try again.");
i--; // To repeat this iteration
}
}
}

double totalArea() {
double total = 0;
for (Shape shape : shapes) {
total = total + shape.area();
}
return total;
}

void displayShapes() {
for (Shape shape : shapes) {
System.out.println(shape);
}
}
}
RESULT:

Program 2

PROBLEM Create the Stack interface which lists the methods push(), pop(), peek()
STATEMENT: Write a class called Integer Stack which creates a stack of numbers by
implementing the Stack interface.

PROGRAM: import java.util.Scanner;

public class Main {


public static void main(String[] args) {

int n;
Scanner sc = new Scanner(System.in); //new scanner class
System.out.println("Enter the number of stack elements you want to
push");
n=sc.nextInt();
IntegerStack stack = new IntegerStack(n);

System.out.println("=======================================
============");

for(int i=0 ; i<n ; i++){


System.out.println("Enter value for element " + (i + 1) + ":");
int value = sc.nextInt();
stack.push(value);
}

System.out.println("=======================================
============");
System.out.println("Top item: " + stack.peek());
System.out.println("Popped item 1 : " + stack.pop());
System.out.println("Popped item 2: " + stack.pop());
System.out.println("Top item after pop: " + stack.peek());

System.out.println("=======================================
============");
}
}

interface Stack {
void push(int item);
int pop();
int peek();
}

class IntegerStack implements Stack {


private int[] stack; // stack of integer type elements
private int top;
private int size;

// parameterized constructor
public IntegerStack(int size) {
this.size = size;
stack = new int[size];
top = -1; // Indicates an empty stack
}

public void push(int item) { //we override the push method


if (top == size - 1) {
System.out.println("Stack overflow, prathamesh's program cant push
the element");
}
stack[++top] = item;
}

public int pop() { //we override the pop method


if (top == -1) {
System.out.println("Stack underflow, prathamesh's program cant
pop the element");
}
return stack[top--];
}

public int peek() { //we override the peak method


if (top == -1) {
System.out.println("Stack is empty, prathamesh's program cant give
the peek");
}
return stack[top];
}

public boolean isEmpty() {


return top == -1;
}

public int size() {


return top + 1;
}
}
RESULT:

Case 1 :

Case 2 : Two times pop


Case 3 : Peek error (because there is no element)\

CONCLUSION: In this experiment, we successfully implemented Abstract Classes and


Interfaces in Java through two distinct programs. The first program
demonstrated the use of an abstract class Shape with derived classes
Rectangle and Circle, allowing for a flexible design that computes the total
area of various shapes within a Diagram class. This showcased the benefits
of abstraction in managing complex entities. The second program illustrated
a stack data structure via the Stack interface and the IntegerStack class,
where users could dynamically input values and perform operations like
push, pop, and peek, highlighting the importance of encapsulation and error
handling.

You might also like