Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees,...
Transcript of Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees,...
![Page 1: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/1.jpg)
MobileDevelopmentWorkshopDAY1: INTRODUCTIONTOJAVA
![Page 2: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/2.jpg)
OverviewMorningsession◦ Computerhardware,software,andprogramming languages◦ TheJavaprogramming language◦ Simple Javastatements◦ Data,variables,andtypes◦ Classes,objects,andmethods
Afternoonsession◦ Controlflow◦ Conditional statements◦ Loops
![Page 3: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/3.jpg)
HardwareandSoftwareComputersystemsconsistofhardware andsoftware.◦ Hardwareincludesthetangible partsofcomputersystems.◦ Softwareincludesprograms - setsofinstructionsforthecomputertofollow.
Familiaritywithhardwarebasicshelpsusunderstandsoftware.
![Page 4: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/4.jpg)
MemoryMemoryholds◦ programs◦ dataforthecomputer toprocess◦ theresultsof intermediateprocessing.
Twokindsofmemory◦ mainmemory◦ auxiliarymemory
![Page 5: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/5.jpg)
Bits,Bytes,andAddressesAbit isadigitwithavalueofeither0or1.Abyte consistsof8bits.Eachbyteinmainmemoryresidesatanumberedlocationcalleditsaddress.
![Page 6: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/6.jpg)
MainMemoryFigure1.1
![Page 7: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/7.jpg)
ProgramsAprogram isasetofinstructionsforacomputertofollow.Weuseprogramsalmostdaily(email,wordprocessors,videogames,bankATMs,etc.).Followingtheinstructionsiscalledrunning orexecuting theprogram.
![Page 8: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/8.jpg)
RunningaProgram
![Page 9: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/9.jpg)
ProgrammingLanguagesHigh-level languagesarerelativelyeasytouse◦ Java,C#,C++,VisualBasic,Python,Ruby.
Unfortunately,computerhardwaredoesnotunderstandhigh-levellanguages.◦ Therefore,ahigh-levellanguageprogrammustbetranslatedintoalow-levellanguage.
![Page 10: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/10.jpg)
CompilersAcompilertranslatesaprogramfromahigh-levellanguagetoalow-levellanguagethecomputercanrun.Youcompile aprogrambyrunningthecompileronthehigh-level-languageversionoftheprogramcalledthesourceprogram.Compilersproducemachine- or assembly-languageprogramscalledobjectprograms.
![Page 11: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/11.jpg)
JavaByte-CodeTheJavacompiler doesnottranslateaJavaprogramintoassemblylanguage ormachinelanguage foraparticularcomputer.Instead,ittranslatesaJavaprogramintobyte-code.◦ Byte-codeisthemachinelanguage forahypotheticalcomputer(or interpreter)calledtheJavaVirtualMachine.
![Page 12: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/12.jpg)
CompilingandRunningaProgram
![Page 13: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/13.jpg)
WritingourfirstJavaProgramsWriteaprogramthatoperateslikethis:
Samplescreenoutput
![Page 14: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/14.jpg)
PrintingtothescreenSystem.out.println (“Whatever you want to print”);
System.out isanobjectforsendingoutputtothescreen.println isamethodtoprintwhateverisinparenthesestothescreen.
![Page 15: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/15.jpg)
PrintingtothescreenThe object performs an action when you invoke or call one of its methodsobjectName.methodName(argumentsTheMethodNeeds);
![Page 16: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/16.jpg)
Object-OrientedProgrammingOurworldconsistsofobjects (people,trees,cars,cities,airlinereservations,etc.).
Objectscanperformactionswhichaffectthemselvesandotherobjectsintheworld.
Object-orientedprogramming(OOP)treatsaprogramasacollectionofobjectsthatinteractbymeansofactions.
![Page 17: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/17.jpg)
OOPTerminologyObjects,appropriately,arecalledobjects.Actions arecalledmethods.Objectsofthesamekindhavethesametype andbelongtothesameclass.◦ Objectswithinaclasshaveacommonsetofmethodsandthesamekindsofdata
◦ buteachobjectcanhaveit’sowndatavalues.
![Page 18: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/18.jpg)
OOPDesignPrinciplesOOPadherestothreeprimarydesignprinciples:◦ Encapsulation◦ Polymorphism◦ Inheritance
![Page 19: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/19.jpg)
IntroductiontoEncapsulationThedataandmethodsassociatedwithanyparticularclassareencapsulated(“puttogetherinacapsule”),butonlypartofthecontentsismadeaccessible.◦ Encapsulationprovidesameansofusingtheclass,butitomitsthedetailsofhowtheclassworks.
◦ Encapsulationofteniscalledinformationhiding.
![Page 20: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/20.jpg)
ExampleAnautomobileconsistsofseveralpartsandpiecesandiscapableofdoingmanyusefulthings.◦ Awarenessoftheacceleratorpedal,thebrakepedal,andthesteeringwheelisimportanttothedriver.
◦ Awarenessofthefuelinjectors,theautomaticbrakingcontrolsystem,andthepowersteeringpumpisnotimportanttothedriver.
![Page 21: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/21.jpg)
IntroductiontoInheritanceClassescanbeorganizedusinginheritance.
Aclassatlowerlevelsinheritsallthecharacteristicsofclassesaboveitinthehierarchy.
Ateachlevel,classificationsbecomemorespecializedbyaddingothercharacteristics.
Higherclassesaremoreinclusive;lowerclassesarelessinclusive.
![Page 22: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/22.jpg)
IntroductiontoInheritance
![Page 23: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/23.jpg)
VariablesVariables storedatasuchasnumbersandletters.◦ Thinkofthemasplacestostoredata.◦ Theyareimplementedasmemorylocations.
Thedatastoredbyavariableiscalleditsvalue.◦ Thevalueisstoredinthememorylocation.
Itsvaluecanbechanged.
![Page 24: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/24.jpg)
NamingandDeclaringVariablesChoosenamesthatarehelpfulsuchascount orspeed,butnotc ors.
Whenyoudeclare avariable,youprovideitsnameandtype.
int heightInCentimeters,age;
Avariable'stype determineswhatkindsofvaluesitcanhold(int, double, char, etc.).
Avariablemustbedeclaredbeforeitisused.
![Page 25: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/25.jpg)
DataTypesAclasstype isusedforaclassofobjectsandhasbothdataandmethods.◦ "Java is fun" isavalueofclasstypeString
Aprimitivetype isusedforsimple,nondecomposablevaluessuchasanindividualnumberorindividualcharacter.◦ int, double, andchar areprimitivetypes.
![Page 26: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/26.jpg)
PrimitiveTypes
![Page 27: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/27.jpg)
PrimitiveTypesFourintegertypes(byte, short, int, andlong)◦ int ismostcommon
Twofloating-pointtypes(float anddouble)◦ double ismorecommon
Onecharactertype(char)Onebooleantype(boolean)
![Page 28: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/28.jpg)
ExamplesofPrimitiveValuesIntegertypes
0 -1 365 12000
Floating-pointtypes0.99 -22.8 3.14159 5.0
Charactertype'a' 'A' '#' ' '
Booleantypetrue false
![Page 29: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/29.jpg)
AssignmentStatementsAnassignmentstatementisusedtoassignavaluetoavariable.answer = 42;
The"equalsign"iscalledtheassignmentoperator.Wesay,"Thevariablenamedanswer isassignedavalueof42,"ormoresimply,"answer isassigned42."
![Page 30: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/30.jpg)
AssignmentExamplesamount = 3.99;
firstInitial = 'W';
birthYear = currentYear - age;
total = total + 2;
![Page 31: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/31.jpg)
AssignmentCompatibilitiesJavaissaidtobestronglytyped.◦ Youcan't,forexample,assignafloatingpointvaluetoavariabledeclaredtostoreaninteger.
Sometimesconversionsbetweennumbersarepossible.doubleVariable = 7;
ispossibleevenifdoubleVariable isoftypedouble,forexample.
![Page 32: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/32.jpg)
ArithmeticOperatorsArithmeticexpressionscanbeformedusingthe+, -, *, and/operatorstogetherwithvariablesornumbersreferredtoasoperands.◦ Whenbothoperandsareofthesametype,theresultisofthattype.◦ Whenoneoftheoperandsisafloating-pointtypeandtheotherisaninteger,theresultisafloatingpointtype.
![Page 33: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/33.jpg)
ArithmeticOperatorsExample
IfhoursWorked isanint towhichthevalue40 hasbeenassigned,andpayRate isadouble towhich8.25 hasbeenassigned
hoursWorked * payRate
isadouble withavalueof500.0.
![Page 34: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/34.jpg)
TheDivisionOperatorThedivisionoperator(/)behavesasexpectedifoneoftheoperandsisafloating-pointtype.Whenbothoperandsareintegertypes,theresultistruncated,notrounded.◦ Hence,99/100hasavalueof0.
![Page 35: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/35.jpg)
Themod OperatorThemod (%)operatorisusedwithoperatorsofintegertypetoobtaintheremainderafterintegerdivision.14dividedby4is3witharemainderof2.◦ Hence,14 % 4 isequalto2.
Themodoperatorhasmanyuses,including◦ determiningifanintegerisoddoreven◦ determiningifoneintegerisevenlydivisiblebyanotherinteger.
![Page 36: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/36.jpg)
SampleExpressions
![Page 37: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/37.jpg)
TheClassStringWe'veusedconstantsoftypeString already."Enter a whole number from 1 to 99."
AvalueoftypeString isa◦ Sequenceofcharacters◦ Treatedasasingleitem.
![Page 38: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/38.jpg)
ConcatenationofStringsTwostringsareconcatenated usingthe+ operator.String greeting = "Hello";String sentence;sentence = greeting + " madam";System.out.println(sentence);
Anynumberofstringscanbeconcatenatedusingthe+ operator.
![Page 39: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/39.jpg)
ClassandMethodDefinitions
![Page 40: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/40.jpg)
ClassandMethodDefinitions
Objects that are instantiations of the
class Automobile
![Page 41: Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect](https://reader035.fdocuments.us/reader035/viewer/2022070723/5f01e2df7e708231d4018479/html5/thumbnails/41.jpg)
MethodsWhenyouuseamethodyou"invoke"or"call"itTwokindsofJavamethods◦ Returnasingleitem◦ Performsomeotheraction– avoid method
Themethodmain isavoidmethod◦ Invokedbythesystem◦ Notbytheapplicationprogram