120 likes | 210 Views
CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 alphonce@buffalo.edu. Announcements. Exam 3 on Wednesday 11/11 covers material from last exam up to and including Friday 11/06 Review on Monday 11/09
E N D
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 alphonce@buffalo.edu
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<E> • (some) classes implementing interface: • java.util.HashSet<E> • java.util.ArrayList<E> • E is the type of element contained in the collection – replace by an actual type
Use of a collection HashSet<String> names = new HashSet<String>(); 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<E> myCollection; myCollection = new ArrayList<E>(); myCollection = new HashSet<E>();
Polymorphism and Collections(polymorphic dispatch, just like we saw on Friday) Collection<ITool> 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<E>) 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.