Russian Revolutions 1905 & 1917 Russian Revolutions 1905 & 1917.
Additional Pair Programming Exercises 1. Problem 1 For a wheel of radius r feet, how many...
-
Upload
angel-lang -
Category
Documents
-
view
220 -
download
0
Transcript of Additional Pair Programming Exercises 1. Problem 1 For a wheel of radius r feet, how many...
Additional Pair Programming Exercises
1
Problem 1For 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 Π * 2r2. The distance traveled with one revolution of
the wheel is the circumference3. There are 5280 feet per statute mile 2
r
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
Problem 2Given 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
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
Pseudocode for Problem 1
1. Declare variables to hold user input and results of calculations
2. Tell the user what the program does3. Ask the user to enter value for the radius of the
wheel in feet4. Get the radius and save it in a variable5. Calculate the circumference of the wheel6. Divide feet per mile by circumference in feet to
give number of revolutions per mile7. Display the result, labeled appropriately
6
Some Problem 1 Test Data
7
Radius (ft) Circumference (ft) Revolutions/Mile
1 6.283 840.3
0.1592 1 5280
840.3 5280 1
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
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
Pseudocode for Problem 2
1. Declare variables to hold user input and results2. Tell the user what the program does3. Ask the user to enter value for the temperature
in Fahrenheit4. Get the temperature in Fahrenheit and save it
in a variable5. Calculate the corresponding temperature in
Celsius6. Display the result, labeled appropriately
10
Some Problem 2 Test Data
11
Degrees F Degrees C
212 100
32 0
100 37.8
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
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
Problem 2 Pitfallsconst 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