CS 180 Problem Solving and Object Oriented Programming Fall 2011

Similar documents
CS 180 Problem Solving and Object Oriented Programming Fall 2011

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

CS 180 Problem Solving and Object Oriented Programming Fall 2011

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

PESIT Bangalore South Campus

ECE 462 Object-Oriented Programming using C++ and Java. Key Inputs in Java Games

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

The AWT Event Model 9

Graphical User Interfaces 2

Advanced Java Programming (17625) Event Handling. 20 Marks

Graphical User Interfaces 2

Graphical User Interfaces 2

Unit 7: Event driven programming

GUI in Java TalentHome Solutions

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Programming Languages and Techniques (CIS120)

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

Programming Languages and Techniques (CIS120)

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CS 180 Problem Solving and Object Oriented Programming Fall 2011

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Event Driven Programming

CS 180 Problem Solving and Object Oriented Programming Fall 2011

AP Computer Science Unit 13. Still More Graphics and Animation.

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Programming Languages and Techniques (CIS120)

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Programming Languages and Techniques (CIS120)

CS 180 Problem Solving and Object Oriented Programming Fall 2010

Use the Scantron sheets to write your answer. Make sure to write your Purdue ID (NOT Purdue Login ID) and your name on the Scantron answer sheet.

CS 134 Test Program #2

Programming Languages and Techniques (CIS120)

CS 180 Problem Solving and Object Oriented Programming Fall 2011

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

CSIS 10A Assignment 14 SOLUTIONS

Programming Languages and Techniques (CIS120e)

CS 180 Problem Solving and Object Oriented Programming Fall 2010

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

CS 251 Intermediate Programming GUIs: Event Listeners

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events

SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

COMPSCI 230. Software Design and Construction. Swing

CSIS 10A Practice Final Exam Solutions

Handling Mouse and Keyboard Events

CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed

CS2110. GUIS: Listening to Events

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

GUI Event Handlers (Part II)

CS 134 Programming Exercise 7:

mith College Computer Science CSC231-Assembly Week #1 Fall 2018 Dominique Thiébaut

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each

EECS168 Exam 3 Review

61A Lecture 3. Friday, September 5

Correctness of specifications. Correctness. Correctness of specifications (2) Example of a Correctness Proof. Testing versus Correctness Proofs

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam

THE UNIVERSITY OF AUCKLAND

Java for Interfaces and Networks (DT3010, HT10)

CS 106A, Lecture 14 Events and Instance Variables

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

Computer Science II Data Structures

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Programming Languages and Techniques (CIS120)

H212 Introduction to Software Systems Honors

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims

CS 177 Week 15 Recitation Slides. Review

3 Events and Listeners

Lecture 3: Java Graphics & Events

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

CSE 113 A. Announcements - Lab

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

Programming Languages and Techniques (CIS120)

: 15. (x, y) (x, y ) (x, y) ( x x = 1 y = y ) (x = x y y = 1) (12.1.1)

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Computer Programming: C++

CS 2113 Software Engineering

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Interacción con GUIs

CS 170 Java Programming 1. Week 9: Learning about Loops

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

1 Definitions & Short Answer (5 Points Each)

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

CS 106A, Lecture 14 Events and Instance Variables

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Science and Engineering

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

H212 Introduction to Software Systems Honors

CS Fall Homework 5 p. 1. CS Homework 5

61A Lecture 4. Monday, September 9

CS18000: Problem Solving And Object-Oriented Programming

Transcription:

CS 180 Problem Solving and Object Oriented Programming Fall 2011 hlp://www.cs.purdue.edu/homes/apm/courses/cs180fall2011/ This Week: Notes for Week : Oct 24-28, 2011 Aditya Mathur Department of Computer Science Purdue University West LafayeLe, IN, USA /24-28 1. Again: MulQdimensional arrays 2. Methods 3. Menus 4. MenuListener 5. KeyListener 6. Quiz

Readings and Exercises for Week Readings: Interfaces:.2,.3 Methods: 8.2, 8.3 GUI: 13.2, 13.3, 13.4 Exercises: 13.16, 13.17, 13.22

Announcements 1. No Feast with Faculty on Wednesday October 26, will resume next week. 2. No office hours on Wednesday Oct 26. 3. Wednesday lecture by Dr Gustavo Rodriguez- Rivera. 4. Homework 7 on the course site. 5. Extended offie hours on Friday 2-5pm. 6. This week s lab will be an extended version of last week s lab. 7. Try this for fun: Qc- tac- toe data collecqon: hlp://oj.cs.purdue.edu:60080/sss/

Methods, method signatures, Interfaces and abstract classes /17/2011 9 4

Method declaraqon Syntax: modifier type name (parameters){ body consisqng of 0 or more statements. }; Examples: public String getmodel (); // return string, no parameters public boolean search(int []a, int n); // return boolean, two parameters private void doit(); // No return value, no parameter /17/2011 9 5

Method call: Examples DeclaraQon: public String getmodel (){. } String s=getmodel();// Return value assigned to s. DeclaraQon: public boolean search(int []a, int n){.} boolean found=search(b, 15); // Return value assigned to found DeclaraQon: public void doit();{.. } doit(); // No return value. /17/2011 9 6

Method call: Parameter correspondence Formal parameters DeclaraQon: public boolean search(int []a, int n){.} boolean found=search(b, 15); // Return value assigned to found Actual parameters: /17/2011 9 7

Method call: Parameter passing Formal parameters DeclaraQon: public boolean search(int []a, int n){.} boolean found=search(b, 15); // Return value assigned to found Actual parameters: Reference to array b is passed to search; thus, inside search a reference to a is actually a reference to b. 15 is passed to search for n. Thus, inside search n is 15. /17/2011 9 8

