CSSE221: Software Dev. Honors Day 10 Announcements Announcements Fifteen due Monday 11:59 pm. Monday...

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

    20-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    0

Transcript of CSSE221: Software Dev. Honors Day 10 Announcements Announcements Fifteen due Monday 11:59 pm. Monday...

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

CSSE221: Software Dev. Honors CSSE221: Software Dev. Honors Day 10Day 10 AnnouncementsAnnouncements

Fifteen due Monday 11:59 pm. Monday will be Fifteen due Monday 11:59 pm. Monday will be a workday.a workday.

Quizzes passed backQuizzes passed back Sec 2, question 8. Are all events handled by some Sec 2, question 8. Are all events handled by some

sort of listener?sort of listener? Questions on GUIs, EventListeners or Fifteen?Questions on GUIs, EventListeners or Fifteen?

Fire up Angel and Eclipse nowFire up Angel and Eclipse now

Page 2: 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.

This week: Fifteen This week: Fifteen assignmentassignment

Monday:Monday: GUIs using Java’s Swing library GUIs using Java’s Swing library

Tuesday:Tuesday: Fifteen specificationFifteen specification EventListeners: responding to user inputEventListeners: responding to user input Intro to UML as a Intro to UML as a designdesign tool tool Time to work on projectTime to work on project

Thursday:Thursday: Start prep for round 2 capsulesStart prep for round 2 capsules Anonymous classes (capsule) Anonymous classes (capsule) Function objects and comparators Function objects and comparators

(capsule) (capsule)

Page 3: 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.

How to do a capsule?How to do a capsule?Round 2: +Demo and Round 2: +Demo and

ActivityActivity 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 How will you know if your classmates are

understanding it?understanding it? Now, you create a Now, you create a hands-on hands-on activity for the activity for the

class, like?class, like? Start the demo code together (like SwingDemo)Start the demo code together (like SwingDemo) Have them extend the demo code (like Have them extend the demo code (like

SalariedEmployee)SalariedEmployee) Do a kinesthetic activity (like having the class act out Do a kinesthetic activity (like having the class act out

a sort method)a sort method) Use your creativity! Use your creativity!

Page 4: 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.

More about the More about the Demo/ActivityDemo/Activity

Total time for both: ~25-30 minutes Total time for both: ~25-30 minutes IntegrateIntegrate your quiz with your your quiz with your

demo/activity:demo/activity: 2-3 questions must relate to them.2-3 questions must relate to them.

Roles of Teammates:Roles of Teammates:1.1. Demo DriverDemo Driver: explains the code and adds any : explains the code and adds any

live codelive code2.2. Roving ExpertRoving Expert: checks if any students are : checks if any students are

having difficulties, asks if they need helphaving difficulties, asks if they need help3.3. QuestionerQuestioner: chooses students to ask the : chooses students to ask the

questions on the quiz, asks them, and provides questions on the quiz, asks them, and provides encouragement or corrective feedback as encouragement or corrective feedback as appropriate.appropriate.

Page 5: 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.

Capsule DeliverablesCapsule 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 Email the quiz, key, and summary to me (as before)before)

Commit your demo to csse221-200810-publicCommit your demo to csse221-200810-public Include your section number in the project name: Include your section number in the project name:

csse221 csse221

Bring to class printed versions (as before):Bring to class printed versions (as before): 1 copy of summary1 copy of summary 2 copies of key2 copies of key Enough copies of quiz for the class (20)Enough copies of quiz for the class (20)

Page 6: 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.

Choose topic preferences Choose topic preferences for group 2for group 2

Angel survey in Lessons > OtherAngel survey in Lessons > Other 2 minutes2 minutes

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

Anonymous ClassesAnonymous Classes

Say I have a really small class that Say I have a really small class that won’t get re-used by other classes. won’t get re-used by other classes.

If I don’t want to write it in its own If I don’t want to write it in its own file, I have 3 options:file, I have 3 options: Nested classesNested classes Local classes Local classes Anonymous classesAnonymous classes

Page 8: 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.

Nested ClassesNested Classes

A class defined within another class. A class defined within another class. It is only visible to the outer class.It is only visible to the outer class.

It must be declared static, or else it It must be declared static, or else it is an is an inner class. inner class. Inner classes are Inner classes are typically made visible to the world typically made visible to the world (like Ellipse2D.Double)(like Ellipse2D.Double)

Page 9: 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.

Local ClassesLocal Classes

Declared inside a methodDeclared inside a method Their scope is only the method in Their scope is only the method in

which they are declaredwhich they are declared Not declared private or static.Not declared private or static. Have access to local Have access to local final final variables variables

of the method.of the method. Weiss doesn’t use these Weiss doesn’t use these

Page 10: 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.

