NotesWeek3.Sept 6-10

NotesWeek3.Sept 6-10 - 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 hMp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ NotesforWeek3: September610,2010 AdityaMathur Thisweek: 1. 2. 3. 4. FeedbackforWeek2 Review PrimiTvetypes WriTngsimpleprogramsto solvesimpleproblems DepartmentofComputerScience PurdueUniversity WestLafayeMe,IN,USA ReadingsandExercisesforWeek3 Readings: Chapter1:1.7 Chapter2:2.1,2.2,2.3,2.4 Exercises: 2.18,2.19,2.20 9/8/2010 CS180.Fall2010.Week3 2 Specialhelpsessions Sundays:24pmLWSNB158 9/8/2010 CS180.Fall2010.Week3 3 FeedbackforWeek2 9/8/2010 CS180.Fall2010.Week3 4 Q1. Thelabexerciseswereuseful (Usescaleof110:10mostuseful,1not usefulatall) (a) 810 (b) 47 (c) 13 (d) MissedWeek2lab 9/8/2010 CS180.Fall2010.Week3 5 Q2. TherecitaTonexerciseswereuseful (Usescaleof110:10mostuseful,1not usefulatall) (a) 810 (b) 47 (c) 13 (d) Missedweek2recitaTon 9/8/2010 CS180.Fall2010.Week3 6 Q3. TherecitaToninstructorwashelpful. (Usescaleof110:10mosthelpful,1not helpfulatall) (a) 810 (b) 47 (c) 13 (d) Missedweek2recitaTon 9/8/2010 CS180.Fall2010.Week3 7 Q4. Iunderstandhowtoassignastringtoa stringvariable. (a) 810 (b) 47 (c) 13 (d) Missedweek2lecture 9/8/2010 CS180.Fall2010.Week3 8 Q5. Iunderstandtheneedforimportstatements. (a) 810 (b) 47 (c) 13 (d) Missedweek2lecture 9/8/2010 CS180.Fall2010.Week3 9 Q6. SofarIamlikingthecourse(10likingalot,1 notlikingatall). (a) 810 (b) 47 (c) 13 (d) Missedweek2lecture 9/8/2010 CS180.Fall2010.Week3 10 Review 9/8/2010 CS180.Fall2010.Week3 11 Theedit,compile,executecycle .javafile(s) .classfile(s) (bytecode) Correct Nosyntax program error Compileyour Executeyour program program Syntax Error RunTme Erroror IncorrectOutput Edita Javaprogram InCS180weshalluseDrJavaforediTng,compilingandexecuTon.DrJavaisan IntegratedDevelopmentEnvironmentalsoknownasanIDE.Eclipse,JBuilder,and IntelliJIDEAareafewotherJavaIDEs.ForprogrammingtheRidgeSo`robotwe shalluseRoboJDE. 9/8/2010 CS180.Fall2010.Week3 12 ClassesandObjects Setofrealor virtualobjects Represent Template inJava Create Objects created ClassAnimal animal vehicle student ClassStudent flower dog ClassFlower myDog student ClassVehicle truck ClassDog marysDog 9/8/2010 CS180.Fall2010.Week3 13 String IsasequenceofzeroormoreUnicodecharacters. Examples: "GreeTngs!" "Yourtotaltaxis:" "Pleaseentertheprice:" "" "" DeclaraTon: Stringname="Beatles";//nameisanobjectoftypeString Stringstore="Macy's";//storeisanobjectoftypeString 9/8/2010 CS180.Fall2010.Week3 14 String Expressions: StringfirstName,middleIniTal,lastName; StringfullName; fullName=firstName+middleIniTal+lastName; Stringmessage="Pleaseentertheprice:"; 9/8/2010 CS180.Fall2010.Week3 15 Strings:OtheroperaTons YoumayapplyavarietyofoperaTonstostrings.Examplesfollow. Statement Opera+onused Stringcommend="Bently,"+"goodgirl!"; CatenaTon charfirstChar=commend.charAt(0); movieName.equals("FugiTve") String.valueOf(29) Character extracTon Comparison Conversionto String 9/8/2010 CS180.Fall2010.Week3 16 Types 9/8/2010 CS180.Fall2010.Week3 17 Types Setofvalues SetofOperaTons x a b c 9/8/2010 CS180.Fall2010.Week3 18 PrimiTvetypes:short,int,long Setofintegers SetofOperaTons 12 2010 14 + * % short:2bytes int:4bytes long:8bytes 180 1751 Integer.MAX_VALUE:2311 Long.MAX_VALUE:2631 9/8/2010 Integer.MIN_VALUE:231 Integer.MIN_VALUE:263 19 CS180.Fall2010.Week3 PrimiTvetypes:short,int,long:Examples Realworlden+tyorexpression Type PossiblenameinJava PopulaTonofacountry int countryPopulaTon AgeofapaTent(inyears) short paTentAge Numberofdifferentwaysto arrange15booksinabookshelf Differencebetweentwointegers long bookArrangementCount intor long long diff Numberofwebsites numberOfWebSites 9/8/2010 CS180.Fall2010.Week3 20 PrimiTvetypes:float,double Setofintegers 2010.98135 12.77 Infinity 3.14 Infinity NaN + == * > SetofOperaTons (sample) float:4bytes double:8bytes .2010E4 180.0 1751.0 Float.MAX_VALUE:3.40282347e+38f Float.MIN_VALUE:1.40239846e45f Double.MAX_VALUE:1.79769313486231570e+308 Double.MIN_VALUE:4.94065645841246544e324 9/8/2010 CS180.Fall2010.Week3 21 PrimiTvetypes:float,double:Examples Realworlden+tyorexpression Type PossiblenameinaJava program height Heightofaperson float VoTngpercentage float votePercent Wavelengthofgreenlight double wavelengthLIght PriceofaTcket float TcketPrice double pi(Note:PIisaconstant inJava) CS180.Fall2010.Week3 22 9/8/2010 PrimiTvetypes:boolean Setoflogicalvalues SetofOperaTons (sample) == || false | && != true boolean:1bit;sizenotdefined 9/8/2010 CS180.Fall2010.Week3 23 PrimiTvetypes:boolean:Examples Realworlden+tyorexpression Type PossiblenameinaJava program Valueofx<y; boolean result she/hedrivesacar boolean canDriveCar Classended boolean classEnded 9/8/2010 CS180.Fall2010.Week3 24 PrimiTvetypes:char Setofcharacters (samplevaluesshown) SetofOperaTons (sample) `a' `&' `+' `$' == || | && != char:2bytes,unicodecharacter 9/8/2010 CS180.Fall2010.Week3 25 PrimiTvetypes:char:Examples Realworlden+tyorexpression Type PossiblenameinaJava program middleIniTal MiddleiniTal char LeMerofthealphabet char leMer UScurrencysign char usCurrency 9/8/2010 CS180.Fall2010.Week3 26 Names Usedtodenoteclasses,objects,data Containcharacters;muststartwithaleMer,ora$ signoranunderscore. Examples:height,area1,Dog,$great Lengthunlimited,casesensiTve. Doganddogaredifferentnames. ConvenTon:Allclassnamesbeginwithan uppercaseleMer;allothernamesbeginwith alowercaseleMer. 9/8/2010 CS180.Fall2010.Week3 27 Constants Aconstantissomethingthatcannotchangeduringprogram execuTon. Examples: Integerconstants:0,1,1,+24,29,300009998,O14,0x1B FloaTngpointconstants:0.0,2.345e28,0.000976512 Booleanconstants:true,false Characterconstants:``,`a',`A',`$' Stringconstants:"","","Hi!","AliceinWonderland" 9/8/2010 CS180.Fall2010.Week3 28 NamedConstants Aconstantcanbenamedandthenameusedinsteadofthe constantitself. Examples: finalfloatpi=3.14159; finalbooleandogsExist=true; 9/8/2010 CS180.Fall2010.Week3 29 Variables Avariableissomethingwhosevaluemaychangeduring programexecuTon. Everyvariablehasanameandatype. Everyvariablemustbedeclaredbeforeitisused. 9/8/2010 CS180.Fall2010.Week3 30 DeclaraTons intage; floatheight,area; Stringname boolean intx=1,y=0; StringfirstName="Harry"; 9/8/2010 CS180.Fall2010.Week3 31 Simpleexpressions Expressionsareusedtocompute"something". floatx,y,z; x*y+z;//ArithmeTcexpression,resultsinfloatvalue x<y;//Booleanexpression,resultsinbooleanvalue StringfirstName="Mary",lastName="Jones"; firstName+""+lastName;//Resultsinastring MoreinChapter2!Andyetmoretocome! 9/8/2010 CS180.Fall2010.Week3 32 Assignmentstatement Anassignmentstatementallowsassigningthevalueofanexpression toavariable. floatp=x*y+z;//pgetsthevalueofx*y+z booleanq=x<y;//qgetsthevalueofx<y StringfirstName="Mary",lastName="Jones"; Stringname=firstName+""+lastName; MoreinChapter2!Andyetmoretocome! 9/8/2010 CS180.Fall2010.Week3 33 LetuswriteasimpleJavaprogram: Theproblem WriteaJavaprogramtocomputethe netsaleindollarsgiventhepriceof eachTcketindollarsandthenumber ofTcketssold. 9/8/2010 CS180.Fall2010.Week3 34 Problem:Understanding Thisisaneasyproblem! 9/8/2010 CS180.Fall2010.Week3 35 Problem:DesignofsoluTon KeepthisstepindependentofJava! Step1:Getdata Step2:Computetotalsale Step3:Displaytotalsales 9/8/2010 CS180.Fall2010.Week3 36 Problem:RefinesoluTon:Getdata KeepthisstepindependentofJava! Step1.1:Promptforprice Step1.2:Readprice Step1.3:PromptfornumberofTcketssold Step1.4:ReadnumberofTcketssold 9/8/2010 CS180.Fall2010.Week3 37 Problem:RefinesoluTon:Computetotalsale KeepthisstepindependentofJava! Step2:Totalsale=priceofaTcket*numberof Tcketssold 9/8/2010 CS180.Fall2010.Week3 38 CodethesoluToninJava 9/8/2010 CS180.Fall2010.Week3 39 Week3:September310,2010 Hopeyouenjoyedthisweek! QuesTons? ContactyourrecitaToninstructor.Make fulluseofourofficehours. 9/8/2010 CS180.Fall2010.Week3 40 ...
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