Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSSE221: Software Dev. Honors Day 10 Announcements Announcements Fifteen due Monday 11:59 pm. Monday will be a workday. Fifteen due Monday 11:59 pm. Monday.

Similar presentations


Presentation on theme: "CSSE221: Software Dev. Honors Day 10 Announcements Announcements Fifteen due Monday 11:59 pm. Monday will be a workday. Fifteen due Monday 11:59 pm. Monday."— Presentation transcript:

1 CSSE221: Software Dev. Honors Day 10 Announcements Announcements Fifteen due Monday 11:59 pm. Monday will be a workday. Fifteen due Monday 11:59 pm. Monday will be a workday. Quizzes passed back Quizzes passed back Sec 2, question 8. Are all events handled by some sort of listener? Sec 2, question 8. Are all events handled by some sort of listener? Questions on GUIs, EventListeners or Fifteen? Questions on GUIs, EventListeners or Fifteen? Fire up Angel and Eclipse now Fire up Angel and Eclipse now

2 This week: Fifteen assignment Monday: Monday: GUIs using Java’s Swing library GUIs using Java’s Swing library Tuesday: Tuesday: Fifteen specification Fifteen specification EventListeners: responding to user input EventListeners: responding to user input Intro to UML as a design tool Intro to UML as a design tool Time to work on project Time to work on project Thursday: Thursday: Start prep for round 2 capsules Start prep for round 2 capsules Anonymous classes (capsule) Anonymous classes (capsule) Function objects and comparators (capsule) Function objects and comparators (capsule)

3 How to do a capsule? Round 2: +Demo and Activity I still lecture (15-20 min). I still lecture (15-20 min). You still create a summary and quiz. You still create a summary and quiz. Now, you create the demonstration. Now, you create the demonstration. Code that shows a concept. Code that shows a concept. How will you know if your classmates are understanding it? How will you know if your classmates are understanding it? Now, you create a hands-on activity for the class, like? Now, you create a hands-on activity for the class, like? Start the demo code together (like SwingDemo) Start the demo code together (like SwingDemo) Have them extend the demo code (like SalariedEmployee) Have them extend the demo code (like SalariedEmployee) Do a kinesthetic activity (like having the class act out a sort method) Do a kinesthetic activity (like having the class act out a sort method) Use your creativity! Use your creativity!

4 More about the Demo/Activity Total time for both: ~25-30 minutes Total time for both: ~25-30 minutes Integrate your quiz with your demo/activity: Integrate your quiz with your demo/activity: 2-3 questions must relate to them. 2-3 questions must relate to them. Roles of Teammates: Roles of Teammates: 1. Demo Driver: explains the code and adds any live code 2. Roving Expert: checks if any students are having difficulties, asks if they need help 3. Questioner: chooses students to ask the questions on the quiz, asks them, and provides encouragement or corrective feedback as appropriate.

5 Capsule Deliverables By 7:30 am on the day you are presenting: By 7:30 am on the day you are presenting: Email the quiz, key, and summary to me (as before) Email the quiz, key, and summary to me (as before) Commit your demo to csse221-200810-public Commit your demo to csse221-200810-public Include your section number in the project name: csse221 Include your section number in the project name: csse221 Bring to class printed versions (as before): Bring to class printed versions (as before): 1 copy of summary 1 copy of summary 2 copies of key 2 copies of key Enough copies of quiz for the class (20) Enough copies of quiz for the class (20)

6 Choose topic preferences for group 2 Angel survey in Lessons > Other Angel survey in Lessons > Other 2 minutes 2 minutes

7 Anonymous Classes Say I have a really small class that won’t get re- used by other classes. Say I have a really small class that won’t get re- used by other classes. If I don’t want to write it in its own file, I have 3 options: If I don’t want to write it in its own file, I have 3 options: Nested classes Nested classes Local classes Local classes Anonymous classes Anonymous classes

8 Nested Classes A class defined within another class. It is only visible to the outer class. A class defined within another class. It is only visible to the outer class. It must be declared static, or else it is an inner class. Inner classes are typically made visible to the world (like Ellipse2D.Double) It must be declared static, or else it is an inner class. Inner classes are typically made visible to the world (like Ellipse2D.Double)

9 Local Classes Declared inside a method Declared inside a method Their scope is only the method in which they are declared Their scope is only the method in which they are declared Not declared private or static. Not declared private or static. Have access to local final variables of the method. Have access to local final variables of the method. Weiss doesn’t use these Weiss doesn’t use these

10 Anonymous Inner Classes A class with no name! A class with no name! So it can only be used once! So it can only be used once! Used to provide a class that implements an interface (or extends a class). Used to provide a class that implements an interface (or extends a class). Instead of writing new Inner(), we write new Interface(), then provide the definition of the interface’s methods in curly braces {}. Instead of writing new Inner(), we write new Interface(), then provide the definition of the interface’s methods in curly braces {}. To extend a class, we provide definitions of the overridden methods To extend a class, we provide definitions of the overridden methods Limitation: can’t specify a constructor Limitation: can’t specify a constructor

