1 / 19

CSE115: Introduction to Computer Science I

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu. Agenda. Announcements Cell phones / laptops off & away / Name signs out Last time type conversions green UML class diagram tool demo Today searching. Search.

tyrone
Télécharger la présentation

CSE115: Introduction to Computer Science I

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. CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu

  2. Agenda • Announcements • Cell phones / laptops off & away / Name signs out • Last time • type conversions • green UML class diagram tool demo • Today • searching

  3. Search • Computers are good at storing large amounts of data • Finding a particular datum in a large collection is a typical operation • TODAY • how to search for a value in an unordered collection • determining the smallest/largest value in a collection

  4. general search • General problem: determine whether a value exists in a given collection • Assumption: to make things easier we assume that the collection contains Strings • Approach: define a method which accepts as arguments a Collection<String> and a value of type String, and returns a boolean indicating whether the value is in the collection

  5. steps in defining method Step 1 – stub out the method public booleanisMemberOf(String s, Collection<String> c){ return false; }

  6. steps in defining method Step 2 – set up loop Can use any of while/for/for-each. public booleanisMemberOf(String s, Collection<String> c){ for (String s : c) { } return false; }

  7. steps in defining method Step 3 – set up test in body of loop public booleanisMemberOf(String s, Collection<String> c){ for (String x : c) { if (s.equals(x)) { } } return false; }

  8. steps in defining method Step 4 – but we need to be careful, as s could be null! Relies on short-circuit evaluation of boolean operators. public booleanisMemberOf(String s, Collection<String> c){ for (String x : c) { if ((s==null) && (x==null) || s.equals(x)) { } } return false; }

  9. steps in defining method Step 5 – If we found the value, return true. If we search the entire collection without finding the value, return false public booleanisMemberOf(String s, Collection<String> c){ for (String x : c) { if ((s==null) && (x==null) || s.equals(x)) { // we found the value --- return true return true; } } // we searched entire collection // but did not find the value --- return false return false; }

  10. wrapper types • Collections can hold only references to objects. • Values of primitive types cannot be put into collections directly. • Each primitive type has an associated wrapper class:

  11. wrapper types • Each instance of a wrapper class holds a primitive value. • Examples: inti = 3; Integer wi = new Integer(i); // create wrapper int y = wi.intValue(); // get int value double d = 3.5; Double wd = new Double(d); // create wrapper double z = wd.doubleValue(); // get double value

  12. autoboxing/unboxing • In many cases the compiler can insert an automatic conversion to (boxing) or from (unboxing) a wrapper class object. • Examples: Collection<Integer> c = new ArrayList<Integer>(); c.add(4); // equivalent to c.add(new Integer(4)); Integer wx = new Integer(3); Integer wy = new Integer(4); intpz; pz = wx + wy; // equivalent to: // pz = wx.intValue() + wy.intValue(); Integer wz; wz = wx + wy; // equivalent to: // wz = new Integer(wx.intValue() + wy.intValue());

  13. finding the max • General problem: determine the maximum value in a collection • Assumption: to make things easier we assume that the collection contains non-null Integers, AND that the collection is not empty. • Approach: define a method which accepts as argument a Collection<Integer> and returns the smallest value is in the collection

  14. steps in defining method Step 1 – stub out the method public Integer maximum(Collection<Integer> c){ return 0; }

  15. steps in defining method Step 2 – Take first value of collection as a candidate for the maximum value. Extract first value using an iterator. public Integer maximum(Collection<Integer> c){ Iterator<Integer> it = c.iterator(); Integer candidate = it.next(); // under assumption that c is not // empty, this is safe // FIND THE MAXIMUM, STORING IT IN ‘candidate’ return candidate; // return the candidate }

  16. steps in defining method Step 3 – set up iterator-controlled loop to look at all values in collection public Integer maximum(Collection<Integer> c){ Iterator<Integer> it = c.iterator(); Integer candidate = it.next(); // under assumption that c is not // empty, this is safe while (it.hasNext()) { // FIND THE MAX } return candidate; // return the candidate }

  17. steps in defining method Step 4 – extract a vale from the collection public Integer maximum(Collection<Integer> c){ Iterator<Integer> it = c.iterator(); Integer candidate = it.next(); // under assumption that c is not // empty, this is safe while (it.hasNext()) { Integer temp = it.next(); // extract next value from collection } return candidate; // return the candidate }

  18. steps in defining method Step 5 – if it’s larger than current candidate, update candidate public Integer maximum(Collection<Integer> c){ Iterator<Integer> it = c.iterator(); Integer candidate = it.next(); // under assumption that c is not // empty, this is safe while (it.hasNext()) { Integer temp = it.next(); // extract next value from collection if ( temp > candidate ) { // if we found a larger value candidate = temp; // update candidate } } return candidate; // after loop completes, candidate contains // the largest value in the collection }

  19. minimum • Follow same basic steps as for finding maximum. • Change test (temp > candidate) • to (temp < candidate)

More Related