learning to program with c# - 81 unit 8 introduced two of the three key aspects in any programming...

18
Learning to Program with Learning to Program with C# - 8 C# - 8 1 Unit 8 Unit 8 Introduced two of the three key Introduced two of the three key aspects in any programming language aspects in any programming language structuring of data structuring of data statements and control flow statements and control flow Final key area Final key area data processing data processing how are new values created from old? how are new values created from old? Using additional features of Visual Using additional features of Visual Studio Studio to understand clearly how all this fits to understand clearly how all this fits together together

Upload: isaac-carr

Post on 14-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

11

Unit 8Unit 8

• Introduced two of the three key aspects in Introduced two of the three key aspects in any programming language any programming language – structuring of datastructuring of data– statements and control flowstatements and control flow

• Final key areaFinal key area– data processingdata processing– how are new values created from old?how are new values created from old?

• Using additional features of Visual StudioUsing additional features of Visual Studio– to understand clearly how all this fits togetherto understand clearly how all this fits together

Page 2: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

22

Key aspect of object-Key aspect of object-orientationorientation

– structuring of the problem solution according structuring of the problem solution according to the data inherent in the modelto the data inherent in the model

– in the whole rocket project, the smallest data in the whole rocket project, the smallest data values arevalues are• arena size, obstacles, obstacle size, obstacle arena size, obstacles, obstacle size, obstacle

position, rocket position, thrust level, velocity, position, rocket position, thrust level, velocity, distance travelled in last time period, acceleration distance travelled in last time period, acceleration due to gravity, rocket attitude, sky colour, ground due to gravity, rocket attitude, sky colour, ground colourcolour

– object model allows these values to be stored object model allows these values to be stored in clumps – e.g. rocket values togetherin clumps – e.g. rocket values together• along with the operations over those values – the along with the operations over those values – the

methodsmethods

Page 3: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

33

But this is But this is computingcomputing

• The whole point is that we The whole point is that we computecompute new new values from oldvalues from old

• It is all these individual little values that It is all these individual little values that must be updated for the model to change must be updated for the model to change over time, and be interestingover time, and be interesting– the rocket velocity for example changes over the rocket velocity for example changes over

time, and as adjustments are made through time, and as adjustments are made through the flight controlsthe flight controls

• Time to see how this computing takes Time to see how this computing takes placeplace

Page 4: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

44

ExpressionsExpressions• In order to compute new values during the execution In order to compute new values during the execution

of a programof a program– we must write down the description of that computation we must write down the description of that computation

when we write the programwhen we write the program– these descriptions are called these descriptions are called expressionsexpressions, and get , and get

combined together with the textual descriptions for combined together with the textual descriptions for statements etc.statements etc.

– it is very valuable to keep track of these different it is very valuable to keep track of these different grammatical categoriesgrammatical categories

• Expressions look quite mathematicalExpressions look quite mathematical– unsurprisingly, since they are a form of algebraunsurprisingly, since they are a form of algebra– they can operate over numbers, but also the wide range of they can operate over numbers, but also the wide range of

other values/objects available in the programming languageother values/objects available in the programming language

Page 5: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

55

Of what do expressions Of what do expressions consist of?consist of?

• Three primary components may appear:Three primary components may appear:– IdentifiersIdentifiers

• these are names for stored valuesthese are names for stored values• we won't know exactly what the value is until the we won't know exactly what the value is until the

program runsprogram runs

– Literal valuesLiteral values• these are values known about at the time we write the these are values known about at the time we write the

expression e.g. 1, 7.3, trueexpression e.g. 1, 7.3, true

– OperatorsOperators• these are the actual functions that take existing values these are the actual functions that take existing values

and produce new valuesand produce new values• familiar examples are + and – from mathematicsfamiliar examples are + and – from mathematics

– they have just the same meaning herethey have just the same meaning here• many others alsomany others also

Page 6: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

66

One One crucialcrucial new statement new statement

• The The assignmentassignment statement statement– once new values are computed, we need to once new values are computed, we need to

store them often, for use laterstore them often, for use later– a new value is a new value is assignedassigned to a storage location to a storage location

• the location has a name – an the location has a name – an identifieridentifier• the data members of an object are storage locationsthe data members of an object are storage locations

• First the expression is evaluated to get our new value, First the expression is evaluated to get our new value, then this is stored into the location identified by the then this is stored into the location identified by the location identifierlocation identifier

• The = sign is NOT a test for equality, but signifies The = sign is NOT a test for equality, but signifies assignmentassignment

LocationIdentifier = Expression;

Page 7: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

77

Let's go on an expression Let's go on an expression hunt…hunt…

• In the AdvancedRocketry solutionIn the AdvancedRocketry solution– use Object Browser to get to the code use Object Browser to get to the code

for the EnginesOn/EnginesOff methods for the EnginesOn/EnginesOff methods of Rocketof Rocket• manipulate themanipulate the

EngineThrust locationEngineThrust location• setting it to an initialsetting it to an initial

