18
Learning to Program with Learning to Program with C# - 2 C# - 2 1 Unit 2 Unit 2 Explore a problem in detail Explore a problem in detail See how it can be modelled See how it can be modelled Introduce the first set of Introduce the first set of fundamental C# concepts fundamental C# concepts used to model this problem used to model this problem

Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Embed Size (px)

Citation preview

Page 1: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

11

Unit 2Unit 2

• Explore a problem in detailExplore a problem in detail– See how it can be modelledSee how it can be modelled

• Introduce the first set of Introduce the first set of fundamental C# conceptsfundamental C# concepts– used to model this problemused to model this problem

Page 2: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

22

ProblemProblem

• Simulate a rocket flightSimulate a rocket flight– display flight in a window using display flight in a window using

simple graphicssimple graphics

Page 3: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

33

What must be included in What must be included in the rocket model?the rocket model?

• A rocketA rocket– Engine thrust levelEngine thrust level– Attitude of rocketAttitude of rocket– Speed and direction of travelSpeed and direction of travel

• Controls to adjust these valuesControls to adjust these values– increase/decrease thrust levelincrease/decrease thrust level– adjust attitudeadjust attitude– coast along for a given time under current coast along for a given time under current

settingssettings

• Rocket flies in an Rocket flies in an arena……arena……

Page 4: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

44

ArenaArena

• In an arenaIn an arena– size of the space in which the rocket can flysize of the space in which the rocket can fly– record of where the ground is and where record of where the ground is and where

other obstacles areother obstacles are

• The rocket can interrogate the arena to The rocket can interrogate the arena to determine whetherdetermine whether– it has hit the groundit has hit the ground– it has attempted to fly out of known spaceit has attempted to fly out of known space– it has collided with another objectit has collided with another object

Page 5: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

55

Rocket ControllerRocket Controller

• Something must be in charge of Something must be in charge of the controls of the rocket, the controls of the rocket, submitting appropriate instructions submitting appropriate instructions to gain the required flight.to gain the required flight.

• This is the Rocket ControllerThis is the Rocket Controller

Page 6: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

66

Three fundamental Three fundamental components: Rocket, components: Rocket,

Arena, Rocket ControllerArena, Rocket Controller

Rocket ControllerRocket Controller

RocketRocket

ArenaArena

Rocket Controller directs Rocket Controller directs the rocket's flight using the rocket's flight using the rocket's controlsthe rocket's controls

Rocket appears Rocket appears on the Arenaon the Arena

Information about ground, Information about ground, obstacles and arena edges obstacles and arena edges supplied on request to the Rocketsupplied on request to the Rocket

Page 7: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

77

What are we finding?What are we finding?

• Model made up of components – Model made up of components – objectsobjects• Objects have internal valuesObjects have internal values

– e.g. rocket has thrust level, attitude, velocitye.g. rocket has thrust level, attitude, velocity

• Each object has a number of controlsEach object has a number of controls• An object may activate a control of another An object may activate a control of another

objectobject• When a control is activated…When a control is activated…

– either an action is performed – either an action is performed – increase thrustincrease thrust– or some information is gathered – or some information is gathered – have I hit the have I hit the

ground?ground?

Page 8: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

88

ControlsControls

• Whenever a control is activated, Whenever a control is activated, some activity takes place inside itsome activity takes place inside it– New values for the internal values New values for the internal values

may be may be calculatedcalculated• e.g. updating the thrust levele.g. updating the thrust level• e.g. new position when coastinge.g. new position when coasting

– Controls of other objects are activatedControls of other objects are activated• e.g. while coasting, rocket must e.g. while coasting, rocket must

communicate with Arena to check on the communicate with Arena to check on the groundground

Page 9: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

99

Applying this to the Applying this to the problem:problem:

• Arena consists ofArena consists of– HitGround? controlHitGround? control– LeftUniverse? controlLeftUniverse? control– Collision? controlCollision? control

• Whenever the rocket activates one Whenever the rocket activates one of these controls, the rocket of these controls, the rocket supplies details of the its current supplies details of the its current position.position.

Page 10: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

1010

Applying this to the Applying this to the problem(2):problem(2):

• Rocket consists ofRocket consists of– Position, Attitude, Velocity, Acceleration, Position, Attitude, Velocity, Acceleration,

ThrustLevel valuesThrustLevel values– IncThrustLevel & DecThrustLevel conrolsIncThrustLevel & DecThrustLevel conrols– TurnLeft & TurnRight controlsTurnLeft & TurnRight controls– Coast controlCoast control

• Again, when these controls are activated, Again, when these controls are activated, they are supplied with values indicating they are supplied with values indicating e.g. how long to coast for, how much to e.g. how long to coast for, how much to turn byturn by

Page 11: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

1111

Applying this to the Applying this to the problem(3):problem(3):

• RocketController consists ofRocketController consists of– Rocket & Arena valuesRocket & Arena values– Fly control, to control the rocket's Fly control, to control the rocket's

flightflight• internally, this contains the precise internally, this contains the precise

instructions required for the desired flightinstructions required for the desired flight

Page 12: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

1212

How is this represented in How is this represented in C#?C#?

• C# has C# has objectsobjects• C# objects have values and controls as C# objects have values and controls as

described here, known asdescribed here, known as– fieldsfields – for the values – for the values– methodsmethods – for the controls – for the controls

• Calling Calling a method of an objecta method of an object– this is the activation of a methodthis is the activation of a method– a method of an object may internally a method of an object may internally callcall a a

method of another objectmethod of another object– it may it may pass pass valuesvalues, , or or parametersparameters, to the , to the

methodmethod

Page 13: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

1313

Crucially, theCrucially, the Class Class

• We may want many rockets, arenas, We may want many rockets, arenas, rocket controllersrocket controllers

• Hence, we define a blueprint or factory Hence, we define a blueprint or factory for building each of these objectsfor building each of these objects– This blueprint is constructed using the This blueprint is constructed using the

language constructs of the programming language constructs of the programming language language

• Each blueprint is known as a Each blueprint is known as a ClassClass• The Class for a kind of object can The Class for a kind of object can

produce limitless produce limitless instancesinstances of the object of the object

Page 14: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

1414

ConstructorConstructor

• Every class has a Every class has a constructorconstructor methodmethod– when called, this method creates a when called, this method creates a

new instance of the class, a new new instance of the class, a new objectobject

Page 15: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

1515

Many concepts now…Many concepts now…

• ClassClass– blueprint to describe the fields and methods blueprint to describe the fields and methods

of a particular kind of objectof a particular kind of object– has a constructor used to create new has a constructor used to create new

instances of its objectinstances of its object

• Object instanceObject instance– contains values for the fields of the objectcontains values for the fields of the object

• Calling methodsCalling methods– a method defined in an object's class may a method defined in an object's class may

be called on that objectbe called on that object

Page 16: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

1616

Complex modelComplex model

• Pick up the gist of the model nowPick up the gist of the model now– collection of objects that can collection of objects that can

communicate with one another to communicate with one another to make things happenmake things happen

• It will make sense once you see it It will make sense once you see it in actionin action

Page 17: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

1717

Expressing these ideas:Expressing these ideas:

• The programming language allows The programming language allows all these concepts to be expressed all these concepts to be expressed in a textual formin a textual form

• Skill developmentSkill development– reading the flat textual description, reading the flat textual description,

then breathing life into it in your mind then breathing life into it in your mind in order to understand what it will do in order to understand what it will do when operatingwhen operating

Page 18: Learning to Program with C# - 21 Unit 2 Explore a problem in detailExplore a problem in detail –See how it can be modelled Introduce the first set of fundamental

Learning to Program with C# Learning to Program with C# - 2- 2

1818

SummarySummary• Examined a problem – flying a rocketExamined a problem – flying a rocket

– consists of three major componentsconsists of three major components– they communicate with one anotherthey communicate with one another

• Introduced C# fundamentals that can be Introduced C# fundamentals that can be used to build the modelused to build the model– Classes, objects, fields, methods, method calls, Classes, objects, fields, methods, method calls,

parametersparameters

• Next:Next:– Look at the C# solution to this problemLook at the C# solution to this problem– Use Visual Studio to explore it, adjust it, see it Use Visual Studio to explore it, adjust it, see it

runningrunning