CS计算机代考程序代写 Java CS61B, Midterm 2, Spring 2001

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, wit 0 applications of myPrev never produces the node n, and k > 0 applications of myNext never produces the node n.
6. myMedian != null. Your properties:
1342.
Code for problem 4, part a
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 element.
public void deleteMedian ( ) {
… }
private class DListNode {
public Object myItem;
public DListNode myPrev;
public DListNode myNext;
// EFFECTS: Initialize a DListNode with myItem obj
// and the given values for myPrev and myNext.
public DListNode (Object obj, DListNode prev, DListNode next) {
myItem = obj;
myPrev = prev;
myNext = next;
} }
private DListNode myHead;
private DListNode myMedian;
private int mySize;
Problem #5 (4 points, 11 minutes) 6

}
Solutions!
CS61B, Midterm 2, Spring 2001
Code for problem 4, part b
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
Posted by HKN (Electrical Engineering and Computer Science Honor Society) University of California at Berkeley
If you have any questions about these online exams
please contact examfile@hkn.eecs.berkeley.edu.
Code for problem 4, part b 7