value, or to zero,value, or to zero,respectivelyrespectively

• the expressions are asthe expressions are assimple as they come – just a single literal simple as they come – just a single literal valuevalue

– remember, expression is the bit on the right of remember, expression is the bit on the right of the =the =

public void EnginesOn(){

// 10.0 is the initial valueEngineThrust = 10.0;

}

public void EnginesOff(){

EngineThrust = 0.0;}

Page 8: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

88

Now find IncThrustLevel…Now find IncThrustLevel…

– Still Still EngineThrustEngineThrust that is being updated that is being updated• but by a more complicated expressionbut by a more complicated expression

– EngineThrustEngineThrust and and levellevel are being added together – are being added together – the + operator combines themthe + operator combines them• in passing, in passing, levellevel is an integer – so it is being converted to a is an integer – so it is being converted to a

doubledouble or floating point number, before being added or floating point number, before being added

– How is this expression evaluated?How is this expression evaluated?• first, the identifiers are replaced by whatever values they first, the identifiers are replaced by whatever values they

currently havecurrently have– ie, the current value stored as the rocket's thrust levelie, the current value stored as the rocket's thrust level– and the value passed to the method on the call that has caused and the value passed to the method on the call that has caused

this line to be executed – this value is stored under the identifier this line to be executed – this value is stored under the identifier levellevel as specified in the first line of the method as specified in the first line of the method

public void IncThrustLevel( int level ){

EngineThrust = EngineThrust + (double)level;}

Page 9: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

99

Assistance from Visual Assistance from Visual StudioStudio• Complexity is increasingComplexity is increasing

• Where do the identifiers come from? What do they Where do the identifiers come from? What do they mean?mean?

• VS can helpVS can help– move the pointer over an identifiermove the pointer over an identifier

• the location and type of the identifier should appearthe location and type of the identifier should appear

• Here, EngineThrust is shown to be a location of type Here, EngineThrust is shown to be a location of type doubledouble – that is a floating point value, and it is a member – that is a floating point value, and it is a member of Rocket (from Rocket.EngineThrust)of Rocket (from Rocket.EngineThrust)

• A short description may also be givenA short description may also be given– depends how thorough the programmer who created this depends how thorough the programmer who created this

waswas

– Try this on the body of the Try this on the body of the TurnLeftTurnLeft method method

Page 10: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

1010

Further assistanceFurther assistance• Visual Studio can also show you the values Visual Studio can also show you the values

that these locations contain during executionthat these locations contain during execution– using its using its debuggerdebugger– you can step through the instructions of a you can step through the instructions of a

program, seeing how values change and how the program, seeing how values change and how the output developsoutput develops

• First attempt at thisFirst attempt at this– Move the pointer into the left margin of the Move the pointer into the left margin of the

IncThrustLevel method, click the mouseIncThrustLevel method, click the mouse• a purple circle should appear and the line go purplea purple circle should appear and the line go purple• this is a this is a breakpointbreakpoint – when the program runs, it will halt – when the program runs, it will halt

every time execution reaches this point every time execution reaches this point

Page 11: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

1111

This is the Debug ToolbarThis is the Debug Toolbar

– Display it, through View menu, if it isn't visibleDisplay it, through View menu, if it isn't visible– The elements are as follows, respectivelyThe elements are as follows, respectively

• Start – gets the program running (same as the menu option)Start – gets the program running (same as the menu option)– if you're stopped in a program, this will simply continue executionif you're stopped in a program, this will simply continue execution

• Pause and Stop and Restart – self-explanatoryPause and Stop and Restart – self-explanatory• Show next statement – jumps to the next statement to be executedShow next statement – jumps to the next statement to be executed• Step IntoStep Into

– if you've stopped at a method call, this will take you one statement INTO if you've stopped at a method call, this will take you one statement INTO the code of the method, so you can see what's happeningthe code of the method, so you can see what's happening

• Step OverStep Over– steps to the next statement in sequence, executing the one you're atsteps to the next statement in sequence, executing the one you're at

• Step OutStep Out– jumps out to the call of the method that you're stopped in, executing jumps out to the call of the method that you're stopped in, executing

any statements between the stopping point and the end of the methodany statements between the stopping point and the end of the method

Page 12: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

1212

Try it outTry it out• Resize Visual Studio so that you will Resize Visual Studio so that you will

be able to see the rocket flying at be able to see the rocket flying at the same time as you can see the the same time as you can see the VS windowVS window

• With your breakpoint set, click StartWith your breakpoint set, click Start– The animation window will partially The animation window will partially

displaydisplay– and then VS will halt at the breakpointand then VS will halt at the breakpoint

Page 13: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

1313

First, let's examine values First, let's examine values • Remember, this is happening on the first call Remember, this is happening on the first call

to IncThrustLevelto IncThrustLevel– check where you expect that is, in the AutoFly check where you expect that is, in the AutoFly

method of the RocketController you are currently method of the RocketController you are currently using in ExecuteMissionusing in ExecuteMission

