Create a new project called simplestack. The following code shows a simple Java
ID: 3638660 • Letter: C
Question
Create a new project called simplestack.The following code shows a simple Java implementation of a Stacks Stack.java class (parts of the code is missing, you are to complete it). ==========
Stack.java
/**
* class Stack
*/
public class Stack
{
private Object[] stack ;
private int total; // to track number of items
public Stack(int size)
{
stack = new Object[size]; // create array
total = 0; // set number of items to zero
}
/**
* add an item to the array
*/
public boolean push(Object obj)
{
==========
==========
==========
}
/**
* remove an item by obeying LIFO rule
*/
public Object pop()
{
if (isEmpty() == false) // check if empty
{
Object obj = stack[total-1]; // last item
stack[total-1]= null; // remove item
total--; // update total
return obj; // return item
}
else
return null; // to indicate failure
}
/**
* checks if array is empty
*/
public boolean isEmpty()
{
==========
==========
}
/**
* checks if array is full
*/
public boolean isFull()
{
if (total ==stack.length)
return true;
else
return false;
}
/**
* returns the item at index i
*/
public Object getItem(int i)
{
return stack[i-1]; // ith item at position i-1
}
/**
* return the number of items in the array
*/
public int getTotal()
{
return total;
}
}
Using a Stack
Data structure classes are intended to be used in programs as utility classes which contain the data the program needs to work with. To use the Stack class, you need to know how to write code to call the Stack operations, for example to add data to the
Stack.
Controller Class: The following class StackTester.java shows how to use a Stack to hold Integer objects. Calls to Stack operations are shown in bold. (parts of the code is missing, you are to complete it).
/**
* class StackTester
*
*/
public class StackTester
{
private Stack stack;
public StackTester()
{
stack = new Stack(10);
}
public StackTester(Stack stack)
{
this.stack = stack;
}
/**
* push item onto stack
*/
public void pushNumber(int num)
{
boolean ok = stack.push(new Integer(num));
if (!ok)
System.out.println("Push unsuccessful");
else
System.out.println("Push successful");
}
/**
* pop number from stack
*/
public void popNumber()
{
Integer result = (Integer) stack.pop();
if (result!=null)
System.out.println("Number is :" +
result.intValue());
else
System.out.println("Pop unsuccessful");
}
/**
* check if stack is empty
*/
public void checkIfEmpty()
{
if (stack.isEmpty())
System.out.println("Stack empty");
else
System.out.println("Stack is not empty");
}
/**
* check if stack is full
*/
public void checkIfFull()
{
if (stack.isFull())
System.out.println("Stack full");
else
System.out.println("Stack is not full");
}
/**
* list the numbers in stack
*/
public void listNumbersInStack()
{
if (stack.isEmpty())
System.out.println("Stack empty");
else
{
System.out.println("Numbers in stack are:");
System.out.println();
for (int i=stack.getTotal(); i>=1; i--)
System.out.println(stack.getItem(i));
System.out.println();
}
}
}
Create a Driver.java class (it contains main).
1. Create an instance of Stack called st with size 5.
2. Create a new instance of Controller Class called StackTester called test with the Stack st created above.
3. Call the checkIfEmpty method of your StackTester. What was the result?
4. Call the pushNumber method of your Stacktester to add the number 5 to the stack. Print the stack contents.
5. Repeat this to add the number 7 and repeat again to add the number 2. What result would you expect if you pop from the Stack?
6. Call the popNumber method of your StackTester and check that you got the correct result.
7. Call the pushNumber method of your StackTester to add the number 9 to the stack. Print the stack contents.
Explanation / Answer
Stack.java: public class Stack { private Object[] stack ; private int total; // to track number of items public Stack(int size) { stack = new Object[size]; // create array total = 0; // set number of items to zero } /** * add an item to the array */ public boolean push(Object obj) { if (isFull()) { return false; } else { stack[total] = obj; total++; return true; } } /** * remove an item by obeying LIFO rule */ public Object pop() { if (isEmpty() == false) // check if empty { Object obj = stack[total-1]; // last item stack[total-1]= null; // remove item total--; // update total return obj; // return item } else return null; // to indicate failure } /** * checks if array is empty */ public boolean isEmpty() { return (total == 0); } /** * checks if array is full */ public boolean isFull() { if (total ==stack.length) return true; else return false; } /** * returns the item at index i */ public Object getItem(int i) { return stack[i-1]; // ith item at position i-1 } /** * return the number of items in the array */ public int getTotal() { return total; } } StackTester.java: public class StackTester { private Stack stack; public StackTester() { stack = new Stack(10); } public StackTester(Stack stack) { this.stack = stack; } /** * push item onto stack */ public void pushNumber(int num) { boolean ok = stack.push(new Integer(num)); if (!ok) System.out.println("Push unsuccessful"); else System.out.println("Push successful"); } /** * pop number from stack */ public void popNumber() { Integer result = (Integer) stack.pop(); if (result!=null) System.out.println("Number is :" + result.intValue()); else System.out.println("Pop unsuccessful"); } /** * check if stack is empty */ public void checkIfEmpty() { if (stack.isEmpty()) System.out.println("Stack empty"); else System.out.println("Stack is not empty"); } /** * check if stack is full */ public void checkIfFull() { if (stack.isFull()) System.out.println("Stack full"); else System.out.println("Stack is not full"); } /** * list the numbers in stack */ public void listNumbersInStack() { if (stack.isEmpty()) System.out.println("Stack empty"); else { System.out.println("Numbers in stack are:"); System.out.println(); for (int i=stack.getTotal(); i>=1; i--) System.out.println(stack.getItem(i)); System.out.println(); } } } Driver.Java: public class Driver { public static void main(String[] args) { Stack st = new Stack(5); StackTester test = new StackTester(st); test.checkIfEmpty(); test.pushNumber(5); test.listNumbersInStack(); test.pushNumber(7); test.listNumbersInStack(); test.pushNumber(2); test.listNumbersInStack(); test.popNumber(); test.pushNumber(9); test.listNumbersInStack(); } } Output: Stack empty Push successful Numbers in stack are: 5 Push successful Push successful Number is :2 Push successful Numbers in stack are: 9 7 5 E:Program FilesMinGWin>javac *.java E:Program FilesMinGWin>java Driver Stack empty Push successful Numbers in stack are: 5 Push successful Numbers in stack are: 7 5 Push successful Numbers in stack are: 2 7 5 Number is :2 Push successful Numbers in stack are: 9 7 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.