0% found this document useful (0 votes)
29 views24 pages

PRU211m - Slot 4 - Check Assignment 1

This document provides an overview of data types in C#, including integers, decimals, characters, Booleans, and reference types. It discusses operations, assigning variables, type conversions, and common mistakes. Examples are given of writing programs to calculate circle areas using data types in both a console app and Unity script. The document emphasizes choosing the appropriate data type based on required precision and range.

Uploaded by

trinhminh2907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views24 pages

PRU211m - Slot 4 - Check Assignment 1

This document provides an overview of data types in C#, including integers, decimals, characters, Booleans, and reference types. It discusses operations, assigning variables, type conversions, and common mistakes. Examples are given of writing programs to calculate circle areas using data types in both a console app and Unity script. The document emphasizes choosing the appropriate data type based on required precision and range.

Uploaded by

trinhminh2907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Module 2

Data Types, Variables, and Constants


Lab exercise and Assignment
Introduction
1
Content
• Decimals/ Characters/ Booleans
• Operations
• Choosing Between Variables and Constants
• Giving Variables a Value
• Type Conversions
• What About Reference Types?
• Putting It All Together in a Console App
• Putting It All Together in a Unity Script
• Common Mistakes
2
Decimals
• The floating point data types could not handle all our real number needs.
• Because everything in a computer is stored using a limited number
of bits, the computer works in the discrete domain.
• Let's talk about the double data type
• a double is stored in 64 bits, so the total number of unique values that we can
store in a double is 2^64, which is about 1.8 * 10^19  it's not infinite.
• That means that we have to approximate lots of those infinite numbers in the real
world with a single sequence of bits in the computer, making them
indistinguishable from each other.
• We can't get perfect precision with our floating point data types.

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:

• Changes the {1} to {1:N2}:

• 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

• Write the Code, Revisited


• Change our calls to the Debug Log method back to our original approach using
concatenation instead of formatting.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy