Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSSE221: Software Dev. Honors Day 12 Announcements Announcements No baby yet… No baby yet… Fifteen done. Fifteen done. Please read Class Announcements.

Similar presentations


Presentation on theme: "CSSE221: Software Dev. Honors Day 12 Announcements Announcements No baby yet… No baby yet… Fifteen done. Fifteen done. Please read Class Announcements."— Presentation transcript:

1 CSSE221: Software Dev. Honors Day 12 Announcements Announcements No baby yet… No baby yet… Fifteen done. Fifteen done. Please read Class Announcements forum. Info about submitting IEPs there. Please read Class Announcements forum. Info about submitting IEPs there. New “late day” policy: New “late day” policy: You get 2 “late days” over the course of the term that each give you a 24-hour extension You get 2 “late days” over the course of the term that each give you a 24-hour extension The burden of turning in an assignment on a non-class day is on you. The burden of turning in an assignment on a non-class day is on you. You may earn late days for later use by turning in the assignment a day early. You may earn late days for later use by turning in the assignment a day early. You may use only 1 late day (or earn 1 early day) per assignment You may use only 1 late day (or earn 1 early day) per assignment Some restrictions apply (like right before an exam) Some restrictions apply (like right before an exam) I’ll allow this to be retroactive to any assignment you tried to submit late or thought about doing; just resubmit it on Thursday and I’ll enter the grade. I’ll allow this to be retroactive to any assignment you tried to submit late or thought about doing; just resubmit it on Thursday and I’ll enter the grade.

2 Research questionnaire

3 This week: CarsTrucksTrains Monday: Monday: Project workday Project workday Tuesday: Tuesday: Generics (capsule) Generics (capsule) Iterators Iterators Thursday: Thursday: Lists (capsule) Lists (capsule) Implementation of linked lists Implementation of linked lists

4 Generics We really want our algorithms to operate on any type of data, without having to re-write the whole method. We really want our algorithms to operate on any type of data, without having to re-write the whole method. In Java, we can do this two ways: In Java, we can do this two ways: Use inheritance (pre-Java 1.5, a bit clunky) Use inheritance (pre-Java 1.5, a bit clunky) Use Generics (newer, nicer) Use Generics (newer, nicer)

5 Using Inheritance ArrayList list = new ArrayList(); list.add(3);list.add("hello"); int num = list.get(0); // doesn’t work int num = (Integer)list.get(0); Problems/concerns? We don’t have control over what goes into the list We don’t have control over what goes into the list Typecasting is a pain (and since we don’t have control, then we have to check for compatibility using instanceof to avoid ClassCastExceptions Typecasting is a pain (and since we don’t have control, then we have to check for compatibility using instanceof to avoid ClassCastExceptions Note: in Java 1.4, it was even worse… Note: in Java 1.4, it was even worse… ArrayList list = new ArrayList(); list.add(new Integer(3)); // 3 needs to be boxed list.add("hello"); int num = list.get(0); // doesn’t work Integer temp = (Integer)list.get(0); int num = temp.intValue(); // unboxing

6 Using Generics ArrayList list = new ArrayList (); list.add(3); list.add(“hello”); // invalid int num = list.get(0); // works! Solves both those problems! We have control over the types of what goes into the list We have control over the types of what goes into the list No typecasting needed No typecasting needed

7 Creating code with Generics To use generics, use the type in a parameter. To use generics, use the type in a parameter. Example showing: Example showing: The use of a type in a class The use of a type in a class Various places where the type parameter can be used: Various places where the type parameter can be used: public class SomeClass { public E someMethod(E param) { E retValue = 2 * param; return retValue; }…} Unfortunately, this example doesn’t work, since we can’t multiply 2 by an unknown, possibly non-numeric type. Unfortunately, this example doesn’t work, since we can’t multiply 2 by an unknown, possibly non-numeric type.

8 Demo

