Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters.
Your input/output interface should look something like the following:
What operation do you want to do?
push
What number do you want to push?
5
5 has been pushed on the stack
What operation do you want to do?
push
Error: Stack is full
What operation do you want to do? pop
The number popped is 5
What operation do you want to do?
pop
Error: Stack is empty
Variations on the wording are acceptable. But the program needs to acknowledge what number has been pushed or popped. Also, when the user tries to push onto a full stack, the program should print an alert, but otherwise not change the stack. Similarly, if the user tries to pop from an empty stack, the program needs to print an alert, but not change the stack. You will need to implement several methods, including: Push , Pop , Stack Full , Stack Empty Depending on how you structure your program, you may need more methods.
Rate my solution and comment if any doubts
Code:
import java.util.*;
public class Stack
{
//all the variables , arrays and methods are declared
as static because
//they are called by static method main()
// or else we should create object for every for non
static method to use them in static method
static int max=5;
// max is the length of the stack
static int[] stack_arr=new int[max];
// it is array implementation of stack
static int top=-1;
// initilly the stack will be empty so top will be
-1
public static void main(String[] args) {
// object is created for Scanner
class to take input
Scanner scan=new
Scanner(System.in);
// menu
System.out.println("Enter your
choice(pop,push,print or -1 to exit) \n");
System.out.print("What operation do
you want to do? ");
// asking input
String x=scan.nextLine();
// equals() method return true if
the both strings are equal
// this loop terminate until user
enters -1
while(!x.equals("-1"))
{
// if user
enters pop then pop() method is called
if(x.equals("pop"))
{
pop();
}
// if the user
enters push then push() method is called
else
if(x.equals("push"))
{
push();
}
// if user
enters print then print() method is called
else
if(x.equals("print"))
{
display();
}
// is user
enters otherthan push,pop,print then Invalid input is printed
else
{
System.out.println("Invalid Input");
}
System.out.print("\nWhat operation do you want to do? ");
// asking next
input
x=scan.nextLine();
}
}
// this method return true if the stack is full
// which means the top element is at end of
stack
public static boolean isFull(){
return top==max-1;
}
// this method return true if the stack is empty
// which means the top is -1
public static boolean isEmpty(){
return top==-1;
}
// this method will pop the element from stack
public static void pop()
{
// if stack is empty then error
message will be displayed
if(isEmpty()){
System.out.println("Error: Stack is empty");
}
// if the is stack is not
empty
// then top element of stack is
stored and top is decremented
else
{
int
i=stack_arr[top];
top--;
System.out.println("The number popped is "+i);
}
}
// this method will push the element to stack
public static void push()
{
// Scanner object to ask
input
Scanner scan=new
Scanner(System.in);
// if the stack is full then error
message wil be displayed
if(isFull())
{
System.out.println("Error: Stack is full");
}
// if the stack is not full then it
asking for element
// incrementing the top and storing
it in the stack
else
{
System.out.println("What number do you want to push?");
int
i=scan.nextInt();
stack_arr[++top]=i;
System.out.println(i+" has been pushed on the stack");
}
}
// this method is used to print the elements in the
stack
public static void display()
{
// is stack is empty error message
will be displayed
if(isEmpty())
{
System.out.println("Error: Stack is empty");
}
// the elements in stack will be
displayed
else
{
for(int
i=top;i>=0;i--)
{
System.out.print(stack_arr[i]+" ");
}
System.out.println();
}
}
}
Code(Screenshot):
Output(Screenshot):
Use Java to implement a basic stack using an array of integers. For the stack, you...
Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...
Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...
C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...
I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array. //Array implementation of stacks. import java.util.Arrays; public class ArrayStack implements Stack { //Declare a class constant called DEFAULT_STACK_SIZE with the value 10. private static final int DEFAULT_STACK_SIZE = 10; /* Declare two instance variables: 1. An integer called...
Description: \\ implement in Java Based on Chapter 6, programming project 1 Hewlett-Packard has a tradition of creating stack-based calculators. Rather than using standard algebraic notation ( 1 + 1 = ), the user would enter the values, then the operator. The calculator had an “enter” key to push each value onto a stack, then pop the stack when an operation key (e.g. “+” or “-”) was pressed. Assignment: Create a Calculator class that implements the provided StackCalculator interface Requirement Constructor: + Calculator() ...
Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...
I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...
I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (onto the queue) or pushed and popped (onto the stack). I are required to use STL data structures to implement and create the stack and queue for my program. ----- testfile1.tst -------- enqueue 5 enqueue 7 push blooper push rookie dequeue push demerits pop enqueue 3 enqueue 8 push showplace enqueue 9 dequeue pop dequeue push palmetto push zebra...
Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...
Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...