PRU211m - Slot 4 - Check Assignment 1
PRU211m - Slot 4 - Check Assignment 1
3
Decimals
• Think about a program that runs a cash register. That program needs to be
able to precisely store dollars and cents.
• Allowing imprecision in the variables we use won't be acceptable.
• The decimal type helps solve that problem.
• The 128 bits used for the decimal type can more precisely represent the
possible values in its range
• range from -79,228,162,514,264,337,593,543,950,335 to
79,228,162,514,264,337,593,543,950,335.
• stores up to 28 decimal places.
4
Decimals
• Example:
• The valid operations for decimal: +, -, *, and /
• The tradeoff we make between decimal and the floating point types is that
decimal gives us more precise numbers in a smaller range.
• the best choice for some domains, like cash register software, but
• a worse choice for other domains, like quantum physics
• By considering what range of numbers your program needs to store and
with what precision, you can make a reasoned choice about which data
type to use.
5
Characters
• A variable or constant declared as a char (short for character) can hold a
single 16-bit Unicode character.
• A character can be a letter, a digit, a space, a punctuation mark, or other
characters.
• Example:
6
Read yourself
• Booleans
• Operations
• Choosing Between Variables and Constants
• Giving Variables a Value
• Type Conversions
• What About Reference Types?
7
Putting It All Together in a Console App
• The problem description:
• Calculate the area of the circles with integer radii from 0 to 5, then print out the
radius and area for each circle.
• Understand the Problem:
• “from 0 to 5” includes 0 and 5
• Design a Solution:
• The Main method in our Program class.
8
Putting It All Together in a Console App
• Write Test Cases:
• All we have to do is run it to make sure it prints out the required circle info.
9
Putting It All Together in a Console App
• Write the Code
10
Putting It All Together in a Console App
• Write the Code
11
Putting It All Together in a Console App
• Write the Code
12
Putting It All Together in a Console App
• Test the Code
13
Putting It All Together in a Console App
• Change the way we call the method to make formatting the output a little
easier:
• Output
14
Putting It All Together in a Console App
• If we’re going to change the way we expect the code to behave, we need
to change the test case to reflect the new behavior:
15
Putting It All Together in a Console App
• Our final change to the first call on the Console.WriteLine method is
• Final output:
16
Putting It All Together in a Unity Script
• Let's solve essentially the same problem we solved in the previous
section in Unity using the same problem description:
• Calculate the area of the circles with integer radii from 0 to 5, then print out
the radius and area for each circle.
• Understand the Problem
• Provide our output in the Console window in the Unity editor.
• Design a Solution
• Write a new PrintCircleInformation script and implement the required
functionality in the Start method in that script.
17
Putting It All Together in a Unity Script
• Write Test Cases
18
Putting It All Together in a Unity Script
• Write the Code
• Copy the code from the Main method in our console app into the Start method in our new script
and change all the calls to the Console.WriteLine method to calls to the Debug Log method instead.
• Unity provides a Mathf class that provides functionality similar to C#’s Math class, but with floats
not doubles. Change (float)Math.PI to Mathf.PI
19
Putting It All Together in a Unity Script
• Write the Code
• The Debug Log method is provided in Unity, not in standard C#, so we should search the Unity
Scripting Reference for that method.
20
Putting It All Together in a Unity Script
• Write Test Cases, Revisited
21
Putting It All Together in a Unity Script
• Test the Code
22
Common Mistakes
• Forgetting that C# is Case Sensitive
• a variable named firstInitial is NOT the same as a variable named firstinitial
• Forgetting How Integer Division Works
• when you divide two integers, you get the integer quotient, not a floating
point result from the division.
• If you do need a floating point result from dividing two integers, you can
simply use type casting as described above.
23
Summary
• Value Types, Variables, and Constants
• Integers/ Floating Point Numbers/ Decimals/ Characters/ Booleans
• Operations
• Type Conversions
• Reference Types
24