Top Banner
Learning to Program with Learning to Program with C# - 3 C# - 3 1 Unit 3 Unit 3 Introduction to Visual Studio Introduction to Visual Studio Examine the Rocket problem Examine the Rocket problem modelled in C# modelled in C# Learn to make small changes Learn to make small changes to a C# program to a C# program
25

Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Dec 29, 2015

Download

Documents

Wendy Sullivan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

11

Unit 3Unit 3

• Introduction to Visual StudioIntroduction to Visual Studio• Examine the Rocket problem Examine the Rocket problem

modelled in C#modelled in C#• Learn to make small changes to a Learn to make small changes to a

C# programC# program

Page 2: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

22

C# vs. Visual Studio C# vs. Visual Studio (VS)(VS)

• A Programming A Programming LanguageLanguage

• Used to represent Used to represent models developed models developed from problems in the from problems in the real worldreal world

• This is the world of This is the world of the the classclass, the , the object,object, the the methodmethod, etc., etc.

• A Programming A Programming EnvironmentEnvironment

• a tool used to enable us to a tool used to enable us to manipulate models manipulate models developed in a range of developed in a range of programming languagesprogramming languages

• manages all the manages all the administrative administrative requirements to make the requirements to make the programs runprograms run

Page 3: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

33

Remember purpose at this Remember purpose at this stage:stage:

• Aiming to gain Aiming to gain minimumminimum knowledge and skills knowledge and skills in C# & Visual Studioin C# & Visual Studio– enough to get an appreciation of programming onlyenough to get an appreciation of programming only

• C# and VS are very complex toolsC# and VS are very complex tools– in the following, aim to ignore the detail…in the following, aim to ignore the detail…– … … just follow what we're suggesting for nowjust follow what we're suggesting for now

• Watch what I'm doing…Watch what I'm doing…– you'll have a chance to practice and explore later, you'll have a chance to practice and explore later,

with these slides to handwith these slides to hand

Page 4: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

44

A reminder about A reminder about programming languages programming languages

firstfirst• Rocket model consists of Rocket model consists of

– a number of objects…a number of objects…– … … that communicate with one another that communicate with one another

and change over timeand change over time

• All this described using a textual All this described using a textual languagelanguage– some imagination required to bring the some imagination required to bring the

flat text to lifeflat text to life

Page 5: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

55

Need for a Prog. EnvironmentNeed for a Prog. Environment• We We couldcould write the entire text describing a write the entire text describing a

problem in a single file, but…problem in a single file, but…– can't share/reuse parts of the filecan't share/reuse parts of the file

• would be like requiring a different wooden spoon for would be like requiring a different wooden spoon for every cookery recipe that required oneevery cookery recipe that required one

– can't update one part in isolation from otherscan't update one part in isolation from others

• … … so the text is broken into pieces, and can so the text is broken into pieces, and can be shared aroundbe shared around– The prog. env. is required to manage all these The prog. env. is required to manage all these

pieces appropriatelypieces appropriately– It is a kind of complex word processor, for It is a kind of complex word processor, for

programming language text filesprogramming language text files

Page 6: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

66

Fundamental process…Fundamental process…

• Using the programming environment…Using the programming environment…– we enter a description of the solution to our we enter a description of the solution to our

problemproblem• some of it is new textsome of it is new text• some specifies existing classes/methods etcsome specifies existing classes/methods etc

– the programming environment checks that our the programming environment checks that our description matches language rulesdescription matches language rules• if it does not, we edit it and try againif it does not, we edit it and try again

– with a successful check, we can with a successful check, we can executeexecute our our solutionsolution• the classes, objects etc come to life, modelling whatever the classes, objects etc come to life, modelling whatever

was in our descriptionwas in our description• sometimes we don't get what we expected – we need to sometimes we don't get what we expected – we need to

go back and rework our solutiongo back and rework our solution

Page 7: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

77

Visual Studio basicsVisual Studio basics

• VS organises problems using VS organises problems using SolutionsSolutions & & ProjectsProjects– A Solution solves a major problem areaA Solution solves a major problem area

• it may consist of a number of smaller, stand-alone, problemsit may consist of a number of smaller, stand-alone, problems• each of these is represented using a projecteach of these is represented using a project• these individual project components may be set up for reuse these individual project components may be set up for reuse

