Lesson 03 In-Class Exercises

PCE 02 Review: Fahrenheit to Celsius

You should predict what each of the following expressions will evaluate to, given reasonable values for the variable f.  (Note that since you are not being given specific values of f you may not be able to predict exact values of celsius.

When doing these, remember the order of operations (just like math class), and remember to track the data type of the values and variables (unlike math class).  Here's a (potentially) useful link: Precedence and Order of Evaluation in C#

Example:

int f = /* exact value omitted */
double celsius = (5 / 9 ) * ( f - 32 );

The second statement will be evaluated in the following steps:

  1. The stuff inside the () goes first, and since there are multiple (), we go left to right. 
  2. The left-most () contains 5 / 9, which you know will be 0, due to integer division.
    (The fact that celsius is a double doesn't matter because we evaluate the expression on the right hand side of the =, and only assign that result to celsius as the last step)
  3. The right-most () contains f - 32, which will be 32 less than whatever f started as, but this won't matter, because of the next step
  4. The next (and final) step is to multiply the 0 from step 2 with whatever we got from step 3, which will result in zero.

Thus, celsius will always have the value zero, regardless of what value f contains

  1. int f = /* exact value omitted */
    double celsius = 5 / 9  * ( f - 32 );
  2. int f = /* exact value omitted */
    double celsius = ( f - 32 ) * 5 / 9;
  3. int f = /* exact value omitted */
    double celsius = 5 * ( f - 32 ) / 9;
  4. double f = /* exact value omitted */
    double celsius = 5 / 9  * ( f - 32 );
  5. double f = /* exact value omitted */
    double celsius = ( f - 32 ) * 5 / 9;
  6. double  f = /* exact value omitted */
    double celsius = 5.0 / 9.0  * ( f - 32 ); // what if only 5 or 9 has the .0 after it?
  7. double  f = /* exact value omitted */
    double celsius = (double)5 / 9  * ( f - 32 );

Loops

Within the starter project you'll see a class named Loops, and within that you'll see a method named RunExercise (this is normally refered to using ClassName.MethodName, as in Loops.RunExercise).  Loops.RunExercise in turn calls three other methods, each of which repeats some action several times (in each method). 

Your task is to work with a partner and examine each of the three methods in turn.  You'll notice that within each method, the same task is done several times - each time with a different type of loop.  Your job is to compare and contrast the different loops - discuss both the advantages of each type of loop, as well as the disadvantages of each type of loop, for the given task.

Be ready to summarize and explain your list of pros and cons (for each type of loop within a given task) for  the rest of the class.

Increment, decrement, and compound operators

Within the starter project you should examine the IncrementDecrementCompound.RunExercise method in detail.  You should predict what the output of the method will be (and you should write down your prediction - either on paper or in an electronic format) AND THEN run the program in order to check your results.  If the actual output differs from your output in any way you should figure out what went wrong with your prediction.

For each loop you should be prepared to explain why the code produces the answer it does.   

Random numbers: Foundations

Within the starter project you should examine the Random_Problems.RunExercise method in detail - feel free to run the method, if you want.

You need to fix all the errors in that method, so that the method will do the following:

  1. The first loop should print 10 random, whole numbers between 1 and 10 (including, potentially, 1 or 10).
  2. The second and third loops should print out the same sequence of 5 random, real numbers.

Random numbers: Using Modulus

Within the starter project you should fill in the Random_Via_Modulus.RunExercise method so that it will print out 10 random, whole numbers that range from 0 to 3 (including, possibly, 0 and 3).  For this exercise you can only use the version of .Next that takes no parameters (there's an example in the method already).  In other words you'll need to use the modulus operator to limit the possible random numbers that you might generate.

Once you've gotten that first part done, you should have this method also generate random numbers in the following ranges:

  1. [0,3] (this was the range described above - it's numbered here so y'all can put your answers on the white board to discuss them)

  2. [1, 10]

  3. [-4, 10]

  4. [2,20], but ONLY the even numbers (this will require more than 1 line of code)

Be ready to explain your answers to the rest of the class (both by giving the formula, and by explaining how the formula accomplishes the given task)

Parameters & Return Values: Fixing broken code

For this exercise you will take the following code and paste into the provided starter project, then find and fix all the errors in the code so that the Add method works as described here.  While it's recommended that you put this code into the Params_Return_Values class, you are not required to.

The Add method should take two parameters, add them up,and return the sum of the two numbers.  The Add method should compile and run with all of the examples listed in the RunExercise method, below:

public void RunExercise()
{
	double result = Add(3, 5);
	Console.WriteLine("3 + 5 is: {0}", result);
 
	Console.WriteLine("3.1 + 5 is: {0}", Add(3.1, 5));
 
	Console.WriteLine("3.1 + -5.7 is: {0}", Add(3.1, -5.7));
}
// paste the functions (methods) here
public int Add( a, b)
{
	int sum = a + b;
	return sum;
}
public static void Main() 
{
Program theObject = new Program();
theObject.RunExercise();
}

Future Ideas

Review: Evaluating logical expressions?

Params & return values: Writing up a small function?

Student Code Samples

PEX for fun?

Skipped loops, increment, did the rest.  Did explain increment in class, briefly

One Minute Paper

"A “one-minute paper” may be defined as a very short, in-class writing activity (taking one-minute or less to complete) in response to an instructor-posed question, which prompts students to reflect on the day’s lesson and provides the instructor with useful feedback." (from http://www.oncourseworkshop.com/Awareness012.htm).

For this One Minute Paper, I would like you to think back on both the preview videos / viewing quiz and today's in-class exercises, and quickly write up answers to the questions listed therein - it should take a couple minutes or so.

Head on over to the Google Docs form, and fill out the One Minute Paper for this lecture.