NotesWeek11.Nov 1-5

NotesWeek11.Nov 1-5 - Programming Fall2010...

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: CS180ProblemSolvingandObjectOriented Programming Fall2010 hLp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ ThisWeek: NotesforWeek11: Nov15,2010 AdityaMathur 11/1 1. Q&A 2. Classandinstancevariables 3. private,public,staTc Methodsandparameters 2. Inheritance 3. Threadclass 4. Concurrency: TaskdecomposiTon DatadecomposiTon 11/3 1. DepartmentofComputerScience PurdueUniversity WestLafayeLe,IN,USA ReadingsandExercisesforWeek11 Readings: Methodanddatahiding: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,8.3 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 2 SpecialSessions LabHelp: ThursdayNovember4,5:307:00pm MondayNovember11,5:307pm LWSNB158 Project4Due:MondayNovember15,2010 SpecialClass: SundayNovember7,57pm LWSN3102AB 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 3 LunchmeeTng When:ThursdayNovember4,2010.Noon1:30pm Where:FordDiningHall Meet:Upstairsintheseparatediningroom ALendees: PleasesignupwithyourrecitaToninstructororemail mebyWednesdayNovember3 Lookforwardtoseeingyou! 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 4 Feedback:IlikedAndroidlabs. (a) Alot (b) Somewhat (c) Notatall 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 5 Feedback:Labinstructorsarehelpful. (a) Alot (b) Somewhat (c) Notatall 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 6 Feedback:SofarIamlikingthecourse(10likinga lot,1notlikingatall). (a) 810 (b) 47 (c) 13 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 7 EndofFeedback:11/1/2010 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 8 BacktoClasses,objects,methods! 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 9 Review Listeners:MenuListener,KeyListener Widgets:JMenuBar,JMenu,JMenuItem Menuevents:menuSelected(),menuDeselected(), menuCanceled() Keyevents:keyPressed(),keyReleased(),keyTyped() getSource()andgetKeyChar() 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 10 Dividingworkintosmallsegments Program class Instancevariables Classvariables Methods class Instancevariables Classvariables Methods class Instancevariables Classvariables Methods 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 11 Instancevariables publicclassFruit{ publicStringname; publicStringcolor; publicinttype } publicclassMarket{ publicstaTcvoidmain(Stringargs){ System.out.println(Fruit.color); } } Willthiscompile? AdityaMathur.CS180.Fall2010.Week 11 11/5/2010 12 Instancevariables publicclassFruit{ publicStringname; publicStringcolor="Red"; publicinttype; publicvoid changeColor(Stringc){ color=c; } } publicclassMarket{ publicstaTcvoidmain(Stringargs){ Fruitapple=newFruit(); Fruitpear=newFruit(); pear.changeColor("Green"); System.out.println(apple.color); System.out.println(pear.color); } } Willthiscompile? 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 13 Classvariables publicclassFruit{ publicclassMarket{ publicStringname; publicstaTcvoidmain(Stringsargs){ publicstaTcStringcolor; publicinttype; System.out.println(Fruit.color); } } } Willthiscompile? AdityaMathur.CS180.Fall2010.Week 11 11/5/2010 14 Class:variables publicclassFruit{ publicStringname; publicstaTcStringcolor; publicinttype } publicclassMarket{ publicstaTcvoidmain(Stringsargs){ Fruitapple=newFruit(); System.out.println(apple.color); } Willthiscompile? AdityaMathur.CS180.Fall2010.Week 11 11/5/2010 15 Private/Publicvariables publicclassFruit{ publicStringname; privateStringcolor="Red"; inttype; } publicclassMarket{ publicstaTcvoidmain(Stringargs){ Fruitapple=newFruit(); Fruitpear=newFruit(); System.out.println(apple.color); System.out.println(pear.color); } } Willthiscompile? AdityaMathur.CS180.Fall2010.Week 11 11/5/2010 16 Private/Publicvariables publicclassFruit{ publicStringname; privateStringcolor="Red"; inttype; publicclassMarket{ publicstaTcvoidmain(Stringargs){ Fruitapple=newFruit(); Fruitpear=newFruit(); pear.changeColor("Green"); System.out.println(apple.getColor()); System.out.println(pear.getColor()); } } publicvoid changeColor(Stringc){ color=c; } PublicStringgetColor(){ Willthiscompile? returncolor; } } AdityaMathur.CS180.Fall2010.Week 11/5/2010 11 17 Accessorandmutatormethods publicclassFruit{ publicStringname; privateStringcolor="Red"; inttype; publicvoid changeColor(Stringc){ color=c; } publicStringgetColor(){ returncolor; } } 11/5/2010 Mutatormethod Accessormethod AdityaMathur.CS180.Fall2010.Week 11 18 Accessibilityrules:Letusfillthistable Ifavariableoran objectdeclaraTon usesthismodifier Modifier public private none thencanthis variableorobject beusedinside? Package Y N Y Y:Yes. N:No. Class Y Y Y Subclass Y N N World Y N N Package:AcollecTonofclassesidenTfiedasaJavapackage. World:CollecTonofpackages 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 19 Localandglobalvariables/objects publicclassX{ publicJBuLonplus; publicvoiddoSomething(Stringc){ intz; for(inti=0;i<10;i++){ icanbe intp; Usedhere } System.out.println(i,p); }//endofmethod }//endofclass i,poutofscope 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 pluscan beusedhere z,ccan beusehere 20 Methodsandparameters 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 21 Method[header]declaraTon (OpTonal) Return Access type(required) modifier publicintfind(JBuLonb,Objecto) Method Name (required) 11/5/2010 Formal Parameters 0ormore 22 AdityaMathur.CS180.Fall2010.Week 11 Methodcall publicintfind(JBuLonb,Objecto) Onetoone correspondence offormalandactual parameters intindex=find(buLon,source) Actual Parameters 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 23 Parametertypes Valueparameters: Thatarepassedbyvalue. AllparametersinJavaarepassedbyvalue. Referenceparameters: Objectsandarraysarereferenceparameters.This meansthatareferencetoanobject,nottheobject itself,ispassedtothemethod. 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 24 Passbyvalue:valueofaprimiTvetypeispassed Formalparameter publicvoidtest(intx){ x=3; } Actualparameter Thevalueofpdoesnotchange eventhoughtestsetsitsformal parameterto3. intp=0; test(p); System.out.println(p); 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 25 Passbyvalue:referencetoanobjectispassed Formalparameter publicvoidtest(inta){ a[0]=15; } intb=newint[100]; Actualparameter b[0]=21; test(b); System.out.println(b[0]); Thevalueofb[0]changesto15 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 26 Passbyvalue:referencetoanobjectispassed publicstaTcvoidchangeString(Stringx){ Formalparameter x[0]="Zero"; x=newString[2]; } publicstaTcvoidmain(Stringargs){ Stringy=newString[5]; y[0]="Z"; y[1]="O"; System.out.println(y[0]+""+y[1]); Actualparameter changeString(y); System.out.println(y[0]+""+y[1]); } 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 27 Parameterpassing:anotherexample publicstaTcvoidtest(Stringx,intz){ x[0]="Zero";//Anelementchangedto"Zero" x=newString[2]; z=100;//zsetto100 } publicstaTcvoidmain(Stringargs){ Stringy=newString[5]; intp=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); } 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 28 Parameterpassing:yetanotherexample publicclassPassByRefArray{ staTcintb=newint[100]; publicstaTcvoidtest(inta){ b=a; a[0]=99; a[1]=299; } publicstaTcvoidmain(Stringargs){ b[0]=15; System.out.println(b[0]+""+b[1]); test(b); System.out.println(b[0]+""+b[1]); } } 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 Whatwillbe displayed? 29 Methodparameters publicintfind(JBuLonb,Objecto){ intlen=b.length,index=0; booleanfound=false; while(!found&&index<len){ if(o==b[i]){ found=true; break; } index++; }//endofwhile if(!found) return(1); else return(index); }//endofmethod 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 Parameters Methodtofind whichbuLonwas clicked.Returns buLonindexif foundelsereturnsa 1. 30 Methodparameter:summary Amethodmayhavezeroormoreparameters. Eachparameterhasaname. Eachparametermusthaveatype PrimiTvetypesarepassedbyvalue;referencestoobjectsare passedandnottheobjectitself Eachmethodmusthaveareturntype. Eachparameterbecomesalocalvariableforthemethod. Aconstructorisaspecialmethodthathasnoreturntype. 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 31 Inheritance 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 32 Inheritance publicclassFruit{ publicStringname; privateStringcolor="Red"; inttype; publicvoid changeColor(Stringc){ Color=c; } publicStringgetColor(){ returncolor; } } 11/5/2010 publicclassMangoextendsFruit{ publicStringorigin; publicvoidharvest(){ } publicStringgetOrigin(){ returncolor; } } Allmethodsandlocalvariables/ objectsareavailabletoMango. 33 AdityaMathur.CS180.Fall2010.Week 11 Inheritance:Anotherexample publicclassGuiextendsJFrame{ } Allmethodsandlocalvariables/ objectsofJFrameareavailableto Gui. 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 34 Moreoninheritancelater! 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 35 ConcurrentProgramming 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 36 Concurrency Mainthread controlsthe distribuTonofwork Distribute work T TaskTisdividedinto Nsimplertasksand executedin parallel NthreadsexecuTng concurrently toexecuteNtasksT1, T2...TN. T1 T2 .... TN T 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 37 Threads Threadisaclass AthreadisasequenceofcomputaTonsthat canruninparallelwithotherthreads. OneusestheThreadclasstocreateathread. 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 38 Problem Givendoublesxandyandabooleanz,write aprogramtocomputethefollowing sin(x)/cos(y)+(x2+y2);ifzistrue sin(x)/cos(y)+(x2y2);ifzisfalse 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 39 Problem:SoluTonarchitecture T T1:Computea partofthe expression T:Performthegiventask T1 T2 T2:Computea otherpartof theexpression T Howmanythreads? Combinethe resultsofT1 andT2 40 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 Problem:Algorithm ThreadA: Input:xandy ThreadC(controlthread): nput:x,y,z; I Createanobjecte1(ThreadA); Computesin(x)/cos(y) Createcanobjecte2(ThreadB); tarte1; S tarte2; S aitfore1ande2tocomplete; W ThreadB: Getvaluecomputedbye1; Input:x,y,z Getvaluecomputedbye2; Addthetwovaluesanddisplaythe Compute result; x2+y2ifzistrueor x2y2ifzisfalse 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 41 Problem:Program 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 42 Week11:November15,2010 Hopeyouenjoyedthisweek! QuesTons? ContactyourrecitaToninstructor.Make fulluseofourofficehours. 11/5/2010 AdityaMathur.CS180.Fall2010.Week 11 43 ...
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.

Ask a homework question - tutors are online