in other solutionsin other solutions

– Remember – VS created for large applicationsRemember – VS created for large applications• here, we will primarily be concentrating on Solutions that here, we will primarily be concentrating on Solutions that

contain just a single projectcontain just a single project• concentrate for now on the concept of a concentrate for now on the concept of a projectproject

Page 8: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

88

Exploring: the Exploring: the RocketryRocketry projectproject

• Open the Rocketry.sln fileOpen the Rocketry.sln file• Having opened up the project/solution…Having opened up the project/solution…

– Don't panic!Don't panic!– The screen contains a lot of information, but it is The screen contains a lot of information, but it is

well organisedwell organised

• There are fundamentally two views available There are fundamentally two views available through the environmentthrough the environment– The C# view of classes, objects etc.The C# view of classes, objects etc.– The Visual Studio view of solutions, projects, files The Visual Studio view of solutions, projects, files

etcetc

Page 9: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

99

The Object BrowserThe Object Browser

• This is the C# viewThis is the C# view• Views C# items available in Views C# items available in

this solutionthis solution• Key one here is the project Key one here is the project

RocketryRocketry• Press on the + symbol to find Press on the + symbol to find

out more about the projectout more about the project• We'll address the others laterWe'll address the others later

Page 10: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1010

Solution & Class Solution & Class explorerexplorer

• Visual Studio viewsVisual Studio views• Solution Explorer shows the Solution Explorer shows the

various files required in order to various files required in order to make up the complete solutionmake up the complete solution

• Class Explorer is a quick way to Class Explorer is a quick way to find a required textual find a required textual description, of a class or description, of a class or method saymethod say– It's hiding behind the little icon tab It's hiding behind the little icon tab

above the Solution Explorer tababove the Solution Explorer tab

Page 11: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1111

Supporting the process…Supporting the process…• Various tabbed windows at the bottom of the Various tabbed windows at the bottom of the

screen, they'll concern us more laterscreen, they'll concern us more later– Task List keeps track of tasks that must be Task List keeps track of tasks that must be

performed to get your program to workperformed to get your program to work– Output displays output from the various processes Output displays output from the various processes

taking placetaking place– Other tabs appear here periodicallyOther tabs appear here periodically

Page 12: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1212

Back to the Browser – let's Back to the Browser – let's exploreexplore

• Click on + next to Rocketry, Click on + next to Rocketry, then on + of Rockets to open then on + of Rockets to open themthem– Rockets is a Rockets is a namespacenamespace

• our place to keep the various C# our place to keep the various C# components making up the projectcomponents making up the project

– Listed here are the major C# Listed here are the major C# components of our rocket modelcomponents of our rocket model• Rocket, RocketController and Arena Rocket, RocketController and Arena

you'll recognise from the last unityou'll recognise from the last unit

Page 13: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1313

Experiment…Experiment…

• Move the mouse over the small iconsMove the mouse over the small icons– a short description of the item pops upa short description of the item pops up– some of them are some of them are classesclasses, remember them?, remember them?– others are others are interfacesinterfaces, we'll talk about them soon, we'll talk about them soon

• Single click on an itemSingle click on an item– what happens at bottom & right panes?what happens at bottom & right panes?

• membersmembers (remember them?) of the item appear in the right (remember them?) of the item appear in the right panepane

• a longer description of the item appears in the lower panea longer description of the item appears in the lower pane

– you can explore the members in just the same wayyou can explore the members in just the same way• do you recognise some of them from the previous unit – do you recognise some of them from the previous unit –

TurnRight, Coast, IncThrustLevel in the Rocket, for example?TurnRight, Coast, IncThrustLevel in the Rocket, for example?

Page 14: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1414

Exploring the textual Exploring the textual descriptiondescription

• Double click on the class Double click on the class RocketControllerRocketController– a new window appears, a new window appears,

covering the browsercovering the browser– use the tab to switch themuse the tab to switch them– this text describes how to this text describes how to

create create instancesinstances of rocket of rocket controllers – remember, this controllers – remember, this is what a class is foris what a class is for

– scroll around if you wishscroll around if you wish– then close it using the little then close it using the little

