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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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)
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
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education