





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for implementing the intvector class, a specialized vector for storing integers, and the queuecircular class, a circular queue, in java. The intvector class is based on an array representation, and the queuecircular class uses a mutable linked list. Constructor methods and instance methods for both classes, as well as notes and examples for implementation.
Typology: Assignments
1 / 9
This page cannot be seen from the preview
Don't miss anything!






CS230 Data Structures Handout # 17 Prof. Lyn Turbak February 26, 2007 Wellesley College Revised March 2, 2007
This is the final draft of PS3, containing both problems.
Exam 1 Notice: The first exam (a take-home exam) will be posted on the evening of Wed. Mar. 7 and will be due at 6pm on Fri. Mar. 16. This is a hard deadline. No extensions will be given after this time. The exam will cover the material in class through Lecture 9 (Mon. Mar. 5), Lab 6 (Tue. Mar. 6), and Problem Sets 1 – 3. Because you should focus on the exam after it is posted, it is strongly recommended that you submit PS3 on time (11:59pm Wednesday, March 7).
Overview: The purpose of this assignment is to give you practice with implementing abstract data types (ADTs). As usual, it is recommended that you (1) start early and (2) work with a partner (a different one than you worked with on PS1 and PS2).
Submission: Each team should turn in a single hardcopy submission packet for all problems by slipping it under Lyn’s office door by 1:30pm on the due date. The packet should include:
Each team should also submit a single softcopy (consisting of your entire final ps3 directory) to the drop directory ~cs230/drop/ps3/username, where username is the username of one of the team members (indicate which drop folder you used on your hardcopy header sheet). To do this, execute the following Linux commands in the team member’s account where the code is stored:
cd /students/username/cs cp -R ps3 ~cs230/drop/ps3/username/
Problem 1 [50]: An Array Implementation of Integer Vectors
The IntVector class We have seen that the Java Vector class is a versatile data structure handy for a wide range of uses. However, unlike arrays, the slots of a Java Vector instance may not directly hold values of primitive data types like int, double, and char. Instead, primitive data must be packaged in an instance of a wrapper class (e.g., Integer, Double, Character) before they are stored in a vector, and must be unpackaged upon extraction. This is both cumbersome for the programmer and inefficient for execution.^1 In this problem, we will explore the implementation of an IntVector class is a specialized kind of vector that can store only integers. The IntVector class has the same kinds of methods as the Vector
The elts instance variable points to an integer array that stores the elements of the vector. The number of slots in the array (known as the capacity of the vector) can be any number greater than or equal to the number of elements in the vector. The size instance variable indicates the number of slots (from left to right, starting at slot 0) that contain the elements of the vector. The remaining slots are extra space for the vector to “grow into”. These extra slots may contain any integers whatsoever; their values do not matter because they are ignored by the IntVector methods. We shall refer to the values of these extra slots as “garbage”. In the above example, the size of 3 indicates that only slots 0 through 2 hold vector elements and slots 3 and 4 hold garbage. We will assume that as long as the number of elements in the vector remains less than or equal to the capacity, the array held by the elts variable does not change. In this case, vector operations that change, add, or remove elements must rearrange the elements within the slots of the existing array. For instance, performing v.add(-9) and then v.add(2,230) on the above vector representation should change the state of the objects as follows, where the fact that the array label has not changed indicates that it is the same array as in the previous diagram.
(^1) Java 1.5 has a so-called autoboxing feature (see http://java.sun.com/j2se/1.5.0/docs/guide/language/ autoboxing.html) that can automatically perform the packaging and unpackaging for the programmer, thus making wrapper classes less cumbersome. However, the wrapping is still performed, so the inefficiency still remains.
// Instance Methods public boolean add (int elt); // Note: always returns true. public void add (int index, int elt); public int capacity(); public void ensureCapacity (int minCapacity); public int get (int index); public int indexOf (int elt); public int remove (int index); public void removeAllElements(); public boolean removeElement (int elt); public int set (int index, int elt); public int size ();
Implementing the IntVector Class Your task in this problem is to implement the IntVector class using the “extensible array” rep- resentation discussed above. You should flesh out the skeletons for the above constructor methods and instance methods in the IntVector class in the file ~/cs230/ps3/IntVector.java. Invoking java IntVector tests your implementation on a suite of test cases. The test cases will be run on vectors whose (initialCapacity, capacityIncrement are (1,1), (2,8), (3,0), and (0,0).
Notes
throw new ArrayIndexOutOfBoundsException(message );
where message is a string indicating the specific error message.
Problem 2 [50]: Circular Queues In Lecture 9, we saw that a queue can be efficiently represented as a mutable list as long as it maintains two pointers to the list: a front pointer to the first node of the list (where elements are dequeued) and a back pointer to the last node of the list (where elements are enqueued). In this problem, we shall see that it is possible for a queue representation to use just a single back pointer if we represent the queue as a circular list (a.k.a. a cyclic list) – i.e., a list that wraps back on itself. For instance, in this representation, enqueuing A, B, and C in order onto an initially empty queue would lead to a sequence structures depicted by the box-and-pointer diagrams in Fig. 1.
QueueCircular back •
QueueCircular back A
QueueCircular back A^ B
QueueCircular back A^ B^ C
Figure 1: Enqueuing the elements A, B, and C onto an initially empty circular queue.
In the diagrams, the elements stored in the heads of the nodes never change. However, the tails of nodes are rewired to give the depicted structure. In all but the empty queue case, the back instance variable of a QueueCircular instance holds a pointer to the back node of the queue (the node holding the most recently enqueued element). This facilitates enqueueing a new back node as well as dequeuing the front (least recently enqueued) node, which is always the tail of the back node.
You are welcome to use either of the above two strategies in your size, copy, and toString methods. You can use different strategies for different methods if you like.
The MList
public static String toString (); Returns a string representation of this list, in which parentheses delimit the comma-separated string representations of this list’s elements.
public static Iterator
public static boolean equals (Object x); If x is an MList instance, returns true if it has the same length as this list and each element is equal (via the equals() instance method) to the corresponding element of this list. Otherwise returns false.
Most of the methods of the MList
public static
public static
public static
public static
public static
public static
public static
public static
public static
public static
public static
public static
public static