SlideShare a Scribd company logo
Introduction and Background
In recent lectures we discussed using arrays, classes and interfaces (see newly added course
notes if you want to read ahead about interfaces – we will cover them this week in lecture). In
this lab you will utilize all of these topics to build a simple yet useful new class. Consider the
following interface describing the methods for a simple double ended queue (or deque):
public interface SimpleDeque
{
public void addFront(Object X); // Add Object X at front of list
public void addRear(Object X); // Add Object X at rear of list
// If array is full, add methods should do nothing
public Object removeFront(); // Remove and return Object X from
// front of list
public Object removeRear(); // Remove and return Object X from
// rear of list
// If array is empty, remove methods should return null
public boolean isEmpty(); // Return true if the list is empty
// Return false otherwise
}
A queue has the behavior such that items are added at the rear and removed from the front,
thereby giving a First In First Out (FIFO) access to the items added and subsequently removed
from the list. No other manipulations of the data are permitted (for example, we cannot add or
remove anywhere in the middle). Looking at it "in reverse", we could add new items at the
front of the queue and remove them from the rear. This is still providing FIFO access, but just
from a different point of view. Now consider both adding and removing items at the rear of the
list (without ever accessing the front). This is called stack access and gives us Last In First Out
(LIFO) access to the items (the data is removed in reverse order). The same behavior occurs if
we both add and remove at the front without ever accessing the rear of the list.
The simple deque above is expressed as an interface rather than a class, because we are not
describing the data or how it is represented -- we are simply describing its access behavior.
However, to actually build a working deque, we need a class that implements the interface
above. For example:
public class MyDeque implements SimpleDeque
{
Object [] theData;
int numItems;
public MyDeque(int maxItems)
{
theData = new Object[maxItems];
numItems = 0;
}
// Implementation of the five methods of SimpleDeque, plus
// perhaps other methods as well
}
Note that the implementation above uses an array of Object to store the items in the deque. Since
Object is the base class to all other Java classes, an array of Object can thus be used to store any
Java class types (we can even store primitive values if we utilize their wrapper classes). Also
note that nothing in the SimpleDeque interface requires an array to be used to store the data. You
will see in your CS 0445 course that a linked list may in fact be a better implementation than an
array in this case. However, for this implementation we will use an array because it is simple and
easy to understand.
Another important thing to notice about the partial implementation above is that the size of the
array used is not equal to the number of items in the deque. The number of items in the deque is
maintained in the separate int variable numItems. Since Java array sizes are fixed once the array
object is created, to avoid having to recreate new array objects with each add or removal we
simply allocate an array that is some reasonable size (specified by the parameter in the
constructor) when we create the deque. At that time we also set numItems to zero since there are
no actual items in the deque. We then increment numItems with each addition and decrement
numItems with each removal. This is the same maintenance technique used in the MovieDB
class of Lab 7. As we discussed in lecture, and as you did in Assignment 3, when the array fills
we could copy the data into a new, larger array. However, in this case, to keep things simple, we
will not resize the array and simply not allow any new items to be added once the array fills.
Adding or removing at the rear of the array is a relatively simple process -- to add we simply put
the new object in location numItems and then increment numItems. To remove we store the last
item in a temp object, decrement numItems and then return the item. It is probably a good idea to
also set the location back to null before returning.
Adding or removing at the front of the array is a bit more complicated. For this simple
implementation we will do it in the following way:
addFront -- move objects in locations 0..numItems-1 over one spot to "the right" (i.e. into
locations 1..numItems), then put the new object into location 0 and increment numItems. For
example, given the array below of length 9 with 6 items in it, doing an addFront of 25 will have
the effect shown in the three lines below:
0
1
2
3
4
5
6
7
8
50
30
10
40
20
80
50
30
10
40
20
80
25
50
30
10
40
20
80
removeFront -- store the object in location 0 in a temp variable, then move objects in locations
1..numItems-1 over one spot to "the left" (i.e. into locations 0..numItems-2). Set location
numItems-1 to null, decrement numItems and return the temp object. In effect, you are doing the
opposite of what is shown in the addFront above.
Note that the addFront and removeFront methods as described above are not implemented in the
most efficient manner. If you take CS 0445 you will likely discuss better ways of implementing
these methods. Also note the special cases for inserting into a full list (do nothing in this case)
and for removing from an empty list (return null in this case).
Program
For this lab you will complete the MyDeque class so that it works with the simple test program
Lab9.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab9.java). An outline of MyDeque.java
(http://people.cs.pitt.edu/~ramirez/cs401/labs/MyDeque.java) is provided for you to complete.
Make sure you handle the special cases shown. You will also need SimpleDeque.java
(http://people.cs.pitt.edu/~ramirez/cs401/labs/SimpleDeque.java), in which the interface is
defined. Download all three files onto your account through the links provided or through the
AFS file system. Then complete MyDeque.java, and compile and run Lab9.java so that it works
correctly. The output of your program should be:
Lab9> java Lab9
Stack adds and removes at rear
Marge Ingmar Ingrid Bertha Herb
Queue adds at rear and removes at front
0 1 2 3 4
Queue adds at front and removes at rear
0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0
Grading and Submission
Demonstrate that your MyDeque class works correctly by running the Lab9.java program for
your TA. Be ready to show your code to your TA as well.
There will be 4 raw points for this lab, broken down in the following way.
addFront() method – 1 point
removeFront() method – 1 point
addRear() method – 1 point
removeRear() method – 1 point
Note that your program must work with the provided driver program in order to receive credit for
these items. The 4 raw points will be normalized to give this lab equal weight to the other labs in
the CS 401 course.
Notes and Hints
Shifting the items in the array to allow the addFront() and removeFront() operations is not hard,
but it IS tricky. I recommend tracing it with a pencil and paper before coding it in your class. Be
especially careful with the order that you move the items.
Introduction and Background
In recent lectures we discussed using arrays, classes and interfaces (see newly added course
notes if you want to read ahead about interfaces – we will cover them this week in lecture). In
this lab you will utilize all of these topics to build a simple yet useful new class. Consider the
following interface describing the methods for a simple double ended queue (or deque):
public interface SimpleDeque
{
public void addFront(Object X); // Add Object X at front of list
public void addRear(Object X); // Add Object X at rear of list
// If array is full, add methods should do nothing
public Object removeFront(); // Remove and return Object X from
// front of list
public Object removeRear(); // Remove and return Object X from
// rear of list
// If array is empty, remove methods should return null
public boolean isEmpty(); // Return true if the list is empty
// Return false otherwise
}
A queue has the behavior such that items are added at the rear and removed from the front,
thereby giving a First In First Out (FIFO) access to the items added and subsequently removed
from the list. No other manipulations of the data are permitted (for example, we cannot add or
remove anywhere in the middle). Looking at it "in reverse", we could add new items at the
front of the queue and remove them from the rear. This is still providing FIFO access, but just
from a different point of view. Now consider both adding and removing items at the rear of the
list (without ever accessing the front). This is called stack access and gives us Last In First Out
(LIFO) access to the items (the data is removed in reverse order). The same behavior occurs if
we both add and remove at the front without ever accessing the rear of the list.
The simple deque above is expressed as an interface rather than a class, because we are not
describing the data or how it is represented -- we are simply describing its access behavior.
However, to actually build a working deque, we need a class that implements the interface
above. For example:
public class MyDeque implements SimpleDeque
{
Object [] theData;
int numItems;
public MyDeque(int maxItems)
{
theData = new Object[maxItems];
numItems = 0;
}
// Implementation of the five methods of SimpleDeque, plus
// perhaps other methods as well
}
Note that the implementation above uses an array of Object to store the items in the deque. Since
Object is the base class to all other Java classes, an array of Object can thus be used to store any
Java class types (we can even store primitive values if we utilize their wrapper classes). Also
note that nothing in the SimpleDeque interface requires an array to be used to store the data. You
will see in your CS 0445 course that a linked list may in fact be a better implementation than an
array in this case. However, for this implementation we will use an array because it is simple and
easy to understand.
Another important thing to notice about the partial implementation above is that the size of the
array used is not equal to the number of items in the deque. The number of items in the deque is
maintained in the separate int variable numItems. Since Java array sizes are fixed once the array
object is created, to avoid having to recreate new array objects with each add or removal we
simply allocate an array that is some reasonable size (specified by the parameter in the
constructor) when we create the deque. At that time we also set numItems to zero since there are
no actual items in the deque. We then increment numItems with each addition and decrement
numItems with each removal. This is the same maintenance technique used in the MovieDB
class of Lab 7. As we discussed in lecture, and as you did in Assignment 3, when the array fills
we could copy the data into a new, larger array. However, in this case, to keep things simple, we
will not resize the array and simply not allow any new items to be added once the array fills.
Adding or removing at the rear of the array is a relatively simple process -- to add we simply put
the new object in location numItems and then increment numItems. To remove we store the last
item in a temp object, decrement numItems and then return the item. It is probably a good idea to
also set the location back to null before returning.
Adding or removing at the front of the array is a bit more complicated. For this simple
implementation we will do it in the following way:
addFront -- move objects in locations 0..numItems-1 over one spot to "the right" (i.e. into
locations 1..numItems), then put the new object into location 0 and increment numItems. For
example, given the array below of length 9 with 6 items in it, doing an addFront of 25 will have
the effect shown in the three lines below:
0
1
2
3
4
5
6
7
8
50
30
10
40
20
80
50
30
10
40
20
80
25
50
30
10
40
20
80
removeFront -- store the object in location 0 in a temp variable, then move objects in locations
1..numItems-1 over one spot to "the left" (i.e. into locations 0..numItems-2). Set location
numItems-1 to null, decrement numItems and return the temp object. In effect, you are doing the
opposite of what is shown in the addFront above.
Note that the addFront and removeFront methods as described above are not implemented in the
most efficient manner. If you take CS 0445 you will likely discuss better ways of implementing
these methods. Also note the special cases for inserting into a full list (do nothing in this case)
and for removing from an empty list (return null in this case).
Program
For this lab you will complete the MyDeque class so that it works with the simple test program
Lab9.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab9.java). An outline of MyDeque.java
(http://people.cs.pitt.edu/~ramirez/cs401/labs/MyDeque.java) is provided for you to complete.
Make sure you handle the special cases shown. You will also need SimpleDeque.java
(http://people.cs.pitt.edu/~ramirez/cs401/labs/SimpleDeque.java), in which the interface is
defined. Download all three files onto your account through the links provided or through the
AFS file system. Then complete MyDeque.java, and compile and run Lab9.java so that it works
correctly. The output of your program should be:
Lab9> java Lab9
Stack adds and removes at rear
Marge Ingmar Ingrid Bertha Herb
Queue adds at rear and removes at front
0 1 2 3 4
Queue adds at front and removes at rear
0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0
Grading and Submission
Demonstrate that your MyDeque class works correctly by running the Lab9.java program for
your TA. Be ready to show your code to your TA as well.
There will be 4 raw points for this lab, broken down in the following way.
addFront() method – 1 point
removeFront() method – 1 point
addRear() method – 1 point
removeRear() method – 1 point
Note that your program must work with the provided driver program in order to receive credit for
these items. The 4 raw points will be normalized to give this lab equal weight to the other labs in
the CS 401 course.
Notes and Hints
Shifting the items in the array to allow the addFront() and removeFront() operations is not hard,
but it IS tricky. I recommend tracing it with a pencil and paper before coding it in your class. Be
especially careful with the order that you move the items.
Introduction and Background
In recent lectures we discussed using arrays, classes and interfaces (see newly added course
notes if you want to read ahead about interfaces – we will cover them this week in lecture). In
this lab you will utilize all of these topics to build a simple yet useful new class. Consider the
following interface describing the methods for a simple double ended queue (or deque):
public interface SimpleDeque
{
public void addFront(Object X); // Add Object X at front of list
public void addRear(Object X); // Add Object X at rear of list
// If array is full, add methods should do nothing
public Object removeFront(); // Remove and return Object X from
// front of list
public Object removeRear(); // Remove and return Object X from
// rear of list
// If array is empty, remove methods should return null
public boolean isEmpty(); // Return true if the list is empty
// Return false otherwise
}
A queue has the behavior such that items are added at the rear and removed from the front,
thereby giving a First In First Out (FIFO) access to the items added and subsequently removed
from the list. No other manipulations of the data are permitted (for example, we cannot add or
remove anywhere in the middle). Looking at it "in reverse", we could add new items at the
front of the queue and remove them from the rear. This is still providing FIFO access, but just
from a different point of view. Now consider both adding and removing items at the rear of the
list (without ever accessing the front). This is called stack access and gives us Last In First Out
(LIFO) access to the items (the data is removed in reverse order). The same behavior occurs if
we both add and remove at the front without ever accessing the rear of the list.
The simple deque above is expressed as an interface rather than a class, because we are not
describing the data or how it is represented -- we are simply describing its access behavior.
However, to actually build a working deque, we need a class that implements the interface
above. For example:
public class MyDeque implements SimpleDeque
{
Object [] theData;
int numItems;
public MyDeque(int maxItems)
{
theData = new Object[maxItems];
numItems = 0;
}
// Implementation of the five methods of SimpleDeque, plus
// perhaps other methods as well
}
Note that the implementation above uses an array of Object to store the items in the deque. Since
Object is the base class to all other Java classes, an array of Object can thus be used to store any
Java class types (we can even store primitive values if we utilize their wrapper classes). Also
note that nothing in the SimpleDeque interface requires an array to be used to store the data. You
will see in your CS 0445 course that a linked list may in fact be a better implementation than an
array in this case. However, for this implementation we will use an array because it is simple and
easy to understand.
Another important thing to notice about the partial implementation above is that the size of the
array used is not equal to the number of items in the deque. The number of items in the deque is
maintained in the separate int variable numItems. Since Java array sizes are fixed once the array
object is created, to avoid having to recreate new array objects with each add or removal we
simply allocate an array that is some reasonable size (specified by the parameter in the
constructor) when we create the deque. At that time we also set numItems to zero since there are
no actual items in the deque. We then increment numItems with each addition and decrement
numItems with each removal. This is the same maintenance technique used in the MovieDB
class of Lab 7. As we discussed in lecture, and as you did in Assignment 3, when the array fills
we could copy the data into a new, larger array. However, in this case, to keep things simple, we
will not resize the array and simply not allow any new items to be added once the array fills.
Adding or removing at the rear of the array is a relatively simple process -- to add we simply put
the new object in location numItems and then increment numItems. To remove we store the last
item in a temp object, decrement numItems and then return the item. It is probably a good idea to
also set the location back to null before returning.
Adding or removing at the front of the array is a bit more complicated. For this simple
implementation we will do it in the following way:
addFront -- move objects in locations 0..numItems-1 over one spot to "the right" (i.e. into
locations 1..numItems), then put the new object into location 0 and increment numItems. For
example, given the array below of length 9 with 6 items in it, doing an addFront of 25 will have
the effect shown in the three lines below:
0
1
2
3
4
5
6
7
8
50
30
10
40
20
80
50
30
10
40
20
80
25
50
30
10
40
20
80
removeFront -- store the object in location 0 in a temp variable, then move objects in locations
1..numItems-1 over one spot to "the left" (i.e. into locations 0..numItems-2). Set location
numItems-1 to null, decrement numItems and return the temp object. In effect, you are doing the
opposite of what is shown in the addFront above.
Note that the addFront and removeFront methods as described above are not implemented in the
most efficient manner. If you take CS 0445 you will likely discuss better ways of implementing
these methods. Also note the special cases for inserting into a full list (do nothing in this case)
and for removing from an empty list (return null in this case).
Program
For this lab you will complete the MyDeque class so that it works with the simple test program
Lab9.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab9.java). An outline of MyDeque.java
(http://people.cs.pitt.edu/~ramirez/cs401/labs/MyDeque.java) is provided for you to complete.
Make sure you handle the special cases shown. You will also need SimpleDeque.java
(http://people.cs.pitt.edu/~ramirez/cs401/labs/SimpleDeque.java), in which the interface is
defined. Download all three files onto your account through the links provided or through the
AFS file system. Then complete MyDeque.java, and compile and run Lab9.java so that it works
correctly. The output of your program should be:
Lab9> java Lab9
Stack adds and removes at rear
Marge Ingmar Ingrid Bertha Herb
Queue adds at rear and removes at front
0 1 2 3 4
Queue adds at front and removes at rear
0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0
Grading and Submission
Demonstrate that your MyDeque class works correctly by running the Lab9.java program for
your TA. Be ready to show your code to your TA as well.
There will be 4 raw points for this lab, broken down in the following way.
addFront() method – 1 point
removeFront() method – 1 point
addRear() method – 1 point
removeRear() method – 1 point
Note that your program must work with the provided driver program in order to receive credit for
these items. The 4 raw points will be normalized to give this lab equal weight to the other labs in
the CS 401 course.
Notes and Hints
Shifting the items in the array to allow the addFront() and removeFront() operations is not hard,
but it IS tricky. I recommend tracing it with a pencil and paper before coding it in your class. Be
especially careful with the order that you move the items.
Solution
Code:
package lab9;
public class MyDeque implements SimpleDeque
{
Object [] theData;
int numItems;
public MyDeque(int maxItems)
{
theData = new Object[maxItems];
numItems = 0;
}
public boolean isEmpty()
{
return (numItems == 0);
}
public void addFront(Object X)
{
// Add new item at front of list (shifting old items
// to right first). If the list is full, do not add
// the item (just do nothing).
if (X == null)
throw new IllegalArgumentException("item must be non-null");
System.out.println(numItems);
if (numItems == theData.length)
return; // no more room!
else {
for(int i=numItems-1;i>=0;i--)
theData[i+1]=theData[i];
theData[0]=X;
numItems++;
}
}
public void addRear(Object X)
{
// Add new item at rear of list. If the list is full,
// do not add the item (just do nothing).
if (numItems == theData.length)
return; // no more room!
else {
theData[numItems]=X;
numItems++;
}
}
public Object removeFront()
{
// Remove and return front item from list, shifting remaining
// items to the left to fill the spot. If list is empty,
// return null.
if (numItems ==0)
{
//System.out.println("Empty");
return null;// no more room!
}
else {
Object x=theData[0];
for(int i=0;i

More Related Content

Similar to Introduction and BackgroundIn recent lectures we discussed usi.pdf

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.pdfIn 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
 
Chapter11
Chapter11Chapter11
Chapter11
Rohini Sharma
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
Aijaz Ali Abro
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
mareeswari15
 
ECET 370 HELPS Education Counseling--ecet370helps.com
ECET 370 HELPS  Education Counseling--ecet370helps.comECET 370 HELPS  Education Counseling--ecet370helps.com
ECET 370 HELPS Education Counseling--ecet370helps.com
claric64
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
HusnainNaqvi2
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 
stack coding.pptx
stack coding.pptxstack coding.pptx
stack coding.pptx
JamJahanzaib
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
Rajitha Reddy Alugati
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
2 b queues
2 b queues2 b queues
2 b queues
Nguync91368
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Hassan Ahmed
 
ECET 370 HELPS Redefined Education--ecet370helps.com
ECET 370 HELPS Redefined Education--ecet370helps.comECET 370 HELPS Redefined Education--ecet370helps.com
ECET 370 HELPS Redefined Education--ecet370helps.com
GVlaxmi7
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4Bianca Teşilă
 
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.pdfJAVA 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
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 

Similar to Introduction and BackgroundIn recent lectures we discussed usi.pdf (20)

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.pdfIn 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
 
Chapter11
Chapter11Chapter11
Chapter11
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
ECET 370 HELPS Education Counseling--ecet370helps.com
ECET 370 HELPS  Education Counseling--ecet370helps.comECET 370 HELPS  Education Counseling--ecet370helps.com
ECET 370 HELPS Education Counseling--ecet370helps.com
 
Collections
CollectionsCollections
Collections
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
stack coding.pptx
stack coding.pptxstack coding.pptx
stack coding.pptx
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
2 b queues
2 b queues2 b queues
2 b queues
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
 
ECET 370 HELPS Redefined Education--ecet370helps.com
ECET 370 HELPS Redefined Education--ecet370helps.comECET 370 HELPS Redefined Education--ecet370helps.com
ECET 370 HELPS Redefined Education--ecet370helps.com
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4
 
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.pdfJAVA 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
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 

More from arpitaeron555

match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
arpitaeron555
 
Multiple-Choice Which one is a discrete variableRadius of a ball.pdf
Multiple-Choice Which one is a discrete variableRadius of a ball.pdfMultiple-Choice Which one is a discrete variableRadius of a ball.pdf
Multiple-Choice Which one is a discrete variableRadius of a ball.pdf
arpitaeron555
 
Looking for some help with this assignment. 1. Define distribut.pdf
Looking for some help with this assignment. 1. Define distribut.pdfLooking for some help with this assignment. 1. Define distribut.pdf
Looking for some help with this assignment. 1. Define distribut.pdf
arpitaeron555
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
arpitaeron555
 
Is there any information out there about decorator crabs picking up .pdf
Is there any information out there about decorator crabs picking up .pdfIs there any information out there about decorator crabs picking up .pdf
Is there any information out there about decorator crabs picking up .pdf
arpitaeron555
 
Heliconius melpomene is a species of butterfly with ZW sex determina.pdf
Heliconius melpomene is a species of butterfly with ZW sex determina.pdfHeliconius melpomene is a species of butterfly with ZW sex determina.pdf
Heliconius melpomene is a species of butterfly with ZW sex determina.pdf
arpitaeron555
 
Identify the key mechanism that determines which X remains active an.pdf
Identify the key mechanism that determines which X remains active an.pdfIdentify the key mechanism that determines which X remains active an.pdf
Identify the key mechanism that determines which X remains active an.pdf
arpitaeron555
 
For each of the given functions, list the domain and range. Also list.pdf
For each of the given functions, list the domain and range. Also list.pdfFor each of the given functions, list the domain and range. Also list.pdf
For each of the given functions, list the domain and range. Also list.pdf
arpitaeron555
 
Given to girls born in the U.S. in the years The tables below give t.pdf
Given to girls born in the U.S. in the years  The tables below give t.pdfGiven to girls born in the U.S. in the years  The tables below give t.pdf
Given to girls born in the U.S. in the years The tables below give t.pdf
arpitaeron555
 
How do open source projects work How is it different from closed sou.pdf
How do open source projects work How is it different from closed sou.pdfHow do open source projects work How is it different from closed sou.pdf
How do open source projects work How is it different from closed sou.pdf
arpitaeron555
 
Ferdinand is affected by hemophilia, a recessive x-linked condition..pdf
Ferdinand is affected by hemophilia, a recessive x-linked condition..pdfFerdinand is affected by hemophilia, a recessive x-linked condition..pdf
Ferdinand is affected by hemophilia, a recessive x-linked condition..pdf
arpitaeron555
 
Explain whether you think IFRS and GAAP will be fully converged. Wha.pdf
Explain whether you think IFRS and GAAP will be fully converged. Wha.pdfExplain whether you think IFRS and GAAP will be fully converged. Wha.pdf
Explain whether you think IFRS and GAAP will be fully converged. Wha.pdf
arpitaeron555
 
Does the following picture depict a function It so, is it a one to o.pdf
Does the following picture depict a function It so, is it a one to o.pdfDoes the following picture depict a function It so, is it a one to o.pdf
Does the following picture depict a function It so, is it a one to o.pdf
arpitaeron555
 
Do patients have health care rights that are not legal or statuatory.pdf
Do patients have health care rights that are not legal or statuatory.pdfDo patients have health care rights that are not legal or statuatory.pdf
Do patients have health care rights that are not legal or statuatory.pdf
arpitaeron555
 
Describe the authentication process in the figure below Solutio.pdf
Describe the authentication process in the figure below  Solutio.pdfDescribe the authentication process in the figure below  Solutio.pdf
Describe the authentication process in the figure below Solutio.pdf
arpitaeron555
 
Discuss the history of bureaucratic and administrative management..pdf
Discuss the history of bureaucratic and administrative management..pdfDiscuss the history of bureaucratic and administrative management..pdf
Discuss the history of bureaucratic and administrative management..pdf
arpitaeron555
 
Describe or explain how SNPs in genes at evolutionary conserved brea.pdf
Describe or explain how SNPs in genes at evolutionary conserved brea.pdfDescribe or explain how SNPs in genes at evolutionary conserved brea.pdf
Describe or explain how SNPs in genes at evolutionary conserved brea.pdf
arpitaeron555
 
Data x word 23 y word -3 Write assembly code for the following p.pdf
Data  x word 23  y word -3  Write assembly code for the following p.pdfData  x word 23  y word -3  Write assembly code for the following p.pdf
Data x word 23 y word -3 Write assembly code for the following p.pdf
arpitaeron555
 
Can Variance Inflation factors be used to detect Heteroscedasticity .pdf
Can Variance Inflation factors be used to detect Heteroscedasticity .pdfCan Variance Inflation factors be used to detect Heteroscedasticity .pdf
Can Variance Inflation factors be used to detect Heteroscedasticity .pdf
arpitaeron555
 
A study undertaken by the Miami-Dade Supervisor of Elections in 2002.pdf
A study undertaken by the Miami-Dade Supervisor of Elections in 2002.pdfA study undertaken by the Miami-Dade Supervisor of Elections in 2002.pdf
A study undertaken by the Miami-Dade Supervisor of Elections in 2002.pdf
arpitaeron555
 

More from arpitaeron555 (20)

match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
 
Multiple-Choice Which one is a discrete variableRadius of a ball.pdf
Multiple-Choice Which one is a discrete variableRadius of a ball.pdfMultiple-Choice Which one is a discrete variableRadius of a ball.pdf
Multiple-Choice Which one is a discrete variableRadius of a ball.pdf
 
Looking for some help with this assignment. 1. Define distribut.pdf
Looking for some help with this assignment. 1. Define distribut.pdfLooking for some help with this assignment. 1. Define distribut.pdf
Looking for some help with this assignment. 1. Define distribut.pdf
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
 
Is there any information out there about decorator crabs picking up .pdf
Is there any information out there about decorator crabs picking up .pdfIs there any information out there about decorator crabs picking up .pdf
Is there any information out there about decorator crabs picking up .pdf
 
Heliconius melpomene is a species of butterfly with ZW sex determina.pdf
Heliconius melpomene is a species of butterfly with ZW sex determina.pdfHeliconius melpomene is a species of butterfly with ZW sex determina.pdf
Heliconius melpomene is a species of butterfly with ZW sex determina.pdf
 
Identify the key mechanism that determines which X remains active an.pdf
Identify the key mechanism that determines which X remains active an.pdfIdentify the key mechanism that determines which X remains active an.pdf
Identify the key mechanism that determines which X remains active an.pdf
 
For each of the given functions, list the domain and range. Also list.pdf
For each of the given functions, list the domain and range. Also list.pdfFor each of the given functions, list the domain and range. Also list.pdf
For each of the given functions, list the domain and range. Also list.pdf
 
Given to girls born in the U.S. in the years The tables below give t.pdf
Given to girls born in the U.S. in the years  The tables below give t.pdfGiven to girls born in the U.S. in the years  The tables below give t.pdf
Given to girls born in the U.S. in the years The tables below give t.pdf
 
How do open source projects work How is it different from closed sou.pdf
How do open source projects work How is it different from closed sou.pdfHow do open source projects work How is it different from closed sou.pdf
How do open source projects work How is it different from closed sou.pdf
 
Ferdinand is affected by hemophilia, a recessive x-linked condition..pdf
Ferdinand is affected by hemophilia, a recessive x-linked condition..pdfFerdinand is affected by hemophilia, a recessive x-linked condition..pdf
Ferdinand is affected by hemophilia, a recessive x-linked condition..pdf
 
Explain whether you think IFRS and GAAP will be fully converged. Wha.pdf
Explain whether you think IFRS and GAAP will be fully converged. Wha.pdfExplain whether you think IFRS and GAAP will be fully converged. Wha.pdf
Explain whether you think IFRS and GAAP will be fully converged. Wha.pdf
 
Does the following picture depict a function It so, is it a one to o.pdf
Does the following picture depict a function It so, is it a one to o.pdfDoes the following picture depict a function It so, is it a one to o.pdf
Does the following picture depict a function It so, is it a one to o.pdf
 
Do patients have health care rights that are not legal or statuatory.pdf
Do patients have health care rights that are not legal or statuatory.pdfDo patients have health care rights that are not legal or statuatory.pdf
Do patients have health care rights that are not legal or statuatory.pdf
 
Describe the authentication process in the figure below Solutio.pdf
Describe the authentication process in the figure below  Solutio.pdfDescribe the authentication process in the figure below  Solutio.pdf
Describe the authentication process in the figure below Solutio.pdf
 
Discuss the history of bureaucratic and administrative management..pdf
Discuss the history of bureaucratic and administrative management..pdfDiscuss the history of bureaucratic and administrative management..pdf
Discuss the history of bureaucratic and administrative management..pdf
 
Describe or explain how SNPs in genes at evolutionary conserved brea.pdf
Describe or explain how SNPs in genes at evolutionary conserved brea.pdfDescribe or explain how SNPs in genes at evolutionary conserved brea.pdf
Describe or explain how SNPs in genes at evolutionary conserved brea.pdf
 
Data x word 23 y word -3 Write assembly code for the following p.pdf
Data  x word 23  y word -3  Write assembly code for the following p.pdfData  x word 23  y word -3  Write assembly code for the following p.pdf
Data x word 23 y word -3 Write assembly code for the following p.pdf
 
Can Variance Inflation factors be used to detect Heteroscedasticity .pdf
Can Variance Inflation factors be used to detect Heteroscedasticity .pdfCan Variance Inflation factors be used to detect Heteroscedasticity .pdf
Can Variance Inflation factors be used to detect Heteroscedasticity .pdf
 
A study undertaken by the Miami-Dade Supervisor of Elections in 2002.pdf
A study undertaken by the Miami-Dade Supervisor of Elections in 2002.pdfA study undertaken by the Miami-Dade Supervisor of Elections in 2002.pdf
A study undertaken by the Miami-Dade Supervisor of Elections in 2002.pdf
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 

Introduction and BackgroundIn recent lectures we discussed usi.pdf

  • 1. Introduction and Background In recent lectures we discussed using arrays, classes and interfaces (see newly added course notes if you want to read ahead about interfaces – we will cover them this week in lecture). In this lab you will utilize all of these topics to build a simple yet useful new class. Consider the following interface describing the methods for a simple double ended queue (or deque): public interface SimpleDeque { public void addFront(Object X); // Add Object X at front of list public void addRear(Object X); // Add Object X at rear of list // If array is full, add methods should do nothing public Object removeFront(); // Remove and return Object X from // front of list public Object removeRear(); // Remove and return Object X from // rear of list // If array is empty, remove methods should return null public boolean isEmpty(); // Return true if the list is empty // Return false otherwise
  • 2. } A queue has the behavior such that items are added at the rear and removed from the front, thereby giving a First In First Out (FIFO) access to the items added and subsequently removed from the list. No other manipulations of the data are permitted (for example, we cannot add or remove anywhere in the middle). Looking at it "in reverse", we could add new items at the front of the queue and remove them from the rear. This is still providing FIFO access, but just from a different point of view. Now consider both adding and removing items at the rear of the list (without ever accessing the front). This is called stack access and gives us Last In First Out (LIFO) access to the items (the data is removed in reverse order). The same behavior occurs if we both add and remove at the front without ever accessing the rear of the list. The simple deque above is expressed as an interface rather than a class, because we are not describing the data or how it is represented -- we are simply describing its access behavior. However, to actually build a working deque, we need a class that implements the interface above. For example: public class MyDeque implements SimpleDeque { Object [] theData; int numItems; public MyDeque(int maxItems) { theData = new Object[maxItems]; numItems = 0; } // Implementation of the five methods of SimpleDeque, plus // perhaps other methods as well
  • 3. } Note that the implementation above uses an array of Object to store the items in the deque. Since Object is the base class to all other Java classes, an array of Object can thus be used to store any Java class types (we can even store primitive values if we utilize their wrapper classes). Also note that nothing in the SimpleDeque interface requires an array to be used to store the data. You will see in your CS 0445 course that a linked list may in fact be a better implementation than an array in this case. However, for this implementation we will use an array because it is simple and easy to understand. Another important thing to notice about the partial implementation above is that the size of the array used is not equal to the number of items in the deque. The number of items in the deque is maintained in the separate int variable numItems. Since Java array sizes are fixed once the array object is created, to avoid having to recreate new array objects with each add or removal we simply allocate an array that is some reasonable size (specified by the parameter in the constructor) when we create the deque. At that time we also set numItems to zero since there are no actual items in the deque. We then increment numItems with each addition and decrement numItems with each removal. This is the same maintenance technique used in the MovieDB class of Lab 7. As we discussed in lecture, and as you did in Assignment 3, when the array fills we could copy the data into a new, larger array. However, in this case, to keep things simple, we will not resize the array and simply not allow any new items to be added once the array fills. Adding or removing at the rear of the array is a relatively simple process -- to add we simply put the new object in location numItems and then increment numItems. To remove we store the last item in a temp object, decrement numItems and then return the item. It is probably a good idea to also set the location back to null before returning. Adding or removing at the front of the array is a bit more complicated. For this simple implementation we will do it in the following way:
  • 4. addFront -- move objects in locations 0..numItems-1 over one spot to "the right" (i.e. into locations 1..numItems), then put the new object into location 0 and increment numItems. For example, given the array below of length 9 with 6 items in it, doing an addFront of 25 will have the effect shown in the three lines below: 0 1 2 3 4 5 6 7 8 50
  • 6. 80 25 50 30 10 40 20 80 removeFront -- store the object in location 0 in a temp variable, then move objects in locations 1..numItems-1 over one spot to "the left" (i.e. into locations 0..numItems-2). Set location
  • 7. numItems-1 to null, decrement numItems and return the temp object. In effect, you are doing the opposite of what is shown in the addFront above. Note that the addFront and removeFront methods as described above are not implemented in the most efficient manner. If you take CS 0445 you will likely discuss better ways of implementing these methods. Also note the special cases for inserting into a full list (do nothing in this case) and for removing from an empty list (return null in this case). Program For this lab you will complete the MyDeque class so that it works with the simple test program Lab9.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab9.java). An outline of MyDeque.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/MyDeque.java) is provided for you to complete. Make sure you handle the special cases shown. You will also need SimpleDeque.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/SimpleDeque.java), in which the interface is defined. Download all three files onto your account through the links provided or through the AFS file system. Then complete MyDeque.java, and compile and run Lab9.java so that it works correctly. The output of your program should be: Lab9> java Lab9 Stack adds and removes at rear Marge Ingmar Ingrid Bertha Herb Queue adds at rear and removes at front 0 1 2 3 4 Queue adds at front and removes at rear 0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0
  • 8. Grading and Submission Demonstrate that your MyDeque class works correctly by running the Lab9.java program for your TA. Be ready to show your code to your TA as well. There will be 4 raw points for this lab, broken down in the following way. addFront() method – 1 point removeFront() method – 1 point addRear() method – 1 point removeRear() method – 1 point Note that your program must work with the provided driver program in order to receive credit for these items. The 4 raw points will be normalized to give this lab equal weight to the other labs in the CS 401 course. Notes and Hints Shifting the items in the array to allow the addFront() and removeFront() operations is not hard, but it IS tricky. I recommend tracing it with a pencil and paper before coding it in your class. Be especially careful with the order that you move the items. Introduction and Background In recent lectures we discussed using arrays, classes and interfaces (see newly added course notes if you want to read ahead about interfaces – we will cover them this week in lecture). In this lab you will utilize all of these topics to build a simple yet useful new class. Consider the following interface describing the methods for a simple double ended queue (or deque): public interface SimpleDeque { public void addFront(Object X); // Add Object X at front of list
  • 9. public void addRear(Object X); // Add Object X at rear of list // If array is full, add methods should do nothing public Object removeFront(); // Remove and return Object X from // front of list public Object removeRear(); // Remove and return Object X from // rear of list // If array is empty, remove methods should return null public boolean isEmpty(); // Return true if the list is empty // Return false otherwise } A queue has the behavior such that items are added at the rear and removed from the front, thereby giving a First In First Out (FIFO) access to the items added and subsequently removed from the list. No other manipulations of the data are permitted (for example, we cannot add or remove anywhere in the middle). Looking at it "in reverse", we could add new items at the front of the queue and remove them from the rear. This is still providing FIFO access, but just from a different point of view. Now consider both adding and removing items at the rear of the list (without ever accessing the front). This is called stack access and gives us Last In First Out (LIFO) access to the items (the data is removed in reverse order). The same behavior occurs if we both add and remove at the front without ever accessing the rear of the list. The simple deque above is expressed as an interface rather than a class, because we are not describing the data or how it is represented -- we are simply describing its access behavior. However, to actually build a working deque, we need a class that implements the interface
  • 10. above. For example: public class MyDeque implements SimpleDeque { Object [] theData; int numItems; public MyDeque(int maxItems) { theData = new Object[maxItems]; numItems = 0; } // Implementation of the five methods of SimpleDeque, plus // perhaps other methods as well } Note that the implementation above uses an array of Object to store the items in the deque. Since Object is the base class to all other Java classes, an array of Object can thus be used to store any Java class types (we can even store primitive values if we utilize their wrapper classes). Also note that nothing in the SimpleDeque interface requires an array to be used to store the data. You will see in your CS 0445 course that a linked list may in fact be a better implementation than an array in this case. However, for this implementation we will use an array because it is simple and easy to understand. Another important thing to notice about the partial implementation above is that the size of the array used is not equal to the number of items in the deque. The number of items in the deque is maintained in the separate int variable numItems. Since Java array sizes are fixed once the array object is created, to avoid having to recreate new array objects with each add or removal we
  • 11. simply allocate an array that is some reasonable size (specified by the parameter in the constructor) when we create the deque. At that time we also set numItems to zero since there are no actual items in the deque. We then increment numItems with each addition and decrement numItems with each removal. This is the same maintenance technique used in the MovieDB class of Lab 7. As we discussed in lecture, and as you did in Assignment 3, when the array fills we could copy the data into a new, larger array. However, in this case, to keep things simple, we will not resize the array and simply not allow any new items to be added once the array fills. Adding or removing at the rear of the array is a relatively simple process -- to add we simply put the new object in location numItems and then increment numItems. To remove we store the last item in a temp object, decrement numItems and then return the item. It is probably a good idea to also set the location back to null before returning. Adding or removing at the front of the array is a bit more complicated. For this simple implementation we will do it in the following way: addFront -- move objects in locations 0..numItems-1 over one spot to "the right" (i.e. into locations 1..numItems), then put the new object into location 0 and increment numItems. For example, given the array below of length 9 with 6 items in it, doing an addFront of 25 will have the effect shown in the three lines below: 0 1 2 3
  • 14. 10 40 20 80 removeFront -- store the object in location 0 in a temp variable, then move objects in locations 1..numItems-1 over one spot to "the left" (i.e. into locations 0..numItems-2). Set location numItems-1 to null, decrement numItems and return the temp object. In effect, you are doing the opposite of what is shown in the addFront above. Note that the addFront and removeFront methods as described above are not implemented in the most efficient manner. If you take CS 0445 you will likely discuss better ways of implementing these methods. Also note the special cases for inserting into a full list (do nothing in this case) and for removing from an empty list (return null in this case). Program For this lab you will complete the MyDeque class so that it works with the simple test program Lab9.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab9.java). An outline of MyDeque.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/MyDeque.java) is provided for you to complete. Make sure you handle the special cases shown. You will also need SimpleDeque.java
  • 15. (http://people.cs.pitt.edu/~ramirez/cs401/labs/SimpleDeque.java), in which the interface is defined. Download all three files onto your account through the links provided or through the AFS file system. Then complete MyDeque.java, and compile and run Lab9.java so that it works correctly. The output of your program should be: Lab9> java Lab9 Stack adds and removes at rear Marge Ingmar Ingrid Bertha Herb Queue adds at rear and removes at front 0 1 2 3 4 Queue adds at front and removes at rear 0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0 Grading and Submission Demonstrate that your MyDeque class works correctly by running the Lab9.java program for your TA. Be ready to show your code to your TA as well. There will be 4 raw points for this lab, broken down in the following way. addFront() method – 1 point removeFront() method – 1 point addRear() method – 1 point removeRear() method – 1 point Note that your program must work with the provided driver program in order to receive credit for these items. The 4 raw points will be normalized to give this lab equal weight to the other labs in the CS 401 course. Notes and Hints
  • 16. Shifting the items in the array to allow the addFront() and removeFront() operations is not hard, but it IS tricky. I recommend tracing it with a pencil and paper before coding it in your class. Be especially careful with the order that you move the items. Introduction and Background In recent lectures we discussed using arrays, classes and interfaces (see newly added course notes if you want to read ahead about interfaces – we will cover them this week in lecture). In this lab you will utilize all of these topics to build a simple yet useful new class. Consider the following interface describing the methods for a simple double ended queue (or deque): public interface SimpleDeque { public void addFront(Object X); // Add Object X at front of list public void addRear(Object X); // Add Object X at rear of list // If array is full, add methods should do nothing public Object removeFront(); // Remove and return Object X from // front of list public Object removeRear(); // Remove and return Object X from // rear of list // If array is empty, remove methods should return null public boolean isEmpty(); // Return true if the list is empty // Return false otherwise
  • 17. } A queue has the behavior such that items are added at the rear and removed from the front, thereby giving a First In First Out (FIFO) access to the items added and subsequently removed from the list. No other manipulations of the data are permitted (for example, we cannot add or remove anywhere in the middle). Looking at it "in reverse", we could add new items at the front of the queue and remove them from the rear. This is still providing FIFO access, but just from a different point of view. Now consider both adding and removing items at the rear of the list (without ever accessing the front). This is called stack access and gives us Last In First Out (LIFO) access to the items (the data is removed in reverse order). The same behavior occurs if we both add and remove at the front without ever accessing the rear of the list. The simple deque above is expressed as an interface rather than a class, because we are not describing the data or how it is represented -- we are simply describing its access behavior. However, to actually build a working deque, we need a class that implements the interface above. For example: public class MyDeque implements SimpleDeque { Object [] theData; int numItems; public MyDeque(int maxItems) { theData = new Object[maxItems]; numItems = 0; } // Implementation of the five methods of SimpleDeque, plus // perhaps other methods as well
  • 18. } Note that the implementation above uses an array of Object to store the items in the deque. Since Object is the base class to all other Java classes, an array of Object can thus be used to store any Java class types (we can even store primitive values if we utilize their wrapper classes). Also note that nothing in the SimpleDeque interface requires an array to be used to store the data. You will see in your CS 0445 course that a linked list may in fact be a better implementation than an array in this case. However, for this implementation we will use an array because it is simple and easy to understand. Another important thing to notice about the partial implementation above is that the size of the array used is not equal to the number of items in the deque. The number of items in the deque is maintained in the separate int variable numItems. Since Java array sizes are fixed once the array object is created, to avoid having to recreate new array objects with each add or removal we simply allocate an array that is some reasonable size (specified by the parameter in the constructor) when we create the deque. At that time we also set numItems to zero since there are no actual items in the deque. We then increment numItems with each addition and decrement numItems with each removal. This is the same maintenance technique used in the MovieDB class of Lab 7. As we discussed in lecture, and as you did in Assignment 3, when the array fills we could copy the data into a new, larger array. However, in this case, to keep things simple, we will not resize the array and simply not allow any new items to be added once the array fills. Adding or removing at the rear of the array is a relatively simple process -- to add we simply put the new object in location numItems and then increment numItems. To remove we store the last item in a temp object, decrement numItems and then return the item. It is probably a good idea to also set the location back to null before returning. Adding or removing at the front of the array is a bit more complicated. For this simple implementation we will do it in the following way:
  • 19. addFront -- move objects in locations 0..numItems-1 over one spot to "the right" (i.e. into locations 1..numItems), then put the new object into location 0 and increment numItems. For example, given the array below of length 9 with 6 items in it, doing an addFront of 25 will have the effect shown in the three lines below: 0 1 2 3 4 5 6 7 8 50
  • 21. 20 80 25 50 30 10 40 20 80 removeFront -- store the object in location 0 in a temp variable, then move objects in locations
  • 22. 1..numItems-1 over one spot to "the left" (i.e. into locations 0..numItems-2). Set location numItems-1 to null, decrement numItems and return the temp object. In effect, you are doing the opposite of what is shown in the addFront above. Note that the addFront and removeFront methods as described above are not implemented in the most efficient manner. If you take CS 0445 you will likely discuss better ways of implementing these methods. Also note the special cases for inserting into a full list (do nothing in this case) and for removing from an empty list (return null in this case). Program For this lab you will complete the MyDeque class so that it works with the simple test program Lab9.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/Lab9.java). An outline of MyDeque.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/MyDeque.java) is provided for you to complete. Make sure you handle the special cases shown. You will also need SimpleDeque.java (http://people.cs.pitt.edu/~ramirez/cs401/labs/SimpleDeque.java), in which the interface is defined. Download all three files onto your account through the links provided or through the AFS file system. Then complete MyDeque.java, and compile and run Lab9.java so that it works correctly. The output of your program should be: Lab9> java Lab9 Stack adds and removes at rear Marge Ingmar Ingrid Bertha Herb Queue adds at rear and removes at front 0 1 2 3 4 Queue adds at front and removes at rear
  • 23. 0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0 Grading and Submission Demonstrate that your MyDeque class works correctly by running the Lab9.java program for your TA. Be ready to show your code to your TA as well. There will be 4 raw points for this lab, broken down in the following way. addFront() method – 1 point removeFront() method – 1 point addRear() method – 1 point removeRear() method – 1 point Note that your program must work with the provided driver program in order to receive credit for these items. The 4 raw points will be normalized to give this lab equal weight to the other labs in the CS 401 course. Notes and Hints Shifting the items in the array to allow the addFront() and removeFront() operations is not hard, but it IS tricky. I recommend tracing it with a pencil and paper before coding it in your class. Be especially careful with the order that you move the items. Solution Code: package lab9; public class MyDeque implements SimpleDeque { Object [] theData; int numItems; public MyDeque(int maxItems) { theData = new Object[maxItems]; numItems = 0;
  • 24. } public boolean isEmpty() { return (numItems == 0); } public void addFront(Object X) { // Add new item at front of list (shifting old items // to right first). If the list is full, do not add // the item (just do nothing). if (X == null) throw new IllegalArgumentException("item must be non-null"); System.out.println(numItems); if (numItems == theData.length) return; // no more room! else { for(int i=numItems-1;i>=0;i--) theData[i+1]=theData[i]; theData[0]=X; numItems++; } } public void addRear(Object X) { // Add new item at rear of list. If the list is full, // do not add the item (just do nothing). if (numItems == theData.length) return; // no more room! else { theData[numItems]=X; numItems++; } }
  • 25. public Object removeFront() { // Remove and return front item from list, shifting remaining // items to the left to fill the spot. If list is empty, // return null. if (numItems ==0) { //System.out.println("Empty"); return null;// no more room! } else { Object x=theData[0]; for(int i=0;i