PRU211m - Slot 6 - Module 3 - Continute

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

Slot 6: Module 3 (Cont’d)

Classes and Objects

1
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

2
The Circle Problem Revisited
• 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
• The problem hasn’t changed, so you should still understand it.
• Design a Solution

3
The Circle Problem Revisited
• Write Test Cases:
• Test case is exactly the same as it was in the previous chapter.

4
The Circle Problem Revisited
• Write the Code:

5
The Circle Problem Revisited
• Write the Code:

6
The Circle Problem Revisited
• Write the Code:

7
The Circle Problem Revisited
• Write the Code:

8
The Circle Problem Revisited
• Write the Code:

9
The Circle Problem Revisited
• Test the Code:

10
The Circle Problem Revisited
• Test the Code:
• Test Case 1: Checking Radius and Area Info Results

11
The Circle Problem in Unity Revisited
• Problem description:
• Display circles with integer radii from 1 to 5 in the game window, with each circle
displaying its own radius and area in the Console window.
• Understand the Problem:
• First, we removed the circle with a radius of 0 because a circle with a 0 radius will
be pretty hard to see!
• Second, the problem description says to display the circles “in the game window”.
• Design a Solution:
• Write a new Circle script and implement the required functionality in the Start
method in that script.

12
The Circle Problem in Unity Revisited
• Write Test Cases:

13
The Circle Problem in Unity Revisited
• Write the Code:
• Our Circle script will be significantly different from the Circle class we wrote in the
previous section because we need to make lots of changes to make things work
properly in Unity.
• Look at our Circle game object in the Unity editor.
• Our Circle game object has a Transform component, a Sprite Renderer component,
and the Circle script we added as a component.

14
The Circle Problem in Unity Revisited
• Write the Code:
• Circle Object in Inspector

15
The Circle Problem in Unity Revisited
• Write the Code:

16
The Circle Problem in Unity Revisited
• Write the Code:
• The UnityEngine namespace contains a Mathf class that essentially provides all the
functionality of the Math class in the System namespace, but using floats instead
of doubles.
• To scale the displayed circle based on its radius, a transform is used to read and set
(write) the position, rotation, and scale of an object in the game world.
• Look at the documentation for the Transform class in the Unity Scripting Reference.

17
The Circle Problem in Unity Revisited
• Write the Code:

18
The Circle Problem in Unity Revisited
• Write the Code:
• Each Vector3 object has x, y, and z properties for the vector.
• we don't have to modify the z component of the vector because we're working in
2D in this Unity project.

19
The Circle Problem in Unity Revisited
• Test the Code: Test Case 1: Checking Radius and Area Info Results

20
Putting It All Together in a Console App
• The problem description:
• Create three cards with ranks and suits of your choosing, then print out the
ranks and suits of those cards in reverse order.
• Understand the Problem:
• What does “reverse order” mean? let's assume that means we print the last
card we created first, then the second card we created, and finally the first
card.
• Pick the ranks and suits: the Ace of Spades, the Jack of Diamonds, and the
Queen of Hearts.
• Design a Solution:
• Using an application class that creates the three cards, then prints the
pertinent information in reverse order.
21
Putting It All Together in a Console App
• Write Test Cases:
• assumption here – that the Card class has already been tested, so we don't
have to test it again here.

22
Putting It All Together in a Console App
• Write the Code:
Card Class

23
Putting It All Together in a Console App
• Write the Code:

24
Putting It All Together in a Console App
• Write the Code:

25
Putting It All Together in a Console App
• Write the Code:

26
Putting It All Together in a Console App
• Write the Code: Application class

27
Putting It All Together in a Console App
• Test the Code

28
Putting It All Together in a Unity Script
• The problem description:
• Create three cards with ranks and suits of your choosing, then display the
three cards, face up, on the screen.
• Understand the Problem: pretty straightforward
• Design a Solution:
• develop a Card script that includes access to strings for the rank and suit of
the card as well as the ability to flip the card over.

29
Putting It All Together in a Unity Script
• Design a Solution: UML for Card Script

30
Putting It All Together in a Unity Script
• Write Test Cases:

• Write the Code

31
Putting It All Together in a Unity Script
• Write the Code
• Project window

32
Putting It All Together in a Unity Script
• Write the Code:
• Inspector

33
Putting It All Together in a Unity Script
• Write the Code:

34
Putting It All Together in a Unity Script
• Write the Code:

35
Putting It All Together in a Unity Script
• Write the Code:

36
Putting It All Together in a Unity Script
• Write the Code:

37
Putting It All Together in a Unity Script
• Test the Code:
Test Case 1: Checking Card Display

38
Common Mistakes
• Trying to Use an Object Before It's Created
• Adding Parentheses After the Property Name
• Forgetting the Parentheses After the Method Name

39
Summary
1. Classes and Objects
2. Calling Methods
7. Common Mistakes

40

You might also like