cross next to the Solution cross next to the Solution Explorer tabExplorer tab

– you should have just the you should have just the Browser againBrowser again

Page 15: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1515

Exploring the Fly methodExploring the Fly method

• This timeThis time– double click on Fly in the double click on Fly in the

members window when members window when RocketController is selected in RocketController is selected in the left panethe left pane

– You are taken directly to the text You are taken directly to the text oppositeopposite

• This is the This is the sequencesequence of of instructions that will fly the instructions that will fly the rocketrocket– Each one is executed in turnEach one is executed in turn– Final one keeps window openFinal one keeps window open– How do you predict the rocket How do you predict the rocket

will fly?will fly?

Page 16: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1616

Running the rocket Running the rocket simulationsimulation

• Choose Choose StartStart from from DebugDebug menu menu– VS will do some preparatory work, and VS will do some preparatory work, and

then an Animation window should then an Animation window should appear, with the rocket in itappear, with the rocket in it

– impressive graphics, eh?impressive graphics, eh?

– When the rocket has landed, remove When the rocket has landed, remove the window by clicking the close boxthe window by clicking the close box

– Did it fly as you expected? Do you Did it fly as you expected? Do you agree that it flew according to the agree that it flew according to the instructions contained in the Fly instructions contained in the Fly method?method?

– If not, run the program again, as aboveIf not, run the program again, as above

Page 17: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1717

Ignoring complexity…Ignoring complexity…• Huge behind-the-scenes activity to Huge behind-the-scenes activity to

make the rocket fly, butmake the rocket fly, but– we were only interested in the we were only interested in the FlyFly method method

• just 10 lines of codejust 10 lines of code• thousands or even millions of lines of code were thousands or even millions of lines of code were

probably executed for the whole simulation!!probably executed for the whole simulation!!

– it is important to be able to ignore all the it is important to be able to ignore all the complexity that surrounds you at this stagecomplexity that surrounds you at this stage• compare with any complex machine in your compare with any complex machine in your

environmentenvironment• e.g. video recorder – you don't care about the e.g. video recorder – you don't care about the

inner complexity of the machine, just as long as it inner complexity of the machine, just as long as it records your favourite program at the right timerecords your favourite program at the right time

– for now we're going to experiment with for now we're going to experiment with FlyFly

Page 18: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1818

Making a change to a Making a change to a programprogram

• We're going to adjust the rocket's flightWe're going to adjust the rocket's flight– by making a change to the by making a change to the FlyFly method method– find it again in Visual Studiofind it again in Visual Studio

• We'll fly off the left edge of the arenaWe'll fly off the left edge of the arena– consider what instructions might be requiredconsider what instructions might be required– need to take off, head left and just keep on need to take off, head left and just keep on

goinggoing– remember the model – thrust/attitude settings remember the model – thrust/attitude settings

only have an effect when you use only have an effect when you use CoastCoast to give to give them time to operatethem time to operate

Page 19: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

1919

Making the changeMaking the change

• Simply edit the instructions to get the Simply edit the instructions to get the effect you want. Something like:effect you want. Something like:

Rocket.EnginesOn();Rocket.EnginesOn();

Rocket.IncThrustLevel( 60 );Rocket.IncThrustLevel( 60 );

Rocket.Coast( 1 );Rocket.Coast( 1 );

Rocket.TurnLeft( 90 );Rocket.TurnLeft( 90 );

Rocket.Coast( 10 );Rocket.Coast( 10 );

• You must use exactly the right syntax:You must use exactly the right syntax:– Names of methods matching those seen in Names of methods matching those seen in

the object browser, correct use of brackets the object browser, correct use of brackets and semicolons – just as aboveand semicolons – just as above

Page 20: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

2020

To run your new version:To run your new version:

• Visual Studio must check that your new Visual Studio must check that your new flight description is legal C# codeflight description is legal C# code– Choose Start in Debug menu againChoose Start in Debug menu again– This automatically saves & checks your new This automatically saves & checks your new

code before trying to run itcode before trying to run it

• If you have made no errors, it will runIf you have made no errors, it will run– did it do what you expected?did it do what you expected?– feel free to experiment some morefeel free to experiment some more

