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
7). (Sum ArrayList) Write the following method that returns the sum of all numbers in an ArrayList:           public static double sum(ArrayList<Double> list)
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.6 is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows:public class Node<E> { E element; Node<E> next; Node<E> previous;public Node(E e) { element = e; } }Implement a new class named TwoWayLinkedList that uses a doubly linked list to store elements. The MyLinkedList class in the text extends MyAbstractList. Define TwoWayLinkedList to extend the java.util.AbstractSequentialList class. You need to implement all the methods defined in MyLinkedList as well as the methods listIterator() and listIterator(int index). Both return an instance of java.util. ListIterator<E>. The former sets the cursor to the head of the list and the latter to the element at the specified index.
7). (Sum ArrayList) Write the following method that returns the sum of all numbers in an ArrayList:           public static double sum(ArrayList<Double> list) Note:- Please write a java code and also need an output for this program. (Also, let me know with what name file should be saved to get output)

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