• Bring up QuickWatch from Debug menuBring up QuickWatch from Debug menu– type in, for example, EngineThrust followed by type in, for example, EngineThrust followed by

EnterEnter– You will see details about the current value of You will see details about the current value of

EngineThrust appearing in the windowEngineThrust appearing in the window– Try Try levellevel also also– Are you getting the values you expectednt ?Are you getting the values you expectednt ?

• I got 10 for EngineThrust – the default value it is set to I got 10 for EngineThrust – the default value it is set to when the engines are switched onwhen the engines are switched on

• and 60 for level, the amount of the first call to and 60 for level, the amount of the first call to IncThrustLevelIncThrustLevel

Page 14: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

1414

Now let's try the step Now let's try the step commandscommands

• These allow you to see statement by These allow you to see statement by statement as the program executesstatement as the program executes– first, press Step Over a couple of timesfirst, press Step Over a couple of times

• this should take you out of IncThrustLevel, back to this should take you out of IncThrustLevel, back to the point it was called fromthe point it was called from

• Is this what you expected?Is this what you expected?

– continue pressing Step Over until the continue pressing Step Over until the Land(); Land(); statement is highlightedstatement is highlighted• notice that the Animation window is updated each notice that the Animation window is updated each

time you pass a call to Coast, and that it takes a time you pass a call to Coast, and that it takes a little time for a Coast statement to be completedlittle time for a Coast statement to be completed

Page 15: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

1515

Exploring Land();Exploring Land();• Choose Step Into this timeChoose Step Into this time

– you are taken to the code for Landyou are taken to the code for Land

• Now choose Step Over repeatedlyNow choose Step Over repeatedly– when you get to the while loop, you see the behaviour you when you get to the while loop, you see the behaviour you

should expectshould expect• cycling between the test and the statement, as the rocket is cycling between the test and the statement, as the rocket is

brought back to verticalbrought back to vertical• you can even see Theta changingyou can even see Theta changing

– choose 'Autos' in Windows in Debug menuchoose 'Autos' in Windows in Debug menu– click + next to ARocket, the continue pressing +s until a whole list of click + next to ARocket, the continue pressing +s until a whole list of

data is displayeddata is displayed– look for Theta in there – it'll have some radian valuelook for Theta in there – it'll have some radian value

• now go back to pressing Step Overnow go back to pressing Step Over– you should see the value of Theta changingyou should see the value of Theta changing

• you'll probably get bored of this after a whileyou'll probably get bored of this after a while– pick Stop from the tool bar thenpick Stop from the tool bar then

Page 16: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

1616

Back to expressionsBack to expressions

• This is a startThis is a start– a full treatment is beyond this coursea full treatment is beyond this course

• A few important operatorsA few important operators– the the .. (full stop) is the member access operator (full stop) is the member access operator

• used to access methods and data values of an objectused to access methods and data values of an object• e.g. ARocket.EnginesOn accesses the EnginesOn e.g. ARocket.EnginesOn accesses the EnginesOn

methodmethod

– the round brackets the round brackets (( … … )) after a method are after a method are the method call operatorthe method call operator• any necessary parameters should be supplied within any necessary parameters should be supplied within

the bracketsthe brackets

Page 17: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

1717

Method calls…Method calls…

• Are sometimes statementsAre sometimes statements– when it is marked as when it is marked as voidvoid

• e.g. move pointer over the method names in the body e.g. move pointer over the method names in the body of the TakeOff method – they are all marked initially of the TakeOff method – they are all marked initially as void, and are all being used as statementsas void, and are all being used as statements

• and at other times, they're expressionsand at other times, they're expressions– when they deliver a valuewhen they deliver a value– when they are marked with a type/class namewhen they are marked with a type/class name

• e.g. move pointer over IsTilting in the Land methode.g. move pointer over IsTilting in the Land method• it is marked as it is marked as boolbool indicating that it returns a value indicating that it returns a value

of boolean type when it is evaluatedof boolean type when it is evaluated

Page 18: Learning to Program with C# - 81 Unit 8 Introduced two of the three key aspects in any programming languageIntroduced two of the three key aspects in any

Learning to Program with C# Learning to Program with C# - 8- 8

1818

SummarySummary• Amazingly, this limited treatment covers most Amazingly, this limited treatment covers most

of the expressions coming up in the Rocketry of the expressions coming up in the Rocketry projectproject– explore around and see them in actionexplore around and see them in action– use the debugger to move around the code as it use the debugger to move around the code as it

executesexecutes• the best way to really get on board how these instructions the best way to really get on board how these instructions

workwork• make small changes and see if they do what you expectmake small changes and see if they do what you expect

• Everything we've done so far has involved Everything we've done so far has involved mostly looking at existing codemostly looking at existing code– essential first step in programming is understanding essential first step in programming is understanding

what the language can dowhat the language can do

• Next, we'll start building some small programs Next, we'll start building some small programs from scratchfrom scratch