Anonymous Inner Anonymous Inner ClassesClasses

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 Used to provide a class that implements an interface (or extends a class).an interface (or extends a class).

Instead of writing new Instead of writing new InnerInner(), we write (), we write new new InterfaceInterface(), then provide the (), then provide the definition of the interface’s methods in definition of the interface’s methods in curly braces {}.curly braces {}. To extend a class, we provide definitions of the To extend a class, we provide definitions of the

overridden methodsoverridden methods Limitation: can’t specify a constructorLimitation: can’t specify a constructor

Page 11: 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.

DemoDemo

Check out Check out SwingDemoWithAnonymousClassesSwingDemoWithAnonymousClasses

DisconnectDisconnect Together, we’ll make the listeners Together, we’ll make the listeners

anonymousanonymous

Then, 5-minute breakThen, 5-minute break

Page 12: 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.

Function Objects (a.k.a. Function Objects (a.k.a. Functors)Functors)

Consider sorting an array of objects.Consider sorting an array of objects. Sorting requires elements can be Sorting requires elements can be

comparable, such as numbers.comparable, such as numbers. However, what if there were more than one However, what if there were more than one

way to compare the elements?way to compare the elements? We’d like to be able to tell the sorting function We’d like to be able to tell the sorting function

how to compare the elements.how to compare the elements.

Page 13: 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.

Function Objects (a.k.a. Function Objects (a.k.a. Functors)Functors)

We'd like to be able to We'd like to be able to pass a method as an pass a method as an argument to another argument to another method. method. (what is the (what is the role of arguments to role of arguments to methods in general?)methods in general?) This is not a new or This is not a new or

unusual idea.unusual idea. We frequently pass We frequently pass

other functions as other functions as arguments to Maple's arguments to Maple's plotplot and and solvesolve functions .functions .

Page 14: 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.

Function Objects (a.k.a. Function Objects (a.k.a. Functors)Functors)

We'd like to be able to pass a method as an We'd like to be able to pass a method as an argument to another method. argument to another method. This is not a new or unusual idea.This is not a new or unusual idea. Maple's Maple's plotplot and and solvesolve can take function arguments. can take function arguments. C and C++ provide C and C++ provide qsortqsort, whose first argument is , whose first argument is

a comparison function.a comparison function. Scheme has Scheme has sortsort, which can take a function as its , which can take a function as its

first argument.first argument.

Page 15: 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.

Function ObjectsFunction Objects

What's it all about?What's it all about? Unfortunately, Java (unlike C++) doesn't allow Unfortunately, Java (unlike C++) doesn't allow

functions to be passed directly as arguments.functions to be passed directly as arguments. But we can create objects whose only purpose is to But we can create objects whose only purpose is to

pass a function into a method. They are called pass a function into a method. They are called functionfunction objectsobjects, a.k.a. , a.k.a. functorsfunctors..

Weiss' example:Weiss' example: Uses Uses ComparatorComparator objects objects

(interface is defined in (interface is defined in java.util.Comparatorjava.util.Comparator ). ). What is What is ComparatorComparator used for? used for? Why not just use Why not just use ComparableComparable??

OrderRectByWidth, SimpleRect, CompareTestOrderRectByWidth, SimpleRect, CompareTest

Page 16: 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.

How to pronounce How to pronounce Comparator, ComparableComparator, Comparable

Page 17: 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.

Comparator Comparator InterfaceInterface

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.

Page 18: 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.

Comparator Example Comparator Example part 1part 1

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

Page 19: 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.

Comparator Example Comparator Example part 2part 2

Two comparator classes.

Page 20: 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.

Comparator Example Comparator Example part 3part 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.

Page 21: 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.

DemoDemo

Passing a comparator to a sort Passing a comparator to a sort method.method.

Check out the ComparatorDemo Check out the ComparatorDemo project.project.

Follow the directions on the top of Follow the directions on the top of the program.the program.

Page 22: 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.

On HW5…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 nowRead over now

EqualsK should implement your interface EqualsK should implement your interface from the first problem. Add extra tests to your from the first problem. Add extra tests to your main method to create some different equalsK main method to create some different equalsK objects and pass them, along with an objects and pass them, along with an interesting array, to the countMatches() interesting array, to the countMatches() method. method.

You can submit (to the drop box for WA2) one You can submit (to the drop box for WA2) one set of classes that solves and tests both set of classes that solves and tests both problems.problems.

Page 23: 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.

HintsHints

Analogy with the Rectangle example:Analogy with the Rectangle example:

countMatches countMatches (like findMax) is the method (like findMax) is the method that takes an array and a function object as that takes an array and a function object as parameters.parameters.

EqualsZero EqualsZero (like findMaxByWidth) is specific (like findMaxByWidth) is specific function object.function object.

?????? (like Comparator) is the function object (like Comparator) is the function object interface: you pick the name.interface: you pick the name.