[Go to site: main page, start]

0% found this document useful (0 votes)
4 views12 pages

Java Program Assignment19

The document defines two interfaces, IntOperations and StackOperations, for performing various operations on integers and stacks, respectively. It includes the implementation of these interfaces in the MyNumber and MyStack classes, which provide methods for checking number properties and stack operations. The main method in each class demonstrates a menu-driven program allowing user interaction for executing these operations.

Uploaded by

Parineeta Deb
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)
4 views12 pages

Java Program Assignment19

The document defines two interfaces, IntOperations and StackOperations, for performing various operations on integers and stacks, respectively. It includes the implementation of these interfaces in the MyNumber and MyStack classes, which provide methods for checking number properties and stack operations. The main method in each class demonstrates a menu-driven program allowing user interaction for executing these operations.

Uploaded by

Parineeta Deb
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

1.

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.

import [Link];

// Interface for integer operations


interface IntOperations {
boolean isPositive();
boolean isNegative();
boolean isEven();
boolean isOdd();
boolean isPrime();
boolean isPalindrome();
long factorial();
int sumOfDigits();
}

// Class MyNumber implementing IntOperations


class MyNumber implements IntOperations {
private int number;

// Default constructor
public MyNumber() {
[Link] = 0;
}

// Constructor to initialize with a value


public MyNumber(int number) {
[Link] = number;
}

@Override
public boolean isPositive() {
return [Link] > 0;
}

@Override
public boolean isNegative() {
return [Link] < 0;
}

@Override
public boolean isEven() {
return [Link] % 2 == 0;
}

@Override
public boolean isOdd() {
return !isEven();
}
@Override
public boolean isPrime() {
if ([Link] <= 1) {
return false;
}
for (int i = 2; i <= [Link]([Link]); i++) {
if ([Link] % i == 0) {
return false;
}
}
return true;
}

@Override
public boolean isPalindrome() {
if ([Link] < 0) {
return false; // Negative numbers are not palindromes
}
int originalNumber = [Link];
int reversedNumber = 0;
while (originalNumber > 0) {
int remainder = originalNumber % 10;
reversedNumber = reversedNumber * 10 + remainder;
originalNumber /= 10;
}
return [Link] == reversedNumber;
}

@Override
public long factorial() {
if ([Link] < 0) {
return -1; // Factorial is not defined for negative numbers
}
if ([Link] == 0) {
return 1;
}
long fact = 1;
for (int i = 1; i <= [Link]; i++) {
fact *= i;
}
return fact;
}

@Override
public int sumOfDigits() {
int num = [Link]([Link]); // Handle negative numbers
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}

public int getNumber() {


return [Link];
}

public void setNumber(int number) {


[Link] = number;
}
}

public class NumberChecker {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
MyNumber myNum = new MyNumber();

[Link]("Enter an integer: ");


int inputNumber = [Link]();
[Link](inputNumber);

int choice;
do {
[Link]("\n--- Number Operations Menu ---");
[Link]("1. Check if Positive");
[Link]("2. Check if Negative");
[Link]("3. Check if Even");
[Link]("4. Check if Odd");
[Link]("5. Check if Prime");
[Link]("6. Check if Palindrome");
[Link]("7. Calculate Factorial");
[Link]("8. Calculate Sum of Digits");
[Link]("0. Exit");
[Link]("Enter your choice: ");
choice = [Link]();

switch (choice) {
case 1:
[Link]([Link]() + " is positive: " + [Link]());
break;
case 2:
[Link]([Link]() + " is negative: " + [Link]());
break;
case 3:
[Link]([Link]() + " is even: " + [Link]());
break;
case 4:
[Link]([Link]() + " is odd: " + [Link]());
break;
case 5:
[Link]([Link]() + " is prime: " + [Link]());
break;
case 6:
[Link]([Link]() + " is a palindrome: " + [Link]());
break;
case 7:
long factorialResult = [Link]();
if (factorialResult == -1) {
[Link]("Factorial is not defined for negative numbers.");
} else {
[Link]("Factorial of " + [Link]() + " is: " + factorialResult);
}
break;
case 8:
[Link]("Sum of digits of " + [Link]() + " is: " +
[Link]());
break;
case 0:
[Link]("Exiting program.");
break;
default:
[Link]("Invalid choice. Please try again.");
}
} while (choice != 0);

[Link]();
}
}

Output:-
2. 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.

import [Link];

// Interface for stack operations


interface StackOperations {
void push(int value);
int pop();
int peek();
boolean isEmpty();
boolean isFull();
}

// Class MyStack implementing StackOperations


class MyStack implements StackOperations {
private int[] stack;
private int top;
private int capacity;

// Constructor to initialize the stack


public MyStack(int capacity) {
[Link] = capacity;
[Link] = new int[capacity];
[Link] = -1; // Initialize top to -1 (empty stack)
}

@Override
public void push(int value) {
if (isFull()) {
[Link]("Stack is full. Cannot push " + value);
} else {
top++;
stack[top] = value;
[Link](value + " pushed onto the stack.");
}
}

@Override
public int pop() {
if (isEmpty()) {
[Link]("Stack is empty. Cannot pop.");
return -1; // Return a sentinel value to indicate an error
} else {
int poppedValue = stack[top];
top--;
[Link](poppedValue + " popped from the stack.");
return poppedValue;
}
}

@Override
public int peek() {
if (isEmpty()) {
[Link]("Stack is empty. Cannot peek.");
return -1; // Return a sentinel value to indicate an error
} else {
return stack[top];
}
}

@Override
public boolean isEmpty() {
return top == -1;
}

@Override
public boolean isFull() {
return top == capacity - 1;
}
}

// Main class to demonstrate stack operations


public class StackDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the capacity of the stack: ");
int capacity = [Link]();
MyStack stack = new MyStack(capacity);

int choice;
do {
[Link]("\n--- Stack Operations Menu ---");
[Link]("1. Push");
[Link]("2. Pop");
[Link]("3. Peek");
[Link]("4. Check if Empty");
[Link]("5. Check if Full");
[Link]("0. Exit");
[Link]("Enter your choice: ");
choice = [Link]();

switch (choice) {
case 1:
[Link]("Enter the value to push: ");
int value = [Link]();
[Link](value);
break;
case 2:
int poppedValue = [Link]();
if (poppedValue != -1) { // Check for the sentinel value
[Link]("Popped value: " + poppedValue);
}
break;
case 3:
int peekedValue = [Link]();
if (peekedValue != -1) { // Check for the sentinel value
[Link]("Top element: " + peekedValue);
}
break;
case 4:
[Link]("Is the stack empty? " + [Link]());
break;
case 5:
[Link]("Is the stack full? " + [Link]());
break;
case 0:
[Link]("Exiting program.");
break;
default:
[Link]("Invalid choice. Please try again.");
}
} while (choice != 0);

[Link]();
}
}

Output:-

You might also like