Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 19, Problem 19.1PE

(Revising Listing 19.1) Revise the GenericStack class in Listing 19.1 to implement it using an array rather than an ArrayList. You should check the array size before adding a new element to the stack. If the array is full, create a new array that doubles the current array size and copy the elements front the current array to the new array.

Expert Solution & Answer
Check Mark
Program Plan Intro

Revising Listing 19.1

Program Plan:

  • Define the class named “GenericStack<E>”.
    • Declare appropriate variables to program.
    • Define constructors “GenericStack()” which assigns initial size of stack and “GenericStack(initialCapaciy)” which assigns size of the stack.
    • Define a method “push()” which pass “E o” as parameter.
      • Using “if” condition, check the value of “N” greater than “elements.length”.
        • Assign size of new array.
        • Copy old array into new array.
      • Return array elements.
    • Define a method “pop()” which pop out stack elements.
      • Return array elements.
    • Define a method “peek()” which returns top of the stack.
    • Define a method “isEmpty()” which returns “0”.
    • Define a method “getSize()” which returns size of the stack.
    • Define the main method.
      • Declare the GenericStack object “obj”, using the object insert elements into stack.
      • Using “while” loop, print the elements on screen.
Program Description Answer

The following JAVA code is to revise the “GenericStack” class which implements array rather than an “ArrayList”.

Explanation of Solution

Program:

//Class definition

class GenericStack<E>

{

/*Declaration of variables*/

public final static int INITIAL_SIZE = 16;

private E[] elements;

private int N;

/*Construct a stack with the default initial capacity */

public GenericStack()

{

//Assign initial size

this(INITIAL_SIZE);

}

/*Construct a stack with the specified initial capacity */

public GenericStack(int initialCapacity)

{

//Assign size

elements = (E[])new Object[initialCapacity];

}

/*Push a new element into the top of the stack */

public E push(E o)

{

//Condition

if (N >= elements.length)

{

//Assign array size into variable "t"

E[] t = (E[])new Object[elements.length * 2];

//Copy array elements

System.arraycopy(elements, 0, t, 0, elements.length);

//Assign "t" into "elements"

elements = t;

}

//Return statement

return elements[N++] = o;

}

/*Return and remove the top element from the stack*/

public E pop()

{

//Return statement

return elements[--N];

}

/*Return the top element from the stack */

public E peek()

{

//Return statement

return elements[N - 1];

}

/*Function definition to check empty*/

public boolean isEmpty()

{

//Return statement

return N == 0;

}

/*Return the number of elements in the stack */

public int getSize()

{

//Return statement

return N;

}

//Main method

public static void main(String[] args)

{

//Assign object to "GenericStack"

GenericStack<String> obj = new GenericStack<>();

//Push elements into stack

obj.push("London");

obj.push("Paris");

obj.push("Berlin");

//Print elements

System.out.print("Stack 1: ");

//Loop

while (!obj.isEmpty())

{

//Print statement

System.out.print(obj.pop() + " ");

}

//Print statement

System.out.println("\n");

}

}

Sample Output

Stack 1: Berlin Paris London

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
What transpires when a parameter for an array is sent in with either the ref or out keyword, and the results are observed?
Try using the ref or out keyword to pass in an array argument and see what happens.
What happens when you use either the ref or out keyword with an array parameter and see what happens?

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar 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
1.1 Arrays in Data Structure | Declaration, Initialization, Memory representation; Author: Jenny's lectures CS/IT NET&JRF;https://www.youtube.com/watch?v=AT14lCXuMKI;License: Standard YouTube License, CC-BY
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License