Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 20, Problem 1FTE

Explanation of Solution

Program purpose:

The given program is for implementing stack using an array.

Compile-time error:

An error which occurs on the source code of a program when user violate the rules of writing syntax are called as compile-time error. If the code fails to prove its language syntax format, then the compiler will throw an error.

Error in the given code:

The flow of execution never reaches the statement top--; The execution of code will never reach the line written after the return statement. So the top--; should give before return statement to get affected.

Correct statement:

//Decrementing the value of top by 1

top--;

//returning the value at position top-1 in the stack

return s[top];

Corrected code:

/*An array implementation of a stack

function for popping a value from the stack.*/

int pop()

{

/*checks whether the value of top is equal to zero*/

        if (top == 0)

/*if the value of top is equal to zero function throws an exception to indicate the stack is empty*/

            throw new EmptyStackException( );

...

Blurred answer
Students have asked these similar questions
don't use others answers java 1. Write a generic static method that takes a Stack of any type element as a parameter, pops each element from the stack, and prints it. It should have a type parameter that represents the Stack’s element type.
Determine the stack's efficacy when allowed to be who it is.
1. Write a generic static method that takes a Stack of any type element as a parameter, pops each element from the stack, and prints it. It should have a type parameter that represents the Stack's element type.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning