Methods Challenges
Complete the following challenges.
Optional Practice: Bugs
Visit each of the Repl projects below. They all have something wrong with them; try to fix the errors!
Many Methods
Create a new Repl project, and complete the challenges below. For each challenge:
- Define a new method
- Call the method from within the body of the
Main
method
String Length
Define a method that can display how long a message is.
Method Setup
First, define an empty method in the program.
- Make a new line in the code where a method can be defined
- On that line, type in the basic method definition keywords:
public static void
- On the same line, type in the method name:
StringLength
- On the same line, type in the open and close parentheses:
()
- After that, type in a left curly bracket (the right one should appear automatically):
{}
- Between the curly brackets, press
Enter
to create a new line
The method should look something like this:
public static void StringLength() {
// body of the method: between curly brackets
}
Run the code to make sure there are no errors.
Adding Code to the Method
Next, it's time to add some code to the method so it actually does something.
- In the body of the method (between the brackets), add a
Console.WriteLine
command - Make the text for the
WriteLine
command say "Enter a message" - Beneath the
WriteLine
, declare a newstring
variable namedmessage
- Set the
message
variable to aConsole.ReadLine()
command - Beneath that, add another
Console.WriteLine
command - For that
WriteLine
command, putmessage.Length
between the parentheses- This will display the length of the
message
string
- This will display the length of the
At this point, the method should look something like this:
public static void StringLength() {
Console.WriteLine("Enter a message");
string message = Console.ReadLine();
Console.WriteLine(message.Length);
}
Run the code to make sure there are no errors.
Calling the Method
Now, all that's left is to call the method. This is where the program will actually run the code within the method.
- Make a new line in the body of the
Main
method (between theMain
method brackets)
- On the new line, type in the name of the method:
StringLength
- On the same line, type in open and close parentheses:
()
- Finally, add a semi-colon at the end of the line:
;
At this point, the whole program should look something like this:
using System;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine ("Hello World");
StringLength();
}
public static void StringLength() {
Console.WriteLine("Enter a message");
string message = Console.ReadLine();
Console.WriteLine(message.Length);
}
}
Run the program, and make sure it works! It should ask for a message, accept an answer in the console, and display the length of the message.
Times Two
Use the same Repl project as the StringLength
challenge to complete this one. Create a method that asks the user for a number, and then shows them that number multiplied by two.
Method Setup
First, define an empty method in the program.
- Make a new line in the code where a method can be defined
- Under the closing curly bracket of the previous method, above the final curly bracket
- On that line, type in the basic method definition keywords:
public static void
- On the same line, type in the method name:
TimesTwo
- On the same line, type in the open and close parentheses:
()
- After that, type in a left curly bracket (the right one should appear automatically):
{}
- Between the curly brackets, press
Enter
to create a new line
Run the code to make sure there are no errors.
Adding Code to the Method
Next, it's time to add some code to the method so it actually does something.
- In the body of the method (between the brackets), add a
Console.WriteLine
command - Make the text for the
WriteLine
command say "Enter a number" - Beneath the
WriteLine
, declare a newint
variable namednumber
- Set the
number
variable to aConvert.ToInt32(Console.ReadLine())
command - Beneath that, add another
Console.WriteLine
command - For that
WriteLine
command, putnumber * 2
between the parentheses- This will display the
number
integer multiplied by 2
- This will display the
Run the code to make sure there are no errors.
Calling the Method
Now, all that's left is to call the method. This is where the program will actually run the code within the method.
- Make a new line in the body of the
Main
method (between theMain
method brackets) - On the new line, type in the name of the method:
TimesTwo
- On the same line, type in open and close parentheses:
()
- Finally, add a semi-colon at the end of the line:
;
Run the program, and make sure it works! It should ask for a number, accept an answer in the console, and display the value of the number multiplied by two.
Ghost
Use the same Repl project as the previous challenges to complete this one. Create a method that prints out a ghost.
Method Setup
First, define an empty method in the program.
- Make a new line in the proper place in the file
- Write out the basic code for an empty method, named
Ghost
- Run the code to make sure there are no errors.
Adding Code to the Method
Next, it's time to add some code to the method so it actually does something.
- In the body of the method (between the brackets), add a
Console.WriteLine
command - Paste in the ghost emoji between the double quotes:
👻
- Run the code to make sure there are no errors.
Calling the Method
Now, all that's left is to call the method. This is where the program will actually run the code within the method.
- Make a new line in the proper place within the
Main
method - Call the method by typing its name followed by parentheses
- Run the program, and make sure a ghost appears
(BONUS) Another Emoji
Repeat the steps above to create another emoji-printing method. This method should print out something other than a ghost! Make sure to follow these steps:
- Setup the method by defining it in the proper place
- Make sure to give it a good name!
- Add code to the method
- Use
Console.WriteLine
, and paste in your favorite emoji between the double quotes
- Use
- Call the method from within the
Main
method