Docsity

Computer Programming II - Sequence Laboratory 2 | CSC 220, Lab Reports of Computer Science

Toggle sidebar
bg1
Lecture 6
CSC220: Programming II (Data Structures) - Fall 2002
Dr. Christian A. Duncan
1 Announcements
๎˜€
Read Chapter 4 by next Tuesday.
๎˜€
Quiz this Thursday (on Chapters 1 and 2)
๎˜€
Lab 2 due this Wednesday (at midnight).
๎˜€
Lab 3 due next Wednesday has been posted.
๎˜€
Weโ€™ll start class by going overthe implementation details of some more methods in the IntArrayBag Class.
2 Sequence class
The sequence class is very much like the bag class but now the order (or sequence) of the values is important. This
does not mean they are sorted, that is another class. It means they are arranged in a given order. For this version, the
sequence will be stored using an array of doubles. Hence, the name of the class is DoubleArraySeq. Since this
class is one of your programming assignments, we shall go over some of the methods (but not all) briefly. All (but a
few) of the methods are described in more detail in Section 3.3 (page 133-146). And the program stub (API) from the
assignment has the method declarations for all of them. In particular you will need the following:
๎˜€
Constructors: Default (size 10), and one with initial capacity given. Same as with IntArrayBag.
๎˜€
size(): How many elements are in the sequence.
๎˜€
Examining the sequence:
public void start(): Make current point to the start of the sequence
public double getCurrent(): Return the value at the current position in the sequence
public void advance(): Move to the next element in the sequence.
public boolean isCurrent(): Is there a current position? This is useful for cases when the advance
goes beyond the end of the sequence.
๎˜€
Adding to the sequence:
public void addBefore(double element): Add element to the sequence before the current posi-
tion.
public void addAfter(double element): Add element to the sequence after the current position.
๎˜€
removeCurrent(): Remove the current element from the sequence.
๎˜€
Merging sequences
void addAll(DoubleArraySeq addend): Adds the addend to the end of the (this) sequence.
public static DoubleArraySeq concatenation(DoubleArraySeq s1, DoubleArraySeq
s2):
Combines the two sequences together into a new sequence... very similar to the union() method of IntArray-
Bag.
๎˜€
Cloning: Using clone(), to make an exact duplicate of the sequence.
Basically, the code is standard for this and I believe even included in the source stub.
Note this is not all of them nor did I mention all the details like pre-condition and post-condition here. But we
shall scan over most of them in class.
1
pf3

Partial preview of the text

Download Computer Programming II - Sequence Laboratory 2 | CSC 220 and more Lab Reports Computer Science in PDF only on Docsity!

Lecture 6

CSC220: Programming II (Data Structures) - Fall 2002 Dr. Christian A. Duncan [email protected]

1 Announcements

Read Chapter 4 by next Tuesday.

Quiz this Thursday (on Chapters 1 and 2)

Lab 2 due this Wednesday (at midnight).

Lab 3 due next Wednesday has been posted.

Weโ€™ll start class by going over the implementation details of some more methods in the IntArrayBag Class.

2 Sequence class

The sequence class is very much like the bag class but now the order (or sequence ) of the values is important. This does not mean they are sorted, that is another class. It means they are arranged in a given order. For this version, the sequence will be stored using an array of doubles. Hence, the name of the class is DoubleArraySeq. Since this class is one of your programming assignments, we shall go over some of the methods (but not all) briefly. All (but a few) of the methods are described in more detail in Section 3.3 (page 133-146). And the program stub (API) from the assignment has the method declarations for all of them. In particular you will need the following:

Constructors: Default (size 10), and one with initial capacity given. Same as with IntArrayBag.

size(): How many elements are in the sequence.

Examining the sequence: public void start(): Make current point to the start of the sequence public double getCurrent(): Return the value at the current position in the sequence public void advance(): Move to the next element in the sequence. public boolean isCurrent(): Is there a current position? This is useful for cases when the advance goes beyond the end of the sequence. Adding to the sequence: public void addBefore(double element): Add element to the sequence before the current posi- tion. public void addAfter(double element): Add element to the sequence after the current position. removeCurrent(): Remove the current element from the sequence.

Merging sequences void addAll(DoubleArraySeq addend): Adds the addend to the end of the (this) sequence. public static DoubleArraySeq concatenation(DoubleArraySeq s1, DoubleArraySeq s2): Combines the two sequences together into a new sequence... very similar to the union() method of IntArray- Bag. Cloning: Using clone(), to make an exact duplicate of the sequence. Basically, the code is standard for this and I believe even included in the source stub.

Note this is not all of them nor did I mention all the details like pre-condition and post-condition here. But we shall scan over most of them in class.

3 Applets for Interactive Testing

For those that remember how to do GUIs, we can also do applets for writing testers but they are often a bit time consuming although the end result is much nicer... graphical! So, we will skip it for now, though I encourage everyone to read the section. We will come back to it at a later time as a break from regular data structures.

4 Linked Lists

Until now, we have been focusing our data structure development using arrays. It is time to learn the next generic building block, the linked list. Linked lists are as versatile to use as an array. They are a bit harder to work with initially. But with practice they will become, maybe not your best friend in programming, but a nice buddy to have around. I prefer a slightly different structure to the linked list class but to avoid any confusion we shall use the books presentation, as the key ideas are still there. A linked list is a sequence of elements connected to each other by โ€œlinksโ€. You can imagine it as forming a chain of elements. The general organization of a linked list is to group the element and link together into one structure (or class) called a node. Since the book deals with integers, we shall first look at a linked list of integers. In this case, you would define an IntNode which would look something like:

public class IntNode { private int data; private IntNode link; // methods go here... }

The biggest the thing to notice here is that the link variable is of type IntNode, which is the current class being defined! How is this possible? Well, java doesnโ€™t care too much because it only really is interested in the fact that link stores a reference to some other object. When you use it, then it cares why type of object that really is. If I had time, Iโ€™d draw a simple figure illustrating the linked list structure graphically but for now look at Figure 4.1 (page 172) and copy one from the board when we go over it in lecture. That simple class along with many methods gives you a lot of power. There are several approaches to creating a linked list. We shall now look at one. First, we need a start of our sequence. The head of the sequence is usually identified by a variable called: IntNode head Remember head is not the actual node. It is a reference to the first node! It is also often useful to know the end of the sequence. In this case, we have the IntNode tail. Since every node has a link to another node, how do you identify the last node? What does it store a link to? For a standard linked list, that link would be simply null, meaning it does not point to anything.

4.1 Reminder: NullPointer Exceptions

Just a friendly reminder as the book also points it out. What happens with the following bit of code?

String text; text = null; System.out.println(text.length());

Always be careful when dereferencing (aka accessing) a variable of value null. If you try to do that, a NullPoint- erException will be thrown and if not caught (as is most likely) the program will crash. Another warning, although I havenโ€™t seen it with newer versions, one old java implementation actually core dumped when this happened. Java should always exit cleanly either by displaying the exception that was thrown or by ending normally at the end of the program. If it core dumps, there is a bug in the java implementation but it probably means you also have an error in your program as well. I donโ€™t think this will happen for us but in case it does be aware of it.