11.6. Exercises: Methods¶
To become good at solving problems with code you need to be able to break large problems into small ones. Usually, these smaller problems will take the form of methods that are used to solve the larger problem. Therefore, to become good at solving problems you need to become good at writing methods. And to master methods, you need to write a lot of them.
These exercises ask you to write lots of methods, starting with small one and end with a larger, more complicated one.
At the end, you will be able to create strings of shapes, like this nifty diamond:
#
###
#####
#######
#########
#########
#######
#####
###
#
Note
As you build your methods, remember to keep the output in mind. Do you want a value returned or should it print directly to the console when invoked? How many things are being repeated?
11.6.1. Getting Started¶
Click here and fork the starter code.
Tip
As you create these methods, keep the shape of your output in mind.
Remember the differences between Console.WriteLine
and Console.Write
.
11.6.2. Rectangles¶
Write a method
MakeLine(size)
that prints a line with exactlysize
hashes.MakeLine(5);
Console Output
#####
Write a method called
MakeSquare(size)
that prints asize
bysize
iteration of hashes.MakeSquare(5);
Console Output
##### ##### ##### ##### #####
Asthetically it’s not a “perfect” square, but a single argument led to its creation.
Write a method
MakeRectangle(width, height)
that prints a rectangle with the given width and height.MakeRectangle(8, 3);
Console Output
######## ######## ########
11.6.3. Triangles¶
Write a method
MakeDownwardStairs(height)
that prints the staircase pattern shown below, with the given height.MakeDownwardStairs(5);
Console Output
# ## ### #### #####
Write a method
MakeSpaceLine(numSpaces, numChars)
that prints a line with exactly the specified number of spaces, followed by the specified number of hashes, followed again bynumSpaces
more spaces.MakeSpaceLine(3, 5);
Console Output
---#####---
Note
We have inserted dashes to represent spaces, so they are visible in the output. This can be helpful for building the pattern, but make sure to remove them from your final code.
Write a method
MakeIsoscelesTriangle(height)
that prints a triangle of the given height.MakeIsoscelesTriangle(5);
Console Output
# ### ##### ####### #########
11.6.4. Diamonds¶
Write a method
MakeDiamond(height)
that prints a diamond where the triangle formed by the top portion has the given height.MakeDiamond(5);
Console Output
# ### ##### ####### ######### ######### ####### ##### ### #
Tip
Consider what happens if you create a triangle and reverse it within the same method?
11.6.5. Bonus Mission¶
Refactor your methods so that they also take a single character as a parameter, and draw the shapes with that character instead of always using
#
. Some ideas*
or>
.Make the new parameter optional, with default value
#
.