CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Announcements Exam 3 on Wednesday 11/11 –covers material from last exam up to and including Friday 11/06 Review on Monday 11/09 Exercises: FA09-CSE115-Exercises project in LectureCode repository Review session – stay tuned for announcement!
Agenda Lab 6 overview and tips Collections Inheritance – our last relationship!
Lab 6 tips and notes Eclipse –//TODO comments –Tasks view Java –Implicit constructors –System.out.println General –work systematically and incrementally
Lab 6 class diagram State pattern shown on board Polymorphic dispatch is selection based on type –foreshadowing upcoming slides: no if- else or similar statements are needed (or allowed) in lab 6.
Collections We have seen that a variable can hold one object reference at a time. How do we effectively deal with multiple references? –arbitrarily many –what does addActionListener do?
Collections interface: java.util.Collection (some) classes implementing interface: –java.util.HashSet –java.util.ArrayList E is the type of element contained in the collection – replace by an actual type
Use of a collection HashSet names = new HashSet (); names.add(“Amy”);names.remove(“Bob”); names.add(“Bob”); names.add(“Cindy”); names.add(“Dave”); names.add(“Emma”); …
for-each loop for (String name : names) { System.out.println(name); } This would print out: Amy Cindy Dave Emma
Polymorphism and Collections (always code to interface) Collection myCollection; myCollection = new ArrayList (); myCollection = new HashSet ();
Polymorphism and Collections (polymorphic dispatch, just like we saw on Friday) Collection toolBox; toolBox = new...;... toolBox.add(new CircleTool()); toolBox.add(new SquareTool()); toolBox.add(new NullTool());... java.awt.Point p =...; for (ITool tool : toolBox) { tool.apply(p); }
Collections.shuffle (a useful method to know) Collections.shuffle(List ) This method rearranges the elements in the list passed as an argument, in a random order. Note that the collection passed in must be a List, not a general collection, because the collection must support order operations.