Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

A) Write a generic Java queue class (a plain queue, not a priority queue). Then, call it GenericQueue, because the JDK already has an interface called Queue. This class must be able to create a queue of objects of any reference type. Consider the GenericStack class shown below for some hints.  Like the Stack class below, the GenericQueue should use an underlying ArrayList<E>. 

 

Write these methods and any others you find useful:

enqueue() adds an E to the queue

peek() returns a reference to the object that has been in the queue the longest, without removing it from the queue

dequeue() returns the E that has been in the queue the longest, and removes it from the queue

contains(T t) returns true if the queue contains at least one object that is equal to t *in the sense that calling .equals() on the object with t the parameter returns true.* Otherwise contains returns false. 

size() and isEmpty() are obvious.

 

```java
package stackdemo;
// http://cs.fit.edu/~ryan/java/programs/generic/GenericStack-java.html
import java.util.*;

public class GenericStack<T> {
    private ArrayList<T> stack = new ArrayList<T>();
    private int top = 0;  // the list index where the next T added to the stack will be

    public int size() {
        return top;
    }

    public void push(T item) {  // this method takes a T as an argument
        stack.add(top++, item);
    }

    public T pop() {  // this method returns a T
        return stack.remove(--top);
    }
}
```

### Code Explanation:

- **Package Declaration:** 
  - `package stackdemo;` indicates that this class is part of the `stackdemo` package.
  
- **Import Statement:** 
  - `import java.util.*;` imports all utility classes from the `java.util` package, including `ArrayList`.

- **Class Definition:** 
  - `public class GenericStack<T>` declares a generic class `GenericStack` with a type parameter `T`.

- **Class Members:**
  - `private ArrayList<T> stack`: A private list to hold the stack elements of type `T`.
  - `private int top`: An integer representing the index for the next element to be added to the stack.

- **Methods:**
  - `public int size()`: Returns the current size of the stack, represented by `top`.
  - `public void push(T item)`: Adds an item to the stack and increments the `top` index.
  - `public T pop()`: Removes and returns the top element on the stack, decrementing the `top` index.
expand button
Transcribed Image Text:```java package stackdemo; // http://cs.fit.edu/~ryan/java/programs/generic/GenericStack-java.html import java.util.*; public class GenericStack<T> { private ArrayList<T> stack = new ArrayList<T>(); private int top = 0; // the list index where the next T added to the stack will be public int size() { return top; } public void push(T item) { // this method takes a T as an argument stack.add(top++, item); } public T pop() { // this method returns a T return stack.remove(--top); } } ``` ### Code Explanation: - **Package Declaration:** - `package stackdemo;` indicates that this class is part of the `stackdemo` package. - **Import Statement:** - `import java.util.*;` imports all utility classes from the `java.util` package, including `ArrayList`. - **Class Definition:** - `public class GenericStack<T>` declares a generic class `GenericStack` with a type parameter `T`. - **Class Members:** - `private ArrayList<T> stack`: A private list to hold the stack elements of type `T`. - `private int top`: An integer representing the index for the next element to be added to the stack. - **Methods:** - `public int size()`: Returns the current size of the stack, represented by `top`. - `public void push(T item)`: Adds an item to the stack and increments the `top` index. - `public T pop()`: Removes and returns the top element on the stack, decrementing the `top` index.
Expert Solution
Check Mark
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
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