+ All Categories
Home > Documents > Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees,...

Mobile Development WorkshopObject-Oriented Programming Our world consists of objects (people, trees,...

Date post: 04-Jun-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
41
Mobile Development Workshop DAY 1: INTRODUCTION TO JAVA
Transcript
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

MethodsWhenyouuseamethodyou"invoke"or"call"itTwokindsofJavamethods◦ Returnasingleitem◦ Performsomeotheraction– avoid method

Themethodmain isavoidmethod◦ Invokedbythesystem◦ Notbytheapplicationprogram


Recommended