Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Debugging Programs Session 10.2. Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio.

Similar presentations


Presentation on theme: "11 Debugging Programs Session 10.2. Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio."— Presentation transcript:

1 11 Debugging Programs Session 10.2

2 Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio to find out what a program is “thinking”  Learn more about how C# performs calculations, and the way that integer and floating point variables behave  Find out how the C# compiler tries to ensure that programs do not do the wrong thing  Make the image scaling program work correctly  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio to find out what a program is “thinking”  Learn more about how C# performs calculations, and the way that integer and floating point variables behave  Find out how the C# compiler tries to ensure that programs do not do the wrong thing  Make the image scaling program work correctly Chapter10.2: Debugging Programs2

3 The Tests for getPercentage  These are the tests that we have set for the getPercentage method  If it can work out all these answers it might be a working solution  These are the tests that we have set for the getPercentage method  If it can work out all these answers it might be a working solution Chapter10.2: Debugging Programs3 if ( (getPercentage(0, 0) == 0) && // 0 percent of 0 (getPercentage(0, 100) == 0) && // 0 percent of 100 (getPercentage(50,100) == 50) && // 50 percent of 100 (getPercentage(100,50) == 50) && // 100 percent of 50 (getPercentage(10,100) == 10) ) // 10 percent of 100 { graphics.GraphicsDevice.Clear(Color.Green); }

4 A Percentage Algorithm  We could describe how to work out percentages as follows: 1. Calculate the fraction of the amount that you want (this is the percentage divided by 100; in other words, 50 percent would give you 50/100, which is a half). 2. Multiply the incoming amount by this fraction to create the result.  Now we need to create some C# that will perform these calculations for us  We could describe how to work out percentages as follows: 1. Calculate the fraction of the amount that you want (this is the percentage divided by 100; in other words, 50 percent would give you 50/100, which is a half). 2. Multiply the incoming amount by this fraction to create the result.  Now we need to create some C# that will perform these calculations for us Chapter10.2: Debugging Programs4

5 Our First getPercentage Method  This is a literal translation of the algorithm  The / operator means divide and the * operator means multiply  Now we can test the method in the test harness  This is a literal translation of the algorithm  The / operator means divide and the * operator means multiply  Now we can test the method in the test harness Chapter10.2: Debugging Programs5 int getPercentage(int percentage, int inputValue) { int fraction = percentage / 100; int result = fraction * inputValue; return result; }

6 1. Failed Tests Chapter10.2: Debugging Programs6  From the title of the demonstration you can deduce that this is not going to work  And it does fail  Now we need to find out why  From the title of the demonstration you can deduce that this is not going to work  And it does fail  Now we need to find out why

7 Debugging  What we are doing now is debugging  We have a program that should work, but doesn’t  The algorithm we have used is correct but the way that the program implements it must be wrong  We need to look inside the program and find out what it is doing  Fortunately, Visual Studio provides some very powerful tools to help with this  What we are doing now is debugging  We have a program that should work, but doesn’t  The algorithm we have used is correct but the way that the program implements it must be wrong  We need to look inside the program and find out what it is doing  Fortunately, Visual Studio provides some very powerful tools to help with this Chapter10.2: Debugging Programs7

8 Visual Studio and Debugging  What we really want is a way of stopping the program and taking a look inside it  Visual Studio provides tools to do this  These tools will even work if the game is running on an Xbox or a Zune  They are also the way that professional programmers debug their programs  The first thing we are going to do is put a breakpoint in the program to stop it at a particular statement  What we really want is a way of stopping the program and taking a look inside it  Visual Studio provides tools to do this  These tools will even work if the game is running on an Xbox or a Zune  They are also the way that professional programmers debug their programs  The first thing we are going to do is put a breakpoint in the program to stop it at a particular statement Chapter10.2: Debugging Programs8

9 Adding a Breakpoint  You add a breakpoint by clicking to the left of the statement you want to break at  This adds a red dot and highlights the breakpoint line  When the program reaches this line it will stop running  You add a breakpoint by clicking to the left of the statement you want to break at  This adds a red dot and highlights the breakpoint line  When the program reaches this line it will stop running Chapter10.2: Debugging Programs9

10 Hitting a Breakpoint  When the program reaches a breakpoint it stops running  The statement that was reached is highlighted as shown  The programmer can now take a look inside the program  When the program reaches a breakpoint it stops running  The statement that was reached is highlighted as shown  The programmer can now take a look inside the program Chapter10.2: Debugging Programs10

11 Viewing a Variable  You can view the contents of a variable simply by resting the cursor over the variable in the program  Visual Studio will then pop up the value for you  This is showing us that the value of fraction is 0  You can view the contents of a variable simply by resting the cursor over the variable in the program  Visual Studio will then pop up the value for you  This is showing us that the value of fraction is 0 Chapter10.2: Debugging Programs11

12 Resuming a Paused Program  When a program is paused at a breakpoint, Visual Studio provides controls you can use to restart the program  The green Run button will start the program running again  When a program is paused at a breakpoint, Visual Studio provides controls you can use to restart the program  The green Run button will start the program running again Chapter10.2: Debugging Programs12

13 2. Debugging Chapter10.2: Debugging Programs13  We are now going to take a look inside the program and try to find out why getPercentage is not working

14 Faulty Fractions  The fraction variable is declared as an integer  This means that it cannot hold the value of 0.5, which is the value we want to use this time  The fraction variable is declared as an integer  This means that it cannot hold the value of 0.5, which is the value we want to use this time Chapter10.2: Debugging Programs14 int getPercentage(int percentage, int inputValue) { int fraction = percentage / 100; int result = fraction * inputValue; return result; }

15 Making the Fraction Variable into a Float  A floating point variable can hold fractions  We just need to declare fraction as a float rather than an int, and it can hold the values that we want to use  A floating point variable can hold fractions  We just need to declare fraction as a float rather than an int, and it can hold the values that we want to use Chapter10.2: Debugging Programs15 int getPercentage(int percentage, int inputValue) { float fraction = percentage / 100; int result = fraction * inputValue; return result; }

16 3. Making the Fraction Float Chapter10.2: Debugging Programs16  It looks as if making the fraction variable a floating point may not be smooth sailing

17 The Compilation Error  After the fraction variable was made into a floating point variable the program no longer compiles  The compiler is complaining about the statement that calculates the result  After the fraction variable was made into a floating point variable the program no longer compiles  The compiler is complaining about the statement that calculates the result Chapter10.2: Debugging Programs17

18 Unpicking Error Messages  The error message from the compiler is a bit hard to understand: “Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)”  What it is saying is that it will not allow you to take a floating point value and put it into an integer  By declaring fraction as float you make calculations that involve the variable fraction produce floating point results  The error message from the compiler is a bit hard to understand: “Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)”  What it is saying is that it will not allow you to take a floating point value and put it into an integer  By declaring fraction as float you make calculations that involve the variable fraction produce floating point results Chapter10.2: Debugging Programs18

