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
PrintHat
in the main.cs file- The method should take in no parameters
- The method should return
void
(nothing)
- In the body of the
PrintHat
method, use 6Console.WriteLine
statements to print the following ASCII hat:.--------. / `. | .-. . | | `.| |_________| <|> (___________)
- In the body of the
Main
method, call thePrintHat
method a few times to see multiple hats appear!
Inches to Centimeters
- Define a new method named
InToCm
in 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
InToCm
method, ask the user for a measurement in inches usingConsole.ReadLine
- Convert the number to a
double
type usingConvert.ToDouble
- Store the double in a new variable named
inches
- Convert the number to a
- Define a new
double
variable namedcentimeters
, and set its value to the value ofinches
multiplied by2.54
- Return the new
centimeters
variable at the end of the method - In the body of the
Main
method, call theInToCm
method - Store the returned value in a new variable named
centimeters
- Note that this
centimeters
variable and thecentimeters
variable from theInToCm
method 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
HiName
in the main.cs file- The method should take in no parameters
- The method should return a
string
(the message)
- In the body of the
HiName
method, 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
HiName
method 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
PasswordCheck
in 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
PasswordCheck
method, use anif
statement 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
Main
method, ask the user to enter their password, and store their input in a variable - Call the
PasswordCheck
method 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
CouponCode
in 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
CouponCode
method, 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
Main
method, ask the user to input their initial price- Convert the
string
value to adouble
and 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
CouponCode
method, passing in the initial price and coupon code- Store the returned value in a new
double
variable
- 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