Practical 1: Expressions

CAB201 - Programming Principles

School of Computer Science, Faculty of Science

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

About me

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Agenda

  1. Setting up the development environment
    1. Visual Studio
    2. Visual Studio Code
  2. Recap: Introduction to C#
  3. Practical activities
    1. Hello World
    2. Miles to Kilometers
    3. Area of Room
    4. Locate Symbol
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Setting up the development environment - Visual Studio

Download and install Visual Studio Community Edition: https://visualstudio.microsoft.com/vs/community/

In the installer, install .NET 7.0 SDK.

To create a new project, open Visual Studio and select Create a new project.

  • Select Console App (.NET) and click Next.
  • For the Framework, select .NET 6.0 (or newer), check the Do not use top-level statements box, and click Create.
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Setting up the development environment - Visual Studio Code

Download and install Visual Studio Code: https://code.visualstudio.com/

Install the C# extension by Microsoft: https://code.visualstudio.com/docs/languages/csharp

Needs the command line:

  • To create a new project, use dotnet new console -n <project_name> --use-program-main. This will create the project in a new folder, and --use-program-main will create a Program.cs file with a Main method.
  • To run the project, use dotnet run in the project folder.
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Recap: Introduction to C# - Variables

C# is a modern, general-purpose, object-oriented programming language. It is strongly, and mostly statically typed, so variables must be declared with a type:

int x = 5;
string name = "John";

You cannot change the type of a variable after it has been declared:

int x = 5;
x = "John"; // Causes error
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Recap: Introduction to C# - Main

All code in C# is written inside a class, contained in a namespace. The Main method is the entry point of the program:

using System; // Import System to use Console
namespace HelloWorld
{
    class Program // Class declaration
    {
        static void Main(string[] args) // Entry point
        {
            Console.WriteLine("Hello World!"); // Method
        }
    }
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Recap: Introduction to C# - Console Methods

A console application is a program that interacts with the user through a command-line interface. It is a simple way to learn the basics of programming.

The Console class provides methods to read and write to the console:

Console.WriteLine("Hello World!"); // Write a line
string name = Console.ReadLine(); // Read a line from the console and store it in a variable

Convert a string to other types using Parse methods:

int x = int.Parse("5");
double y = double.Parse("3.14");
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Practical Activities - Gradescope

Gradescope is used for all assignments and practical activities in this unit. Programming submitted to Gradescope will be automatically tested for correctness, and the results will be available immediately.

Accessed through the CAB201 Canvas, on the left-hand side.

Refer to the Submitting to Gradescope guide for instructions.

Solutions to the practical activities will be published on Canvas at the end of the week.

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Practical Activities - Hello World

Click here for hints
  • Use the Console.WriteLine method to print the message, like:
Console.WriteLine("Hello World!");

Replace the Hello World message with Hello there, welcome to CAB201..

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Practical Activities - Miles to Kilometers

Click here for hints
  • Use Console.ReadLine to read a line as a string, and use double.Parse to convert it to a double.
  • 1 mile is 1.609344 kilometers.
  • Use the Numberic format strings to format the output.
  • String interpolation Console.WriteLine($"Message {variable:format}") can be useful.
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Practical Activities - Area of Room

Click here for hints
  • Use Console.ReadLine to read a line as a string, and use double.Parse to convert it to a double.
  • .
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Practical Activities - Locate Symbol

Click here for hints
  • Use Console.ReadLine to read a line as a string, and use char.Parse to convert it to a char.
  • Use the char type to store a single character.
  • May need the String methods that has something to do with an index.
CAB201 - Programming Principles
School of Computer Science, Faculty of Science