Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 4 Boxing classes Boxing classes Array initialization Array initialization Lists: recursion and iteration Lists: recursion and iteration.

Similar presentations


Presentation on theme: "Section 4 Boxing classes Boxing classes Array initialization Array initialization Lists: recursion and iteration Lists: recursion and iteration."— Presentation transcript:

1 Section 4 Boxing classes Boxing classes Array initialization Array initialization Lists: recursion and iteration Lists: recursion and iteration

2 Boxing classes Many library methods have Objects as parameters, i.e. 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. However, primitive types, like int, double, float etc. are not objects, not to mention Objects. Solution: Boxing/wrapper classes 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: For each primitive type there is a corresponding wrapper class: Primitive type Primitive type Wrapper class intInteger floatFloat charCharacter......

4 Wrapper classes Usage: 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 int intValue Float float floatValue

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

7 Wrapper classes - usage Java container classes store Objects. Java container classes store Objects. TreeSet s = new TreeSet; TreeSet s = new TreeSet; s.add(new Integer(7)); s.add(new Integer(7)); We cannot store ints directly! 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! 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! arr contains 100 nulls! for (int i = 0; i < 100; i++) arr[i] = new Integer(i); arr[i] = new Integer(i); Now every element of an array is initialized. Now every element of an array is initialized.

9 Array initialization The same thing happens during creation of multidimensional arrays: 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++) for (int j = 0; j < 23; j++) arr[i][j] = new Integer(i*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. Cells containg Objects. Each Cell has a pointer to the next cell in the list. class ListCell {...... public ListCell getNext(); public ListCell getNext(); // returns the next element. // returns the next element. public void setNext(ListCell l); public void setNext(ListCell l); // sets the pointer to point to l // sets the pointer to point to l public Object getDatum(); public Object getDatum(); // returns the object stored in the cell // returns the object stored in the cell}

12 Lists: Iteration and recursion class Course { public Book textBook; public Book textBook;......} class Student { private ListCell courses; 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. Suppose that student studies by reading all textbooks for all his/her courses. How does a study method look like? How does a study method look like?

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

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

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

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

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

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

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

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

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

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

24 Lists: recursion private void study1(ListCell c) { if (c == null) if (c == null) return; return; readBook(((Course)c.getDatum()).textBook; readBook(((Course)c.getDatum()).textBook; study1(c.next()); 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. 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: 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. 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. 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; ListCell current = l, previous = null; while (current != null) while (current != null) { if (current.getDatum().equals(o)) // found the object if (current.getDatum().equals(o)) // found the object { if (previous == null) return l.getNext() ; if (previous == null) return l.getNext() ; // it was the first one // it was the first one else else { previous.setNext(current.getNext()); return l; } { previous.setNext(current.getNext()); return l; } } else else previous = current; previous = current; current = current.getNext(); current = current.getNext(); } return l; return l;} Difficult to understand! Difficult to understand!

28 Lists: Recursion Deleting element: recursive way: 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 first element of l, and the list that results from deleting o from the rest of list l.

29 Lists: Recursion Deleting an element from list: Deleting an element from list: ListCell delete(ListCell l, Object o) { if (l == null) return l; if (l == null) return l; if (l.getDatum().equals(o)) return l.getNext(); if (l.getDatum().equals(o)) return l.getNext(); l.setNext(delete(l.getNext(), o); l.setNext(delete(l.getNext(), o); return l; 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. Many functions on lists look better when you use recursion.


Download ppt "Section 4 Boxing classes Boxing classes Array initialization Array initialization Lists: recursion and iteration Lists: recursion and iteration."

Similar presentations


Ads by Google