Lesson 02 In-Class Exercises

Lecture 01: Tell them to bring PCE 01, completed, to Lecture #2, and skip the ST stuff for now SudentTracker:
 Make sure to demonstrate StudentTracker first, then make them sign up in waves
Better yet, video the ST stuff, and have them do it on their own

Review: PCE 01

This exercise is actually a quick review of some of the PCEs from Lesson 01.  While not every set of In-Class Exercises will start with such a review, the instructor felt that it would be good to do a quick check-in and verify that everyone understands some of the basic skills that were covered in the previous lesson.

For this exercise you should partner up with someone (triples are ok only if there is an odd number of people).  You & your partner should download the starter project for the In-Class Exercises and make sure that you're able to compile and run it (note that the project should not do anything just yet). 

Next, add a class into the file (there's a comment to help remind you where to put it), and in that class put a RunExercise method.  Within that method start by printing out a message asking the user to type in a number. 

(While you're free to do the entire exercise all at once I would recommend that you get the program to compile and run at this point in order to confirm that the program is doing what you want it to.  In general I would recommend frequently checking your program as you're developing it).

Once that's done you should actually get the number that the user has typed in, and print that number back to the user.

Note: There should be a folded-up #region within the provided program.  If it's not folded up you should fold it by clicking on the box in the left margin, next to the word #region.  If it is folded up (as it hopefully should be) then keep it closed, just so that you don't accidentally give yourelf the answer by seeing it in the provided code for a future exercise.

Formatting Real Numbers

You might notice that when you print out real numbers, you mostly get what the variable contains.  For example, if your double x = 17.7, executing  Console.WriteLine(x); will get you 17.7.

One exception to this rule is printing out real numbers that are exactly equal to a whole number.  For example, the following:

float af = 10f;
float bf = 10.00f;
Console.WriteLine("floats:\n\taf: {0} bf:{1}", af, bf);
 
double ad = 10f;
double bd = 10.00f;
Console.WriteLine("doubles:\n\tad: {0} bd:{1}", ad, bd);

Produces the following:

floats:
    af: 10 bf:10
doubles:
    ad: 10 bd:10

What makes this interesting is that decimals always print out the exact value assigned to them, even if they're exactly equal to a whole number.  For example, the following:

decimal a = 10m;
decimal b = 10.00m;
Console.WriteLine("decimals:\n\ta: {0} b:{1}", a, b);

Produces the following:

decimals:
a: 10 b:10.00

In order to print out real numbers consistently (no matter what the underlying type - float, double, or decimal) we'll use a custom formatting string in order to tell Console.Write/.WriteLine that we want to always display exactly two digits after the decimal place, like so:

Console.Writeline(“af:{0:0.00} ad:{1:0.00} a:{2:0.00}”, af, ad, a);

In order to produce the following output:

af:10.00 ad:10.00 a:10.00

For this exercise you should experiment with the provided code (located in the class named FormattingExercise in the provided starter project) in order to be able to answer the following questions:

  1. What if the the variable has, say, 1 digit after the decimatel point (such as 17.7) and we tell Console.WriteLine to print out 2 digits?  What gets filled in to the 'extra' digit?

  2. What if the the variable has, say, 2 digits of precision (such as 17.77) and we tell Console.WriteLine to print out only 2 digits? 

  3. What if the the variable has, say, 3 digits of precision (such as 17.777) and we tell Console.WriteLine to print out only 2 digits?  Does it round off? Does it truncate?  (And what does it mean to 'truncate' something?)

  4. Is there anything else that's tricky (or interesting) about this formatting trick that you would particularly like to share with other students in the class?

Evaluating Expressions: Order of operations & data types

Order of Ops & Data Types - exam questions
Expression evaluation (and the bit about the type promotion?)

Conditional Statements: If/Else and Switch

For this exercise you should examine the class named ConvertSwitchToIfElse in the provided starter project.  You should take the provided code (most importantly, the switch statement) and convert into equivalent C# source code that only uses if and if/else statement(s) to accomplish the same task.

Integer Division

For this exercise you should examine the class named IntegerDivision in the provided starter project.

Within that class you should examine the RunExercise method, and write down what the output of each of the different lines of code will be BEFORE you run the program.  Once you've done that you should then run the program and compare the actual output to your predicted output.  Go back & reexamine anything that you got wrong, and make sure that you're clear on why the correct answer is, in fact, correct.

Modulus: By Hand

For this exercise you should write out (by hand, on paper) the result of doing the following operations:

  1. 10 % 2
  2. 10 % 5
  3. 13 % 7
  4. 10 % 10
  5. 2 % 10
  6. 5 % 10
  7. 93 % 107
  8. 107 % 93

Modulus: By Computer

For this exercise you should fill in the missing code within the ModulusExericse.RunExercise method (this means the RunExericse method in the ModulusExericse class). 

The program will help the user figure out how many buses are necessary to transport a given number of people.  The program should ask the user for the total number of people, and how many people each bus can hold.  You will need to use the various, mathematical operations that we've studied to determine how many buses are needed to transport all those people (assuming that you pack each and every bus completely full, except (possibly) for the last bus).  You also need to tell the user how many people are actually on the last bus (this may very well be less than the capacity of the bus)

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.