Solutions Exercises

of 17
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
  Solutions to the Exercises Part I 1.  Programming paradigms  (a) Not true, Fortran is an imperative language but not object-oriented.(b) Not true, objects are modified by methods which are a kind of procedure.(c) Not true, PHP is a procedural language and it is interpreted, not compiled.(d) Not true, a programming language can hide certain aspects of the underlying processor.2.  The compiler as a program  (a) Yes, if you already possess a compiler or interpreter for that language. Only the firstcompiler ever had to be written in machine code.(b) Yes, that is possible.(c) Yes it can, assuming that you have another compiler available to compile it for the firsttime so you have an executable.3.  Names  Mathematical constants:  e ,  π . Mathematical variables:  x  for something that varies,  f   for afunction,  φ  for an angle. Physical constants: the speed of light  c , gravity constant  g . Physicalvariables: velocity  v , acceleration  a , mass  m . If everyone uses the same symbols for certainconstants and variables, then it is much easier to communicate.4.  Classes and types  Standard classes:  GraphicsDeviceManager ,  Game ,  SpriteBatch ,  GameTime ,  Console . Methods: GraphicsDeviceManager.Clear ,  SpriteBatch.Draw ,  Console.WriteLine . Three types that are not a class: int ,  double ,  bool .5.  Comments  Comments can be written on a single line with the  //  notation, or spread over multiple lines bystarting the comment with  / ∗  and ending it with  ∗ / .6.  Concepts  An instruction is a programming construct that can be executed. A variable is a location inmemory with a name. A method is a group of instructions with a name. An object is a groupof variables that belong together. A class is a group of methods, and also the type of an objectthat these methods can modify.1  7.  Declaration, instruction, expression  A declaration determines the type of a variable. An instruction can be executed, which willchange the memory. An expression can be calculated to determine its value.8.  Statement versus instruction  A ‘statement’ means something like a ‘remark’ or a ‘point of view’, while we give the computera  command   or an  instruction   to do something. A statement can be something like: “the grassis purple”. This is different from giving the computer a command or an instruction to  change  the color of grass into purple:  grass.Color = Color.Purple; .9.  Changing names  We have to change the name of the class, the name of the constructor method, and the call tothe constructor method from  Main . It is not necessary to change the name of the .cs file, but ingeneral it is a good idea to assign the name of the class to the file in which it is defined.10.  Syntactical categories  int  x;  [D]  int  23;  []  (y+1) ∗ x  [E]  new  Color(0,0,0)  [EM] ( int )x  [E]  23  [EC]  (x+y)(x − 1)  []  new  Color black;  [] int (x)  []  23x0  []  x+1=y+1;  []  Color blue; [D] int  x  []  x=23;  [IA]  x=y+1;  [IA]  GraphicsDevice.Clear(Color.CornflowerBlue); [IM] int  x,  double  y;  []  ”x=23;”  [EC]  spriteBatch.Begin();  [IM]  Content.RootDirectory = ”Content”; [IA] int  x, y;  [D]  x23  [E]  Math.Sqrt(23)  [EM]  Color.CornflowerBlue [E] ”/”  [EC]  0x23  [EC]  ” \\ ”  [EC]  Color.CornflowerBlue.ToString() [EM] ” \ ”  []  23%x  [E]  (x%23)  [E]  game.Run()  [M] ”//”  [EC]  x/ ∗ 23 ∗ /  [E]  ””  [EC]  23=x; []11.  Relation between syntactical categories  The following combination of categories are possible: EC, IA, IM, EM. An assignment alwaysgoes together with an instruction. A constant is also an expression. An assignment, a declarationand an instruction always end with a semicolon. An expression and a constant never end witha semicolon. A method call sometimes ends with a semicolon, depending on whether it is calledas a part of an expression or an instruction.12.  Variable assignment  y = 41 x = 12; x = 13; x = 12;x = 42; y = 12; y = 12; y = 40;y = 13; y = 0; y = 4;x = 39; x = 26; x = 6; The swap between  x  and  y  works for all cases. You can see that this works if you replace theconstants 40 and 12 by  a  and  b . If you fill in these values on the right hand side the finalsituation will be  y = a, x = b . However,  x  and  y  shouldn’t be so big that  x+y  doesn’t fit into an int  anymore.13.  Multiplying and dividing  Mathematically speaking, there is no difference. But if we are dealing with variables of type int  then the rounding of the division will result in a different outcome. For example, considerthe case where  time  contains the value 5. Then  3 ∗ 5/2 = 15/2 = 7 . But  3/2 ∗ 5 = 1 ∗ 5 = 5 , and 5/2 ∗ 3 = 2 ∗ 3 = 6 .2  14.  Hours, minutes, seconds  int  hours = 0; int  minutes = 0; int  seconds = 0; while  (time  > = 3600) { hours++;time  − = 3600; } while  (time  > = 60) { minutes++;time  − = 60; } seconds = time; Or simpler: int  hours = time/3600; int  minutes = (time%3600)/60; int  seconds = time%60; The reverse operation is much easier:  time = 3600 ∗ hours + 60 ∗ minutes + seconds; .15.  The game loop The game loop consists of   LoadContent ,  Update , and  Draw .  LoadContent  is executed once, the othertwo methods are executed in a loop. In the  LoadContent  method the game assets are loaded. Inthe  Update  method the game world is updated. In the  Draw  method the game world is drawn onthe screen.16.  Updating and drawing  The first advantage is that it is easier to read for the programmer if the code for updating thegame world is separated from the code for drawing the game world. Secondly, a game engine canmake certain optimizations to the performance of the game by splitting the code. For example,if the game world didn’t change, then it doesn’t have to be redrawn.3  Part II 1.  Keywords  The word ‘void’ means ‘empty’. We use this word to indicate that a method doesn’t have areturn value. The word ‘int’ is an abbreviation of integer, which is used to define the integertype. The word ‘return’ is used in the body of a method to return a result, as well as returnthe control of the program to the caller of the method. The word ‘this’ is used to indicate theobject that we’re currently manipulating. The ‘this’ reference is needed so that we can accessthe variables stored in that object. We cannot use the ‘this’ word in a static method, since astatic method doesn’t manipulate an object.2.  Type conversions  x = ( int )d;x =  int .Parse(s);s = x.ToString();s = d.ToString();d = x; // no type conversion neededd =  double .Parse(s); 3.  Methods with a result  (a)  int  RemainderAfterDivision( int  x,  int  y) { return  x  −  y ∗ (x/y); } (b)  double  Circumference( double  height,  double  width) { return  2 ∗ height + 2 ∗ width; } (c)  double  Diagonal( double  height,  double  width) { return  Math.Sqrt(height ∗ height + width ∗ width); } (d)  string  ThreeTimes( string  s) { return  s + s + s; } (e)  string  SixtyTimes( string  s) { string  result = ””; for  ( int  i=0; i < 60; i++)result += s; return  result; } 4


Apr 18, 2019


Apr 18, 2019
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks