Game Development Reference
In-Depth Information
A.2 Exercises and Challenges for Part
II
Exercises
1.
Keywords
(a) What does the word
void
mean, and when do we need this keyword?
(b) What does the word
int
mean, and when do we need this keyword?
(c) What does the word
return
mean in a C# instruction, and when do we need
it?
(d) What does the word
this
mean in a C# instruction, and when do we need it?
In what kind of method can we not use this word?
2.
Type conversions
Suppose that the following declarations have been made:
int
x;
string
s;
double
d;
Expand the following assignments with the necessary type conversions (assum-
ing that the string indeed represents a number):
x=d;
x=s;
s=x;
s=d;
d=x;
d=s;
3.
Methods with a result
(a) Write a method
RemainderAfterDivision
with two parameters
x
and
y
, which
returns the value of
x%y
,
without
using the
%
operator.
(b) Write a method
Circumference
that gives as a result the circumference of a
rectangle, whose width and height are given as parameters.
(c) Write a method
Diagonal
that gives as a result the length of the diagonal of a
rectangle, whose width and height are given as parameters.
(d) Write a method
ThreeTimes
, that returns three concatenated copies of a string
passed as a parameter. So,
ThreeTimes("hurray!")
should result in the string
"hurray!hurray!hurray!"
.
(e) Write a method
SixtyTimes
. that returns sixty concatenated copies of a string
passed as a parameter. Try to limit the number of instructions in that method.
(f) Write a method
ManyTimes
. that returns a number of concatenated copies of a
string passed as a parameter, where that number is also passed as a parameter
(you may assume that this number is 0 or larger). So,
ManyTimes("what?", 4)
should result in
"what?what?what?what?"
.