SlideShare a Scribd company logo
Java Foundations
StackADT.java
/**
* Defines the interface to a stack collection.
*
* @author Java Foundations
* @version 4.0
*/
public interface StackADT<T> {
/**
* Adds the specified element to the top of this stack.
*
* @param element element to be pushed onto the stack
*/
public void push(T element);
/**
* Removes and returns the top element from this stack.
*
* @return the element removed from the stack
* @throws EmptyCollectionException if the stack is empty
*/
public T pop();
/**
* Returns the top element of this stack without removing it from the stack.
*
* @return the element on top of the stack. It is not removed from the stack
* @throws EmptyCollectionException if the stack is empty
*/
public T peek();
/**
* Returns true if this stack contains no elements.
*
* @return true if the stack is empty, false if the stack is not empty
*/
public boolean isEmpty();
/**
* Returns the number of elements in this stack.
*
* @return the number of elements in the stack
*/
public int size();
}
QueueADT.java
/**
* QueueADT defines the interface to a queue collection.
*
* @author Java Foundation
* @version 4.0
*/
public interface QueueADT<T> {
/**
* Adds one element to the rear of this queue.
*
* @param element the element to be added to the rear of the queue
*/
public void enqueue(T element);
/**
* Removes and returns the element at the front of this queue.
*
* @return the element at the front of the queue
* @throws EmptyCollectionException if the queue is empty
*/
public T dequeue();
/**
* Returns without removing the element at the front of this queue.
*
* @return the first element in the queue
* @throws EmptyCollectionException if the queue is empty
*/
public T first();
/**
* Returns true if this queue contains no elements.
*
* @return true if the queue is empty, false if the queue is not empty
*/
public boolean isEmpty();
/**
* Returns the number of elements in this queue.
*
* @return the number of elements in the queue
*/
public int size();
}
LinkedDeque.java
public class LinkedDeque<T> implements QueueADT<T>, StackADT<T>, DequeADT<T> {
// inner class for a double linked list node
private class DNode<T> {
private T element;
private DNode<T> prev, next;
}
// data fields for the LinkedDeque class
private DNode<T> front, rear;
private int size;
// deque interface methods
@Override
public void addFirst(T element) {
// create a new node and set it up
DNode<T> newNode = new DNode<T>();
newNode.element = element; // from param to new node obj
newNode.prev = newNode.next = null;
if(this.isEmpty()) {
// we are making the only node in the deque
this.rear = this.front = newNode;
} else {
// there already exists a new node
// so, put the new node before the front node
newNode.next = this.front;
this.front.prev = newNode;
this.front = newNode;
}
this.size++;
}
@Override
public T removeFirst() {
T grabbedElt = this.getFirst(); // checks for empty for us
if(this.size() == 1) {
// we are removing the only node
// so the deque is becoming empty
} else {
// there are multiple nodes in the queue
// so the rear won't change and we just mess with front end
}
this.size--;
return grabbedElt;
}
@Override
public T getFirst() {
if(this.isEmpty()) {
throw new EmptyCollectionException("LinkedDeque");
}
return this.front.element;
}
@Override
public void addLast(T element) {
}
@Override
public T removeLast() {
return null;
}
@Override
public T getLast() {
if(this.isEmpty()) {
throw new EmptyCollectionException("LinkedDeque");
}
return this.rear.element;
}
// stack interface methods
@Override
public void push(T element) {
this.addFirst(element);
}
@Override
public T pop() {
return this.removeFirst();
}
@Override
public T peek() {
return this.getFirst();
}
// queue interface methods
@Override
public void enqueue(T element) {
// TODO Auto-generated method stub
}
@Override
public T dequeue() {
// TODO Auto-generated method stub
return null;
}
@Override
public T first() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isEmpty() {
return (this.size == 0);
}
@Override
public int size() {
return this.size;
}
@Override
public String toString() {
String s = "LinkedDeque of size" + this.size() + "containing (front to back)";
for (DNode <T> curr = this.front; curr != null; curr = curr.next) {
s = s + curr.element;
if(curr.next != null) {
s = s + ", ";
} else {
s = s + ".";
}
}
}
}
EmptyCollectionException.java
/**
* Represents the situation in which a collection is empty.
*
* @author Java Foundations
* @version 4.0
*/
public class EmptyCollectionException extends RuntimeException
{
/**
* Sets up this exception with an appropriate message.
* @param collection the name of the collection
*/
public EmptyCollectionException(String collection)
{
super("The " + collection + " is empty.");
}
}
DequeADT.java
public interface DequeADT<T> {
/**
* The DequeADT (double-ended queue) provides methods for inserting, deleting
* and getting either the first or last element of a sequence of elements.
*/
/**
* Adds a new element to the head of this deque.
*
* @param element the element to insert at the head of this deque
*/
public void addFirst(T element);
/**
* Removes and returns the head element of this deque.
*
* @return the head element of this deque
* @throws EmptyCollectionException if the deque is empty
*/
public T removeFirst();
/**
* @return the head element of this deque
* @throws EmptyCollectionException if the deque is empty
*/
public T getFirst();
/**
* Adds a new element to the tail of this deque.
*
* @param element the element to insert at the tail of this deque
*/
public void addLast(T element);
/**
* Removes and returns the tail element of this deque.
*
* @return the tail element of this deque
* @throws EmptyCollectionException if the deque is empty
*/
public T removeLast();
/**
* @return the tail element of this deque
* @throws EmptyCollectionException if the deque is empty
*/
public T getLast();
/**
* Returns true if this deque contains no elements.
*
* @return true if the deque is empty, false if the deque is not empty
*/
public boolean isEmpty();
/**
* Returns the number of elements in this deque.
*
* @return the number of elements in the deque
*/
public int size();
}
have a toString() method that prints the contents of the data structure on one line. Implementing
multiple interfaces enqueue method should invoke addLast and dequeue should removeFirst).
Make sure to implement every method of all three interfaces. The Main Class You need to have a
Main class that performs the following operations. First, test the DequeADT: - Create a
DequeADT variable for subtype 'String' that contains a LinkedDeque object. - Add 10 items to
the front (all items should be unique, hint use letters ' A ', ' B '), then print the sate of the Deque. -
Remove 5 items from the back (and print the state of the Deque) - Add 5 items to the back (and
print the state of the Deque) - Perform a circular right shift 10 times - A circular right shift means
to remove an from the back, then add that same element to the front - Be sure to print out the
state of the Deque after each shift - Perform a circular left shift 10 times - A circular left shift
means to remove an from the front, then add that same element to the back - Be sure to print out
the state of the Deque after each shift - getLast() to print out the last element Then, use a Deque
as a stack using the StackADT: - Make variable of type 'StackADT', and instantiate it with a
LinkedDeque object - push() 5 elements, print out stack - pop() each element and print it out in a
while loop Then, use a Deque as a queue using the QueueADT: - Make variable of type
'QueueADT, and instantiate it with a LinkedDeque object - enqueue() 5 elements, print out
queue - dequeue( ) an element each element and print it out in a while loop
Ad

