180 likes | 210 Views
Unit 8. Introduced two of the three key aspects in any programming language structuring of data statements and control flow Final key area data processing how are new values created from old? Using additional features of Visual Studio to understand clearly how all this fits together.
E N D
Unit 8 • Introduced two of the three key aspects in any programming language • structuring of data • statements and control flow • Final key area • data processing • how are new values created from old? • Using additional features of Visual Studio • to understand clearly how all this fits together Learning to Program with C# - 8
Key aspect of object-orientation • structuring of the problem solution according to the data inherent in the model • in the whole rocket project, the smallest data values are • arena size, obstacles, obstacle size, obstacle position, rocket position, thrust level, velocity, distance travelled in last time period, acceleration due to gravity, rocket attitude, sky colour, ground colour • object model allows these values to be stored in clumps – e.g. rocket values together • along with the operations over those values – the methods Learning to Program with C# - 8
But this is computing • The whole point is that we compute new values from old • It is all these individual little values that must be updated for the model to change over time, and be interesting • the rocket velocity for example changes over time, and as adjustments are made through the flight controls • Time to see how this computing takes place Learning to Program with C# - 8
Expressions • In order to compute new values during the execution of a program • we must write down the description of that computation when we write the program • these descriptions are called expressions, and get combined together with the textual descriptions for statements etc. • it is very valuable to keep track of these different grammatical categories • Expressions look quite mathematical • unsurprisingly, since they are a form of algebra • they can operate over numbers, but also the wide range of other values/objects available in the programming language Learning to Program with C# - 8
Of what do expressions consist of? • Three primary components may appear: • Identifiers • these are names for stored values • we won't know exactly what the value is until the program runs • Literal values • these are values known about at the time we write the expression e.g. 1, 7.3, true • Operators • these are the actual functions that take existing values and produce new values • familiar examples are + and – from mathematics • they have just the same meaning here • many others also Learning to Program with C# - 8
One crucial new statement • The assignment statement • once new values are computed, we need to store them often, for use later • a new value is assigned to a storage location • the location has a name – an identifier • the data members of an object are storage locations • First the expression is evaluated to get our new value, then this is stored into the location identified by the location identifier • The = sign is NOT a test for equality, but signifies assignment LocationIdentifier=Expression; Learning to Program with C# - 8
Let's go on an expression hunt… • In the AdvancedRocketry solution • use Object Browser to get to the code for the EnginesOn/EnginesOff methods of Rocket • manipulate theEngineThrust location • setting it to an initialvalue, or to zero,respectively • the expressions are assimple as they come – just a single literal value • remember, expression is the bit on the right of the = public void EnginesOn() { // 10.0 is the initial value EngineThrust = 10.0; } public void EnginesOff() { EngineThrust = 0.0; } Learning to Program with C# - 8
Now find IncThrustLevel… public void IncThrustLevel( int level ) { EngineThrust = EngineThrust + (double)level; } • Still EngineThrust that is being updated • but by a more complicated expression • EngineThrust and level are being added together – the + operator combines them • in passing, level is an integer – so it is being converted to a double or floating point number, before being added • How is this expression evaluated? • first, the identifiers are replaced by whatever values they currently have • ie, the current value stored as the rocket's thrust level • 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 level as specified in the first line of the method Learning to Program with C# - 8
Assistance from Visual Studio • Complexity is increasing • Where do the identifiers come from? What do they mean? • VS can help • move the pointer over an identifier • the location and type of the identifier should appear • Here, EngineThrust is shown to be a location of type double – that is a floating point value, and it is a member of Rocket (from Rocket.EngineThrust) • A short description may also be given • depends how thorough the programmer who created this was • Try this on the body of the TurnLeft method Learning to Program with C# - 8
Further assistance • Visual Studio can also show you the values that these locations contain during execution • using its debugger • you can step through the instructions of a program, seeing how values change and how the output develops • First attempt at this • Move the pointer into the left margin of the IncThrustLevel method, click the mouse • a purple circle should appear and the line go purple • this is a breakpoint – when the program runs, it will halt every time execution reaches this point Learning to Program with C# - 8
This is the Debug Toolbar • Display it, through View menu, if it isn't visible • The elements are as follows, respectively • Start – gets the program running (same as the menu option) • if you're stopped in a program, this will simply continue execution • Pause and Stop and Restart – self-explanatory • Show next statement – jumps to the next statement to be executed • Step 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 happening • Step Over • steps to the next statement in sequence, executing the one you're at • Step Out • 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 method Learning to Program with C# - 8
Try it out • Resize Visual Studio so that you will be able to see the rocket flying at the same time as you can see the VS window • With your breakpoint set, click Start • The animation window will partially display • and then VS will halt at the breakpoint Learning to Program with C# - 8
First, let's examine values • Remember, this is happening on the first call to IncThrustLevel • check where you expect that is, in the AutoFly method of the RocketController you are currently using in ExecuteMission • Bring up QuickWatch from Debug menu • type in, for example, EngineThrust followed by Enter • You will see details about the current value of EngineThrust appearing in the window • Try level also • Are you getting the values you expectednt ? • I got 10 for EngineThrust – the default value it is set to when the engines are switched on • and 60 for level, the amount of the first call to IncThrustLevel Learning to Program with C# - 8
Now let's try the step commands • These allow you to see statement by statement as the program executes • first, press Step Over a couple of times • this should take you out of IncThrustLevel, back to the point it was called from • Is this what you expected? • continue pressing Step Over until the Land(); statement is highlighted • notice that the Animation window is updated each time you pass a call to Coast, and that it takes a little time for a Coast statement to be completed Learning to Program with C# - 8
Exploring Land(); • Choose Step Into this time • you are taken to the code for Land • Now choose Step Over repeatedly • when you get to the while loop, you see the behaviour you should expect • cycling between the test and the statement, as the rocket is brought back to vertical • you can even see Theta changing • choose 'Autos' in Windows in Debug menu • click + next to ARocket, the continue pressing +s until a whole list of data is displayed • look for Theta in there – it'll have some radian value • now go back to pressing Step Over • you should see the value of Theta changing • you'll probably get bored of this after a while • pick Stop from the tool bar then Learning to Program with C# - 8
Back to expressions • This is a start • a full treatment is beyond this course • A few important operators • the . (full stop) is the member access operator • used to access methods and data values of an object • e.g. ARocket.EnginesOn accesses the EnginesOn method • the round brackets ( … ) after a method are the method call operator • any necessary parameters should be supplied within the brackets Learning to Program with C# - 8
Method calls… • Are sometimes statements • when it is marked as void • e.g. move pointer over the method names in the body of the TakeOff method – they are all marked initially as void, and are all being used as statements • and at other times, they're expressions • when they deliver a value • when they are marked with a type/class name • e.g. move pointer over IsTilting in the Land method • it is marked as bool indicating that it returns a value of boolean type when it is evaluated Learning to Program with C# - 8
Summary • Amazingly, this limited treatment covers most of the expressions coming up in the Rocketry project • explore around and see them in action • use the debugger to move around the code as it executes • the best way to really get on board how these instructions work • make small changes and see if they do what you expect • Everything we've done so far has involved mostly looking at existing code • essential first step in programming is understanding what the language can do • Next, we'll start building some small programs from scratch Learning to Program with C# - 8