Question
Program Statement: Write a program that uses loops (both the for-loop and the while loop). This assignment also uses a simple array as a collection
Program Statement:
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 solidify your 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) SimpleStackDemo; and name the worker class SimpleStack.
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. The application class SimpleStackDemo contains a special method main(), which is the entry point of the program. The worker classSimpleStack is created by SimpleStackDemo 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.
SimpleStackDemo
main()
SimpleStack
data: char[]
tos: int
SimpleStatck()
push()
pop()
isEmpty()
isFull()
Supplied Partial Code
To help you get started, we give you following list of code (you can cut and paste them to Eclipse):
1. The SimpleStack Class
The main functionality of the SimpleStack worker class is to offer a Stack data structure that can store character data and be retrieved later. It uses an array as its underlying data structure. List 1 gives the partial code for this class.
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.
}
}
-------------------------------------------------------------------------------------
As you can see, the functionality and the structure of the worker class are pretty clear.
Required Work
You are required to write the missing code in the SimpleStack class. Specifically, you are required to supply the body for
isEmpty()
and for
isFull()
The following are the detailed specifications for these two methods:
// precondition: no
// postcondition: no
boolean isEmpty():this method takes no parameter and returns a boolean that is either true or false. If the instance variable tos is 0, then the stack is empty. Otherwise, the stack is not empty.
tips: use the instance variable tos to test whether the stack is empty or not.
// precondition: no
// postcondition: no
boolean isFull():this method takes no parameter and returns a boolean that is either true or false. If the instance variable tos is equal to the capacity (length) of the array data, then the stack is full. Otherwise, the stack is not full.
tips: use the instance variable tos and the length of the array to test whether the stack is full or not. In Java, the length of an array can be tested using array classs instance variable length. For example, if a is an array in Java, then the following line
a.length
will give the capacity (length) of the array.
Finishing Your Assignment
You are asked to use Eclipse to finish this assignment. Here is what you need to do to finish the assignment. First, you create a new project (ICS141Program2) for this assignment. Second, build the project shell by cut and paste the code given to you (make sure you eliminate all the hidden characters after pasting (for double quotes, you need to delete and retype it inside Eclipse)). Particularly, you cut and paste the code for SimpleStack class; supply the missing code. Third, write the application class SimpleStackDemo according to the given output requirement (given later).
SCORE YOUR POINTS BY FINISHING THE FOLLOWING
Important, the code you supplied in the following must match the given class diagram (for example, the name of classes and the name of methods must be the same; so do the relationships among these classes).
1. (20 pts) After finishing the missing code, cut and paste your Java source code for the class SimpleStack in the space below:
Answer:
2. (20 pts) Write the application class SimpleStackDemo according the given output requirement. You are asked to supply the application class SimpleStackDemo according to the following set of criteria:
1) the code matches the class diagram given above.
2) the code uses the worker class SimpleStack, the for-loop, the while-loop.
3) the code will produce the following output (when pushing the first 10 items, use a for-loop with the header for(ch = A; ch < K; ch++); when pushing the second 10 items, you need to use the helper method isFull() in the conditional part of the for-loop, thus, the for-loop header will be for(ch = A; !stack.isFull(); ch++); the while-loop is used when popping items from the stack; when output the last paragraph, you should create a new stack instance with length of 4).
Outputs:
Demonstrate SimpleStack
Push 10 items onto a 10-element stack.
Pushing: ABCDEFGHIJ
Pop those 10 items from stack.
Popping: JIHGFEDCBA
Next, use isEmpty() and isFull() to fill and empty the stack.
Pushing: ABCDEFGHIJ
Popping: JIHGFEDCBA
Now, use a 4-element stack to generate some errors.
Pushing: 12345 --- Stack is full.
Popping: 4321 --- Stack is empty.
After debugging, cut and paste your Java source code for the class SimpleStackDemo in the space below (Note: you need to show the correctness of your code by cut-and-paste your program output):
Answer:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started