Upload
lenhu
View
244
Download
2
Embed Size (px)
Citation preview
CS 180 Problem Solving and Object Oriented Programming Fall 2011
Notes for Week 11: Oct 31-‐Nov 4, 2011
Aditya Mathur Department of Computer Science Purdue University West LafayeMe, IN, USA
hMp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2011/
1. Layouts, menus, listeners 2. Methods and parameters 3. Inheritance 4. Thread class 5. Concurrency:
• Task decomposiTon • Data decomposiTon
10/31-‐11/1
This Week:
Readings and Exercises for Week 11
Readings: GUI: 13.2, 13.3 Concurrency: 14.1, 14.2, 14.3, 14.4 Method and data hiding: 8.2, 8.3 Inheritance: 11.2, 11.3
Exercises: 9.1, 9.2, 9.3
©Aditya Mathur. CS 180. Fall 2011. Week 11
Announcements
Feast with faculty: Wednesday Nov 2, 2011. 6:30pm. Ford Dining Hall Exam 2: Wednesday November 9, 8-‐10pm. WTHR 200. Review for Exam 2: Monday November 7, 2011 Special class: Sunday November 6, 2011. 4pm. LWSN 3102AB Project 4 available. HW 8 available.
©Aditya Mathur. CS 180. Fall 2011. Week 11
Problem: GUI
©Aditya Mathur. CS 180. Fall 2011. Week 11
Write a Java app that creates the GUI shown below and performs tasks as explained.
buMons
Menu Menu bar
Problem: Menu an Menu items
©Aditya Mathur. CS 180. Fall 2011. Week 11
Menu items
Problem: AcTons
©Aditya Mathur. CS 180. Fall 2011. Week 11
Bird picture
Bird sound
Terminate program
Problem: CreaTng Sounds
©Aditya Mathur. CS 180. Fall 2011. Week 11
import java.applet.*;
AudioClip birdClip;
import java.net.URL;
try{ birdURL=new URL("file:bird.au"); } catch(ExcepTon e){
} birdClip=Applet.newAudioClip(birdURL);
URL birdURL; Could also be a complete URL
Problem: CreaTng Image Icons
©Aditya Mathur. CS 180. Fall 2011. Week 11
import javax.swing.*;
ImageIcon birdPic// Declare an ImageIcon object;
birdPic=new ImageIcon("bird.jpg”);// Create an icon from a picture;
buMon.setIcon(birdPic);// Add an icon to a buMon
JBuMon buMon=new JBuMon(); Could also be a URL
Live demo
©Aditya Mathur. CS 180. Fall 2011. Week 11
Announcements
Feast with faculty: Wednesday Nov 2, 2011. 6:30pm. Ford Dining Hall Exam 2: Wednesday November 9, 8-‐10pm. WTHR 200. Review for Exam 2: Monday November 7, 2011 Special class: Sunday November 6, 2011. 4pm. LWSN 3102AB
©Aditya Mathur. CS 180. Fall 2011. Week 11
Back to GUIs, Classes, objects, methods!
©Aditya Mathur. CS 180. Fall 2011. Week 11
GUIs: What have we covered so far? • Widgets: Frame, panel, buMon, text field, label, menu
bar, menu, menu item
• Listeners: AcTonLsitener, MouseListener, KeyListener • Layouts: FlowLayout, BorderLayout, GridLayout • Incremental development: Layout the GUI in steps
starTng with the frame and ending with the listeners.
©Aditya Mathur. CS 180. Fall 2011. Week 11
GUIs: What have we not covered? • A LOT!
• There are other widgets, layouts, and listeners that we have not covered and will not be able to cover.
• There are many methods available with each widget that we have not covered and will not be able to cover.
• Given what you now know, it should not be difficult to learn more about GUIs on your own!
©Aditya Mathur. CS 180. Fall 2011. Week 11
Instance variables public class Fruit{ public String name; public String color; public int type }
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Market{ public staTc void main(String [] args){ System.out.println(Fruit.color); } }
Will this compile?
Instance variables public class Fruit{
public String name; public String color=“Red”; public int type; public void changeColor
(String c){ color=c;
} }
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Market{ public staTc void main(String [] args){ Fruit apple=new Fruit(); Fruit pear=new Fruit(); pear.changeColor(“Green”); System.out.println(apple.color); System.out.println(pear.color); } }
Will this compile?
Class variables public class Fruit{
public String name; public staTc String color; public int type; }
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Market{ public staTc void main(Strings[] args){ System.out.println(Fruit.color); } }
Will this compile? Yes, because color is staTc and public.
Class: variables public class Fruit{
public String name; public staTc String color; public int type }
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Market{ public staTc void main(Strings[] args){ Fruit apple=new Fruit(); System.out.println(apple.color); }
Will this compile? Yes, because color will be obtained from Fruit which is the parent class of apple.
Private/Public variables public class Fruit{
public String name; private String color=“Red”; int type; }
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Market{ public staTc void main(String [] args){ Fruit apple=new Fruit(); Fruit pear=new Fruit(); System.out.println(apple.color); System.out.println(pear.color); } }
Will this compile?
Private/Public variables public class Fruit{
public String name; private String color=“Red”; int type; public void changeColor
(String c){ color=c;
} public String getColor(){
return color; } }
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Market{ public staTc void main(String [] args){ Fruit apple=new Fruit(); Fruit pear=new Fruit(); pear.changeColor(“Green”); System.out.println(apple.getColor()); System.out.println(pear.getColor()); } }
Will this compile?
Accessor (get) and mutator (set) methods public class Fruit{ public String name; private String color=“Red”; int type; public void changeColor(String c){
color=c; } public String get Color(){
return color; } }
©Aditya Mathur. CS 180. Fall 2011. Week 11
Accessor method
Mutator method
Inheritance
©Aditya Mathur. CS 180. Fall 2011. Week 11
Inheritance: What and Why?
©Aditya Mathur. CS 180. Fall 2011. Week 11
What: A mechanism to create new classes from exisTng classes.
Why: Commonly used stat (variables, objects) and behavior (methods) is encapsulated in one class and can be used by other classes.
Example: JApplet: Your class can extend JApplet and inherit all state and behavior methods in JApplet.
Simple inheritance
©Aditya Mathur. CS 180. Fall 2011. Week 11
Class A
Class B
B inherits from A
Class C
C inherits from B
Class D
D inherits from A
Subclass
Base class
Simple inheritance: Example
©Aditya Mathur. CS 180. Fall 2011. Week 11
Class Vehicle
Class Car
Car inherits from Vehicle
Class MyCar
MyCar inherits from Car
Class RecreaTonalVehicle
RV inherits from Vehicle
MulTple inheritance: Not Allowed in Java!
©Aditya Mathur. CS 180. Fall 2011. Week 11
Class Vehicle
Class Car
Car inherits from Vehicle
Class Engine
and from Engine
Inheritance: Code for a Base class
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Vehicle{ String manufacturer="Unknown"; int wheels=4; int steering=1; int capacity=4; boolean seatBelts=true; public void drive(){ System.out.print("I am driving."); } public void oilChange(String vehicleName){ System.out.println("Oil changed for "+vehicleName); } }
Inheritance: Code for a subclass
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Car extends Vehicle{ int capacity=5; // Overridden instance variable int maxSpeed=130; // Added instance variable public void drive(){ // Overridden method System.out.println("This car drives great. I am driving."); } public staTc void main(String[] args){ Car c=new Car(); Vehicle v=new Vehicle(); System.out.println("This car has "+ c.capacity+ " seats."); System.out.println("This vehicle has a seaTng capacity of "+ v.capacity+"."); System.out.println("This car has seat belts:"+ c.seatBelts); } }
Inheritance
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Fruit{ public String name; private String color=“Red”; int type; public void changeColor
(String c){ Color=c;
} public String getColor(){
return color; } }
public class Mango extends Fruit{ public String origin; public void harvest(){
} public String getOrigin(){
return color; } }
All methods and local variables/objects are available to Mango.
Inheritance: Another example
©Aditya Mathur. CS 180. Fall 2011. Week 11
public class Gui extends JFrame{ }
All methods and local variables/objects of JFrame are available to Gui.
Accessibility rules: Let us fill this table
©Aditya Mathur. CS 180. Fall 2011. Week 11
Modifier Class Package Subclass World
public Y Y Y Y
private Y N N N
None (package private)
Y Y Y/N N
If a variable or an object declaraTon uses this modifier
then can this variable or object be used inside ?
Package: A collecTon of classes idenTfied as a Java package. World: CollecTon of packages
Y: Yes. N: No.
Classes etc.: summary
©Aditya Mathur. CS 180. Fall 2011. Week 11
Base class: Any class, not final, can serve as a base class
Sub class: A class that extends another class
Interface: Not a class but a contract; must be implemented in full before use
Abstract class: Must be extended for use; may contain some methods with implementaTon (concrete methods) and others to be implemented (abstract methods).
Concurrent Programming
©Aditya Mathur. CS 180. Fall 2011. Week 11
Dividing work into small segments
©Aditya Mathur. CS 180. Fall 2011. Week 11
class
Instance variables Class variables Methods
class
Instance variables Class variables Methods
class
Instance variables Class variables Methods
Program
Concurrency
©Aditya Mathur. CS 180. Fall 2011. Week 11
T
T1 T2 TN . . . .
T
Main thread controls the distribuTon of work
N threads execuTng concurrently to execute N tasks T1, T2…TN.
Distribute work
Task T is divided into N simpler tasks and executed in parallel
Threads
©Aditya Mathur. CS 180. Fall 2011. Week 11
• Thread is a class
• A thread is a sequence of computaTons that can run in parallel with other threads.
• One uses the Thread class to create a thread.
Problem
©Aditya Mathur. CS 180. Fall 2011. Week 11
• Given doubles x and y and a boolean z, write a program to compute the following
sin(x)/cos(y)+(x2+y2); if z is true sin(x)/cos(y)+(x2 -‐ y2); if z is false
Problem: SoluTon architecture
©Aditya Mathur. CS 180. Fall 2011. Week 11
T
T1 T2
T
T: Perform the given task
T1: Compute a part of the expression
T2: Compute a other part of the expression
Combine the results of T1 and T2
How many threads?
Problem: Algorithm
©Aditya Mathur. CS 180. Fall 2011. Week 11
Thread A: Input: x and y
Compute sin(x)/cos(y)
Thread B: Input: x, y, z
Compute x2+y2 if z is true or x2-‐y2 if z is false
Thread C (control thread): • Input: x , y, z; • Create an object e1(Thread A ); • Create can object e2(Thread B); • Start e1; • Start e2; • Wait for e1 and e2 to complete; • Get value computed by e1; • Get value computed by e2; • Add the two values and display the
result;
Problem: Program
©Aditya Mathur. CS 180. Fall 2011. Week 11
Week 11: October 31-‐November 4, 2011 Hope you enjoyed this week!
QuesTons?
Contact your recitaTon instructor. Make
full use of our office hours.
©Aditya Mathur. CS 180. Fall 2011. Week 11