CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 11: Nov 1-5, 2010 Aditya Mathur Department of Computer Science Purdue University.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
CS0007: Introduction to Computer Programming Methods: Documentation, Reference Parameters, Modularization 2.
CS 180 Problem Solving and Object Oriented Programming Spring 2011 February 16, 2011 Aditya Mathur Department of Computer Science Purdue University West.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 5: September 19-23, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 13: Nov 14-18, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 10: Oct 24-28, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 8: Oct 10-14, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 13: Nov 15-19, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 12: Nov 7-11, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 9: Oct 17-21, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for the Final Lecture Dec 7, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 3: September 5-9, 2011 Aditya Mathur Department of Computer Science Purdue.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Arrays Recitation – 10/(9,10)/2008 CS 180 Department of Computer Science, Purdue University.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 7: Oct 4-8, 2010 Aditya Mathur Department of Computer Science Purdue University.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 101E – Exam 2 Review Spring 2007 Michele Co. Announcements Review Session Tonight, 7/7:30 p.m., OLS 009 Will be announced via In-class Exam Wednesday.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CSC 142 Computer Science II Zhen Jiang West Chester University
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 5: September 20-24, 2010 Aditya Mathur Department of Computer Science Purdue.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 12: Nov 8-12, 2010 Aditya Mathur Department of Computer Science Purdue.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 10: Oct 25-29, 2010 Aditya Mathur Department of Computer Science Purdue.
Written by: Dr. JJ Shepherd
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
A High Flying Overview CS139 – Fall 2010 How far we have come.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Information and Computer Sciences University of Hawaii, Manoa
CS139 – Fall 2010 How far we have come
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 11: Nov 1-5, 2010 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA 1.Q&A 2.Class and instance variables 3.private, public, static 11/1 This Week: 11/3 1.Methods and parameters 2.Inheritance 3.Thread class 4.Concurrency: Task decomposition Data decomposition

Readings and Exercises for Week 11 Readings: Method and data hiding: 7.2, 7.3 Class: 8.2, 8.3 Inheritance: 10.2, 10.3 Exercises: 7.1, 7.2, 7.3, 7.4, 7.5, 8.1, 8.2, /5/2010 ©Aditya Mathur. CS 180. Fall Week 11 2

Special Sessions Lab Help: Thursday November 4, 5:30-7:00pm Monday November 11, 5:30-7pm LWSN B158 Project 4 Due: Monday November 15, 2010 Special Class: Sunday November 7, 5-7pm LWSN 3102AB 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 3

Lunch meeting When: Thursday November 4, Noon-1:30pm Where: Ford Dining Hall Meet: Upstairs in the separate dining room Attendees: Please sign up with your recitation instructor or me by Wednesday November 3 Look forward to seeing you! 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 4

Feedback: I liked Android labs. 11/5/2010 (a)A lot (b)Somewhat (c)Not at all ©Aditya Mathur. CS 180. Fall Week 11 5

Feedback: Lab instructors are helpful. 11/5/2010 (a)A lot (b)Somewhat (c)Not at all ©Aditya Mathur. CS 180. Fall Week 11 6

Feedback: So far I am liking the course (10 liking a lot, 1 not liking at all). 11/5/2010 (a)8-10 (b)4-7 (c)1-3 ©Aditya Mathur. CS 180. Fall Week 11 7

End of Feedback: 11/1/ /5/2010 ©Aditya Mathur. CS 180. Fall Week 11 8

Back to Classes, objects, methods! 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 9

Review 11/5/2010 Listeners: MenuListener, KeyListener Widgets: JMenuBar, JMenu, JMenuItem Menu events: menuSelected(), menuDeselected(), menuCanceled() Key events: keyPressed(), keyReleased(), keyTyped() getSource() and getKeyChar() ©Aditya Mathur. CS 180. Fall Week 11 10

Dividing work into small segments 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 class Instance variables Class variables Methods class Instance variables Class variables Methods class Instance variables Class variables Methods Program 11

