Computer Science 61B - Spring 2001 - Clancy - Midterm 2

Computer Science 61B - Spring 2001 - Clancy - Midterm 2 -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS61B, Midterm 2, Spring 2001 CS61B, Spring 2001 Midterm 2 Professor Clancy and Professor Yelick Problem #1 (4 points, 8 minutes) Given below is a framework for an IntervalEnumeration class for successively returning Integer values in an Interval in increasing order. For examples, if this represents the interval [3,5], the enumeration should return first an Integer object representing 3, then an Integer object representing 4, then an Integer object representing 5. Complete the method bodies on the next page. // OVERVIEW: This class defines nonempty intervals of consecutive integers, // such as [1,2,3] or [-2,-1,0,1]. Intervals are immutable. public class Interval { public Interval (int a, int b) throws IllegalArgumentException{ ... } // This interval represents all the integers between myLow and myHigh, // inclusive, with myLow <= myHigh. private int myLow, myHigh; // REQUIRES: this interval is not modified while the Enumeration returned // is in use. // EFFECTS: returns an Enumeration of Integer objects corresponding // to the values in this interval, enumerated lowest to highest. public Enumeration values ( ) { return new IntervalEnumeration ( ); } FILL IN THE FOLLOWING DEFINITIONS: private class IntervalEnumeration ( ) implements Enumeration { // EFFECTS: Initializes an enumeration of elements of this interval. public IntervalEnumeration ( ) { } // EFFECTS: Returns true if there are more elements of this interval // to return; returns false otherwise. public boolean hasMoreElements ( ) { } // REQUIRES: hasMoreElements ( ). // MODIFIES: this enumeration. file:///C|/Documents%20and%20Settings/Jason%20Raft. ..20Spring%202001%20-%20Clancy%20-%20Midterm%202.htm (1 of 7)1/27/2007 5:40:35 PM
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS61B, Midterm 2, Spring 2001 // EFFECTS: Returns the next element from this interval; // throws NoSuchElementException if !hasMoreElements ( ). public Object nextElement ( ) { } // any state variables you need go here } } } Problem #2 (5 points, 10 minutes) Given below is a class representing a singly linked list. public class List { private class ListNode { public Object myItem; public ListNode myNext; // EFFECTS: Initializes a one-element linked list. public ListNode (Object item) { myItem = item; myNext = null; } // EFFECTS: Initializes a linked list whose first element is item // and whose remaining items are those of remaining. public ListNode (Object item, ListNode remaining) { myItem = item; myNext = remaining; } } private ListNode myFirst; // reference to the first node in the list // REQUIRES: myFirst != null and the linked list pointed to by myFirst // is noncircular. // MODIFIES: this.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

Computer Science 61B - Spring 2001 - Clancy - Midterm 2 -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online