Methods Practice
Complete the following exercises in a main.cs file.
Reference
Remember the syntax for defining and calling methods:
Defining
public static <<returntype>> <<methodname>>(<<parameters>>)
{
}
Example
public static string MyMethodName(string myInput)
{
}
Calling
<<methodname>>(<<arguments>>);
Example
string myString = "StringVal";
MyMethodName(myString);
Complete the following exercises to practice using methods!
Print Hat
- Define a new method named
PrintHatin the main.cs file- The method should take in no parameters
- The method should return
void(nothing)
- In the body of the
PrintHatmethod, use 6Console.WriteLinestatements to print the following ASCII hat:.--------. / `. | .-. . | | `.| |_________| <|> (___________) - In the body of the
Mainmethod, call thePrintHatmethod a few times to see multiple hats appear!
Inches to Centimeters
- Define a new method named
InToCmin the main.cs file- The method should take in no parameters
- The method should return a
double(the value in centimeters)
- In the body of the
InToCmmethod, ask the user for a measurement in inches usingConsole.ReadLine- Convert the number to a
doubletype usingConvert.ToDouble - Store the double in a new variable named
inches
- Convert the number to a
- Define a new
doublevariable namedcentimeters, and set its value to the value ofinchesmultiplied by2.54 - Return the new
centimetersvariable at the end of the method - In the body of the
Mainmethod, call theInToCmmethod - Store the returned value in a new variable named
centimeters- Note that this
centimetersvariable and thecentimetersvariable from theInToCmmethod are different
- Note that this
- Display a message to the user saying the value in centimeters
- Ex: If the user entered 2, display "5.08 cm"
- Ex: If the user entered 2.1, display "5.334 cm"
Hi Name
- Define a new method named
HiNamein the main.cs file- The method should take in no parameters
- The method should return a
string(the message)
- In the body of the
HiNamemethod, ask the user to input their name, and store their input in a variable - Return a greeting of "Hi, {name}"
- Ex: If the user enters "Alice", return "Hi, Alice"
- Ex: If the user enters "Alex", return "Hi, Alex"
- Call the
HiNamemethod from within theMainMethod, and store the returned message in a variable - Display the returned message to the user
Password Check
- Define a new method named
PasswordCheckin the main.cs file- The method should take in a
string(the password to check) as a parameter - The method should return
void(nothing)
- The method should take in a
- In the body of the
PasswordCheckmethod, use anifstatement to check if the password is equal to your password - If the password is correct, print a message saying "Correct Password"
- Otherwise, if the password is incorrect, print a message saying "Incorrect Password"
- In the
Mainmethod, ask the user to enter their password, and store their input in a variable - Call the
PasswordCheckmethod on the user's input to see if it is correct or incorrect!
(CHALLENGE) Coupon Code
The goal of this method is to calculate the final price of a service depending on a coupon code.
- Define a new method named
CouponCodein the main.cs file- The method should take in a
double(the initial price) and astring(the coupon code) as parameters - The method should return a
double(the final price)
- The method should take in a
- In the body of the
CouponCodemethod, check if the value of the coupon code is "PUNCH"- If it is, return the initial price with a 15% discount applied
- Check if the value of the coupon code is "HANDBOOK"
- If it is, return the initial price with a 25% discount applied
- Check if the value of the coupon code is "FREE"
- If it is, return the initial price with a 100% discount applied
- In body of the
Mainmethod, ask the user to input their initial price- Convert the
stringvalue to adoubleand store it in a variable
- Convert the
- In
Main, under the initial price, ask the user to input their coupon code- Store it in a variable
- Call the
CouponCodemethod, passing in the initial price and coupon code- Store the returned value in a new
doublevariable
- Store the returned value in a new
- Display a message to the user with the final price after the coupon
Extra Challenge
In the body of the CouponCode method, instead of using if statements, use a switch!
Reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch