XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)
2/8/2010

Topic: Topic.2.Input_Output_PrimitiveDataTypes
Example: Ex_13.Formula

Math Operators: Translating Math Formulas Into C#


References:

Goals:



1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source code.

This program is intended to visually show much money you would have in the bank in 20 years, if you started with a particular amount of money now, and you were able to accrue a certain amount of interest each year.  This is is calculated using the formula for compounding interest .  Note that in this program, will will calculate the future value (i.e., how much money you'll have in the future), starting from the present value (i.e., how much money you have now).  Another, different thing that we will NOT calculate here is the present value given a future value sum of money (i.e., how much money you should start with, if you want to end up saving up to a particular amount of money in the future).

Once we compile and run this project, the program displays two rectangles on the screen. The left rectangle is labeled with the present value (i.e., the sum of money that you start with), and the right rectangle is labeled with the future value (i.e., the sum of money you'll end up with).  At the top of the screen, the number of years, and the interest rate are displayed.  The heights of each rectangle are proportional to the values that they represent, and the distance between the 'future value' rectangle and the left edge of the screen is proportional to the number of years.

At the bottom of the screen are instructions for playing the game.  By using the left thumbstick's vertical axis (the Y axis), you can control the present value of the money that you're saving (and thus, you can control the current height of the present value rectangle).  By using the left thumbstick's horizontal axis (the X axis), you can control the rate.  By using the right thumbstick's vertical (Y) axis, you can control the number of years that the money will be saved for (and thus, you can control the location of the future value rectangle).  The height of the future value rectangle depends on the future value of the saved up money, which is calculated for you.

Additionally, when player presses the 'Back' button (or the keyboard equivalent), the program will exit.



2. Examining The Program:

Let's examine the C# source code that produces the behavior we see on-screen


FURTHER EXERCISES::  

  1. Start from a blank starter project (1000.201, if you need it), and re-do the code from this tutorial, from memory, in the starter project, as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without refering the tutorial's code.
    • Repeat this exercise daily for several days, so that you really get the hang of this.  As you go on, periodically review this by re-doing this exercise.
  2. Translating a different formula
    Using the Exercise_ 2 _DifferentFormula's starter project , you should modify the solution so that your program uses a different formula.  Notice that there are two rectangles - one on the left, and one on the right.  You must make sure that each rectangle is always labeled with it's current height.  The user should be allowed to control the current height of the left rectangle using the left thumbstick - pushing that thumbstick up/forward increases the height of the rectangle, and pulling it down/backward decreases the height.   You should set up your code so that the height of the right rectangle is determined by the following formula:


    Hint:
    Use local variables wherever you can
  3. Translating a different formula
    Using the Exercise_ 3 _DifferentFormula's starter project , you should modify the solution so that your program uses a different formula.  Notice that there are two rectangles - one on the left, and one on the right.  You must make sure that each rectangle is always labeled with it's current height.  The user should be allowed to control the current height of the left rectangle using the left thumbstick - pushing that thumbstick up/forward increases the height of the rectangle, and pulling it down/backward decreases the height.   Let's define a math-style equation to describe the height of the right rectangle.  Let's say that the height of the left rectangle is given as X , and the height of the right rectangle is given by Y , in the following equation.  You should set up your code so that the height of the right rectangle is determined by the following formula:
  4. Using the Math.Pow built-in function
    Using the Exercise_ 4 _DifferentFormula's starter project , you should modify the solution so that your program uses a different formula.  Notice that there are two rectangles - one on the left, and one on the right.  You must make sure that each rectangle is always labeled with it's current height.  The user should be allowed to control the current height of the left rectangle using the left thumbstick - pushing that thumbstick up/forward increases the height of the rectangle, and pulling it down/backward decreases the height.   Let's define a math-style equation to describe the height of the right rectangle.  Let's say that the height of the left rectangle is given as X , and the height of the right rectangle is given by Y , in the following equation.  You should set up your code so that the height of the right rectangle is determined by the following formula:

    Hint: Make sure that you can do this using the Math.Pow method, so that you're able to use functions built into C#


    Also: In addition to using Math.Pow, can you do this using nothing more than the arithmetic operators (i.e., nothing more than + , * , - and / ?)

  5. Using other built-in Math functionality
    Using the Exercise_ 6 _MathFunction's starter project , you should modify the solution so that your program demonstrates the use of several common mathematical functions, that are built into the core C# / .Net framework.  Notice that there are several rectangles - left-most one is rectangle X , and others show the result of doing some operation on the current height of rectangle X.  For example, the rectangle next to X starts off with the label Sqrt(10)=3.162278 .  As the height of X is changed (by pushing the left thumbstick up/down), the height (and label) of this "square root" rectangle is updated accordingly.  The goal for this exercise is to give you the opportunity to look up various math functions, and make use of them here, in a graphical manner.

    For this exercise, you need to fill in each of the other rectangles, as they're labeled, with the following functions:

    • Math.Sqrt
      This is already filled in for you - it returns the square root of the number that you give it.
    • Math.Round
      This method will round off a number to the nearest integer.  (You can also use it to round off after a certain number of decimal places, but that's beyond what this exercise is asking you to do)
    • Math.Ceiling
      This method will round up.
    • Math.Floor
      This method will round down.  Note that while this accomplishes the same sort of task as casting a number to an integer (i.e., digits after the decimal place will be removed), this method will NOT convert the number to an integer.  So you should use this if you want to round something down, but you want to keep your calculations as a double-precision number.
    • Math.Abs
      This method returns the absolute value of the number you give it. So Math.Abs( -10 ) will produce 10; Math.Abs(10) will also produce 10.
    • Math.Sign
      This method will return an int with the value -1 to indicate that the number you've given it is negative, 0 if the number you've given it is zero, and +1 if the number you've given is it positive.  For this rectangle, don't change the height, but do update the label to be -1, 0, or +1, depending on what the current height of X is.
    • HINT: It may be useful to look these up online.  The following seems to be a good resource: http://msdn.microsoft.com/en-us/library/system.math_methods.aspx , but you may find other resources online, too.
  6. Using other built-in Math functionality
    Using the Exercise_ 7 _MinMax's starter project , you should modify the solution so that your program demonstrates the use of two common mathematical functions: Max and Min.  These are built into the core C# / .Net framework. 

    Notice that there are four rectangles - left-most one is rectangle min , the rightmost one is named max, and the two in the middle will be referred to as X (on the left) and Y (on the right).  You should set the height of the min rectangle so that it's equal to the height of the smaller of the two middle rectangles, and set the height of the max rectangle so that it's equal to the height of the larger of the two middle rectangles.  Additionally, you should label all rectangles with their name and current height.

    • Math.Min:
      Given two numbers, it will return the smaller number.
    • Math.Max:
      Given two numbers, it will return the larger number.

Project home page : The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu
Michael Panitz
Business And Information Technology
Cascadia Community College
mpanitz@cascadia.eduu

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.
2/8/2010