Instance variables 11/5/2010 public class Fruit{ public String name; public String color; public int type } ©Aditya Mathur. CS 180. Fall Week 11 public class Market{ public static void main(String [] args){ System.out.println(Fruit.color); } Will this compile? No: because color is not static 12

Instance variables 11/5/2010 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 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? Yes because we are accessing instance variables via their objects. 13

Class variables 11/5/2010 public class Fruit{ public String name; public static String color; public int type; } ©Aditya Mathur. CS 180. Fall 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. 14

Class: variables 11/5/2010 public class Fruit{ public String name; public static String color; public int type } ©Aditya Mathur. CS 180. Fall 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. 15

Private/Public variables 11/5/2010 public class Fruit{ public String name; private String color=“Red”; int type; } ©Aditya Mathur. CS 180. Fall 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? No, color is private to the Fruit class. 16

Private/Public variables 11/5/2010 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 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? Yes, because now we are using the public getColor() method to get color. 17

Accessor and mutator methods 11/5/2010 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 Week 11 Accessor method Mutator method 18

Accessibility rules: Let us fill this table 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 ModifierClassPackageSubclassWorld publicYYYY privateYNNN noneYYNN 19 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.

Local and global variables/objects 11/5/2010 public class X{ public JButton plus; public void doSomething(String c){ int z; for (int i=0; i<10; i++){ int p; } System.out.println(i, p); } // end of method }// end of class ©Aditya Mathur. CS 180. Fall Week 11 z, c can be use here i can be Used here i, p out of scope plus can be used here 20

Methods and parameters 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 21

Method [header] declaration 11/5/2010 public int find( JButton [] b, Object o) ©Aditya Mathur. CS 180. Fall Week Method Name (required) Formal Parameters 0 or more Return type (required) (Optional) Access modifier

Method call 11/5/2010 public int find( JButton [] b, Object o) ©Aditya Mathur. CS 180. Fall Week 11 Actual Parameters 23 int index=find( button, source) One-to-one correspondence of formal and actual parameters

Parameter types 11/5/2010 Value parameters: That are passed by value. All parameters in Java are passed by value. Reference parameters: Objects and arrays are reference parameters. This means that a reference to an object, not the object itself, is passed to the method. ©Aditya Mathur. CS 180. Fall Week 11 24

Pass by value: value of a primitive type is passed 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 public void test (int x){ x=3; } int p=0; test(p); System.out.println(p); Formal parameter Actual parameter The value of p does not change even though test sets its formal parameter to 3. 25

Pass by value: reference to an object is passed 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 public void test (int [] a){ a[0]=15; } Formal parameter int [] b=new int [100]; b[0]=21; test(b); System.out.println(b[0]); Actual parameter The value of b[0] changes to 15 26

Pass by value: reference to an object is passed 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 Formal parameter public static void changeString(String [] x){ x[0]="Zero"; x=new String [2]; } public static void main(String [] args){ String [] y=new String [5]; y[0]="Z"; y[1]="O"; System.out.println(y[0]+” “+ y[1]); changeString(y); System.out.println(y[0] +” “+ y[1]); } Actual parameter 27

Parameter passing: another example 11/5/2010 ©Aditya Mathur. CS 180. Fall Week public static void test(String [] x, int z){ x[0]="Zero"; // An element changed to "Zero" x=new String [2]; z=100; // z set to 100 } public static void main(String [] args){ String [] y=new String [5]; int p=0; y[0]="Z"; y[1]="O"; System.out.println(p); System.out.println(y[0]+ " "+y[1]); test (y, p); System.out.println(y[0]+ " "+y[1]); System.out.println(p); }

Parameter passing: yet another example 11/5/2010 ©Aditya Mathur. CS 180. Fall Week public class PassByRefArray{ static int [] b=new int [100]; public static void test(int [] a){ b=a; a[0]=99; a[1]=299; } public static void main(String [] args){ b[0]=15; System.out.println(b[0]+" "+b[1]); test(b); System.out.println(b[0]+" "+b[1]); } What will be displayed?

Method parameters 11/5/2010 public int find( JButton [] b, Object o){ int len=b.length, index=0; boolean found=false; while(!found && index<len){ if(o==b[i]){ found=true; break; } index++; } // end of while if(!found) return (-1); else return (index); } // end of method ©Aditya Mathur. CS 180. Fall Week 11 Method to find which button was clicked. Returns button index if found else returns a -1. Parameters 30

Method parameter: summary 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 A method may have zero or more parameters. Each parameter has a name. Each parameter must have a type Primitive types are passed by value; references to objects are passed and not the object itself Each method must have a return type. Each parameter becomes a local variable for the method. A constructor is a special method that has no return type. 31

Inheritance 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 32

Inheritance 11/5/2010 ©Aditya Mathur. CS 180. Fall 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. 33

Inheritance: Another example 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 public class Gui extends JFrame{ } All methods and local variables/objects of JFrame are available to Gui. 34

More on inheritance later! 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 35

Concurrent Programming 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 36

Concurrency 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 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

Threads 11/5/2010 ©Aditya Mathur. CS 180. Fall 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. 38

Problem 11/5/2010 ©Aditya Mathur. CS 180. Fall 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 39

Problem: Solution architecture 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 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?

Problem: Algorithm 11/5/2010 ©Aditya Mathur. CS 180. Fall 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; 41

Problem: Program 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 42

Week 11: November 1-5, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 43