This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Since we've got all our data parsed, we'll calculate the conversion rate of goals for each game.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we've got our soccer game result
data nicely parsed into a collection,
0:00
we can perform some analysis on it.
0:04
Since we've got data on
the total number of shots and
0:07
goals, we can calculate
the conversion rate.
0:10
To do that,
we'll take the total number of goals and
0:13
divide it by the number of shots.
0:16
We need to add a property to store our
conversion rate in the gameResult class.
0:18
Since this number will
probably have decimals,
0:25
which data type do you
think we should use?
0:27
We don't need to be super precise, so
0:29
let's use the default
floating point type double.
0:32
public double ConversionRate, get, set.
0:36
Now back in our method, ReadSoccerResults.
0:42
Back in our method ReadSoccerResults,
when we load in our data,
0:45
we can add the result of the calculation
for conversion right there.
0:49
GameResult.ConversionRate equals
0:53
gameResult.Goals over
0:59
gameResult.GoalAttempts.
1:04
But since both the goal attempts and
goals properties are integers,
1:09
this calculation will return an int and
not a double like we want.
1:14
So in order to perform
floating point type division,
1:18
we'll need to cast at least one of the
properties of the operation to a double.
1:21
When the compiler sees that the first
operand in the calculation is a double,
1:26
it will implicitly convert
the second operand to a double.
1:30
One common mistake that's easy to make is
to cast the entire operation, like this.
1:38
But this will perform the integer
division first, inside the parenthesis.
1:46
And we'd lose the decimal
portion of the number.
1:51
You can also cast both of the operands
like this, to be explicit.
1:57
But, there's a better way we can do this.
2:03
Instead of storing the value
after we calculate it,
2:05
we can put our calculation into the getter
of our conversion rate property.
2:08
We can copy this calculation, And
go over to our gameResult class.
2:13
We'll remove the setter, And
put the calculation in here.
2:23
But here, I see a typo.
2:30
And double.
2:41
And we need our return.
2:45
Okay.
2:50
And we need another curly brace,
there we go.
2:53
Back in our program class we can
get rid of this calculation.
2:59
This way, the value will only be
calculated when we try to access it.
3:09
This uses less memory.
3:13
And since the property
doesn't have a setter,
3:15
it can't be accidentally
overwritten with the wrong value.
3:17
It's also better to do this because if the
value for either goals or shots changes,
3:21
then the conversion rate would be using
the old values and would be incorrect.
3:25
You'd have to make sure to update
the conversion rate whenever you update
3:30
the goals or shots values.
3:33
It's a good practice to use a calculation
in your properties when it's possible.
3:35
Let's debug and take a look.
3:40
F5.
3:41
And there's our conversion rates.
3:51
Great job,
3:56
we successfully wrote a program to parse
the data from our CSV file into objects.
3:56
We used the DateTime type and
learned a little more about structs.
4:02
We also took a deep dive and
4:06
learned about the different integral types
like byte, sbyte, int, long, and short.
4:08
Then we dove into floating point types.
4:14
And how usually you want to use the double
type unless there are other circumstances,
4:16
like super high precision or
you really need to conserve memory usage.
4:21
All of the types we covered as
we parsed were value types.
4:26
We learned that the difference
between a value type and
4:30
a reference type is how
they're stored in memory.
4:32
We can also pass a value by
reference with the out keyword
4:35
like we used with all
the TryParse methods.
4:39
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up