0% found this document useful (0 votes)
40 views14 pages

Assignment 13-Dipanjan - 155

Uploaded by

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

Assignment 13-Dipanjan - 155

Uploaded by

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

Assignment-13

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;

public abstract double volume();

public abstract double surfaceArea();

class Cube extends Shape {


public Cube(double side) {
this.length = side;
this.breadth = side;
this.height = side;
}

@Override
public double volume() {
return length * length * length;
}

@Override
public double surfaceArea() {
return 6 * length * length;
}
}
class Cylinder extends Shape {
private double radius;

public Cylinder(double radius, double height) {


this.radius = radius;
this.height = height;
}

@Override
public double volume() {
return Math.PI * radius * radius * height;
}

@Override
public double surfaceArea() {
return 2 * Math.PI * radius * (radius + height);
}
}

class Cuboid extends Shape {


public Cuboid(double length, double breadth, double height) {
this.length = length;
this.breadth = breadth;
this.height = height;
}
@Override
public double volume() {
return length * breadth * 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 Fruit(String color, String taste) {


this.color = color;
this.taste = taste;
}

public abstract void display();

public String getColor() {


return color;
}

public String getTaste() {


return taste;
}
}

class Apple extends Fruit {

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());
}
}

class Banana extends Fruit {


public Banana() {
super("Yellow", "Sweet and creamy");
}

@Override
public void display() {
System.out.println("I am a Banana. My color is " + getColor() + " and I taste " + getTaste());
}
}

class Orange extends Fruit {

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());
}
}

class Strawberry extends Fruit {

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());
}
}

public class Main {


public static void main(String[] args) {
Fruit[] fruits = {new Apple(), new Banana(), new Orange(), new Strawberry()};
for (Fruit fruit : fruits) {
fruit.display();
}
}
}
3. Define an interface “IntOperations” with methods to check whether a number is positive/ negative,
even/odd, prime, palindrome and operations like factorial and sum of digits. Define a class
MyNumber having one private data member of type int. Write a default constructor to initialize it to 0
and another constructor to initialize it to a value (Use this). Implement the above interface. Create an
object in main method. Input a number and write a menu driven program to check different properties
of the number using above methods.
Ans –
import java.util.*;
interface IntOperations {
boolean isPositive();

boolean isNegative();

boolean isEven();

boolean isOdd();

boolean isPrime();

boolean isPalindrome();

int factorial();

int sumOfDigits();
}

class MyNumber implements IntOperations {


private int num;

public MyNumber() {
this.num = 0;
}

public MyNumber(int num) {


this.num = num;
}

@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;
}
}

public class Main {


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

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> {

void push(T data);

T pop();

T peek();
}

class MyStack<T> implements StackOperations<T> {


private T[] arr;
private int top;

public MyStack(int size) {


arr = (T[]) new Object[size];
top = -1;
}

@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];
}

private boolean isEmpty() {


return top == -1;
}

private boolean isFull() {


return top == arr.length - 1;
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the stack: ");
int size = scanner.nextInt();

MyStack<Integer> stack = new MyStack<>(size);


int choice;

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);
}
}

You might also like