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 are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.
Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…
What did you find most interesting or surprising about the scientist Lavoiser?

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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
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