Presentation is loading. Please wait.

Presentation is loading. Please wait.

Additional Pair Programming Exercises 1. Problem 1 For a wheel of radius r feet, how many revolutions are required for that wheel to travel 1 mile? What.

Similar presentations


Presentation on theme: "Additional Pair Programming Exercises 1. Problem 1 For a wheel of radius r feet, how many revolutions are required for that wheel to travel 1 mile? What."— Presentation transcript:

1 Additional Pair Programming Exercises 1

2 Problem 1 For a wheel of radius r feet, how many revolutions are required for that wheel to travel 1 mile? What do we know? 1. The circumference of a circle with radius r is Π * 2r 2. The distance traveled with one revolution of the wheel is the circumference 3. There are 5280 feet per statute mile 2 r

3 Problem 1 What do we need from the user? 1. The radius of the wheel What will my program produce? 1. An explanation of what the program does 2. The number of revolutions required to move the wheel one mile 3

4 Problem 2 Given the temperature in degrees Fahrenheit (°F), what is the temperature in degrees Celsius (°C)? What do we know? 1. °C = (°F – 32) * 5/9 4

5 Problem 2 What do we need from the user? 1. Temperature in °F What will my program produce? 1. An explanation of what the program does 2. The equivalent temperature in °C 5

6 Pseudocode for Problem 1 1. Declare variables to hold user input and results of calculations 2. Tell the user what the program does 3. Ask the user to enter value for the radius of the wheel in feet 4. Get the radius and save it in a variable 5. Calculate the circumference of the wheel 6. Divide feet per mile by circumference in feet to give number of revolutions per mile 7. Display the result, labeled appropriately 6

7 Some Problem 1 Test Data 7 Radius (ft)Circumference (ft)Revolutions/Mile 16.283840.3 0.159215280 840.352801

8 A Problem 1 Solution (1 of 2) using System; class Program { static void Main() { const int MULTIPLIER = 2; // Declare constants to avoid magic numbers const int FEET_PER_MILE = 5280; // 1. Declare variables to hold user input and results of calculations double radius, circumference, revolutions; // 2. Tell the user what the program does Console.WriteLine("If you enter the radius of a wheel, this program will calculate"); Console.WriteLine("the number of revolutions of the wheel required to travel 1 mile.\n"); // 3. Ask the user to enter value for the radius of the wheel in feet Console.Write("Please enter the radius of the wheel in feet: "); // 4. Get the radius and save it in a variable radius = double.Parse(Console.ReadLine()); (CONTINUED ON NEXT SLIDE) 8

9 A Problem 1 Solution (2 of 2) (CONTINUED FROM PREVIOUS SLIDE) // 5. Calculate the circumference of the wheel circumference = Math.PI * MULTIPLIER * radius; // 6. Divide feet per mile by circumference in feet to give number of revolutions per mile revolutions = FEET_PER_MILE / circumference; // 7. Display the result, labeled appropriately Console.WriteLine("\nThe number of revolutions required for a wheel of radius {0}", radius); Console.Write("to travel 1 mile is {0:F2}", revolutions); Console.ReadLine(); }//End Main() }//End class Program 9

10 Pseudocode for Problem 2 1. Declare variables to hold user input and results 2. Tell the user what the program does 3. Ask the user to enter value for the temperature in Fahrenheit 4. Get the temperature in Fahrenheit and save it in a variable 5. Calculate the corresponding temperature in Celsius 6. Display the result, labeled appropriately 10

11 Some Problem 2 Test Data 11 Degrees FDegrees C 212100 320 10037.8

12 A Problem 2 Solution (1 of 2) using System; class Program { static void Main() { const int FREEZING_POINT = 32; // Declare constants to avoid magic numbers const double CONVERSION_FACTOR = 5.0 / 9.0; // 1. Declare variables to hold user input and results double tempFahrenheit, tempCelsius; // 2. Tell the user what the program does Console.WriteLine("If you enter the temperature in Fahrenheit, this program will calculate"); Console.WriteLine("the equivalent temperature in Celsius.\n"); // 3. Ask the user to enter value for the temperature in Fahrenheit Console.Write("Please enter the temperature in degrees Fahrenheit: "); (CONTINUED ON NEXT SLIDE) 12

13 A Problem 2 Solution (2 of 2) (CONTINUED FROM PREVIOUS SLIDE) // 4. Get the temperature in Fahrenheit and save it in a variable tempFahrenheit = double.Parse(Console.ReadLine()); // 5. Calculate the corresponding temperature in Celsius tempCelsius = (tempFahrenheit - FREEZING_POINT) * CONVERSION_FACTOR; // 6. Display the result, labeled appropriately Console.WriteLine("\n{0:F1} degrees Celsius is the equivalent of {1} degrees Fahrenheit.", tempCelsius, tempFahrenheit); Console.ReadLine(); }//End Main() }//End class Program 13

14 Problem 2 Pitfalls const double CONVERSION_FACTOR = 5 / 9; 5 and 9 are integers, so the non-rounded (truncated) integer quotient of 5 / 9 = 0 Fix this by using floating point values for the dividend and divisor const double CONVERSION_FACTOR = 5.0 / 9.0; Lesson learned: Be careful with integers when dividing 14


Download ppt "Additional Pair Programming Exercises 1. Problem 1 For a wheel of radius r feet, how many revolutions are required for that wheel to travel 1 mile? What."

Similar presentations


Ads by Google