In: Computer Science
Write a program that uses loops (both the for-loop and the while loop). This assignment also uses a simple array as a collection data structure (give you some exposure to the concept of data structure and the knowledge of array in Java). In this assignment, you are asked to construct an application that will store and retrieve data. The sequence of data retrieval relative to the input is Last In First Out (LIFO). Obviously, the best data structure that can achieve this is a Stack. For simplicity, we require that the Stack to handle only characters. In this assignment, we will use two Java classes: the worker class and the application class. We will name the application class (the one that contains the main() method) SimpleStackApp; and name the worker class SimpleStack.
The code uses the worker class SimpleStack, the for-loop, the while-loop
Design
Since this is a simple application, we are using the two classes: the worker class and the application class. The worker class is problem specific and the application class is generic (means the code in this class is almost the same for different programs). The application class “SimpleStackApp” contains a special method main(), which is the entry point of the program. The worker class “SimpleStack” is created by SimpleStackApp and is used by it. Thus the relationship between the worker class and the application class is an association relationship. The following UML diagram shows this design.
Supplied Partial Code
To help you get started, I give you partial code that implements several methods indicated in the above class diagram (you can cut and paste them to Eclipse). You are required to supply the missing code that will produce the expected outputs.
List 1: partial code for SimpleStack (the worker class)
-----------------------------------------------------------------------------------
class SimpleStack {
char[] data; // this array holds that stack
int tos; // index of top of stack
// Construct an empty stack given its size.
SimpleStack(int size) {
data = new char[size]; // create the array to hold the stack
tos = 0;
}
// Push a character onto the stack.
void push(char ch) {
if(isFull()) {
System.out.println(" – -- Stack is full.”);
return;
}
data[tos] = ch;
tos++;
}
// Pop a character from the stack.
char pop() {
if(isEmpty()) {
System.out.println(" – -- Stack is empty.”);
return (char) 0; // a placeholder value
}
tos--;
return data[tos] ;
}
// You are asked to finish this method.
boolean isEmpty() {
// finish the method according the spec specified later.
}
// You are asked to finish this method.
boolean isFull() {
// finish the method according the spec specified later.
}
}
-------------------------------------------------------------------------------------
List 2: partial code for SimpleStackApp (the application class)
-----------------------------------------------------------------------------------
class SimpleStackApp {
public static void main(String[] args) {
int i;
char ch;
System.out.println(“Dmonstrate SimpleStack\n”);
// Construct 10-element empty stack.
SimpleStack stack = new SimpleStack(10);
System.out.println("Push 10 items onto a 10-element stack.”);
// push the letters A through J onto the stack.
System.out.print("Pushing: ”);
for(ch = ‘A’; ch < ‘K’; ch++) {
System.out.print(ch);
stack.push(ch);
}
System.out.println("\nPop those 10 items from stack.”);
// Now, pop the characters off the stack.
// Notice that order will be the reverse of those pushed.
System.out.print("Popping: ”);
for(i = 0; i < 10; i++) {
ch = stack.pop();
System.out.print(ch);
}
System.out.println("\n\nNext, use isEmpty() and isFull() “ +
“to fill and empty the stack.”);
// Push the letters until the stack is full.
System.out.print("Pushing: ”);
for(ch = ‘A’; !stack.isFull(); ch++) {
System.out.print(ch);
stack.push(ch);
}
System.out.println();
// Now, pop the characters off the stack until it is empty.
System.out.print("Popping: ”);
while(!stack.isEmpty()) {
ch = stack.pop();
System.out.print(ch);
}
System.out.println("\n\nNow, use a 4-element stack to generate “ +
“ some errors.”);
// In the space below, you are asked to supply the missing
//code that will produce the output given in the required work
//section.
}
}
Now, use a 4-element stack to generate some errors
The given partial code will produce the output from “Outputs:” to the line “Now, use a 4-element stack to generate some errors.” Your task here is to supply the missing code in this class that will produce the last 2 lines shown below.
Pushing: 12345 --- Stack is full.
Popping: 4321 --- Stack is empty.
List 1: Code to be filled for SimpleStack (the worker class)
----------------------------------------------------------------------------------------------------------------------------------------------------
// You were asked to finish this method.
boolean isEmpty() {
return (tos<=0); // missing code
}
// You were asked to finish this method.
boolean isFull() {
return (tos>data.length-1); // missing code
}
Explanation:
Here, 'tos' is the index of top of stack and 'data' is character array to hold the elements of the stack.
The purpose of isEmpty() method is to check whether elements are present in stack to perform pop() operation or not. When elements are available(ie) tos>0, pop operation will happen. When no more elements are available(ie) tos<=0 , pop deletions cannot occur in the stack.
The purpose of isFull() method is to check whether empty space is available in stack to perform push() operation or not. When space is available(ie) (tos<=data.length-1), push operation will occur. When the stack is already full(ie) tos>data.length-1, push insertion is not possible.
----------------------------------------------------------------------------------------------------------------------------------------------------
List 2: Code to be filled for SimpleStackClass (the application class)
----------------------------------------------------------------------------------------------------------------------------------------------------
//missing code
SimpleStack stack1 = new SimpleStack(4); //To construct 4-element empty stack.
System.out.print("Pushing: ");
for(ch = '1'; ch < '6'; ch++) {
System.out.print(ch);
stack1.push(ch);
}
System.out.print("Popping: ");
for(i = 1; i < 6; i++) {
ch = stack1.pop();
System.out.print(ch);
}
Explanation:
Requirement is to create a 4-element stack and then to get the
below output
Pushing: 12345 – -- Stack is full.
Popping: 4321 – -- Stack is empty.
So constructed a 4 - element stack and implemented the above mentioned push and pop operations to get the exact output as mentioned.