Recommended

I need help in writing the test cases of the below methods i.pdf
I need help in writing the test cases of the below methods i.pdf
adianantsolutions
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
amrishinda
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
aromalcom
 
public static class LinkedDequeT implements DequeADTT{    priv.pdf
public static class LinkedDequeT implements DequeADTT{    priv.pdf
amaresh6333
 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdf
kisgstin23
 
module2a it is module 2 so it is module 2.pptx
module2a it is module 2 so it is module 2.pptx
bharath555tth
 
public class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdf
annaipowerelectronic
 
A generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdf
ADITIHERBAL
 
A generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdf
adhityafashion
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
arpitaeron555
 
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
vtunali
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
JUSTSTYLISH3B2MOHALI
 
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
WilliamZnlMarshallc
 
Educational slides by venay magen
Educational slides by venay magen
venaymagen19
 
Stacks, Queues, Deques
Stacks, Queues, Deques
A-Tech and Software Development
 
Stacks
Stacks
sardorbek mamazhanov
 
Stacks
Stacks
Temperory mukesh
 
Collection implementation classes - Arraylist, linkedlist, Stack
Collection implementation classes - Arraylist, linkedlist, Stack
RajalakshmiS74
 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
saradashata
 
2 b queues
2 b queues
Nguync91368
 
Lec3
Lec3
Anjneya Varshney
 
Data Structure - Stack.pptx
Data Structure - Stack.pptx
MarlonMagtibay2
 
stacks and queues class 12 in c++
stacks and queues class 12 in c++
Khushal Mehta
 
Module-1 Updated Collection Framework.pptx
Module-1 Updated Collection Framework.pptx
rekhakeerti19
 
Description (Part A) In this lab you will write a Queue implementati.pdf
Description (Part A) In this lab you will write a Queue implementati.pdf
rishabjain5053
 
stack and queue array implementation, java.
stack and queue array implementation, java.
CIIT Atd.
 
stack and queue array implementation in java.
stack and queue array implementation in java.
CIIT Atd.
 
23 stacks-queues-deques
23 stacks-queues-deques
Rishabh Jindal
 
Look at situations such as radiological- nuclear accidents- technologi.docx
Look at situations such as radiological- nuclear accidents- technologi.docx
VictorXUQGloverl
 
Learning Activity 16-1- Innate Immune Defenses in Different Cases List.docx
Learning Activity 16-1- Innate Immune Defenses in Different Cases List.docx
VictorXUQGloverl
 

More Related Content

Similar to Java Foundations StackADT-java --- - Defines the interface to a stack.docx (20)

A generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdf
adhityafashion
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
arpitaeron555
 
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
vtunali
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
JUSTSTYLISH3B2MOHALI
 
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
WilliamZnlMarshallc
 
Educational slides by venay magen
Educational slides by venay magen
venaymagen19
 
Stacks, Queues, Deques
Stacks, Queues, Deques
A-Tech and Software Development
 
Stacks
Stacks
sardorbek mamazhanov
 
Stacks
Stacks
Temperory mukesh
 
Collection implementation classes - Arraylist, linkedlist, Stack
Collection implementation classes - Arraylist, linkedlist, Stack
RajalakshmiS74
 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
saradashata
 
2 b queues
2 b queues
Nguync91368
 
Lec3
Lec3
Anjneya Varshney
 
Data Structure - Stack.pptx
Data Structure - Stack.pptx
MarlonMagtibay2
 
stacks and queues class 12 in c++
stacks and queues class 12 in c++
Khushal Mehta
 
Module-1 Updated Collection Framework.pptx
Module-1 Updated Collection Framework.pptx
rekhakeerti19
 
Description (Part A) In this lab you will write a Queue implementati.pdf
Description (Part A) In this lab you will write a Queue implementati.pdf
rishabjain5053
 
stack and queue array implementation, java.
stack and queue array implementation, java.
CIIT Atd.
 
stack and queue array implementation in java.
stack and queue array implementation in java.
CIIT Atd.
 
23 stacks-queues-deques
23 stacks-queues-deques
Rishabh Jindal
 
A generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdf
adhityafashion
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
arpitaeron555
 
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
vtunali
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
JUSTSTYLISH3B2MOHALI
 
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
WilliamZnlMarshallc
 
Educational slides by venay magen
Educational slides by venay magen
venaymagen19
 
Collection implementation classes - Arraylist, linkedlist, Stack
Collection implementation classes - Arraylist, linkedlist, Stack
RajalakshmiS74
 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
saradashata
 
Data Structure - Stack.pptx
Data Structure - Stack.pptx
MarlonMagtibay2
 
stacks and queues class 12 in c++
stacks and queues class 12 in c++
Khushal Mehta
 
Module-1 Updated Collection Framework.pptx
Module-1 Updated Collection Framework.pptx
rekhakeerti19
 
Description (Part A) In this lab you will write a Queue implementati.pdf
Description (Part A) In this lab you will write a Queue implementati.pdf
rishabjain5053
 
stack and queue array implementation, java.
stack and queue array implementation, java.
CIIT Atd.
 
stack and queue array implementation in java.
stack and queue array implementation in java.
CIIT Atd.
 
23 stacks-queues-deques
23 stacks-queues-deques
Rishabh Jindal
 

More from VictorXUQGloverl (20)

Look at situations such as radiological- nuclear accidents- technologi.docx
Look at situations such as radiological- nuclear accidents- technologi.docx
VictorXUQGloverl
 
Learning Activity 16-1- Innate Immune Defenses in Different Cases List.docx
Learning Activity 16-1- Innate Immune Defenses in Different Cases List.docx
VictorXUQGloverl
 
List and describe the 5 characteristics of epithelial tissues.docx
List and describe the 5 characteristics of epithelial tissues.docx
VictorXUQGloverl
 
LO32 Identify the differences between transcription and translation LO.docx
LO32 Identify the differences between transcription and translation LO.docx
VictorXUQGloverl
 
List + describe the 3 types of skin burns.docx
List + describe the 3 types of skin burns.docx
VictorXUQGloverl
 
List and describe the modes of secretion + give examples where used.docx
List and describe the modes of secretion + give examples where used.docx
VictorXUQGloverl
 
lim had to give a presentail- Planning phase Analysis phase Design pha.docx
lim had to give a presentail- Planning phase Analysis phase Design pha.docx
VictorXUQGloverl
 
Let- if - the event of cutbres a hat Iet N - the tient of fertre a fec.docx
Let- if - the event of cutbres a hat Iet N - the tient of fertre a fec.docx
VictorXUQGloverl
 
Let Y be a continuous random variable with density fY- where Y might t.docx
Let Y be a continuous random variable with density fY- where Y might t.docx
VictorXUQGloverl
 
Let X1- - - - - Xn be a random sample from an uniform distribution U(0.docx
Let X1- - - - - Xn be a random sample from an uniform distribution U(0.docx
VictorXUQGloverl
 
Let X be a normal random variable with mean 201 units and standard dev.docx
Let X be a normal random variable with mean 201 units and standard dev.docx
VictorXUQGloverl
 
Let f be a maximum flow on a network G from vertex s to vertex t- and.docx
Let f be a maximum flow on a network G from vertex s to vertex t- and.docx
VictorXUQGloverl
 
Leaving an employee alone as long as they are performing satisfactoril.docx
Leaving an employee alone as long as they are performing satisfactoril.docx
VictorXUQGloverl
 
Last question- You conduct the Gram stain on two cultures of a Gram+ o.docx
Last question- You conduct the Gram stain on two cultures of a Gram+ o.docx
VictorXUQGloverl
 
Label- F BB W T Figure 24-1.docx
Label- F BB W T Figure 24-1.docx
VictorXUQGloverl
 
Lab 4-Protists 8- Two silica valves Traits 9- Lobe-shaped pseudopodia.docx
Lab 4-Protists 8- Two silica valves Traits 9- Lobe-shaped pseudopodia.docx
VictorXUQGloverl
 
John's boss knows that he has taken purchasing courses at Sault Colleg.docx
John's boss knows that he has taken purchasing courses at Sault Colleg.docx
VictorXUQGloverl
 
Lab1-3 Step by step algorithm- Assembly implementation fuction- (nuirk.docx
Lab1-3 Step by step algorithm- Assembly implementation fuction- (nuirk.docx
VictorXUQGloverl
 
Label the following diagram to describe the overall equation of cellul.docx
Label the following diagram to describe the overall equation of cellul.docx
VictorXUQGloverl
 
Lab 4- Protists Traits 8- Two silica valves 9- Lobe shaped preudopodia.docx
Lab 4- Protists Traits 8- Two silica valves 9- Lobe shaped preudopodia.docx
VictorXUQGloverl
 
Look at situations such as radiological- nuclear accidents- technologi.docx
Look at situations such as radiological- nuclear accidents- technologi.docx
VictorXUQGloverl
 
Learning Activity 16-1- Innate Immune Defenses in Different Cases List.docx
Learning Activity 16-1- Innate Immune Defenses in Different Cases List.docx
VictorXUQGloverl
 
List and describe the 5 characteristics of epithelial tissues.docx
List and describe the 5 characteristics of epithelial tissues.docx
VictorXUQGloverl
 
LO32 Identify the differences between transcription and translation LO.docx
LO32 Identify the differences between transcription and translation LO.docx
VictorXUQGloverl
 
List + describe the 3 types of skin burns.docx
List + describe the 3 types of skin burns.docx
VictorXUQGloverl
 
List and describe the modes of secretion + give examples where used.docx
List and describe the modes of secretion + give examples where used.docx
VictorXUQGloverl
 
lim had to give a presentail- Planning phase Analysis phase Design pha.docx
lim had to give a presentail- Planning phase Analysis phase Design pha.docx
VictorXUQGloverl
 
Let- if - the event of cutbres a hat Iet N - the tient of fertre a fec.docx
Let- if - the event of cutbres a hat Iet N - the tient of fertre a fec.docx
VictorXUQGloverl
 
Let Y be a continuous random variable with density fY- where Y might t.docx
Let Y be a continuous random variable with density fY- where Y might t.docx
VictorXUQGloverl
 
Let X1- - - - - Xn be a random sample from an uniform distribution U(0.docx
Let X1- - - - - Xn be a random sample from an uniform distribution U(0.docx
VictorXUQGloverl
 
Let X be a normal random variable with mean 201 units and standard dev.docx
Let X be a normal random variable with mean 201 units and standard dev.docx
VictorXUQGloverl
 
Let f be a maximum flow on a network G from vertex s to vertex t- and.docx
Let f be a maximum flow on a network G from vertex s to vertex t- and.docx
VictorXUQGloverl
 
Leaving an employee alone as long as they are performing satisfactoril.docx
Leaving an employee alone as long as they are performing satisfactoril.docx
VictorXUQGloverl
 
Last question- You conduct the Gram stain on two cultures of a Gram+ o.docx
Last question- You conduct the Gram stain on two cultures of a Gram+ o.docx
VictorXUQGloverl
 
Label- F BB W T Figure 24-1.docx
Label- F BB W T Figure 24-1.docx
VictorXUQGloverl
 
Lab 4-Protists 8- Two silica valves Traits 9- Lobe-shaped pseudopodia.docx
Lab 4-Protists 8- Two silica valves Traits 9- Lobe-shaped pseudopodia.docx
VictorXUQGloverl
 
John's boss knows that he has taken purchasing courses at Sault Colleg.docx
John's boss knows that he has taken purchasing courses at Sault Colleg.docx
VictorXUQGloverl
 
Lab1-3 Step by step algorithm- Assembly implementation fuction- (nuirk.docx
Lab1-3 Step by step algorithm- Assembly implementation fuction- (nuirk.docx
VictorXUQGloverl
 
Label the following diagram to describe the overall equation of cellul.docx
Label the following diagram to describe the overall equation of cellul.docx
VictorXUQGloverl
 
Lab 4- Protists Traits 8- Two silica valves 9- Lobe shaped preudopodia.docx
Lab 4- Protists Traits 8- Two silica valves 9- Lobe shaped preudopodia.docx
VictorXUQGloverl
 
Ad

Recently uploaded (20)

2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Ad

Java Foundations StackADT-java --- - Defines the interface to a stack.docx

  • 1. Java Foundations StackADT.java /** * Defines the interface to a stack collection. * * @author Java Foundations * @version 4.0 */ public interface StackADT<T> { /** * Adds the specified element to the top of this stack. * * @param element element to be pushed onto the stack */ public void push(T element); /** * Removes and returns the top element from this stack. * * @return the element removed from the stack * @throws EmptyCollectionException if the stack is empty */ public T pop(); /** * Returns the top element of this stack without removing it from the stack. * * @return the element on top of the stack. It is not removed from the stack * @throws EmptyCollectionException if the stack is empty */ public T peek(); /** * Returns true if this stack contains no elements. * * @return true if the stack is empty, false if the stack is not empty */ public boolean isEmpty(); /** * Returns the number of elements in this stack. * * @return the number of elements in the stack
  • 2. */ public int size(); } QueueADT.java /** * QueueADT defines the interface to a queue collection. * * @author Java Foundation * @version 4.0 */ public interface QueueADT<T> { /** * Adds one element to the rear of this queue. * * @param element the element to be added to the rear of the queue */ public void enqueue(T element); /** * Removes and returns the element at the front of this queue. * * @return the element at the front of the queue * @throws EmptyCollectionException if the queue is empty */ public T dequeue(); /** * Returns without removing the element at the front of this queue. * * @return the first element in the queue * @throws EmptyCollectionException if the queue is empty */ public T first(); /** * Returns true if this queue contains no elements. * * @return true if the queue is empty, false if the queue is not empty */ public boolean isEmpty(); /** * Returns the number of elements in this queue. *
  • 3. * @return the number of elements in the queue */ public int size(); } LinkedDeque.java public class LinkedDeque<T> implements QueueADT<T>, StackADT<T>, DequeADT<T> { // inner class for a double linked list node private class DNode<T> { private T element; private DNode<T> prev, next; } // data fields for the LinkedDeque class private DNode<T> front, rear; private int size; // deque interface methods @Override public void addFirst(T element) { // create a new node and set it up DNode<T> newNode = new DNode<T>(); newNode.element = element; // from param to new node obj newNode.prev = newNode.next = null; if(this.isEmpty()) { // we are making the only node in the deque this.rear = this.front = newNode; } else { // there already exists a new node // so, put the new node before the front node newNode.next = this.front; this.front.prev = newNode; this.front = newNode; } this.size++; } @Override public T removeFirst() { T grabbedElt = this.getFirst(); // checks for empty for us
  • 4. if(this.size() == 1) { // we are removing the only node // so the deque is becoming empty } else { // there are multiple nodes in the queue // so the rear won't change and we just mess with front end } this.size--; return grabbedElt; } @Override public T getFirst() { if(this.isEmpty()) { throw new EmptyCollectionException("LinkedDeque"); } return this.front.element; } @Override public void addLast(T element) { } @Override public T removeLast() { return null; } @Override public T getLast() { if(this.isEmpty()) { throw new EmptyCollectionException("LinkedDeque"); } return this.rear.element; } // stack interface methods @Override public void push(T element) {
  • 5. this.addFirst(element); } @Override public T pop() { return this.removeFirst(); } @Override public T peek() { return this.getFirst(); } // queue interface methods @Override public void enqueue(T element) { // TODO Auto-generated method stub } @Override public T dequeue() { // TODO Auto-generated method stub return null; } @Override public T first() { // TODO Auto-generated method stub return null; } @Override public boolean isEmpty() { return (this.size == 0); } @Override public int size() { return this.size; } @Override public String toString() { String s = "LinkedDeque of size" + this.size() + "containing (front to back)";
  • 6. for (DNode <T> curr = this.front; curr != null; curr = curr.next) { s = s + curr.element; if(curr.next != null) { s = s + ", "; } else { s = s + "."; } } } } EmptyCollectionException.java /** * Represents the situation in which a collection is empty. * * @author Java Foundations * @version 4.0 */ public class EmptyCollectionException extends RuntimeException { /** * Sets up this exception with an appropriate message. * @param collection the name of the collection */ public EmptyCollectionException(String collection) { super("The " + collection + " is empty."); } } DequeADT.java public interface DequeADT<T> { /** * The DequeADT (double-ended queue) provides methods for inserting, deleting * and getting either the first or last element of a sequence of elements. */ /** * Adds a new element to the head of this deque. * * @param element the element to insert at the head of this deque */ public void addFirst(T element);
  • 7. /** * Removes and returns the head element of this deque. * * @return the head element of this deque * @throws EmptyCollectionException if the deque is empty */ public T removeFirst(); /** * @return the head element of this deque * @throws EmptyCollectionException if the deque is empty */ public T getFirst(); /** * Adds a new element to the tail of this deque. * * @param element the element to insert at the tail of this deque */ public void addLast(T element); /** * Removes and returns the tail element of this deque. * * @return the tail element of this deque * @throws EmptyCollectionException if the deque is empty */ public T removeLast(); /** * @return the tail element of this deque * @throws EmptyCollectionException if the deque is empty */ public T getLast(); /** * Returns true if this deque contains no elements. * * @return true if the deque is empty, false if the deque is not empty */ public boolean isEmpty(); /** * Returns the number of elements in this deque. * * @return the number of elements in the deque
  • 8. */ public int size(); } have a toString() method that prints the contents of the data structure on one line. Implementing multiple interfaces enqueue method should invoke addLast and dequeue should removeFirst). Make sure to implement every method of all three interfaces. The Main Class You need to have a Main class that performs the following operations. First, test the DequeADT: - Create a DequeADT variable for subtype 'String' that contains a LinkedDeque object. - Add 10 items to the front (all items should be unique, hint use letters ' A ', ' B '), then print the sate of the Deque. - Remove 5 items from the back (and print the state of the Deque) - Add 5 items to the back (and print the state of the Deque) - Perform a circular right shift 10 times - A circular right shift means to remove an from the back, then add that same element to the front - Be sure to print out the state of the Deque after each shift - Perform a circular left shift 10 times - A circular left shift means to remove an from the front, then add that same element to the back - Be sure to print out the state of the Deque after each shift - getLast() to print out the last element Then, use a Deque as a stack using the StackADT: - Make variable of type 'StackADT', and instantiate it with a LinkedDeque object - push() 5 elements, print out stack - pop() each element and print it out in a while loop Then, use a Deque as a queue using the QueueADT: - Make variable of type 'QueueADT, and instantiate it with a LinkedDeque object - enqueue() 5 elements, print out queue - dequeue( ) an element each element and print it out in a while loop