1 / 16

CS100J Lecture 12

CS100J Lecture 12. Previous Lecture Java Constructs arrays and indexing increment and decrement for -statements Lewis and Loftus, Sections 3.8 and 6.1-6.2 This Lecture Java constructs for ( initialized-declaration ; … ) throw Programming Concepts Alternative class implementations

hibbs
Download Presentation

CS100J Lecture 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS100J Lecture 12 • Previous Lecture • Java Constructs • arrays and indexing • increment and decrement • for-statements • Lewis and Loftus, Sections 3.8 and 6.1-6.2 • This Lecture • Java constructs • for( initialized-declaration; … ) • throw • Programming Concepts • Alternative class implementations • Use of an array for maintaining a list of items • Exception handling Lecture 12

  2. Motivating Example • In many applications, we need to maintain an unordered list of natural numbers, where each natural number may occur more than once. • Such a list is called a “multi-set” of natural numbers. • For example, the set of grades obtained on an exam is such a multi-set. • Specification of classNatMultiset //Construct NatMultiset of items <= maxValue. public NatMultiset(int maxValue) // Insert item into multiset. public void insert(int item) // Is item in multiset? public boolean isElement(int item) // Delete item from multiset. public void delete(int item) // Multiplicity of item in multiset. public int multiplicity(int item) Lecture 12

  3. Frequencies of Grades, revisited int grade; // the grade being processed. // Let histogram be represented by a NatMultiset. NatMultiset hist = new NatMultiset(100); /* “Process” grades until (but not including) a stopping signal of -1. */ grade = in.readInt(); while (grade != -1 ) { hist.insert(grade); grade = in.readInt(); } /* Print histogram. */ System.out.println(”Grade Frequency”); for ( grade = 0; grade<=100; grade++ ) System.out.println( grade + ” ” + hist.multiplicity(grade) ); Lecture 12

  4. First Implementation of natMultset maxValue freq • Representation Invariant: For each k in the range 0..maxValue, the frequency of item k in the multiset is freq[k]. maxValue 0 . . . publicclass NatMultiset { /* For each k in range 0..maxValue, frequency of k in multiset is freq[k]. */ private int maxValue; private int[] freq; /* methods */ . . . } Lecture 12

  5. Constructor NatMultiset (Version 1) /* Construct a NatMultiset for elements in the range 0 through maxValue. */ public NatMultiset( int maxValue ) { this.maxValue = maxValue; freq = new int[maxValue+1]; for (int k = 0; k <= maxValue; k++) freq[k]=0; } Running time: Proportional to maxValue Lecture 12

  6. Method insert (Version 1) /* Insert item into the multiset; throw RuntimeException if item out of range. */ publicvoid insert(int item) { if ( item < 0 || item > maxValue ) thrownew RuntimeException( ”NatMultiset: item out of range” ); else freq[item]++; } Running time: Constant time Exceptions: new RuntimeException(String msg)constructs an object that contains messagemsg. [In the absence of a user-defined catch,] throw terminates execution, and prints the message contained in theRuntimeException object. Lecture 12

  7. Method isElement (Version 1) /* Return true if the multiset contains 1 or more instances of item. Throw RuntimeException if item out of range. */ public boolean isElement(int item) { if ( item < 0 || item > maxValue ) thrownew RuntimeException( ”NatMultiset: item out of range” ); elsereturn freq[item] != 0; } Running time: Constant time Lecture 12

  8. Method delete (Version 1) /* Delete one instance of item from the multiset, if there is one. RuntimeException if item out of range. */ publicvoid delete(int item) { if ( item < 0 || item > maxValue ) thrownew RuntimeException( ”NatMultiset: item out of range” ); elseif (freq[item] > 0) freq[item]--; } Running time: Constant time Lecture 12

  9. Method multiplicity (Version 1) /* Return the number of occurrences of item in the multiset. RuntimeException if item out of range. */ publicint multiplicity(int item) { if ( item < 0 || item > maxValue ) thrownew RuntimeException( ”NatMultiset: item out of range” ); elsereturn freq[item]; } Running time: Constant time Lecture 12

  10. Motivation for a Different Implementation • Suppose instead of grading in the range 0..100, we were to grade in the range 0..100,000, but there are no more than 350 students in the class. • Then the declaration // Let histogram be represented by an NatMultiset. NatMultiset hist = new NatMultiset(100000); would lead to an array of length 100,001. • Shortcomings: • Large amount of space • Space utilization is very sparse • The multiplicity “read-out” takes on the order of a 100,001 steps to look at each element of the array. • Because class NatMultiset provides information hiding, its implementation can be changed without affecting its client’s code. Lecture 12

  11. Second Implementation of natMultset currentSize maxSize list • Representation Invariant: The multiset elements are list[0..currentSize-1], where currentSize <= maxSize. currentSize maxSize 0 . . . ? ? ? ? ? ? publicclass NatMultiset { /* The multiset elements are list[0..currentSize-1], where currentSize <= maxSize. */ private int currentSize; private int maxSize = _______________ ; private int[] list; } Lecture 12

  12. Constructor NatMultiset (Version 2) /* Construct a NatMultiset for elements in the range 0 through maxValue. */ public NatMultiset( int maxValue ) { list = new int[maxSize______]; currentSize = 0; // Note: maxValue is ignored // in this implementation. } Running time: Proportional to maxSize Lecture 12

  13. Method insert (Version 2) /* Insert item into the multiset; throw RuntimeException if out of space. */ publicvoid insert(int item) { if ( currentSize==maxSize ) thrownew RuntimeException( "NatMultiset: too many distinct elements” ); else { list[currentSize] = item; currentSize++; } } Running time: Constant time Lecture 12

  14. Method isElement (Version 2) /* Return true if the multiset contains 1 or more instances of item. */ public boolean isElement(int item) { int k; /* Let k be index of leftmost occurrence of item in list, or currentSize if item isn't in list. */ list[currentSize] = item; k = 0; while ( list[k] != item ) k++; return k != currentSize; } Running time: Worst case proportional to currentSize Lecture 12

  15. Method delete (Version 2) /* Delete one instance of item from the multiset, if there is one. */ publicvoid delete(int item) { int k; /* Let k be index of leftmost occurrence of item in list, or currentSize if item isn't in list. */ list[currentSize] = item; k = 0; while (list[k] != item) k++; if ( k!=currentSize ) { list[k] = list[currentSize-1]; currentSize--; } } Running time: Worst case proportional to currentSize Lecture 12

  16. Method multiplicity (Version 2) /* Return the number of occurrences of item in the multiset. */ publicint multiplicity(int item) { if ( item < 0 || item >= maxValue ) thrownew RuntimeException( ”NatMultiset: item out of range” ); else { int count = 0; for (int k=0; k < currentSize; k++) if ( list[k]==item ) count++; return count; } } Running time: Proportional to currentSize Lecture 12

More Related