Method signatures: name and parameters: Yes signature void move (double dx, double dy); signature void move (int dx, int dy); signature void sort (int[] a); signature void sort (double[] a); disqnct disqnct /17/2011 9 9

Method signatures: return type: Not included boolean move (double dx, double dy); Not disqnct int move (double dx, double dy); Even though return types are different /17/2011 9

Classes, Interface, and Abstract Classes Class Constructor, methods; used to create objects Interface: Class with only method signatures, no code An interface does not implement any method Methods are implemented by a class that uses the interface Thus, mulqple implementaqons could exist /17/2011 9 11

Interface: Example 1 interface Car{ } void cruise(double speed); // cruise at speed void startengine(); // Start car engine Methods with signatures; not implemented void slowdown(double speed); // Slow down to speed double getspeed(); // Returns current speed String getlicense(); // Returns license plate number /17/2011 9 12

Interface: Example 2 interface AcQonListener{ } void acqonperformed(acqonevent); A user class implements an AcQonListener and thus implements the acqonperformed() method. /17/2011 9 13

Interface: Example 3 interface MouseListener{ void mouseclicked(mouseevent e) void mouseentered(mouseevent e) void mouseexited(mouseevent e) void mousepressed(mouseevent e) void mousereleased(mouseevent e) } A user class implements a MouseListener. It should implement all of the above methods. /17/2011 9 14

Interface: Example 1 : ImplementaQon public class MyCar implements Car{ public void cruise(double speed){ // cruise at speed // Code to get car into cruise mode } Methods implemented public void startengine(){ // Start car engine // Code to start the engine } // Other methods } /17/2011 9 15

Interface: When and why? Use an interface to specify a contract between two parqes. When a team is developing an applicaqon, a core group of people can specify interfaces while other groups are free to implement these as they consider appropriate. Example: A car manufacturer can specify an interface that will be used by sopware developers of all models made by this manufacturer. Interfaces allow specificaqon of uniform and contractual obligaqons across several products. /17/2011 9 16

Abstract class Similar to interface but may: Implement zero or more methods Provides abstract methods An abstract method is one with only a signature but no implementaqon A Java class can extend an abstract class by implemenqng one or more of its abstract methods. /17/2011 9 17

Abstract class: Example Empty implementaqons that may be overridden public abstract class MouseAdapter{ void mouseclicked(mouseevent e){} void mouseentered(mouseevent e){} void mouseexited(mouseevent e){} void mousepressed(mouseevent e) {} void mousereleased(mouseevent e) {} } A user class extends a MouseAdapter. /17/2011 9 18

Back to GUIs Aditya Mathur. CS 180. Fall 2011. Week

Review Widgets: frame, panel, bulon, text field AcQonListener and MouseListener acqon Performed() mouseentered(), mouseexited(), mouseclicked(), mousepressed(), mousereleased getsource() Interface

Problem 1 Write a program to generate the GUI shown next. It has a menu bar with two menus labeled College and Major and one text box. College has three menu items: Purdue, IU, and Notre Dame. Major has two menu items: CS and History. The text box must display the item and the major selected.

Problem 1: GUI: Menu Items: College

Problem 1: GUI: Menu Items: Major

Live demo: Example 1

Problem 2 Modify the previous program so that the GUI now responds to menu selecqon events. The detected event is displayed in a message text field. The selected college is displayed in the College choice text field and the selected major in the Major choice text field. If Purdue is selected then add Computer Engineering to Majors. Delete this major if IU or Notre Dame are selected.

MenuListener The following methods must be implemented: menuselected(menuevent m) menudeselected(menuevent m) menucanceled(menuevent m)

Live demo: Example 2

Problem 3 Write a program that creates three text boxes. Box 1 has the iniqal focus. When a string is typed in box 1 and enter pressed, the typed string is echoed in box 3 and the focus moves to box 2 When a string is typed in box 3, it is echoed in box 3 an the focus switches to box 1.

KeyListener The following methods must be implemented: keytyped(keyevent m) keypressed(keyevent m) keyreleased(keyevent m) If k is a KeyEvent object then k.getchar() returns the character typed. k.vk_enter is the code for the enter key.

Live demo: Example 3

Quiz: /26/2011

Q1. AcQonListener is (a) An object (b) A variable (c) An interface (d) A method

Q2. When using an interface we must implement (a) all methods in the interface (b) Only the methods needed by the class (c) Only the acqonperformed() method (d) Any one method in the interface

Q3. MouseListener is (a) An object (b) A variable (c) An interface (d) A method

Q4. A method used to find the object that generated an AcQonEvent is (a) acqonperformed() (b) addacqonlistener() (c) getsource() (d) addmouselistener()

Q5. JTextField message=new JTextField( ); creates a text field named message (a) with columns (b) with the default number of columns and displays an empty string (c) with columns and displays (d) with columns and displays a string consisqng of spaces

Q6. When using an abstract class we must implement (a) All methods in the abstract class (b) Only the methods needed by the class (c) Only the acqonperformed() method (d) Any one method in the abstract class

End of Quiz: /26/2011

Problem 4 [Try on your own] Write a math game program to generate a GUI shown below. The GUI has two bulons labeled Div by 3 and Not Div By 3 and two text boxes. A random integer is displayed in one textbox and the player must decide whether or not it is divisible by 3. Score is displayed in the other text box. The game never ends unless the program is forcefully terminated.

Live demo: Example 4

Problem 4 based exercise Modify the Divide by 3 game so that it displays the total duraqon of the game in minutes and seconds in a separate text box.

Week : October 24-28, 2011 Hope you enjoyed this week! QuesQons? Contact your recitaqon instructor. Make full use of our office hours.