Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ICS 141: Programming with Objects Fall 2019 Assignment 2: Problem solving using Loops Due: February 12th, 2020 Total points: 40 Objective To write a program

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

ICS 141: Programming with Objects Fall 2019 Assignment 2: Problem solving using Loops Due: February 12th, 2020 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 maing, 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 SimpleStatcko 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 maing 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. SimpleStackint size) { data = new char[sizely create the array to hold the stack tos = 0; // Push a character onto the stack. void pushchar ch) { if(isFully) { 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!) { I finish the method according the spec specified later. } // You are asked to finish this method. boolean is Full! { 1_ 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 Simple Stack "); // 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

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions