Contents

Kata 4

Setup instructions

  • Download the starting folder for this Kata.
  • Open a Command Prompt window and navigate to the starting folder using cd.
  • Open the starting folder for this Kata in VSCode.

Overview

In this Kata, you’ll look at how to declare variables, and manipulate them with operators.

The VSCode integrated terminal

VSCode has a built-in Command Prompt window called the Integrated Terminal. It’s accessible by going to the toolbar and selecting Terminal -> New Terminal. This will bring up a pane at the bottom of the VSCode window where you can input Command Prompt instructions. You might want to use this instead of opening up a new Command Prompt window each time!

image

Kata Instructions

Open Program.cs. It should look like this:

using System;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello " + args[0]);
            Console.WriteLine("Please tell me how old you are!");
            string age = Console.ReadLine();
            Console.WriteLine(args[0] + ", you are " + age + " years old!");
        }
    }
}

Remember that args contains the arguments the program received from the command line. In this case, the program expects the user’s name to be the first argument.

Store the value of args[0] - the user’s name - in a variable. To do this, at the top of the Main block write:

string name = args[0];

It’s no longer necessary to use args[0] directly in the code. Instead, we can use name.

Replace remaining occurrences of args[0] with name.

Run it to make sure it works. Remember to provide the name as an argument, for instance:

dotnet run "Mikiel Agutu"

Store the user’s age

We need to use an appropriate type for the variable that stores the user’s age. It makes sense to use an int (integer, which is a whole number) for this.

There’s a bit of code that reads the user’s age into a string. It looks like this:

string age = Console.ReadLine();

Replace the string type with an int, like so:

int age = Console.ReadLine();

Try running it with dotnet run, providing your name as an argument. When you get asked for your age, input any number you like.

You should get an error like this:

Cannot implicitly convert type 'string' to 'int'

Converting between types

You get an error when you do this because Console.ReadLine() reads everything from the Standard Input as a string. Therefore we need to convert the data we get from the Standard Input from string to int. The simplest way to do this is to use an operation built in to C# called Convert.ToInt32.

Update the variable declaration to look like this:

int age = Convert.ToInt32(Console.ReadLine());

Convert.ToInt32 turns the value inside the brackets into a type of int. In this case it’s the value from Console.ReadLine().

Try it again with dotnet run, providing your name as an argument.

Quick maths

Let’s help the user determine how old they’ll be after a given number of months. Prompt the user to give the number of months to add to their age in years. After the line that creates (declares) the age variable, write:

Console.WriteLine("How many months to add to your age?");
int monthsToAdd = Convert.ToInt32(Console.ReadLine());

This uses Console.ReadLine to read a number of months from the command line. We use Convert.ToInt32 to convert that number from a string to an int.

Now we need to determine how many years the given number of months equates to. There are 12 months in a year, so the formula is:

Years = Months / 12

But what if you have, say, 6 months? Then Months / 12 would be 0.5. This means we can’t use int because integers are whole numbers.

A type that does support fractional numbers is called double, so we’ll use that.

After the declaration of monthsToAdd, write the following:

double yearsInMonths = monthsToAdd / 12;
double ageWithMonthsAdded = age + yearsInMonths;

Now add a new line to output the new information. Write:

Console.WriteLine("In " + monthsToAdd + " months you will be " + ageWithMonthsAdded + " years old!");`

Notice how this line is using the + operator on strings, doubles, and ints. How does this work? Consider a small part of the above code:

"In " + monthsToAdd 

"In " is of type string because it’s in double quotes. monthsToAdd however, is of type int. In C#, when you add an int to a string using the + operator, the resulting value will be a string. The same thing happens with double. This is very convenient as it allows us to construct sentences like the one above!

Test it out with dotnet run and see if it works! Remember to provide an argument for the users name!

Finished!

You’ve now seen how to use variables and operators in C#.

Try on your own…

Q1

You may have noticed the program doesn’t properly output the users projected age if the months given don’t match a whole year exactly.

For instance, if you input 6 months you get something like:

In 6 months you will be 25 years old!

When really it should be something like:

In 6 months you will be 25.5 years old!.

This is because monthsToAdd is of type int. When the / operator is applied, ints always produce a whole number. So on the line:

double yearsInMonths = monthsToAdd / 12;

monthsToAdd / 12 will be rounded down.

This can be fixed my amending monthsToAdd to be of a more appropriate type - one that can cope with fractional numbers. Which type do you think will work best?

Q2

Alter the program to output the users age in years, months, and days. For instance, if the user was 12 years old it might say: 12 years, or 144 months, or 4380 days old. You don’t have to worry about leap years!

Copyright Mikiel Agutu 2019