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

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Section 5 Lists again. Double linked lists – insertion, deletion. Trees.
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Kernighan/Ritchie: Kelley/Pohl:
Java Software Solutions
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
LISTS & TREES Lecture 8 CS2110 – Fall List Overview 2  Purpose  Maintain an ordered set of elements (with possible duplication)  Common operations.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
1 Lists. 2 Overview Arrays –Random access: –Fixed size: cannot grow on demand after creation:  Characteristics of some applications: –do not need random.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Lists Weiss sec. 6.5 (sort of) ch. 17 (sort of). Arrays Random access a[38] gets 39 th element But fixed size, specified at construction e.g. pickLineFromFile(
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Introduction to C Programming CE Lecture 21 Recursion and Linear Linked Lists.
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Static Class Members Wrapper Classes Autoboxing Unboxing.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
ArrayList, Multidimensional Arrays
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types.
LISTS Slides of K. Birman, Cornell University. List Overview 2  Purpose  Maintain an ordered collection of elements (with possible duplication)  Common.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Section 4 Break Statement Usage Integer Class and other Wrapper class Inside Object Array Remove element from a list –Recursive approach –Iterative approach.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CSE 1201 Object Oriented Programming ArrayList 1.
Variables and memory addresses
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Computer Programming for Engineers
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
OOP Basics Classes & Methods (c) IDMS/SQL News
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Chapter VII: Arrays.
Lecture 20: Wrapper Classes and More Loops
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Object Oriented Programming COP3330 / CGS5409
Arrays and Collections
Presentation transcript:

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

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:

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......

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!

Integer int intValue Float float floatValue

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));

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!

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.

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);

Array initialization - moral ALWAYS initialize array elements.

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}

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() {???} }

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?

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

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

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

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

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

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

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

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

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

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

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_!

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) { ? ? ? }

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.

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!

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.

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 ))

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