Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Total points: 40 Objective To write a program that uses basic flow control constructs such as loops; solidify the knowledge about return, character, for-loop, and

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Total points: 40 Objective To write a program that uses basic flow control constructs such as loops; solidify the knowledge about return, character, for-loop, and while loop. Problem Description 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, 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. SimpleStackApp SimpleStack data: char[] tos: int main SimpleStatck push() pop) isEmpty isFullo 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 data as its underlying data structure. In the use relationship, this class plays the role of being used. 2. The SimpleStackApp Class This is the application class that contains the main) method. In the use relationship, this class plays the role of user. 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 isFullo { I finish the method according the spec specified later. List 2: partial code for SimpleStackApp (the application class) class SimpleStackApr { public static void main(String[] args) { int i; char ch; System.outprintln("Dmonstrate SimpleStack "); // 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions

Question

Describe the options and trends in management education

Answered: 1 week ago