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: 9780134700144
Author: 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
(Sort ArrayList) Write the following method that sorts an ArrayList: public static <E extends Comparable<E>> void sort(ArrayList<E> list) Write a test program that prompts the user to enter 10 integers, invokes this method to sort the numbers, and displays the numbers in increasing order. Sample Run   Enter 10 integers: 3 4 12 7 3 4 5 6 4 7 The sorted numbers are 3 3 4 4 4 5 6 7 7 12 Class Name: Exercise19_09
5. Now examine the for-loops in the above Java program. (assume that we have the have replaced the array with an ArrayList) a) Which ArrayList method should you use to control the for-loop iterations? b) Explain why the first for-loop cannot be controlled by that ArrayList method (but the other two for-loops can). Here is a quick review of how array elements are modified and accessed: 1. public class BasicArray{ 2. public static void main(String[] args) int[] basic = new int[4]; for (int i=0; i
apartmentList is a pointer to the first node in a linked list of nodes. Use this for Parts a, b, and c. Part a. Create an inner class that will hold the following information: the apartment number of the apartment (an int), the number of bedrooms in the apartment (also an int), the current rent of the apartment (a double). Don't forget to include a field for the link! Just create the class itself and the instance variables; you don't need to put any methods into the class.

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
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
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