• If you have made an error, read on…If you have made an error, read on…

Page 21: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

2121

Detecting & correcting Detecting & correcting errorserrors

• [if you made no error, introduce one[if you made no error, introduce one• say, remove a bracket from one of the instructions]say, remove a bracket from one of the instructions]

• When you try to run the program…When you try to run the program…– VS tells you there were VS tells you there were buildbuild errors, asking you errors, asking you

if you want to continueif you want to continue– Do NOT continue! Click 'no'Do NOT continue! Click 'no'– The Task List window pops up showing you the The Task List window pops up showing you the

errors the system founderrors the system found– Read the first error, then double click on it – Read the first error, then double click on it –

you will be taken to the line in the codeyou will be taken to the line in the code– If you can see what is wrong, fix it and try againIf you can see what is wrong, fix it and try again

Page 22: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

2222

Deciphering error Deciphering error messagesmessages• Error messages are sometimes perfect…Error messages are sometimes perfect…

– try removing just one semi-colontry removing just one semi-colon

• ……but not always…but not always…– particularly for beginners using an industrial-strength prog. particularly for beginners using an industrial-strength prog.

environmentenvironment– and also because entirely accurate reporting is a highly skilful and also because entirely accurate reporting is a highly skilful

task, beyond a mere computertask, beyond a mere computer– try removing a brackettry removing a bracket

• Finding the Finding the causecause of the reported error is one of the of the reported error is one of the skills to learnskills to learn– First, see if the message helps you. It may – but don't slavishly First, see if the message helps you. It may – but don't slavishly

follow it, if it seems wrongfollow it, if it seems wrong– If it didn't help, look very carefully in the vicinity of the If it didn't help, look very carefully in the vicinity of the

reported error – can you see anything amiss?reported error – can you see anything amiss?

Page 23: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

2323

Now experiment….!Now experiment….!

• Make changes to the flight of the rocketMake changes to the flight of the rocket– to get used to the edit/rebuild/rerun processto get used to the edit/rebuild/rerun process

• Make some changes that you know Make some changes that you know aren't correct syntacticallyaren't correct syntactically– to get practice at understanding the errors to get practice at understanding the errors

that are reported, and at fixing themthat are reported, and at fixing them– notice that sometimes, one real error may notice that sometimes, one real error may

cause two, three or more error reports – if cause two, three or more error reports – if you really can't see what's wrong and you really can't see what's wrong and you've fixed an error, try rebuilding it againyou've fixed an error, try rebuilding it again

Page 24: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

2424

• Imagine you were a rocket scientist!Imagine you were a rocket scientist!• You've just been tinkering with your rocket You've just been tinkering with your rocket

controller for each new test flightcontroller for each new test flight– Notice that each time you tried out a new flight Notice that each time you tried out a new flight

plan, you threw away the old oneplan, you threw away the old one• [to be precise you deleted or wrote over it][to be precise you deleted or wrote over it]

• This is This is oneone way to adjust the behaviour of way to adjust the behaviour of programsprograms– simply replace the old behaviour with the new, by simply replace the old behaviour with the new, by

overwriting the old code with new codeoverwriting the old code with new code– the old behaviour is discardedthe old behaviour is discarded

• This is fine when developing a programThis is fine when developing a program

Reflect: what have we been Reflect: what have we been doing?doing?

Page 25: Learning to Program with C# - 31 Unit 3 Introduction to Visual StudioIntroduction to Visual Studio Examine the Rocket problem modelled in C#Examine the.

Learning to Program with C# Learning to Program with C# - 3- 3

2525

SummarySummary• Contrasted C# with Visual StudioContrasted C# with Visual Studio• Introduced a minimum set of Visual Studio Introduced a minimum set of Visual Studio

commands to get goingcommands to get going• Experimented with the solution to the Rocket Experimented with the solution to the Rocket

problem in Visual Studioproblem in Visual Studio• Learned how to make Learned how to make destructive updatesdestructive updates to a to a

programprogram– old behaviour is lostold behaviour is lost– ideal when developing programsideal when developing programs

• Next unit considers constructive updatesNext unit considers constructive updates– when we wish to keep both the old & the newwhen we wish to keep both the old & the new