200 likes | 394 Views
Teaching Object-Oriented Concepts Through GUI Programming. Jesse M. Heines Martin J. Schedlbauer Dept. of Computer Science Univ. of Massachusetts Lowell. Eleventh Workshop on Pedagogies and Tools for the Teaching and Learning of Object Oriented Concepts
E N D
Teaching Object-Oriented Concepts Through GUI Programming Jesse M. Heines Martin J. Schedlbauer Dept. of Computer ScienceUniv. of Massachusetts Lowell Eleventh Workshop on Pedagogies and Tools for the Teaching and Learning of Object Oriented Concepts ECOOP, Berlin, Germany, July 30, 2007
The OOP Teaching Field • The Starting Line • Introducing OO concepts • Working OO examples • CS 1 and CS 2
The OOP Teaching Field • The Starting Line • Introducing OO concepts • Working OO examples • CS 1 and CS 2 • The Finish Line • Applying OO concepts • Solving programming problems • Advanced courses
The OOP Workshop Field • 3 papers dealing with “Beginners,” “CS1&2,” and “Novice Programmers” • Schmolitzky • Späh and Schmolitzky • Ma, Ferguson, Roper, and Wood • 2 papers on “Objects First” • Ehlert and Schulte • Shümmer and Kösters • 2 papers on applying OO concepts • Hadar and Hadar • Heines and Schedlbauer
The OOP Cognitive Field • Our experience is that regardless of how and when OO concepts are introduced, students have trouble applying those concepts in project-based courses • Why? • Lack of experience with large programs • The real benefits of OO are difficult to see in the small programs typically used as examples • Consequently, OO concepts do become part of the student’s cognitive field and programming style • “I need an object” instead of “I need a routine”
OOP in Class vs. at Work • Small programsLarge programs • 100s of lines 1000s of lines • StandaloneHierarchies builtprogramsusing previous code • Single files Linked modules • Coded soloCoded in teams • Code is never Documentation is documented absolutely required • Never reusedReused repeatedly • By self or others By self and others
OOP and GUI Programming • Excellent examples of class hierarchies • Java Swing and .NET • Students learn just by browsing • Excellent tools for large programs • NetBeans, Eclipse, Visual Studio, BlueJ • Form designers and code generators • (Mostly) excellent documentation • Using the API is a critical skill • Creating an API is even more critical • Model for student-created documentation
OOP and GUI Programming • Inclusion of design patterns • Observers = MVC architecture & listeners • Strategies = layout managers • Composites = UI components & containers • Decorators = scroll panes and borders • Singletons = calendars • Factories = borders • Commands = menus
Lecture: Building Bridges • Problem:Set the text of the root node of an existingJTree control • Subproblem: There is no method of the JTreeobject to set a node’s text • Solution: Build a bridge from the control’s model • This requires understanding relationshipsbetween objects
Lecture: Building Bridges • Step 1: Create a new root node DefaultMutableTreeNode root = new DefaultMutableTreeNode( "New Text" ); • Step 2: Get reference to the tree model TreeModel myTM = myJTree.getModel(); • Step 3: Cast the tree model reference DefaultTreeModel myDTM = (DefaultTreeModel) myTM; • Step 4: Set the tree root myDTM.setRoot( root );
Lecture: Building Bridges • Note that this is a very practical problem with code generators • For example, NetBeans always calls the default JTreeconstructor myJTree = new JTree(); • One could write “post-creation” code to set the root node text, but this is a static change, not a dynamic one • Thus, this problem forces students to use the API and think about the objects involved and their relationships
Assignment: Object Comm. • Problem:Pass data from a custom dialog box (JFrame) to its parent class • Subproblem: Default member variable access method is private • Solutions: • Change access method • Use Component.getParent()method • Pass reference to parent in overloaded constructor
Exam: FocusTraversalPolicy Human Factors Issues • Why is it important to worry about what happens when the user presses the Tab key? • Because an application in which pressing the Tab key causes focus to jump all over the place is confusing to the user and looks unprofessional.
Exam: FocusTraversalPolicy Use of the API • One way to set focus to a component is call the grabFocus() method. What class is grabFocus() a member of? • JComponent
Exam: FocusTraversalPolicy Deprecated Code • The documentation says that “client code” should not use method grabFocus(). What method does the API recommend using instead of grabFocus()? • requestFocusInWindow()
Exam: FocusTraversalPolicy Class Relationships • A better way to implement your own focus traversal policy is to create a class that extends the built-in FocusTraversalPolicy class. Why must this be a separate class from your application’s main class, which typically extends JFrame? • Because a class can only extend one other class.
Exam: FocusTraversalPolicy Class Relationships • We have seen three ways to create an instance of a class that you might use for your own focus traversal policy. Name two that you might use to create a FocusTraversalPolicy class for use in your application. • Create an anonymous class. • Create a named inner class. • Create a separate external class.
Exam: FocusTraversalPolicy Class Relationships and Use of API • Look up FocusTraversalPolicy and its methods in the Java API. You will see that five of the six methods are abstract. What implication does this have for the class that you create that is derived from the built-in FocusTraversalPolicy class? • You must implement each of the abstract methods or you will not be able to instantiate your class.
Exam: FocusTraversalPolicy Application of the Concept • Once you have defined a focus traversal policy, you must associate it with your JFrame. What method performs this function, and what class is that method a member of? • setFocusTraversalPolicy in class java.awt.Container
Additional Information • Course websites • Lecture notes, assignments, and examples • teaching.cs.uml.edu/~heines • See courses 91.461 and 91.462 on the Teaching page • Contact us • heines@cs.uml.edu • mschedlb@cs.uml.edu • Our websites • http://www.cs.uml.edu/~heines • http://www.cs.uml.edu/~mschedlb