9 Going further What if I have a method that operates on an ArrayList of Vehicles, but I want to pass an ArrayList of Trucks? What if I have a method that operates on an ArrayList of Vehicles, but I want to pass an ArrayList of Trucks? Intuitively, this should work, but Java doesn’t allow it, since it couldn’t catch errors until runtime. Intuitively, this should work, but Java doesn’t allow it, since it couldn’t catch errors until runtime. Solution? In the method declaration, use type bounds with wildcards: Solution? In the method declaration, use type bounds with wildcards: public void processVehicle(ArrayList list) { public void processVehicle(ArrayList list) { for (Vehicle v : list) { … } for (Vehicle v : list) { … }}

10 Yet further and messier-looking Consider static E findMedian(E[] array) Consider static E findMedian(E[] array) Takes an array of type E, returns a value of that type Takes an array of type E, returns a value of that type But we need to restrict the generics to Comparable types only (because we need compareTo ). Use type bounds: But we need to restrict the generics to Comparable types only (because we need compareTo ). Use type bounds: static E findMedian(E[] array) But Comparable is generic: But Comparable is generic: static > E findMedian(E[] array) To enforce that E is Comparable, use To enforce that E is Comparable, use static > E findMedian(E[] array) If Vehicles were Comparable, then E could be Truck. Then ? would be Vehicle

11 Type erasure At compile time, the generics are replaced with the types used At compile time, the generics are replaced with the types used If there are bounds, it uses them and inserts the proper casts If there are bounds, it uses them and inserts the proper casts

12 Some Limitations Can’t use primitives as types Can’t use primitives as types No int, need to use Integer No int, need to use Integer Can’t instantiate a type: E foo = new E(); Can’t instantiate a type: E foo = new E(); What is E? It could even be an abstract class; this wouldn’t make sense! What is E? It could even be an abstract class; this wouldn’t make sense! Can’t make generic arrays: E[] ar = new E[17]; Can’t make generic arrays: E[] ar = new E[17]; Naïve solution: use typecasts: Naïve solution: use typecasts: E[] ar = (E[])(new Object[17]) E[] ar = (E[])(new Object[17]) This gives a compiler warning This gives a compiler warning Better solution: use ArrayList Better solution: use ArrayList

13 Break Pass in quizzes (questions 1 and 2); I’ll use a different quiz for the second half. Pass in quizzes (questions 1 and 2); I’ll use a different quiz for the second half.

14 Intro to Data Structures

15 What is an Abstract Data Type (ADT)? A mathematical model of a data type. Specifies: A mathematical model of a data type. Specifies: The type of data stored The type of data stored the operations supported the operations supported the types and return values of theses operations the types and return values of theses operations Specifies what each operation does, but not how it is implemented. Specifies what each operation does, but not how it is implemented. Example: Stack ADT. Operations are Example: Stack ADT. Operations are push pop top (topAndPop) push pop top (topAndPop) Sample rule: After the operations { s.push(x); y=s.topAndPop(); } s is unchanged and y = = x. Sample rule: After the operations { s.push(x); y=s.topAndPop(); } s is unchanged and y = = x.

16 Data Structures and the Java Collections Framework Most of the time when we talk about a data structure, we mean an approach to storing several items (usually all of the items have the same type). Most of the time when we talk about a data structure, we mean an approach to storing several items (usually all of the items have the same type). When studying a new data structure, consider three aspects: When studying a new data structure, consider three aspects: Specification (interface) Specification (interface) Implementation (sometimes several alternate implementations) Implementation (sometimes several alternate implementations) Applications (how can it be used?) Applications (how can it be used?) Mostly, these can be considered independently. Mostly, these can be considered independently. If we understand the interface and trust the person who says she implemented it, we can feel free to apply it without having to understand the details of the implementation. If we understand the interface and trust the person who says she implemented it, we can feel free to apply it without having to understand the details of the implementation.

17 The most common collection data structure is … An array. An array. Size must be declared when the array is constructed. Size must be declared when the array is constructed. We can look up or store items by index. We can look up or store items by index. a[i+1] = a[i] + 2; a[i+1] = a[i] + 2; Implementation (usually handled by the compiler): Suppose we have an array of N items, each b bytes in size. Let L be the address of the beginning of the array. What is involved in finding the address of a[i] ? What is the Big-oh time required for an array-element lookup? What about lookup in a 2D array of n rows with m items in each row.? a[0] a[1] a[2] a[i] a[n-2] a[n-1] La

18 Data Structures and the Java Collections Framework What is data? What is data? What do we mean by "structure" What do we mean by "structure" A data type A data type But what is a data type, really? But what is a data type, really? An interpretation of the bits An interpretation of the bits An interpretation is basically a set of operations. An interpretation is basically a set of operations. The interpretation may be provided by the hardware, as for int and double types, or by software, as for the java.math.BigInteger type. The interpretation may be provided by the hardware, as for int and double types, or by software, as for the java.math.BigInteger type. Or by software with much assistance from the hardware, as for the java.lang.Array type. Or by software with much assistance from the hardware, as for the java.lang.Array type.

19 Some basic data structures Array (1D, 2D, …) Array (1D, 2D, …) Stack Stack Queue Queue List List ArrayList ArrayList LinkedList LinkedList Set Set MultiSet MultiSet Map (a.k.a. table, dictionary) Map (a.k.a. table, dictionary) HashMap HashMap TreeMap TreeMap PriorityQueue PriorityQueue Tree Tree Graph Graph Network Network What is "special" about each data type? What is each used for? What can you say about time required for adding an element? removing an element? Finding an element? You should know these, inside and out, by the end of CSSE230.

20 Specifying an ADT in Java The main Java tool for specifying an ADT is … The main Java tool for specifying an ADT is … … an interface: … an interface: Some important methods from this interface: Some important methods from this interface: Factory method

21 Iterators Consider a loop to fund the sum of each element in an array: Consider a loop to fund the sum of each element in an array: for (int i = 0; i < ar.length; i++) { sum += ar[i]; } We want to generalize this beyond arrays

22 What's an iterator? More specifically, what is a j ava.util.Iterator ? More specifically, what is a j ava.util.Iterator ? It's an interface: It's an interface: interface java.util.Iterator interface java.util.Iterator with the following methods: with the following methods: We create a new concrete instance of an iterator, but use an interface return type (using polymorphism). This is what a factory method does. We create a new concrete instance of an iterator, but use an interface return type (using polymorphism). This is what a factory method does. The advantage is that if we change the type of collection used in main(), then we don’t have to change the iterator type. The advantage is that if we change the type of collection used in main(), then we don’t have to change the iterator type.

23 Example: Using an Iterator ag is a Collection object. Using Java 1.5’s “foreach” construct: Note that the Java compiler essentially translates the latter code into the former.

24 What's an iterator? More specifically, what is a j ava.util.Iterator ? More specifically, what is a j ava.util.Iterator ? It's an interface: It's an interface: interface java.util.Iterator interface java.util.Iterator with the following methods: with the following methods: Why do iterators have their own remove method, separate from the Collections’ remove? Why do iterators have their own remove method, separate from the Collections’ remove?

25 Sort and Binary Search The java.util.Arrays class provides static methods for sorting and doing binary search on arrays. Examples: The java.util.Arrays class provides static methods for sorting and doing binary search on arrays. Examples:

26 Sort and Binary Search The java.util.Collections class provides similar static methods for sorting and doing binary search on Collection s. Specifically List s. The java.util.Collections class provides similar static methods for sorting and doing binary search on Collection s. Specifically List s. Look up the details in the documentation. Look up the details in the documentation.


Download ppt "CSSE221: Software Dev. Honors Day 12 Announcements Announcements No baby yet… No baby yet… Fifteen done. Fifteen done. Please read Class Announcements."

Similar presentations


Ads by Google