0% found this document useful (0 votes)
79 views27 pages

PRU211m - Slot 5 - Quiz 1 - Module 3 - Classes and Objects

The document discusses classes and objects in C# programming. It covers the differences between classes and objects, encapsulation, instantiation, and reference types. The document also includes exercises on creating dice and card objects and calling their methods.

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)
79 views27 pages

PRU211m - Slot 5 - Quiz 1 - Module 3 - Classes and Objects

The document discusses classes and objects in C# programming. It covers the differences between classes and objects, encapsulation, instantiation, and reference types. The document also includes exercises on creating dice and card objects and calling their methods.

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/ 27

Slot 5:

Quiz 1
Module 3
Classes and Objects
1
Learning Objectives
• Describe the difference between classes and objects.
• Develop an object-oriented console application to meet specific
functional requirements.
• Use C# and custom documentation to effectively utilize C# and
custom C# code.

2
Content (slot 1)
1. Introduction to Classes and Objects
2. Your First C# Program Revisited
3. Calling Methods
4. Exercise 8
5. Exercise 9
6. Reference Types in Memory

3
Content (slot 2)
7. The Circle Problem Revisited
8. The Circle Problem in Unity Revisited
9. Putting It All Together in a Console App
10. Putting It All Together in a Unity Script
11. Common Mistakes

4
Introduction to Classes and Objects
• The main idea behind the object-oriented paradigm is that our
problem solutions consist of a set of collaborating objects.
• These objects reflect all the objects we see around us in the real
world.
• Examples: furniture, cars, bank accounts, students, and bicycles.
• An object has states, behaviors and identity.

5
Introduction to Classes and Objects
• The state (field) of an object indicates what the object “looks like” at any
given time.
• Example: States of a Lotus Elise include its horsepower, speed, its current
location, how many wheels (if any <grin>) are in contact with the road at the
moment, and so on.
• What is state of a book/ a playing card?

6
Introduction to Classes and Objects
• C# provides an easy way to access an object's state through properties.
• Example: For the Lotus Elise, we need to know its location (and other information)
to check if it's colliding with something in our game.
• Methods specify an object's behavior.
• Example: we want to be able to open or close the book, to change the page in the
book, and to look at the current page. For the card object, we’ll want to be able to
flip the card over, so we’d need the card object to do that for us.
• Identity distinguish one object from others.

7
Introduction to Classes and Objects
• states/ fields (lightest color)
• properties (medium color)
• methods (darkest color)

8
Introduction to Classes and Objects
• Encapsulation is the process of combining related fields and behaviors into a
single object, essentially placing them all in a single “capsule.”
• Information hiding means that the fields of an object are “hidden behind the
wall of properties and methods” for the object.
• instantiation used to create a new object (instance) from the class.
• In C# the fields, properties, and methods of a class are called the class
members.
• Important Unity Note: There are lots of classes in Unity that actually do
expose the fields of objects rather than using properties to control access to
those fields. We'll obviously access those fields when we need them without
feeling any guilt whatsoever, but when we design our own Unity scripts (which
are classes) we'll use properties whenever we can. 9
Introduction to Classes and Objects
• UML representation of playing card class.

10
Your First C# Program Revisited
• UML for Message Class
• No property
• The Print method definitely needs access to message field.

11
Your First C# Program Revisited
• Message class document.

12
Your First C# Program Revisited

• To create a tiny message that says Hi!, we'd use:

13
Your First C# Program Revisited
• To know that the constructor requires one argument for the message
string, we click on the Message constructor link in the documentation.

14
Your First C# Program Revisited
• Creating a message object for a message has several lines:

• escape sequence:
• \n
Newline. The cursor is moved to the next line on the screen
• \t
Tab. The cursor moves to the next tab stop on the screen
• \r
Carriage return. Moves the cursor back to the beginning of the current line
• \\
Backslash. Prints a backslash. We can't just include a single backslash, because a single
backslash is interpreted as the escape character
• \"
Double quote. Prints a double quote. We can't just include a double quote in our string,
15
because a double quote is interpreted as the end of the string.
Your First C# Program Revisited
• Objects that have been declared as variables but haven't been created yet
are initialized to a special value, null.
• We can't use any of the object's methods or do anything else particularly
useful with the object until we create it.
• Example:

The compiler will complain to you that rainMessage was never initialized.

16
Your First C# Program Revisited

17
Calling Methods

18
Calling Methods

19
Calling Methods
• Parameters are the things included in the method header
• At this point, we learn about a method’s parameters by reading the
documentation for the method.
• Arguments are the things we include in a method call to “match up with”
the parameters for the method we’re calling.
• Ex:
• Calling a method that doesn't return a value and doesn't have any arguments:

• Calling a method that returns a value but still doesn't have any parameters:

20
Calling Methods
• Ex:
• Calling a method that has arguments but doesn't return a value:
• Or

• Calling a method that has arguments and return a value:

21
Exercise 8: Rolling the Dice

22
Exercise 8: Rolling the Dice
• Problem 1 - Create two dice
• Declare die1 and die2 variables and use the appropriate Die constructor to put new six-sided Die objects
into those variables.
• Use the help documentation I provided to figure out which constructor to use.
• Problem 2 - Tell the dice to roll themselves
• Tell the die1 and die2 variables to roll themselves. Use the help documentation I provided to figure out
which method to use.
• Side note: The solution I gave you includes a RandomNumberGenerator class that's initialized in the
Main method and used by the Die class when a die rolls itself. It's pretty common to have a random
number generator that's used by the whole "game".
• Problem 3 - Print the top sides of the two dice and print their sum
• Print the top sides of the two dice. Use the help documentation I provided to figure out which property
to use.
• Print the sum of the top sides of the two dice.
• Hint: It's easiest to declare a variable that holds the sum of the top sides of the two dice, then print that
23
sum.
Exercise 9: Look at the Cards

24
Exercise 9: Look at the Cards
• Problem 1 - Create a deck and tell it to print itself
• Declare a deck variable and use the Deck constructor to put a new Deck object into the deck variable.
• Tell the deck to print itself. Use the help documentation I provided to figure out which method to use.
• Note: The deck prints itself from bottom to top, so the last card listed is at the top of the deck.
• Problem 2 - Tell the deck to shuffle and print itself
• Tell the deck to shuffle itself and print itself. Use the help documentation I provided to figure out which methods to
use.
• Problem 3 - Take two cards from the deck and print their ranks and suits
• Take a card from the top of the deck and print its rank and suit. Use the help documentation I provided to figure
out which Deck method to use to get the top card and which Card properties to use to print the rank and suit.
• Take another card from the top of the deck and print its rank and suit.
• Hint 1: The Card class doesn't expose a Print method, so you have to access a card's properties to print the required
information.
• Hint 2: You haven't called a method that returns a value yet. Here's a good way to do that for this exercise:
• Card card0=deck.TakeTopCard();
• The DeckTakeTopCard method returns a Card object. You need to save that object in a variable so you can access its
properties. 25
Reference Types in Memory
• Reference types are called that because variables that are reference
types refer to an object that's been created in memory.
• The data type for an object is a class which are also called reference
data types in C#.
• The value of a reference type variable is the memory address of the
actual object in memory.

26
Reference Types in Memory
• Value and Reference
Type Example

27

You might also like