17.3. Studio: If It Ain’t Broke, Add a Breakpoint!¶
The purpose of this studio is to talk about your current debugging strategies and how to make the most of the debugger tools discussed in this chapter.
Before we start practicing with debugging tools, go over with the group one error you encountered with any exercise, studio, or book example using Visual Studio. This could be the result of a typo or a logical error.
What was the error?
How did you solve this error? What have been the strategies and tools you have been using so far to debug your code?
Could one of the debugging tools help you when addressing this error? For example, if you encountered an error where scope was interferring with access to data, could you find the accessibility of the data with a debugging tool?
17.3.2. Part 1: Calculate The Area Of A Circle¶
As you build your code for today’s studio, explore the colorations, icons, and other intellisense features of Visual Studio. Don’t forget to try the debugger if something isn’t working as you planned, or even if it is working as you planned.
To get started: Create a new console application in Visual Studio for the studio. You can refer back to how to create a new C# project.
Write a program that prompts the user for the radius of a circle. Calculate the area of the user’s circle and print the result.
Recall that the area of a circle is
A = pi * r * r where
r is the radius.
Here’s an example of how your program should work:
Enter a radius: 2.5 The area of a circle of radius 2.5 is: 19.625
Some questions to ask yourself:
What data type should the radius be?
What is the best way to get user input into a variable
radiusof that type?
Do you really need more than 3 decimal places?
126.96.36.199. More Calculations¶
Using the same radius, calculate the circumference (
2*pi*r) and diameter of the circle (
Output the results.
188.8.131.52. Road Trip!¶
Ask the user for the miles per gallon of their car.
If the radius that they entered is in miles, output how many gallons of gas they will use to go around this circle.
17.3.3. Part 2: Working With The Debugger¶
To get started, try the following:
circumferenceto the Watch pane to track the value of that property. Does it change?
Add a few breakpoints inside of
Program.csand make note of where you expect the program to break its execution.
After you look through the code and try out these tasks, take it one step further by answering these questions.
When would you use the Call Stack pane? If you run the app and it is already functioning, what shows up there?
Would a conditional breakpoint make sense to use in the context of this app? Try changing one of the breakpoints you have already added to a conditional breakpoint and run your app!
Once you have gone through the code, open up a piece of code you have been struggling with. In what ways could making use of debugging tools help you figure out what is going on with the code?
17.3.4. Bonus Missions If You Want To Expand Your Code¶
Think about how we could make this program more modular by breaking out some of the code into a separate class. For example, we could pull out the circle information into a
Circleclass and leave the user questions and console messages in
Program. Take a look at for a refresher on using another class file.
Extend your program further by creating a way to prevent negative numbers from being accepted.
Add additional validation to your program. If the user enters a non-numeric character or a empty string? Print an error message and quit. You’ll need to peek ahead to learn about conditional syntax in C#.