Create class StackOfIntegers ( StackOfIntegers.java ), using the following UML p
ID: 3908834 • Letter: C
Question
Create class StackOfIntegers (
StackOfIntegers.java
), using the following UML plus the
specification:
StackOfIntegers
Specification
-elements: int[]
-size: int
An array to store integers in the stack.
The number of integers in the stack.
+StackOfIntegers()
+StackOfIntegers(capacity:
int)
+empty(): boolean
+peek: int
+push(value: int): void
+pop(): int
+getSize(): int
Construct an empty stack with a default capacity of 16.
Construct an empty stack with a specified capacity.
Returns true if the stack is empty.
Returns the integer at the top of the stack without
removing it from the stack.
Stores an integer into the top of the stack.
Remove the integer at the top of the stack and returns it.
Returns the number of elements in the stack.
Write a program (
StackOfIntegersTest.java
) to test the class StackofIntegers. Create a
stack, store ten integers 0, 1, 2, ..., 9, and display them in reverse order. The output
should look exactly like the following:
Pop all elements in the stack, you will get:
9 8 7 6 5 4 3 2 1 0
Explanation / Answer
ScreenShot
--------------------------------------------------------------------------------
Program
/*This is a program to create a stack using array
* two member variable and 5 member functions except constructors
*/
//Stack of integer class create a stack with default size=16
class StackOfIntegers{
private int[] elements;
private int size;
//Default constructor
StackOfIntegers(){
elements=new int[16];
}
//Set capacity of stack
StackOfIntegers(int capacity){
elements=new int[capacity];
}
//Check if the stack empty or not
public boolean empty() {
if(size==0)
return false;
else
return true;
}
//top element
public int peek() {
while(empty()) {
return elements[size-1];
}
return -1;
}
//push values into stack
public void push(int value) {
if (size >= elements.length)
{
System.out.println("Stack Overflow");
}
else
{
elements[size] = value;
size=size+1;
}
}
//Pop values one by one from top
public int pop() {
while(empty()) {
int val=elements[size-1];
size=size-1;
return val;
}
return -1;
}
//Size of the stack
public int getSize() {
return size;
}
}
//Tester class
public class StackOfIntegersTest {
public static void main(String[] args) {
//Create object of class StackOfIntegers
StackOfIntegers st=new StackOfIntegers();
//store values in stack
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.push(6);
st.push(7);
st.push(8);
st.push(9);
//display values from the stack
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
}
}
----------------------------------------------------
Output
9
8
7
6
5
4
3
2
1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.