Download presentation
Presentation is loading. Please wait.
Published byGregory Cobb Modified over 9 years ago
2
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 Lafayette, IN, USA http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2011/ 1.Layouts, menus, listeners 2.Methods and parameters 3.Inheritance 4.Thread class 5.Concurrency: Task decomposition Data decomposition 10/31-11/1 This Week:
3
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
4
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
5
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. buttons Menu Menu bar
6
Problem: Menu an Menu items ©Aditya Mathur. CS 180. Fall 2011. Week 11 Menu items
7
Problem: Actions ©Aditya Mathur. CS 180. Fall 2011. Week 11 Bird picture Bird sound Terminate program
8
Problem: Creating 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(Exception e){ } birdClip=Applet.newAudioClip(birdURL); URL birdURL; Could also be a complete URL
9
Problem: Creating 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; button.setIcon(birdPic);// Add an icon to a button JButton button=new JButton(); Could also be a URL
10
Live demo ©Aditya Mathur. CS 180. Fall 2011. Week 11
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
12
Back to GUIs, Classes, objects, methods! ©Aditya Mathur. CS 180. Fall 2011. Week 11
13
GUIs: What have we covered so far? Widgets: Frame, panel, button, text field, label, menu bar, menu, menu item Listeners: ActionLsitener, MouseListener, KeyListener Layouts: FlowLayout, BorderLayout, GridLayout Incremental development: Layout the GUI in steps starting with the frame and ending with the listeners. ©Aditya Mathur. CS 180. Fall 2011. Week 11
14
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
15
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 static void main(String [] args){ System.out.println(Fruit.color); } Will this compile?
16
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 static 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?
17
Class variables public class Fruit{ public String name; public static String color; public int type; } ©Aditya Mathur. CS 180. Fall 2011. Week 11 public class Market{ public static void main(Strings[] args){ System.out.println(Fruit.color); } Will this compile? Yes, because color is static and public.
18
Class: variables public class Fruit{ public String name; public static String color; public int type } ©Aditya Mathur. CS 180. Fall 2011. Week 11 public class Market{ public static 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.
19
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 static 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?
20
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 static 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?
21
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
22
Inheritance ©Aditya Mathur. CS 180. Fall 2011. Week 11
23
Inheritance: What and Why? ©Aditya Mathur. CS 180. Fall 2011. Week 11 What: A mechanism to create new classes from existing 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.
24
Simple inheritance ©Aditya Mathur. CS 180. Fall 2011. Week 11 Class AClass B B inherits from A Class C C inherits from B Class D D inherits from A Subclass Base class
25
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 RecreationalVehicle RV inherits from Vehicle
26
Multiple 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
27
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); }
28
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 static 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 seating capacity of "+ v.capacity+"."); System.out.println("This car has seat belts:"+ c.seatBelts); }
29
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.
30
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.
31
Accessibility rules: Let us fill this table ©Aditya Mathur. CS 180. Fall 2011. Week 11 ModifierClassPackageSubclassWorld publicYYYY privateYNNN None (package private) YYY/NN If a variable or an object declaration uses this modifier then can this variable or object be used inside ? Package: A collection of classes identified as a Java package. World: Collection of packages Y: Yes. N: No.
32
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 implementation (concrete methods) and others to be implemented (abstract methods).
33
Concurrent Programming ©Aditya Mathur. CS 180. Fall 2011. Week 11
34
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
35
Concurrency ©Aditya Mathur. CS 180. Fall 2011. Week 11 TT1T1 T2T2 TNTN.. T Main thread controls the distribution of work N threads executing concurrently to execute N tasks T 1, T 2 …T N. Distribute work Task T is divided into N simpler tasks and executed in parallel
36
Threads ©Aditya Mathur. CS 180. Fall 2011. Week 11 Thread is a class A thread is a sequence of computations that can run in parallel with other threads. One uses the Thread class to create a thread.
37
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)+(x 2 +y 2 ); if z is true sin(x)/cos(y)+(x 2 - y 2 ); if z is false
38
Problem: Solution architecture ©Aditya Mathur. CS 180. Fall 2011. Week 11 T T1T1 T2T2 T T: Perform the given task T 1 : Compute a part of the expression T 2 : Compute a other part of the expression Combine the results of T 1 and T 2 How many threads?
39
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 x 2 +y 2 if z is true or x 2 -y 2 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;
40
Problem: Program ©Aditya Mathur. CS 180. Fall 2011. Week 11
41
Week 11: October 31-November 4, 2011 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. ©Aditya Mathur. CS 180. Fall 2011. Week 11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.