CS 180 Problem Solving and Object Oriented Programming Fall 20 hmp://www.cs.purdue.edu/homes/apm/courses/cs180fall20/ Notes for Week : Oct 31- Nov 4, 20 Aditya Mathur Department of Computer Science Purdue University West LafayeMe, IN, USA This Week: 10/31- /1 1. Layouts, menus, listeners 2. Methods and parameters 3. Inheritance 4. Thread class 5. Concurrency: Task decompositon Data decompositon
Readings and Exercises for Week Readings: GUI: 13.2, 13.3 Concurrency: 14.1, 14.2, 14.3, 14.4 Method and data hiding: 8.2, 8.3 Inheritance:.2,.3 Exercises: 9.1, 9.2, 9.3 Aditya Mathur. CS 180. Fall 20. Week
Announcements Feast with faculty: Wednesday Nov 2, 20. 6:30pm. Ford Dining Hall Exam 2: Wednesday November 9, 8-10pm. WTHR 200. Review for Exam 2: Monday November 7, 20 Special class: Sunday November 6, 20. 4pm. LWSN 3102AB Project 4 available. HW 8 available. Aditya Mathur. CS 180. Fall 20. Week
Problem: GUI Write a Java app that creates the GUI shown below and performs tasks as explained. Menu Menu bar bumons Aditya Mathur. CS 180. Fall 20. Week
Problem: Menu an Menu items Menu items Aditya Mathur. CS 180. Fall 20. Week
Problem: AcTons Bird sound Bird picture Terminate program Aditya Mathur. CS 180. Fall 20. Week
Problem: CreaTng Sounds import java.applet.*; import java.net.url; Could also be a URL birdurl; complete URL AudioClip birdclip; try{ birdurl=new URL("file:bird.au"); catch(excepton e){ birdclip=applet.newaudioclip(birdurl); Aditya Mathur. CS 180. Fall 20. Week
Problem: CreaTng Image Icons import javax.swing.*; ImageIcon birdpic// Declare an ImageIcon object; JBuMon bumon=new JBuMon(); birdpic=new ImageIcon("bird.jpg );// Create an icon from a picture; bumon.seticon(birdpic);// Add an icon to a bumon Could also be a URL Aditya Mathur. CS 180. Fall 20. Week
Live demo Aditya Mathur. CS 180. Fall 20. Week
Announcements Feast with faculty: Wednesday Nov 2, 20. 6:30pm. Ford Dining Hall Exam 2: Wednesday November 9, 8-10pm. WTHR 200. Review for Exam 2: Monday November 7, 20 Special class: Sunday November 6, 20. 4pm. LWSN 3102AB Aditya Mathur. CS 180. Fall 20. Week
Back to GUIs, Classes, objects, methods! Aditya Mathur. CS 180. Fall 20. Week
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 20. Week
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 20. Week
Instance variables public class Fruit{ public String name; public String color; public int type public class Market{ public statc void main(string [] args){ System.out.println(Fruit.color); Will this compile? Aditya Mathur. CS 180. Fall 20. Week
Instance variables public class Fruit{ public String name; public String color= Red ; public int type; public void changecolor (String c){ color=c; 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? Aditya Mathur. CS 180. Fall 20. Week
Class variables public class Fruit{ public String name; public statc String color; public int type; public class Market{ public statc void main(strings[] args){ System.out.println(Fruit.color); Will this compile? Yes, because color is statc and public. Aditya Mathur. CS 180. Fall 20. Week
Class: variables public class Fruit{ public String name; public statc String color; public int type 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. Aditya Mathur. CS 180. Fall 20. Week
Private/Public variables public class Fruit{ public String name; private String color= Red ; int type; 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? Aditya Mathur. CS 180. Fall 20. Week
Private/Public variables public class Fruit{ public class Market{ public String name; private String color= Red ; int type; public void changecolor (String c){ color=c; public String getcolor(){ return color; 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? Aditya Mathur. CS 180. Fall 20. Week
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; Mutator method Accessor method Aditya Mathur. CS 180. Fall 20. Week
Inheritance Aditya Mathur. CS 180. Fall 20. Week
Inheritance: What and Why? 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. Aditya Mathur. CS 180. Fall 20. Week
Simple inheritance Base class D inherits from A Class A Class D B inherits from A Class B Subclass C inherits from B Class C Aditya Mathur. CS 180. Fall 20. Week
Simple inheritance: Example RV inherits from Vehicle Class Vehicle Class RecreaTonalVehicle Car inherits from Vehicle Class Car MyCar inherits from Car Class MyCar Aditya Mathur. CS 180. Fall 20. Week
MulTple inheritance: Not Allowed in Java! Class Vehicle Class Engine Car inherits from Vehicle Class Car and from Engine Aditya Mathur. CS 180. Fall 20. Week
Inheritance: Code for a Base class 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); Aditya Mathur. CS 180. Fall 20. Week
Inheritance: Code for a subclass 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); Aditya Mathur. CS 180. Fall 20. Week
Inheritance 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. Aditya Mathur. CS 180. Fall 20. Week
Inheritance: Another example public class Gui extends JFrame{ All methods and local variables/ objects of JFrame are available to Gui. Aditya Mathur. CS 180. Fall 20. Week
Accessibility rules: Let us fill this table If a variable or an object declaraton uses this modifier then can this variable or object be used inside? Y: Yes. N: No. Modifier Class Package Subclass World public Y Y Y Y private Y N N N None (package private) Y Y Y/N N Package: A collecton of classes identfied as a Java package. World: CollecTon of packages Aditya Mathur. CS 180. Fall 20. Week
Classes etc.: summary 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). Aditya Mathur. CS 180. Fall 20. Week
Concurrent Programming Aditya Mathur. CS 180. Fall 20. Week
Dividing work into small segments Program class class class Instance variables Class variables Methods Instance variables Class variables Methods Instance variables Class variables Methods Aditya Mathur. CS 180. Fall 20. Week
Concurrency Main thread controls the distributon of work Distribute work T T 1 T 2.... T N Task T is divided into N simpler tasks and executed in parallel N threads executng concurrently to execute N tasks T 1, T 2 T N. T Aditya Mathur. CS 180. Fall 20. Week
Threads 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. Aditya Mathur. CS 180. Fall 20. Week
Problem 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 Aditya Mathur. CS 180. Fall 20. Week
Problem: SoluTon architecture T T: Perform the given task T 1 : Compute a part of the expression T 1 T 2 T 2 : Compute a other part of the expression How many threads? T Combine the results of T 1 and T 2 Aditya Mathur. CS 180. Fall 20. Week
Problem: Algorithm Thread A: Input: x and y Compute sin(x)/cos(y) Thread B: Input: x, y, z Compute x 2 +y 2 x 2 - y 2 if z is true or 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; Aditya Mathur. CS 180. Fall 20. Week
Problem: Program Aditya Mathur. CS 180. Fall 20. Week
Week : October 31- November 4, 20 Hope you enjoyed this week! QuesTons? Contact your recitaton instructor. Make full use of our office hours. Aditya Mathur. CS 180. Fall 20. Week