Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a stack interface which includes the following methods: isEmpty() push(Obj

ID: 3724103 • Letter: W

Question

Write a stack interface which includes the following methods:

isEmpty()

push(Object newItem

pop();

popAll();

peek();

display();

Write a class named Astack to implement the above interface.

Utilizing the java.util.Queue, which is a subtype of java.util.Collection interface. It is an ordered list of objects with its use limited to inserting elements at the end of list and deleting elements from the start of list i.e. it follows FIFO principle. Since it is an interface, we need a concrete class during its declaration. There are many ways to initialize a Queue object, most common being As a LinkedList Operations on Queue :

Add()-Adds an element at the tail of queue. More specifically, at the last of linkedlist if it is used.

peek()-To view the head of queue without removing it. Returns null if queue is empty.

element()-Similar to peek(). Throws NoSuchElementException if queue is empty.

remove()-Removes and returns the head of the queue. Throws NoSuchElementException when queue is impty.

poll()-Removes and returns the head of the queue. Returns null if queue is empty.

Therefore, inside your Astack class, your first statement should be:

Queue<Object> queue = new LinkedList<Object>();

Then, you can proceed to implement your interface.

Write a class named StackTest.java to test your stack.

Push ten customers into your stack, display your stack.

Pop three times then display your stack.

Peek your stack and display the peeked item

PopAll you stack and then display your stack.

Explanation / Answer

HI

Thanks for chegging

Astack.java

package stackPackage;

import java.util.LinkedList;

import java.util.Queue;

public class Astack {

private Queue<Customer> queue = new LinkedList<Customer>();

private Queue<Customer> queue2 = new LinkedList<>();

private Customer top;

public void push(Customer e){

queue.add(e);

top=e;

}

public void pop() {

while (queue.size() > 1) {

top = queue.remove();

queue2.add(top);

}

queue.remove();

Queue<Customer> temp = queue;

queue = queue2;

queue2 = temp;

System.out.println("Pop of Customer is done");

}

public void popAll(){

queue.removeAll(queue);

System.out.println("Size of Queue" + queue.size());

}

public boolean Isempty() {

return queue.isEmpty();

  

}

// Get the top element.

public Customer top() {

return top;

}

public void peek(){

System.out.print(queue.peek().c_LastName);

System.out.println(" "+queue.peek().c_Name);

}

public void display(){

if(queue.size()==0){

System.out.println("Your Stack is Empty");

}

else{

for(Customer cust: queue){

System.out.print(cust.c_LastName);

System.out.println(" "+cust.c_Name);

}

}

}

}

customer.java

package stackPackage;

public class Customer {

public Customer(String name,String lastName){

this.c_Name=name;

this.c_LastName=lastName;

}

String c_Name;

String c_LastName;

}

StackTest.java

package stackPackage;

import java.util.Stack;

public class StackTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

Astack aStackInterface=new Astack();

System.out.println("Pushing 10 Customer Objects : ");

for(int i=0;i<10;i++){

Customer cust=new Customer("name"+i,"Lastname"+i);

aStackInterface.push(cust);

}

System.out.println("Display Stack");

aStackInterface.display();

System.out.println("Pop three times : ");

aStackInterface.pop();

aStackInterface.pop();

aStackInterface.pop();

System.out.println("Peek Stack and Display Peeked Item : ");

aStackInterface.peek();

System.out.println(aStackInterface.Isempty());

System.out.println("Pop All the stack");

aStackInterface.popAll();

System.out.println("Display Stack");

aStackInterface.display();

System.out.println(aStackInterface.Isempty());

}

}

Result:

Pushing 10 Customer Objects :

Display Stack

Lastname0 name0

Lastname1 name1

Lastname2 name2

Lastname3 name3

Lastname4 name4

Lastname5 name5

Lastname6 name6

Lastname7 name7

Lastname8 name8

Lastname9 name9

Pop three times :

Pop of Customer is done

Pop of Customer is done

Pop of Customer is done

Peek Stack and Display Peeked Item :

Lastname0 name0

false

Pop All the stack

Size of Queue0

Display Stack

Your Stack is Empty

true

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote