NotesWeek9.Oct 18-22 - CS 180 Problem Solving and Object...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 180 Problem Solving and Object Oriented Programming Fall 2010 hLp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This Week: Notes for Week 9: Oct 1822, 2010 Aditya Mathur 10/18 1. 2. 3. 4. 5. Quiz CreaSng GUIs Frames, Panels, BuLons Text fields AcSon listeners 10/20 1. Department of Computer Science Purdue University West LafayeLe, IN, USA GUI review 2. Interfaces and abstract classes 3. MouseListener and MouseAdapters 4. Examples Readings and Exercises for Week 9 Readings: GUI: 12.2, 12.3 Exercises: 12.1, 12.6, 12.7 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 2 Special Sessions Lab Help: Tuesday October 26, 5:307:30pm LWSN B158 Class: Sunday November 7, 46pm LWSN 3102AB 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 3 Lunch meeSng When: Thursday October 28, 2010. Noon1:30pm Where: Ford Dining Hall Meet: Upstairs in the separate dining room ALendees: All are welcome Look forward to seeing you! 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 4 Programming CompeSSon Round 1 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 5 Quiz: 10/18/2010 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 6 Q1. float a=new float[100]; Number of elements in array a is (a) 101 (b) 100 (c) 99 (d) 102 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 7 Q2. int a=new int[100]; Correct values of the index into a range from (a) 0 to 100 (b) 1 to 100 (c) 0 to 99 (d) 1 to 99 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 8 Q3. boolean a=new boolean [5][3]; Maximum number of values of type boolean that can be stored in a at any Sme is (a) 5 (b) 8 (c) 3 (d) 15 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 9 Q4. int a=new int [5]; Number of columns in a is (a) 5 (b) undefined (c) 0 (d) 1 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 10 Q4. float a=new float [5]; for (int r=0; r<6; r++){ a[r]=7; } Which statement is correct? (a) Elements in a are iniSalized to 7 (b) There will be an index out of bounds excepSon (c) 7 cannot be assigned to a variable of type float (d) r must be of type float 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 11 End of Quiz: 10/18/2010 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 12 GUIs 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 13 A simple GUI: Frame Title Color Border 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 14 A simple GUI: Frame with a Panel and Four BuLons BuLon label BuLon BuLon Panel Background color BuLon BuLon 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 15 Problem 1 Write a program to generate a GUI shown below. It has one buLon and one text box. The number of buLon clicks should be displayed in the text field. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 16 Live demo: Example 1 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 17 Problem 2 Write a program to generate a GUI shown below. The GUI has two buLons labeled + and and a text box. Clicking the plus buLon adds to the click count and clicking the minus buLon decrements it. The count is displayed in the text box. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 18 Live demo: Example 2 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 19 Problem 3 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. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 20 Live demo: Example 3 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 21 Problem 3 based exercise Modify the Divide by 3 game so that it displays the total duraSon of the game in minutes and seconds in a separate text box. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 22 Method signatures, Interfaces and abstract classes More GUI 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 23 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); 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 24 disSnct disSnct Method signatures: return type: NO boolean move (double dx, double dy); Not disSnct int move (double dx, double dy); Even though return types are different 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 25 Classes, Interface, and Abstract Classes Class Constructor, methods; used to create objects Interface: Class with only method signatures (abstract methods) An interface does not implement any method Methods are implemented by a class that uses the interface Thus, mulSple implementaSons could exist 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 26 Interface: Example 1 Abstract methods; not implemented interface Car{ void cruise(double speed); // cruise at speed void startEngine(); // Start car engine void slowDown(double speed); // Slow down to speed double getSpeed(); // Returns current speed String getLicense(); // Returns license plate number } 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 27 Interface: Example 2 interface AcSonListener{ void acSonPerformed(AcSonEvent); } A user class implements an AcSonListener. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 28 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 iimplements a MouseListener. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 29 Interface: Example 1 : ImplementaSon public class MyCar implements Car{ public void cruise(double speed){ // cruise at speed // Code to get car into cruise mode } // Code to start the engine } ...... // Other methods } 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 30 Methods implemented public void startEngine(){ // Start car engine Interface: When and why? Use an interface to specify a contract between two parSes. When a team is developing an applicaSon, 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 sosware developers of all models made by this manufacturer. Interfaces allow specificaSon of uniform and contractual obligaSons across several products. Aditya Mathur. CS 180. Fall 2010. Week 9 31 10/20/2010 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 implementaSon A Java class can extend an abstract class by implemenSng one or more of its abstract methods. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 32 Abstract class: Example Empty implementaSons 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. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 33 Problem Write a program to generate a GUI that has two buLons labeled Rose and Hibiscus and a text box. When the mouse enters Rose, the text box should display "Rose entered" and similarly for Hibiscus. When the mouse exits a buLon a similar message should be displayed. When a buLon is clicked, the Display should be "Rose clicked" and similarly for Hibiscus. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 34 Live demo 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 35 Week 9: October 1822, 2010 Hope you enjoyed this week! QuesSons? Contact your recitaSon instructor. Make full use of our office hours. 10/20/2010 Aditya Mathur. CS 180. Fall 2010. Week 9 36 ...
View Full Document

This note was uploaded on 02/05/2012 for the course CS 180 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Ask a homework question - tutors are online