Assignment 13-Dipanjan - 155
Assignment 13-Dipanjan - 155
16-03-2024
(Abstract Class and Interface)
NAME – Dipanjan Sahoo
ROLL NO - 2370155
1. Create a class Shape having data members length, breadth, height and abstract methods such as
volume and surfaceArea. Inherit this class into cube, cylinder and cuboid classes. Redefine the
required methods to calculate and display the volume and surface area of each shape.
Ans –
abstract class Shape {
protected double length;
protected double breadth;
protected double height;
@Override
public double volume() {
return length * length * length;
}
@Override
public double surfaceArea() {
return 6 * length * length;
}
}
class Cylinder extends Shape {
private double radius;
@Override
public double volume() {
return Math.PI * radius * radius * height;
}
@Override
public double surfaceArea() {
return 2 * Math.PI * radius * (radius + height);
}
}
@Override
public double surfaceArea() {
return 2 * (length * breadth + breadth * height + length * height);
}
}
class Main{
public static void main(String[] args) {
Cylinder c1=new Cylinder(5.0, 7.0);
Cube c2=new Cube(8.0);
Cuboid c3=new Cuboid(12.0, 14.0, 16.0);
System.out.println(c1.volume());
System.out.println(c1.surfaceArea());
System.out.println(c2.volume());
System.out.println(c2.surfaceArea());
System.out.println(c3.volume());
System.out.println(c3.surfaceArea());
}
}
2. Design an abstract class fruit with data members colour, taste and an abstract method display.
Inherit this class to other classes such as Apple, Banana, Orange and Strawberry. Redefine the display
method to show the color and taste of each fruit along with its class name.
Ans –
abstract class Fruit {
private String color;
private String taste;
public Apple() {
super("Red", "Sweet and tangy");
}
@Override
public void display() {
System.out.println("I am an Apple. My color is " + getColor() + " and I taste " + getTaste());
}
}
@Override
public void display() {
System.out.println("I am a Banana. My color is " + getColor() + " and I taste " + getTaste());
}
}
public Orange() {
super("Orange", "Sweet and citrusy");
}
@Override
public void display() {
System.out.println("I am an Orange. My color is " + getColor() + " and I taste " + getTaste());
}
}
public Strawberry() {
super("Red", "Sweet and tart");
}
@Override
public void display() {
System.out.println("I am a Strawberry. My color is " + getColor() + " and I taste " + getTaste());
}
}
boolean isNegative();
boolean isEven();
boolean isOdd();
boolean isPrime();
boolean isPalindrome();
int factorial();
int sumOfDigits();
}
public MyNumber() {
this.num = 0;
}
@Override
public boolean isPositive() {
return num > 0;
}
@Override
public boolean isNegative() {
return num < 0;
}
@Override
public boolean isEven() {
return num % 2 == 0;
}
@Override
public boolean isOdd() {
return !isEven();
}
@Override
public boolean isPrime() {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
@Override
public boolean isPalindrome() {
int originalNum = num;
int reversedNum = 0;
while (num != 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
return originalNum == reversedNum;
}
@Override
public int factorial() {
if (num < 0) {
throw new IllegalArgumentException("Factorial is not defined for negative numbers");
}
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
return factorial;
}
@Override
public int sumOfDigits() {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
}
int choice;
do{
System.out.println("\nMenu:");
System.out.println("1. Check if positive");
System.out.println("2. Check if negative");
System.out.println("3. Check if even");
System.out.println("4. Check if odd");
System.out.println("5. Check if prime");
System.out.println("6. Check if palindrome");
System.out.println("7. Calculate factorial");
System.out.println("8. Calculate sum of digits");
System.out.println("9. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println(number.isPositive() ? "Positive" : "Non-positive");
break;
case 2:
System.out.println(number.isNegative() ? "Negative" : "Non-negative");
break;
case 3:
System.out.println(number.isEven() ? "Even" : "Odd");
break;
case 4:
System.out.println(number.isOdd() ? "Odd" : "Even");
break;
case 5:
System.out.println(number.isPrime() ? "Prime" : "Not prime");
break;
case 6:
System.out.println(number.isPalindrome() ? "Palindrome" : "Not a palindrome");
break;
case 7:
try {
System.out.println("Factorial: " + number.factorial());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
break;
case 8:
System.out.println("Sum of digits: " + number.sumOfDigits());
}
}while(choice<9);
}
}
4. Define an interface “StackOperations” which declares methods for a static stack. Define a class
“MyStack” which contains an array and top as data members and implements the above interface.
Initialize the stack using a constructor. Write a menu driven program to perform all operations(Push,
POP, Peak) on a MyStack object.
Ans –
import java.util.*;
interface StackOperations<T> {
T pop();
T peek();
}
@Override
public void push(T data) {
if (isFull()) {
System.out.println("Stack overflow!");
return;
}
arr[++top] = data;
}
@Override
public T pop() {
if (isEmpty()) {
System.out.println("Stack underflow!");
return null;
}
return arr[top--];
}
@Override
public T peek() {
if (isEmpty()) {
System.out.println("Stack is empty!");
return null;
}
return arr[top];
}
do {
System.out.println("\nMenu:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter data to push: ");
int data = scanner.nextInt();
stack.push(data);
break;
case 2:
Integer popped = stack.pop();
if (popped != null) {
System.out.println("Popped element: " + popped);
}
break;
case 3:
Integer peeked = stack.peek();
if (peeked != null) {
System.out.println("Top element: " + peeked);
}
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 4);
}
}