Method Parameters Exercises
After completing the calculator, work on these exercises to practice using method parameters and returns.
Setup
- Create a new Repl project
- Name the project "Parameters Practice"
- Print a welcome message saying "Parameters Practice"
Now the main.cs file should be ready to go. For each exercise, the goal will be to define a method in the class body (outside of the Main
method body, inside of the MainClass
body), and then call the method from within the body of the Main
method. It may seem somewhat tedious, but it should help with retention.
Code solutions are available for several of the exercises, but try to complete them before checking.
1. Emphasis
For this exercise, create a method that puts an emphasis on some text by surrounding the text with greater-than and less-than signs. For example, if the text were "This is Important"
, the result would be ">This is Important<"
.
Defining the Method
Start by finding the proper place in the code to define the method. It should be under the }
from the Main
method body, above the }
from the class block. Once there, add code to define the method.
Method Information
- Name:
emphasize
- Return type:
string
- Parameters:
string text
Steps
- Start with the
public
keyword - Add the
static
keyword - Next, add the return type
- After that, the method name, followed by parentheses
- Between the parentheses, add any parameters
- Make a new line
- Add curly brackets, and press Enter between them to make some space
- In the body of the method (between the curly brackets), add a
return
- Return the following things added together:
">"
text
"<"
public static string emphasize(string text)
{
return ">" + text + "<";
}
Calling the Method
Now the method has been defined, but it is not actually doing anything yet. It's time to call the method. Start by finding the body of the Main
method (between its curly brackets), and making some space. Once there, add some code to call the method and handle the result.
- Create a new
string
variable namednewText
- Set
newText
to equal a call to theemphasize
method - Pass in
"This is Important"
as the argument for theemphasize
method - Under that, add a
Console.WriteLine
statement - Make the
WriteLine
print out thenewText
variable
Run the code, and verify that the properly emphasized text appears in the console!
string newText = emphasize("This is Important");
Console.WriteLine(newText);
2. Millionaire Check
For this exercise, create a method that checks if someone has over a million dollars in the bank. For example, if the amount were 9987754.00
, the result would be "You are a millionaire!"
. If the amount were 890283.00
, the result would be "You are poor :("
.
Defining the Method
Start by finding the proper place in the code to define the method. It should be under the }
from the previous method body, above the }
from the class block. Once there, add code to define the method.
Method Information
- Name:
millionCheck
- Return type:
string
- Parameters:
double amount
Steps
- Start with the
public
keyword - Add the
static
keyword - Next, add the return type
- After that, the method name, followed by parentheses
- Between the parentheses, add any parameters
- Make a new line
- Add curly brackets, and press Enter between them to make some space
- In the body of the method (between the curly brackets), create an
if
/else
structure - For the
if
condition, check ifamount
is greater than or equal to1000000.00
- In the body of the
if
,return
a message saying"You are a millionaire!"
- In the body of the
else
,return
a message saying"You are poor :("
public static string millionCheck(double amount)
{
if (amount >= 1000000.00)
{
return "You are a millionaire!";
}
else
{
return "You are poor :(";
}
}
Calling the Method
Now the method has been defined, but it is not actually doing anything yet. It's time to call the method. Start by finding the body of the Main
method (between its curly brackets), and making some space. Once there, add some code to call the method and handle the result.
- Create a new
string
variable namedmillionaire
- Set
millionaire
to equal a call to themillionaireCheck
method - Pass in
10.00
as the argument for themillionaireCheck
method - Under that, add a
Console.WriteLine
statement - Make the
WriteLine
print out themillionaire
variable
Run the code, and verify that the proper message appears in the console!
string millionaire = millionCheck(10.00);
Console.WriteLine(millionaire);
3. Bitcoin to Dollars
Note: This exercise provides a little less guidance, but it should be very similar to the previous exercises
For this exercise, create a method that converts Bitcoin to Dollars. For the purposes of this activity, use a conversion rate of 44000
. For example, if the amount were 1.1
bitcoin, the result would be 48400
dollars.
Defining the Method
Start by finding the proper place in the code to define the method. It should be under the }
from the previous method body, above the }
from the class block. Once there, add code to define the method.
- Name:
btcToDollars
- Return type:
double
- Parameters:
double btcAmount
- Return: the
btcAmount
parameter multiplied by44000
public static double btcToDollars(double btcAmount)
{
return btcAmount * 44000;
}
Calling the Method
Now the method has been defined, but it is not actually doing anything yet. It's time to call the method. Start by finding the body of the Main
method (between its curly brackets), and making some space. Once there, add some code to call the method and handle the result.
- Variable name to store the result:
dollars
- Argument to pass:
0.0001
- Print:
"You have $"
+ the result
Run the code, and verify that the proper message appears in the console!
double dollars = btcToDollars(0.0001);
Console.WriteLine("You have $" + dollars);
4. Kelvin to Celsius
Note: This exercise provides a little less guidance, but it should be very similar to the previous exercises
For this exercise, create a method that converts a temperature in Kelvin to Celsius. For the purposes of this activity, Celsius can be Kelvin minus 273
. For example, if the temperature were 290
in Kelvin, the result would be 17
in Celsius.
Defining the Method
Start by finding the proper place in the code to define the method. It should be under the }
from the previous method body, above the }
from the class block. Once there, add code to define the method.
- Name:
kelvinToCelsius
- Return type:
int
- Parameters:
int kTemp
- Return: the
kTemp
parameter minus273
public static int kelvinToCelsius(int kTemp)
{
return kTemp - 273;
}
Calling the Method
Now the method has been defined, but it is not actually doing anything yet. It's time to call the method. Start by finding the body of the Main
method (between its curly brackets), and making some space. Once there, add some code to call the method and handle the result.
- Variable name to store the result:
cTemp
- Argument to pass:
295
- Print:
"The temperature is "
+ the result
Run the code, and verify that the proper message appears in the console!
int cTemp = kelvinToCelsius(295);
Console.WriteLine("The temperature is " + cTemp);
5. Inches to Feet
Note: This exercise does not have a code solution
For this exercise, create a method that converts a height in inches to feet. For example, if the height were 61
in inches, the result would be 5.08
in feet.
Defining the Method
Start by finding the proper place in the code to define the method. It should be under the }
from the previous method body, above the }
from the class block. Once there, add code to define the method.
- Name:
inchesToFeet
- Return type:
double
- Parameters:
int inches
- Return: the
inches
parameter divided by12.0
Calling the Method
Now the method has been defined, but it is not actually doing anything yet. It's time to call the method. Start by finding the body of the Main
method (between its curly brackets), and making some space. Once there, add some code to call the method and handle the result.
- Variable name to store the result:
height
- Argument to pass:
63
- Print:
"You are "
+ the result +" feet tall"
Run the code, and verify that the proper message appears in the console!
6. Exclaim
Note: This exercise does not have a code solution
For this exercise, create a method that adds a bunch of exclamation points to a message. For example, if the message were "Hello"
, the result would be "Hello!!!!!"
.
Defining the Method
Start by finding the proper place in the code to define the method. It should be under the }
from the previous method body, above the }
from the class block. Once there, add code to define the method.
- Name:
exclaim
- Return type:
string
- Parameters:
string message
- Return: the
message
parameter plus"!!!!!"
Calling the Method
Now the method has been defined, but it is not actually doing anything yet. It's time to call the method. Start by finding the body of the Main
method (between its curly brackets), and making some space. Once there, add some code to call the method and handle the result.
- Variable name to store the result:
yelling
- Argument to pass:
hey
- Print: the
yelling
variable
Run the code, and verify that the proper message appears in the console!
Challenges
After completing these exercises, try to work on the challenges.