19 What Went Wrong 1. We need the fraction variable to be able to store fractions so we declare it as float 2. This means that the expression: fraction * inputValue - generates a floating point result 3. The C# compiler does not let a program put a floating point value directly into an integer 4. It therefore refuses to compile the program. 1. We need the fraction variable to be able to store fractions so we declare it as float 2. This means that the expression: fraction * inputValue - generates a floating point result 3. The C# compiler does not let a program put a floating point value directly into an integer 4. It therefore refuses to compile the program. Chapter10.2: Debugging Programs19

20 Discarding Data  When C# puts a floating point value into an integer it discards the fractional part completely:  A $6.99 price would drop to $6.00  This is called a “narrowing” operation  Since this means that data is lost, the compiler will not perform the action automatically  It needs to be told explicitly that the programmer is OK with the requested action  When C# puts a floating point value into an integer it discards the fractional part completely:  A $6.99 price would drop to $6.00  This is called a “narrowing” operation  Since this means that data is lost, the compiler will not perform the action automatically  It needs to be told explicitly that the programmer is OK with the requested action Chapter10.2: Debugging Programs20 int result = fraction * inputValue;

21 Casting  You can tell the compiler it is OK to perform the conversion by adding something called a cast  The cast is expressed as the required type, enclosed in brackets  This tells the compiler to perform the conversion, and makes the result of the expression the type that you want  The fractional part is still lost, but the compiler is OK because you are taking responsibility for it  You can tell the compiler it is OK to perform the conversion by adding something called a cast  The cast is expressed as the required type, enclosed in brackets  This tells the compiler to perform the conversion, and makes the result of the expression the type that you want  The fractional part is still lost, but the compiler is OK because you are taking responsibility for it Chapter10.2: Debugging Programs21 int result = (int) (fraction * inputValue);

