
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)](https://content.bartleby.com/qna-images/question/bc0ef4be-b393-4fab-a253-cbedc48af370/cba019a6-56fd-4c24-b06d-3ad306dd4728/nxxnr4t_thumbnail.jpeg)


Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

- Java Question- Convert this source code into a GUI application using JOptionPane. Make sure the output looks similar to the following picture. Thank you. import java.util.Calendar;import java.util.TimeZone;public class lab11_5{public static void main(String[] args){//menuSystem.out.println("-----------------");System.out.println("(A)laska Time");System.out.println("(C)entral Time");System.out.println("(E)astern Time");System.out.println("(H)awaii Time");System.out.println("(M)ountain Time");System.out.println("(P)acific Time");System.out.println("-----------------"); System.out.print("Enter the time zone option [A-P]: "); //inputString tz = System.console().readLine();tz = tz.toUpperCase(); //change to uppercase //get current date and timeCalendar cal = Calendar.getInstance();TimeZone.setDefault(TimeZone.getTimeZone("GMT"));System.out.println("GMT/UTC:\t" + cal.getTime()); switch (tz){case "A":tz =…arrow_forwardWrite a Java program to implement a stack using an array. The program should have methods to push an element onto the stack, pop an element from the stack, and check if the stack is empty. Additionally, implement a method to return the size of the stack. Provide an explanation of the code and its time and space complexity.arrow_forwardimport java.util.Arrays; import java.util.Random; public class Board { /** * Construct a puzzle board by beginning with a solved board and then * making a number of random moves. That some of the possible moves * don't actually change what the board looks like. * * @param moves the number of moves to make when generating the board. */ public Board(int moves) { throw new RuntimeException("Not implemented"); } The board is 5 X 5. You can add classes and imports like rand.arrow_forward
- Nedd help in this. Must be in java. make the code unique and no plagerism. write a program that prints out the lists in main after the following methods are called. removeOddLength: Takes an ArrayList of strings as a parameter and removes all of the strings that have an odd length (number of characters), from the list. Return the ArrayList to main and print. swapPairs: Takes an ArrayList and switches the order of values of strings in a pairwise fashion. Your method should switch the order of the first two values, then the next two, then the next two and so on. If the number of values in the list is odd, the method should not move the final element. For example, if the list stores [“madam”, “I”, “am”, “your”, “adam”] your method should change this lists content to [“I”, “madam”, “your”, “am”, “adam”] Return the ArrayList to main and print. intersect: Accepts two sorted ArrayList of integers as parameters and returns one containing only the elements that are found in both. For…arrow_forwardWe were given the following code to solve a Pentomino puzzle, several methods might need to be added or editted to solve the problem. The program should take in an input of 3 4 5 6 for 3x20 4x15 5x12 6x10 grids. Then output the number of correct solutions for them (respectively its 2, 368, 1010, and 2339) import java.util.ArrayList;import java.util.LinkedList;import java.util.List; public class DLX { class DataNode { DataNode L, R, U, D; ColumnNode C; public DataNode() { L = R = U = D = this; } public DataNode(ColumnNode c) { this(); C = c; } DataNode linkDown(DataNode node) { node.D = this.D; node.D.U = node; node.U = this; this.D = node; return node; } DataNode linkRight(DataNode node) { node.R = this.R; node.R.L = node; node.L = this; this.R = node; return node;…arrow_forwardThe main question is the entire page labeled Program #1, the little screenshot is for extra steps. The program has to be written in Java.arrow_forward
- StringFun.java import java.util.Scanner; // Needed for the Scanner class 2 3 /** Add a class comment and @tags 4 5 */ 6 7 public class StringFun { /** * @param args not used 8 9 10 11 12 public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter your first name: "); 13 14 15 16 17 18 System.out.print("Please enter your last name: "); 19 20 21 //Output the welcome message with name 22 23 24 //Output the length of the name 25 26 27 //Output the username 28 29 30 //Output the initials 31 32 33 //Find and output the first name with switched characters 34 //All Done! } } 35 36 37arrow_forwardWrite a java GUI program stacking Student objects of a class. Your program should have buttons for push, pop, and peek. Create a student class with these fields: name (type: String), age (type : int), major ( type: String), gpa (type: double) and gender (type:char). Use Java Stack class for implementation and use the GUI for user interaction. At run time books stored in stack should follow the last in First out (LIFO) of a stack.arrow_forwardWrite in Java What is this program's exact outputarrow_forward
- Please help with the following in Java Write a Java program create a stack with elements pushed 1, 2, 3, 4 and 5, removes the middle element. Note that you may use only push(), pop(), peek() and empty() methods of the stack.arrow_forwardJava language Use arrays in creating your class. Stack Interface (LIFO) void push(Object) Object pop() String toString() boolean isEmpty() boolean equals(Object) getIndexOf get remove private static void arrayListTests() { System.out.println("ArrayList Tests"); // todo: make more tests here ArrayList a = new ArrayList(); System.out.println("Check empty array isEmpty:" + a.isEmpty()); a.insert('B', 0); a.insert('a', 0); a.insert('t', 1); System.out.println("Check non-empty array isEmpty:" + a.isEmpty()); System.out.println(a.toString()); while (a.isEmpty() == false) { System.out.println(a.remove(0)); } // Fill over initial capacity and check that it grows for (int i = 0; i < 110; i++) { a.append(new Integer(i)); } System.out.println("Size of array after 110 adds: "+ a.size()); System.out.println("Value of last element: "+ a.get(a.size()-1)); System.out.println("Insert past end of list"); a.insert('z', 200); System.out.println("Insert negative index"); a.insert('z', -3);…arrow_forwardhow to create an interface in java that contains the following methods: boolean myPush(T element); // pushes element onto the stack; returns true if element successfully pushed, false otherwise boolean myPop(); // removes the top element of the stack; returns true if element successfully popped, false otherwise T myTop(); // if the stack is not empty returns the top element of the stack, otherwise returns null String toString(); // returns contents of the stack as a single String int size(); // return number of elements in the stack then create java file that Implements your interface that you created as a linked stack, then use the driver shown in screen shot to test. below is the code for the LLNode class public class LLNode<T> { protected LLNode<T> link; protected T info; public LLNode(T info) { this.info = info; link = null; } public void setInfo(T info) { this.info = info; } public T getInfo() { return info; } public void setLink(LLNode<T> link) {…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





