1 / 30

Section 4

Section 4. Boxing classes Array initialization Lists: recursion and iteration. Boxing classes. Many library methods have Objects as parameters, i.e. add(Object o) However, primitive types, like int, double, float etc. are not objects, not to mention Objects.

nataliaa
Download Presentation

Section 4

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. Section 4 • Boxing classes • Array initialization • Lists: recursion and iteration

  2. Boxing classes • Many library methods have Objects as parameters, i.e. add(Object o) • However, primitive types, like int, double, float etc. are not objects, not to mention Objects. • Solution: Boxing/wrapper classes Note: Cornell’s boxing classes: http://www.pe.cornell.edu/physed/martial-f03.html

  3. Wrapper classes • For each primitive type there is a corresponding wrapper class:

  4. Wrapper classes • Usage: int k = 23; Integer i = new Integer(k); k = i.intValue(); float f = 5.17; Float g = new Float(f); f = g.floatValue(); See Java documentation for all methods!

  5. Integer intValue int int Float floatValue float float

  6. Wrapper classes • Every Java class inherits from Object class. • We can therefore use Integer, Char (and any other) object whenever a method expects an object. • For example, we can call add(new Integer(10));

  7. Wrapper classes - usage • Java container classes store Objects. • TreeSet s = new TreeSet; • s.add(new Integer(7)); • We cannot store ints directly!

  8. Array initialization • In Java (or C++), when an array of objects using new, array elements are not initialized automatically! Integer []arr = new Integer[100]; • arr contains 100 nulls! for (int i = 0; i < 100; i++) arr[i] = new Integer(i); • Now every element of an array is initialized.

  9. Array initialization • The same thing happens during creation of multidimensional arrays: Object [][]arr = new Object[17][23]; for (int i = 0; i < 17; i++) for (int j = 0; j < 23; j++) arr[i][j] = new Integer(i*j);

  10. Array initialization - moral ALWAYS initialize array elements.

  11. Lists • Cells containg Objects. Each Cell has a pointer to the next cell in the list. class ListCell { ... public ListCell getNext(); // returns the next element. public void setNext(ListCell l); // sets the pointer to point to l public Object getDatum(); // returns the object stored in the cell }

  12. Lists: Iteration and recursion class Course { public Book textBook; ... } class Student { private ListCell courses; public void readBook(Book b) {...} private void study() {???} }

  13. Lists • Suppose that student studies by reading all textbooks for all his/her courses. • How does a study method look like?

  14. Lists: Iteration • Iterative version: public void study() { for (ListCell current = courses; current != null; current = current.getNext()) readBook(((Course)current.getDatum()).textBook; } • We simply go through a list doing something to each element. CS 211 CS 212 CS 611 CS 711 null current

  15. Lists: Iteration • Iterative version: public void study() { for (ListCell current = courses; current != null; current = current.getNext()) readBook(((Course)current.getDatum()).textBook; } • We simply go through a list doing something to each element. CS 211 CS 212 CS 611 CS 711 null current

  16. Lists: Iteration • Iterative version: public void study() { for (ListCell current = courses; current != null; current = current.getNext()) readBook(((Course)current.getDatum()).textBook; } • We simply go through a list doing something to each element. CS 211 CS 212 CS 611 CS 711 null current

  17. Lists: Iteration • Iterative version: public void study() { for (ListCell current = courses; current != null; current = current.getNext()) readBook(((Course)current.getDatum()).textBook; } • We simply go through a list doing something to each element. CS 211 CS 212 CS 611 CS 711 null current

  18. Lists: Iteration • Iterative version: public void study() { for (ListCell current = courses; current != null; current = current.getNext()) readBook(((Course)current.getDatum()).textBook; } • We simply go through a list doing something to each element. CS 211 CS 212 CS 611 CS 711 null current

  19. Lists: recursion private void study1(ListCell c) { if (c == null) return; readBook(((Course)c.getDatum()).textBook; study1(c.next()); } CS 211 CS 212 CS 611 CS 711 null

  20. Lists: recursion private void study1(ListCell c) { if (c == null) return; readBook(((Course)c.getDatum()).textBook; study1(c.next()); } CS 212 CS 611 CS 711 null

  21. Lists: recursion private void study1(ListCell c) { if (c == null) return; readBook(((Course)c.getDatum()).textBook; study1(c.next()); } CS 611 CS 711 null

  22. Lists: recursion private void study1(ListCell c) { if (c == null) return; readBook(((Course)c.getDatum()).textBook; study1(c.next()); } CS 711 null

  23. Lists: recursion private void study1(ListCell c) { if (c == null) return; readBook(((Course)c.getDatum()).textBook; study1(c.next()); } null

  24. Lists: recursion private void study1(ListCell c) { if (c == null) return; readBook(((Course)c.getDatum()).textBook; study1(c.next()); } • Note that if we wanted to print out the name of each course the code would look almost exactly the same, but we need to write it anyway. Functional programming ad: study = mapM_ readBook courses printCourses = mapM_ print courses We can abstract the idea of iteration and actually write a function mapM_!

  25. Deleting from lists • We want delete the first occurence of the Object o from the List l and return the modified list: ListCell delete(ListCell l, Object o) { ? ? ? }

  26. Lists: Iteration • Deleting element: iterative version. • Intuition: We look for o in the list in a loop, once we found it we update the list and return.

  27. Lists: Iteration ListCell delete(ListCell l, Object o) { ListCell current = l, previous = null; while (current != null) { if (current.getDatum().equals(o)) // found the object { if (previous == null) return l.getNext() ; // it was the first one else { previous.setNext(current.getNext()); return l; } } else previous = current; current = current.getNext(); } return l; } • Difficult to understand!

  28. Lists: Recursion • Deleting element: recursive way: Intuition: • – If list l is empty, return null. • – If first element of l is o, return rest of list l. • – Otherwise, return list consisting of firstelement of l, and the list that results fromdeleting o from the rest of list l.

  29. Lists: Recursion • Deleting an element from list: ListCell delete(ListCell l, Object o) { if (l == null) return l; if (l.getDatum().equals(o)) return l.getNext(); l.setNext(delete(l.getNext(), o); return l; } Functional programming ad: delete [] o = [] delete (x:xs) o = if x == o then xs else (x: (delete xs o ))

  30. Lists: Moral • Many functions on lists look better when you use recursion.

More Related