Ics 141 Programming Objects Fall 2019 Assignment 2 Problem Solving Using Loops Due Februar Q43875331

![SimpleStackApp SimpleStack data: char[] tos: int main SimpleStatcko push popО isEmpty isFullo 1. The SimpleStack Class The ma](https://media.cheggcdn.com/media/37c/37cf90e2-3fbd-4e63-bd14-04f847a5b5f7/phpB91gp1.png)
![if(isFully) { System.out.println( - -- Stack is full.); return; data[tos] = ch; tos++; // Pop a character from the stack. c](https://media.cheggcdn.com/media/294/29450324-d4bd-486d-9c47-b2322aed81d0/phpkS58a0.png)



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 Stackn”); // 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(cb); 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 pep); System.out.print(ch); System.out.println(“nnNext, use isEmpty!) and isFullO” + “to fill and empty the stack.”); // Push the letters until the stack is full. System.out.print(“Pushing); for ch = ‘A’; !stack.isEull; ch++) { System.out.print(ch); stack.push(cb); System.out.println(); // Now, pop the characters off the stack until it is empty. System.out.print(“Popping ; while stackisEmpty() { ch = stack pep); System.out.print(ch); System.out.println(“nnNow, 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. The code will produce the output shown in “Required Work” bullet 3. 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 isEullo in the conditional part of the for- loop, thus, the for-loop header will be “for(ch = ‘A’; !stack.isFullo; cb++)”; 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. Requirements • Comment your code. At the top of the program include your name, a brief description of the program and what it does and the due date. The program must be written in Java and submitted via D2L. The code matches the class diagram given above. The code uses the worker class SimpleStack, the for-loop, the while-loop. • Grading Your grade in this assignment depends on the following: • Your submission meets all the requirements as described above. The program is robust with no runtime errors or problems; it produces outputs that Meet the requirement. You follow the good programming practices as discussed in class (check document named “codingconventions”) You follow the below submission instructions. Required Work To get points, you need to finish the required work outlined in this session (note: for the implementation of 2 methods (isEmpty!, isEullo), you are not required to use loops): 1. Flesh out the method “isEmpty” in the class SimpleStack (10 pts) // 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 and the method should return true. Otherwise, the stack is not empty and the method should return false. tips: use the instance variable tos to test whether the stack is empty or not. CUT AND PASTE THE SOURSE CODE FOR THIS METHOD IN THE SPCE BELOW TO SCORE YOUR POINTS: 2. Flesh out the method “isFullo” in the class SimpleStack (10 pts) // precondition: no // postcondition: no boolean is Fullo: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 and the method should return true. Otherwise, the stack is not full and method should return false. 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 class’s 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. CUT AND PASTE THE SOURSE CODE FOR THIS METHOD IN THE SPCE BELOW TO SCORE YOUR POINTS: 3. Supply the missing code in the class SimpleStackApp (10 pts) There are some missing codes in the class SimpleStackApp. You are required to supply these missing codes. The expected outputs are: Outputs: Demonstrate SimpleStack Now, use a 4-element stack to generate some errors. Pushing: 12345 — Stack is full. Popping: 4321 … Stack is empty. 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 above. CUT AND PASTE THE SOURSE CODE FOR IN THE SPCE BELOW TO SCORE YOUR POINTS: 4. Cut and paste (or screen capture) your Program output in the space below (10 pts) The output shown in the previous section missed some outputs. In this section, you should supply the missing information and give the complete output. CUT AND PASTE THE COMPLETE PROGRAM OUTPUT IN THE SPCE BELOW TO SCORE YOUR POINTS: Show transcribed image text 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 Stackn”); // 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
Expert Answer
Answer to ICS 141: Programming with Objects Fall 2019 Assignment 2: Problem solving using Loops Due: February 12th, 2020 Total poi…
OR