Methods Exercises - Creating a Zoo

After completing the code-along activity, complete the exercises to practice using methods. Use the same Repl project from the code-along. The goal of these exercises is to add several other animals to the program.

Starter Code

To begin, the code in the main.cs file should look something like this:

using System;

public class Program
{
    public static void Main()
    {
        drawBunny();
        drawBunny();
        drawBunny();
    }

    public static void drawBunny()
    {
        Console.WriteLine();
        Console.WriteLine(" () ()");
        Console.WriteLine(" (. .) ");
        Console.WriteLine(" (u u) ");
        Console.WriteLine();
    }
}

Here, there are two method definitions: Main and drawBunny. The drawBunny method is called within the body of the Main method. Note the placement of the curly brackets:

using System;

public class Program
{
    // All important stuff is in this class block
}
public static void Main()
{ // open
    drawBunny();
    drawBunny();
    drawBunny();
} // close

public static void drawBunny()
{ // open
    Console.WriteLine();
    Console.WriteLine(" () ()");
    Console.WriteLine(" (. .) ");
    Console.WriteLine(" (u u) ");
    Console.WriteLine();
} // close

Every additional method should be defined within the same class block, but outside of the other method blocks.

1. Drawing a Cat

Define and call a new method in the program that prints a cat to the console. The method should be named drawCat.

Defining the Method

The first step is defining the drawCat method.

  1. Find the place in the code under the closing } for the drawBunny method, above the closing } for the class block
  2. There, add the three keywords needed to define a basic method:
    • public static void
  3. After that, make a space and add the name of the method:
    • drawCat
  4. After that, add the opening and closing parentheses: ()
  5. Make a new line
  6. Add the opening and closing curly brackets: {}
  7. Press enter between them to open up the body of the method
  8. In the body (between the curly brackets), add code to draw a cat:
     Console.WriteLine();
     Console.WriteLine(" /\\_/\\");
     Console.WriteLine("( o.o )");
     Console.WriteLine(" > ^ <");
     Console.WriteLine();
    

Now the method should be properly defined! But the program won't do anything new yet...

Calling the Method

To actually use a method after it has been defined, it must be called in the code.

  1. Find the body of the Main method (between the curly brackets)
  2. At the bottom, make a new line (above the closing })
  3. Type in the name of the method: drawCat
  4. After that, type in opening and closing parentheses: ()
  5. Add a semi-colon to end the statement
  6. Use the same code to call the method two more times

Run the program, and three cats should appear!

2. Drawing a Dog

Define and call a new method in the program that prints a dog to the console. The method should be named drawDog. This will be incredibly similar to the drawCat method - the only difference is the name of the method, and the text printed to the console.

Defining the Method

The first step is defining the drawDog method.

  1. Find the place in the code under the closing } for the drawCat method, above the closing } for the class block
  2. There, add the three keywords needed to define a basic method
  3. After that, make a space and add the name of the method
  4. After that, add the opening and closing parentheses
  5. Make a new line
  6. Add the opening and closing curly brackets
  7. Press enter between them to open up the body of the method
  8. In the body, add code to draw a dog:
     Console.WriteLine();
     Console.WriteLine("  __      _");
     Console.WriteLine("o'')}____//");
     Console.WriteLine(" `_/      )");
     Console.WriteLine(" (_(_/-(_/ ");
     Console.WriteLine();
    

Now the method should be properly defined! But the program won't do anything new yet...

Calling the Method

To actually use a method after it has been defined, it must be called in the code.

  1. Find the body of the Main method
  2. At the bottom, make a new line
  3. Type in the name of the method
  4. After that, type in opening and closing parentheses
  5. Add a semi-colon to end the statement
  6. Use the same code to call the method two more times

Run the program, and three dogs should appear!

3. Drawing More Animals

Following the steps from the exercises above, define and call some new methods that draw new animals. Each student should have an opportunity to complete one of the parts below. The name of the method, along with the code for the body of the method, is provided for each animal below.

drawFish Method

Console.WriteLine();
Console.WriteLine("  _");
Console.WriteLine("><_>");
Console.WriteLine();

drawElephant Method

Console.WriteLine();
Console.WriteLine("     __");
Console.WriteLine(" .--()°'.'");
Console.WriteLine("'|, . ,'");
Console.WriteLine(" !_-(_\\");
Console.WriteLine();

drawBat Method

Console.WriteLine();
Console.WriteLine("   _   ,_,   _");
Console.WriteLine("  / `'=) (='` \\");
Console.WriteLine(" /.-.-.\\ /.-.-.\\"); 
Console.WriteLine(" `      '       `");
Console.WriteLine();

drawOwl Method

Console.WriteLine();
Console.WriteLine(" ,_, ");
Console.WriteLine("(O,O)");
Console.WriteLine("(   )");
Console.WriteLine("-m-m-");
Console.WriteLine();

drawDuck Method

Console.WriteLine();
Console.WriteLine("  __     ");
Console.WriteLine("<(o )___ ");
Console.WriteLine(" ( ._> / ");
Console.WriteLine("  `---'  ");
Console.WriteLine();

results matching ""

    No results matching ""