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

Need help with this Java problems. Java files are attached.

Add your code to the file ArrayStack2Lab.java.

Add your tests to the main() method.

Submit ArrayStack2Lab.java.

Problem 1

Implement the method

public void display()

which displays the entries in a stack starting from the top. If the stack is empty, print "The stack

is empty".

Add the method to ArrrayStack2Lab.java. You do not need to modify StackInterface.java.

Problem 2

Implement the method

public int remove(int n)

The method removes the n top most entries for a stack . If the stack contains less than n items,

the stack becomes empty. The method returns the number of items removed.

Add the method to ArrrayStack2Lab.java. You do not need to modify StackInterface.java.

[画像:Main.java : 1 /** 2 3 4 5 */ 6- import java.util.Arrays; 7 public class ArrayStack2Lab<T> implements StackInterface<T> 8. { 9. public static void main(String[] args) { // Add you lab tests here 10 11 12 13 14 15- 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 0 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 A class of stacks whose entries are stored in an array. @author Frank M. Carrano @version 3.0 79 80 81 82 } // Problem 1 public void display() { } // Problem 2 public int remove(int n) { } private T[] stack; // array of stack entries private int topIndex; // index of top entry private static final int DEFAULT_INITIAL_CAPACITY = 50; public ArrayStack2Lab() { 44 public 45 { 46 47 48 49 50 51 52 53 54 55 56 this (DEFAULT_INITIAL_CAPACITY); } // end default constructor public { ArrayStack2Lab(int initialCapacity) // the cast is safe because the new array contains null entries @SuppressWarnings ("unchecked") T[] tempStack = (T[])new Object[initial Capacity]; stack = tempStack; topIndex = -1; } // end constructor void push(T newEntry) ensureCapacity(); top Index++; stack [topIndex] = newEntry; } // end push private void ensureCapacity() { if (topIndex == stack.length - 1) // if array is full, double size stack = Arrays.copyof(stack, 2*stack.length); } // end ensureCapacity public T peek() { T top = null; if (isEmpty()) top = tack[topIndex]; return top; } // end peek public T pop() { T top = null; if (!isEmpty()) { top = stack[topIndex]; stack [topIndex] = null; top Index--; } // end if return top; } // end pop public boolean isEmpty() { return topIndex < 0; } // end is Empty public void clear() { 83 84 85 86 87 } 88 89 } // end ArrayStack2Lab 90 for(int i = 0; i stack[i] = null; topIndex = -1; <= topIndex; ++i)]
expand button
Transcribed Image Text:Main.java : 1 /** 2 3 4 5 */ 6- import java.util.Arrays; 7 public class ArrayStack2Lab<T> implements StackInterface<T> 8. { 9. public static void main(String[] args) { // Add you lab tests here 10 11 12 13 14 15- 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 0 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 A class of stacks whose entries are stored in an array. @author Frank M. Carrano @version 3.0 79 80 81 82 } // Problem 1 public void display() { } // Problem 2 public int remove(int n) { } private T[] stack; // array of stack entries private int topIndex; // index of top entry private static final int DEFAULT_INITIAL_CAPACITY = 50; public ArrayStack2Lab() { 44 public 45 { 46 47 48 49 50 51 52 53 54 55 56 this (DEFAULT_INITIAL_CAPACITY); } // end default constructor public { ArrayStack2Lab(int initialCapacity) // the cast is safe because the new array contains null entries @SuppressWarnings ("unchecked") T[] tempStack = (T[])new Object[initial Capacity]; stack = tempStack; topIndex = -1; } // end constructor void push(T newEntry) ensureCapacity(); top Index++; stack [topIndex] = newEntry; } // end push private void ensureCapacity() { if (topIndex == stack.length - 1) // if array is full, double size stack = Arrays.copyof(stack, 2*stack.length); } // end ensureCapacity public T peek() { T top = null; if (isEmpty()) top = tack[topIndex]; return top; } // end peek public T pop() { T top = null; if (!isEmpty()) { top = stack[topIndex]; stack [topIndex] = null; top Index--; } // end if return top; } // end pop public boolean isEmpty() { return topIndex < 0; } // end is Empty public void clear() { 83 84 85 86 87 } 88 89 } // end ArrayStack2Lab 90 for(int i = 0; i stack[i] = null; topIndex = -1; <= topIndex; ++i)
[画像:onlinegdb.com/online_ · Main.java ⠀ /** 10 11 12- 13 14 15 16 17- 18 19 20 21 22- ▶ Run 23 24 O Debug しかくStop Share H Save 2 3 4 5 */ 6 public interface StackInterface<T> 7. { 8 9 An interface for the ADT stack. @author Frank M. Carrano @version 3.0 + {} Beautify /** Detects whether this stack is empty. @return true if the stack is empty */ public boolean isEmpty(); ± /** Adds a new entry to the top of this stack. @param newEntry an object to be added to the stack */ public void push(T newEntry); 90 /** Removes and returns this stack's top entry. @return either the object at the top of the stack or, if the stack is empty before the operation, null */ public T pop (); Language Java /** Retrieves this stack's top entry. @return either the object at the top of the stack or null if the stack is empty */ public T peek(); 25 26 /** Removes all entries from this stack */ 27 public void clear(); 28} // end StackInterface 29]
expand button
Transcribed Image Text:onlinegdb.com/online_ · Main.java ⠀ /** 10 11 12- 13 14 15 16 17- 18 19 20 21 22- ▶ Run 23 24 O Debug しかくStop Share H Save 2 3 4 5 */ 6 public interface StackInterface<T> 7. { 8 9 An interface for the ADT stack. @author Frank M. Carrano @version 3.0 + {} Beautify /** Detects whether this stack is empty. @return true if the stack is empty */ public boolean isEmpty(); ± /** Adds a new entry to the top of this stack. @param newEntry an object to be added to the stack */ public void push(T newEntry); 90 /** Removes and returns this stack's top entry. @return either the object at the top of the stack or, if the stack is empty before the operation, null */ public T pop (); Language Java /** Retrieves this stack's top entry. @return either the object at the top of the stack or null if the stack is empty */ public T peek(); 25 26 /** Removes all entries from this stack */ 27 public void clear(); 28} // end StackInterface 29
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
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