Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
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
Your answers normally have 50 words. Less than 50 words will not get marks. 1. What is context switch between multiple processes? [2 marks] 2. Draw the memory layout for a C program. [2 marks] 3. How many states does a process has? [2 marks] 4. Compare the non-preemptitve scheduling and preemptive scheduling. [2 marks] 5. Given 4 process and their arrival times and next CPU burst times, what are the average times and average Turnaround time, for different scheduling algorithms including: a. First Come, First-Served (FCFS) Scheduling [2 marks] b. Shortest-Job-First (SJF) Scheduling [2 marks] c. Shortest-remaining-time-first [2 marks] d. Priority Scheduling [2 marks] e. Round Robin (RR) [2 marks] Process Arrival Time Burst Time P1 0 8 P2 1 9 P3 3 2 P4 5 4
a database with multiple tables from attributes as shown above that are in 3NF, showing PK, non-key attributes, and FK for each table? Assume the tables are already in 1NF. [Hint: 3 tables will result after deducing 1NF -> 2NF -> 3NF]
a database with multiple tables from attributes as shown above that are in 3NF, showing PK, non-key attributes, and FK for each table? Assume the tables are already in 1NF. [Hint: 3 tables will result after deducing 1NF -> 2NF -> 3NF]

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