Question

In: Computer Science

Write a program that uses loops (both the for-loop and the while loop). This assignment also...

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.

  • 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

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.

Solutions

Expert Solution

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.


Related Solutions

Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
Write a program which uses a while loop to add the following numbers: 5, 52, 31,...
Write a program which uses a while loop to add the following numbers: 5, 52, 31, and 65 and output the sum of those numbers. Your output should look like this: The sum of (list the numbers) is ( give the result). //In C language Upload a screen shot of your program and the results of running the program.
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
C++ please 1. Write a do while loop that continually loops until the user enters a...
C++ please 1. Write a do while loop that continually loops until the user enters a single digit between 0 and 9. There should only be one prompt for the user to enter a number inside the loop. 2. Write a simple 4 function calculator using a switch statement. The program should switch on the operator: +, -, *, /. The user should enter two numbers (any type is fine) and then enter an operator and the program should perform...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT