Solutions for those exercises.
Questions
- What is the difference between
ref
andout
?
-
ref
variables are “read-only”, their value cannot change inside a method. -
ref
is a keyword,out
is not. - There isn’t any: they are both used to pass a reference to a method.
-
out
variables may not be initialized going into the method, but have to receive a value inside the method. - There isn’t any: they are both used to pass a value to a method.
Warm-up Exercises
-
Consider the following code:
-
What are the values of
x
,y
andz
- Before the
Foo
method is called? - Inside the
Foo
method? - After the
Foo
method executed?
- Before the
-
What is the value of
c
? -
What is the value of
d
?
-
Problems
-
Write the
AddRev
method (header included) such that the following:would display
-
Write the
AddLog
method (header included) such that the following:would display
-
Write the
AddReset
method (header included) such that the following:would display
-
Consider the “regular” implementation of the
Rectangle
class:And try to answer the following questions.
-
Write a
Draw
method that takes one optionalchar
parameter and draw a rectangle of the calling object’s width and length using that character if provided,*
otherwise. If your method is correctly implemented, thenshould display
-
Write a
Copy
method that does not take arguments, and return a copy of the calling object. If your method is correctly implemented, thenshould display
If the length of the original object changed after
copy.Length = 12;
was executed, then your method makes a shallow copy instead of a “deep” copy. -
Write an
Equals
method that returntrue
if the calling object and the argument are both non-null rectangles with the same length and width,false
otherwise. If your method is correctly implemented, thenshould display
-