22 4. Still No Happy Ending Chapter10.2: Debugging Programs22  We can make the program compile  But the program still doesn’t work  We can make the program compile  But the program still doesn’t work

23 Expressions and Operators  In a C# program, the behavior of an operator is determined by the things it is working on  We have seen this with +, which can be used to add numbers or join strings together  The divide ( / ) operator is the same  When applied between integers it produces an integer result, with the fractional part removed  This results in us getting 0 instead of 0.5  In a C# program, the behavior of an operator is determined by the things it is working on  We have seen this with +, which can be used to add numbers or join strings together  The divide ( / ) operator is the same  When applied between integers it produces an integer result, with the fractional part removed  This results in us getting 0 instead of 0.5 Chapter10.2: Debugging Programs23 float fraction = percentage / 100;

24 Casting to the Rescue (Again)  We can get the floating point division by casting one of the variables to a floating point value  This time we are casting an int value into a float  This means that the expression will now evaluate a floating point result  It should mean that the getPercentage method will actually work  We can get the floating point division by casting one of the variables to a floating point value  This time we are casting an int value into a float  This means that the expression will now evaluate a floating point result  It should mean that the getPercentage method will actually work Chapter10.2: Debugging Programs24 float fraction = (float) percentage / 100;

25 5. Success at Last Chapter10.2: Debugging Programs25  We now have a version of getPercentage that passes all the tests  The next thing to do is add it to our program  We now have a version of getPercentage that passes all the tests  The next thing to do is add it to our program

26 Using getPercentage  These statements reduce the width and the height of the display rectangle by 1 percent each time Update is called  This provides a smooth change in the size and should preserve the shape of the image  You will be working with these methods in the practical session  These statements reduce the width and the height of the display rectangle by 1 percent each time Update is called  This provides a smooth change in the size and should preserve the shape of the image  You will be working with these methods in the practical session Chapter10.2: Debugging Programs26 jakeRect.Width = jakeRect.Width – getPercentage(1, jakeRect.Width); jakeRect.Height = jakeRect.Height – getPercentage(1, jakeRect.Height); jakeRect.Width = jakeRect.Width – getPercentage(1, jakeRect.Width); jakeRect.Height = jakeRect.Height – getPercentage(1, jakeRect.Height);

27 6. Smooth Zooming  This version of the game does a much smoother zoom  However, we may need to slow it down a bit  You will work on this in the practical session  This version of the game does a much smoother zoom  However, we may need to slow it down a bit  You will work on this in the practical session Chapter10.2: Debugging Programs27

28 Summary  Visual Studio provides a means by which you can interrupt a running program and view the values of variables  The compiler will not automatically convert between values of different types if this would result in a loss of data  Programmers use casting to force conversions between types  The result produced by an operator is dependent on the things it is working on  Visual Studio provides a means by which you can interrupt a running program and view the values of variables  The compiler will not automatically convert between values of different types if this would result in a loss of data  Programmers use casting to force conversions between types  The result produced by an operator is dependent on the things it is working on Chapter10.2: Debugging Programs28

29 True/False Revision Quiz  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another.  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another. Chapter10.2: Debugging Programs29

30 True/False Revision Quiz  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another.  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another. Chapter10.2: Debugging Programs30

31 True/False Revision Quiz  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another.  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another. Chapter10.2: Debugging Programs31

32 True/False Revision Quiz  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another.  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another. Chapter10.2: Debugging Programs32

33 True/False Revision Quiz  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another.  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another. Chapter10.2: Debugging Programs33

34 True/False Revision Quiz  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another.  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another. Chapter10.2: Debugging Programs34

35 True/False Revision Quiz  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another.  A program will crash when it reaches a breakpoint.  It is not possible to put breakpoints in XNA programs running on the Xbox.  Integer variables can contain fractions.  The C# compiler will not automatically convert from float to int.  The behavior of an operator depends on the things it is working on.  A cast is used to convert from one type to another. Chapter10.2: Debugging Programs35


Download ppt "11 Debugging Programs Session 10.2. Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio."

Similar presentations


Ads by Google