11 Demo Check out SwingDemoWithAnonymousClasses Check out SwingDemoWithAnonymousClasses Disconnect Disconnect Together, we’ll make the listeners anonymous Together, we’ll make the listeners anonymous Then, 5-minute break Then, 5-minute break

12 Function Objects (a.k.a. Functors) Consider sorting an array of objects. Consider sorting an array of objects. Sorting requires elements can be comparable, such as numbers. Sorting requires elements can be comparable, such as numbers. However, what if there were more than one way to compare the elements? However, what if there were more than one way to compare the elements? We’d like to be able to tell the sorting function how to compare the elements. We’d like to be able to tell the sorting function how to compare the elements.

13 Function Objects (a.k.a. Functors) We'd like to be able to pass a method as an argument to another method. (what is the role of arguments to methods in general?) We'd like to be able to pass a method as an argument to another method. (what is the role of arguments to methods in general?) This is not a new or unusual idea. This is not a new or unusual idea. We frequently pass other functions as arguments to Maple's plot and solve functions. We frequently pass other functions as arguments to Maple's plot and solve functions.

14 Function Objects (a.k.a. Functors) We'd like to be able to pass a method as an argument to another method. We'd like to be able to pass a method as an argument to another method. This is not a new or unusual idea. This is not a new or unusual idea. Maple's plot and solve can take function arguments. Maple's plot and solve can take function arguments. C and C++ provide qsort, whose first argument is a comparison function. C and C++ provide qsort, whose first argument is a comparison function. Scheme has sort, which can take a function as its first argument. Scheme has sort, which can take a function as its first argument.

15 Function Objects What's it all about? What's it all about? Unfortunately, Java (unlike C++) doesn't allow functions to be passed directly as arguments. Unfortunately, Java (unlike C++) doesn't allow functions to be passed directly as arguments. But we can create objects whose only purpose is to pass a function into a method. They are called function objects, a.k.a. functors. But we can create objects whose only purpose is to pass a function into a method. They are called function objects, a.k.a. functors. Weiss' example: Weiss' example: Uses Comparator objects (interface is defined in java.util.Comparator ). Uses Comparator objects (interface is defined in java.util.Comparator ). What is Comparator used for? What is Comparator used for? Why not just use Comparable? Why not just use Comparable? OrderRectByWidth, SimpleRect, CompareTest OrderRectByWidth, SimpleRect, CompareTest

16 How to pronounce Comparator, Comparable

17 Comparator Interface Weiss provides code for several classes that are equivalent to those in java.util, so we can see how parts of the java.util classes might be implemented. You can install this  Generics would make this code more complicated; we’ll deal with that later.

18 Comparator Example part 1 The SimpleRectangle class does not implement Comparable, because there is no one "natural" way to order SimpleRectangle objects.

19 Comparator Example part 2 Two comparator classes.

20 Comparator Example part 3 Without something like Comparators, we would need separate findMax functions for finding the max using different comparison criteria  Note that java.util.Collections.max() has similar functionality for ArrayLists and other collections.

21 Demo Passing a comparator to a sort method. Passing a comparator to a sort method. Check out the ComparatorDemo project. Check out the ComparatorDemo project. Follow the directions on the top of the program. Follow the directions on the top of the program.

22 On HW5… Weiss 4.29-30 (was 4.28-29 in ed. 2) Weiss 4.29-30 (was 4.28-29 in ed. 2) Read over now Read over now EqualsK should implement your interface from the first problem. Add extra tests to your main method to create some different equalsK objects and pass them, along with an interesting array, to the countMatches() method. EqualsK should implement your interface from the first problem. Add extra tests to your main method to create some different equalsK objects and pass them, along with an interesting array, to the countMatches() method. You can submit (to the drop box for WA2) one set of classes that solves and tests both problems. You can submit (to the drop box for WA2) one set of classes that solves and tests both problems.

23 Hints Analogy with the Rectangle example: Analogy with the Rectangle example: countMatches (like findMax) is the method that takes an array and a function object as parameters. countMatches (like findMax) is the method that takes an array and a function object as parameters. EqualsZero (like findMaxByWidth) is specific function object. EqualsZero (like findMaxByWidth) is specific function object. ??? (like Comparator) is the function object interface: you pick the name. ??? (like Comparator) is the function object interface: you pick the name.


Download ppt "CSSE221: Software Dev. Honors Day 10 Announcements Announcements Fifteen due Monday 11:59 pm. Monday will be a workday. Fifteen due Monday 11:59 pm. Monday."

Similar presentations


Ads by Google