



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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Main points of this exam paper are: Integer Object, Class Intervalenumeration, Interval Represents, Defines Nonempty, Class Defines, Consecutive Integers, Singly Linked, Linked List, Values Stored, Integer Objects
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!
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 return // 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 ( ); }
private class IntervalEnumeration ( ) implements Enumeration { // EFFECTS: Initializes an enumeration of elements of this inte public IntervalEnumeration ( ) {
// EFFECTS: Returns true if there are more elements of this int // to return; returns false otherwise. public boolean hasMoreElements ( ) {
// REQUIRES: hasMoreElements ( ). // MODIFIES: this enumeration. // EFFECTS: Returns the next element from this interval;
CS61B, Spring 2001 Midterm 2 Professor Clancy and Professor Yelick 1
// throws NoSuchElementException if !hasMoreElements ( ). public Object nextElement ( ) {
// any state variables you need go here
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 it // 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 lis // REQUIRES: myFirst != null and the linked list pointed to by myFirst // is noncircular. // MODIFIES: this. // EFFECTS: exchanges the first two nodes in the linked list pointed to // by myFirst; throws NoSuchElementException if there are fewer than tw // nodes in the list private void exchangeFirstTwoNodes ( ) { ... } }
You are to complete the exchangeFirstTwoNodes method. If the REQUIRES clause does not hold, your solution is allowed to crash or exhibit arbitrary behavior. An easy way to solve this problem is to exchange the myItem variables of the first two nodes. DON'T DO THIS. You will receive no credit for a solution that includes an
Consider a class named NonemptySet that represents a nonempty set of (distinct) integers and supports two operations:
We assume that, for the purposes of this problem, the median in a set S with an even number of elements is the same as the median in the set that results from removing the largest element of S. For example, the median of {2,3,5,9} is 3. Two implementations for this class are described on the following pages.
Part a One implementation of the NonemptySet class maintains a sorted doubly linked list of Integer objects , with a reference to the first node in the list, a reference to the node that contains the median element in the list, and a count of the number of nodes in the list. A box-and-pointer diagram of an example set appears below; all the state variables are represented in the diagram. A framework for this implementation of the NonemptySet class
appears at the end of this exam.
For each operation listed below, circle the estimate that most closely represents the operation's worst-case running time. Assume that the operation is implemented to be as fast as possible.
Adding a randomly chosen element k to the set of n elements (the add method):
finding k or k's proper position in the list, Θ(n) Θ(log n) Θ( then inserting k into the list if it's not already Θ(n) Θ(log n) & then updating the median if necessary. Θ(n) Θ(log n) Θ(1)
Deleting the median of a set of n elements (the deleteMedian method):
locating the median, Θ(n) Θ(log n) Θ(1) then removing it from the linked list, Θ(n) Θ(log n) Θ(1) then updating the median if necessary. Θ(n) Θ(log n) Θ(1)
Part b Another implementation of the NonemptySet class maintains a sorted Vector , as shown below. Again, all state variables are represented in the diagram. A framework for this implementation of the NonemptySet class appears
at the end of this exam. For each operation listed below, circle the estimate that most closely represents the operation's worst-case running time. Assume that the operation is implemented to be as fast as possible.
Adding a randomly chosen element k to the set of n elements (the add method):
finding k or k's proper position in the list, Θ(n) Θ(log n) Θ( then inserting k into the list if it's not already Θ(n) Θ(log n) & then updating the median if necessary. Θ(n) Θ(log n) Θ(1)
Deleting the median of a set of n elements (the deleteMedian method):
locating the median, Θ(n) Θ(log n) Θ(1) then removing it from the linked list, Θ(n) Θ(log n) Θ(1) then updating the median if necessary. Θ(n) Θ(log n) Θ(1)
Part c Circle the estimate that most closely represents the worst-case running time of the entire add method for this implementation. Assume that the method is implemented to be as fast as possible.
Θ(n^2) Θ(n log n) Θ(n) Θ(l
A correctly working implementation for the NonemptySet class in part a of problem 4 will have reasonable values for the instance variables mySize , myHead , and myMedian , as well as a proper list of doubly linked DListNodes from lab assignment 6. (The declaration of DListNode appears in the NonemptySet framework that follows this problem.) To write a repOk method for this NonemptySet class, you would need to check several properties. We give you some of these below. You are to supply four more interesting and distinct properties. (There are more than four possibilities.) Your properties may be written using English prose, Java expressions, or mathematical notation, but they must be precise enough that another CS61B student could easily translate them into code. Given:
1.myHead != null. myHead contains a reference to the first node of a proper doubly linked list. That is, myHead.myPrev == null, and for each node n in the myHead list, if n.myNext != null then n.myNext.myPrev == n; if n.myPrev != null then n.myPrev.myNext == n.
3.myHead.myPrev == null. 4.There exists some node n in the myHead list such that n.myNext == null.
public class NonemptySet { // EFFECTS: Initialize a one-element set that contains the given value. public void NonemptySet (int n) { ... } // MODIFIES: this. // EFFECTS: Add the given integer to the set if it's not already there. public void add (int n) { ... } // MODIFIES: this. // EFFECTS: remove the median element from the set if the set contains // more than one elements. public void deleteMedian ( ) { ... } private Vector myElements; // elements are sorted }
Solutions!
Code for problem 4, part b 7