Download Interfaces Meeting Software Specifications

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Array data structure wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Red–black tree wikipedia , lookup

Quadtree wikipedia , lookup

Interval tree wikipedia , lookup

Binary search tree wikipedia , lookup

Linked list wikipedia , lookup

B-tree wikipedia , lookup

Transcript
ICS 4U1 – Unit 2
Data Structures
Name: _________________________
Interfaces
Meeting Software Specifications
Oftentimes in software design, a programmer generalizes a class based on the data it stores
and the operations on that data. This is called an abstract data type (ADT).
For instance, a common ADT is the Set ADT. Although it has many implementations, it is
essentially a collection of values with the following operations:
- an operation to create an empty set (constructor)
- add(x): an operation that adds the element x to the set
- remove(x): an operation that removes the element x, if it exists, from the set
- size(): an operation that returns the number of elements in the set
- hasItem(x): an operation that returns true if and only if the set contains the element x
Notice that none of the above operations make any assumptions about how the Set is
implemented. This is important – an ADT does not specify how it is implemented, but only the
behaviour.
ADTs in Java
Java specifies ADTs using the interface keyword. The standard template for an
interface is as follows:
public interface iSet {
public void add(Object item);
public void remove(Object item);
public int size();
public boolean hasItem(Object item);
}; // Note that this line ends with a semi-colon!
The above ADT specifies the Set ADT. There is no implementation of the class in an
interface. Think of this like a specification -- it is up to another programmer to
implement this interface.
ICS 4U1 – Unit 2
Data Structures
Name: _________________________
The template for implementing an interface is as follows:
public class MySet implements iSet {
public void add(Object item)
{
return;
}
public void remove(Object item)
{
return;
}
public int size()
{
return 0;
}
public boolean hasItem(Object item)
{
return false; }
} // Note that this line no longer ends with a semi-colon!
It should be clear that the above class is not a correct implementation!
Linked List: An Alternative to Arrays
At this point, any ADT specifying a collection of elements will likely be implemented using an
array. However, there’s a major downfall – what happens when the array is full?
To get around this, another option is to create a data structure that allows a dynamic size. A
common solution is the linked list.
A linked list consists of objects called Nodes. Each node carries one piece of data called an item
(similar to one entry in an array) as well as a reference next to its neighbour Node.
For instance, the following array:
99
46
78
18
<empty> <empty> <empty>
could be stored as nodes in the following way:
Node
Node
Node
Node
item: 99
next:
item: 46
next:
item: 78
next:
item: 18
next:null
A reference to the first node is required. Subsequent nodes are accessed using the next
member variable. The last node has a next value of null to signify the end of the list.
ICS 4U1 – Unit 2
Data Structures
Name: _________________________
A template for the Node class is shown below for your reference:
class Node
{
// Member variables.
private Object data; // May be any type you’d like.
private Node next;
public Node(Object obj)
{
this.data = obj;
this.next = null;
}
// Record my data!
// Set next neighbour to be null.
// Sets the ‘next’ neighbouring node equal to nextNode
public void setNext(Node nextNode)
{ ... }
// Sets the ‘item’ equal to the parameter specified.
public void setItem(Object newItem)
{ ... }
// Returns a reference to the next node.
public Node getNext()
{ ... }
// Returns this node’s item.
public Object getItem() { ... }
}
Assignment – Interfaces
Complete the first question in a Word document. Don’t forget to submit to Google Drive!
1. Determine whether the following are ADTs. Remember the definition – a data space along
with operations on that data.
a) a library book organization system that allows the addition, sign-out, and removal of titles
b) a contact list stored as arrays of strings and phones numbers with the ability to add, edit or
remove contacts
c) an iterator that allows you to iterate (cycle) through a set of objects
e.g. sifting through webpage search results, iterating through the items attached to a video
game character’s backpack, etc.
d) a set of objects
ICS 4U1 – Unit 2
Data Structures
Name: _________________________
2. Complete the implementation of Node.java as shown above. You are responsible for testing
your code.
3. The Queue ADT is a collection of elements arranged in order of arrival (similar to a line-up at
a grocery store). The operations are as follows:
enqueue(Object o): adds o to the end of the queue
dequeue(): returns the object at the front of the queue
The Queue interface is provided on the website. Implement it using either arrays or nodes as
you see fit. Save your implementation as MyQueue.java.