Topic A - Starting Classes

Overview

This topic introduces some of the basic syntax and grammar surrounding Object-Oriented Programming in C#. The following C# keywords are introduced.

  • public
  • class
  • static
  • string
  • void
  • return
  • namespace

This topic will also introduce the following grammars, syntax and language constructs:

  • Simple class declaration syntax (no members)
  • Simple class declaration syntax (with static methods)
  • Method overloading
  • Method declaration syntax (class methods, with the static keyword)
  • Parameter declaration (variable declaration) syntax
  • Class (static) method call syntax (with and without arguments)
  • main function (entry point of program)
  • Console output
  • Simple string concatenation
  • C# as a case-sensitive language
  • Single-line, multi-line and XML comments

LOGs

At the end of this topic, you should be able to …

OOP Basics

  • Define the term “class” as used in OOP
  • Explain the purpose of classes in computer programs
  • Describe the primary keywords used in declaring a class
  • Define the term “method” and give an example
  • Create simple classes with methods only
  • Explain what is meant by “overloading”
  • Describe the syntax of a method declaration (aka, method implementation)
  • Explain how classes with identical names can be distinguished from each other

General Programming Concepts and Terms

  • Define the term “keyword” as it applies to programming languages
  • Define the term “identifier” as it applies to programming languages
  • Define the term “program statement” and identify how the computer recognizes a program statement
  • Define the term “syntax” as it applies to programming languages
  • Identify the entry point for every computer program
  • Perform simple output to the console using System.Console
  • Identify the basic structure of a simple C# program
  • Explain what is meant by a “driver”
  • Explain what is meant by a “case-sensitive” programming language
  • Explain what is meant by a “strongly-typed” programming language
  • Explain what “string concatenation” is and how it works
  • Define and distinguish the terms “argument” and “parameter”
  • Use single-line, multi-line and XML comments as a means of documenting code
  • List the four pieces of information to include in comments at the top of every source file

Intro to IDEs

  • Define the term “IDE” as it relates to software development tools
  • Define the terms “project” and “solution”
  • Identify the various parts of the IDE (Integrated Development Environment) used in this course
  • Create a new project in the IDE for this course
  • Create new source files in the IDE for this course
  • Add existing files to a project

Code Samples

The following examples are used to illustrate this topic.

  1. Nothingness - This class represents the absolute minimum code required to code a class. Even though it’s not a very “useful” class, it does provide and introduction to the ideas of classes, keywords and identifiers.
  2. HelloWorld - This adaptation of the classic “Hello World” program illustrates static methods. This example includes and introduces the concept of a “driver”.
  3. Comments - This class is not really “functional”, but is intended to illustrate the basic types of comments allowed in the programming language.

Nothingness

This class represents the absolute minimum code required to code a class. Even though it’s not a very “useful” class, it does provide and introduction to the ideas of classes, keywords and identifiers.

1 namespace Topic.A.Examples
2 {
3     public class Nothingness
4     {
5 
6     }
7 }

HelloWorld

This adaptation of the classic “Hello World” program illustrates static methods. This example includes and introduces the concept of a “driver”.

 1 namespace Topic.A.Examples
 2 {
 3     public class Salutation // Define a class called "Salutation"
 4     {
 5         public static string Greeting()
 6         {
 7             return "Hello World!";
 8         } // end of Greeting()
 9 
10         public static string Greeting(string name)
11         {
12             return "Hello " + name;
13         } // end of Greeting(string)
14 
15         public static string Farewell()
16         {
17             return "So long!";
18         } // end of Farewell()
19     } // end of Salutation class
20 }
 1 namespace Topic.A.Examples
 2 {
 3     public class HelloWorld_Driver
 4     {
 5         public static void Main(string[] args)
 6         {
 7             System.Console.WriteLine(Salutation.Greeting());
 8             System.Console.WriteLine(Salutation.Greeting("Bob"));
 9             System.Console.WriteLine(); // print a blank line
10             System.Console.WriteLine(Salutation.Farewell());
11         }
12     }
13 }

Comments

This class is not really “functional”, but is intended to illustrate the basic types of comments allowed in the programming language.

 1 /*
 2  * File:    Comments.cs
 3  * Author:  Dan Gilleland
 4  * Date:    2010
 5  * Purpose: To illustrate multi-line, single-line, and XML comments.
 6  *          This is a multi-line comment.
 7  */
 8 namespace Topic.A.Examples
 9 {
10     /// <summary>
11     /// Comments illustrates multi-line, single-line, and XML comments.
12     /// </summary>
13     /// <remarks>
14     /// This class is a stand-alone class used to illustrate comments.
15     /// This particular XML comment is "attached" to the class as
16     /// a whole, while other XML comments are for fields or methods
17     /// in the class (a few methods and fields have been included for
18     /// illustration purposes).
19     /// [Author: Dan Gilleland]
20     /// </remarks>
21     public class Comments
22     {
23         /// <summary>
24         /// This is a method of <see cref="Comments"/>
25         /// </summary>
26         /// <returns>This method returns a string.</returns>
27         public static string SimpleMethod()
28         {
29             return "Hello World!"; // "Hello World!" is a literal string value
30         } // end of SimpleMethod()
31 
32         /// <summary>
33         /// This is a one-sentence description of the method.
34         /// </summary>
35         /// <param name="name">This is where I describe the purpose of this para\
36 meter</param>
37         /// <returns>Here I describe what information is returned from this meth\
38 od.</returns>
39         /// <remarks>
40         /// This method has a single parameter.
41         /// </remarks>
42         public static int MethodWithParameter(string name)
43         {
44             return name.Length; // This is a single-line comment (must be end of\
45  physical line)
46         } // end of MethodWithParameter(string)
47     } // end of Comments class
48 }

Practice Exercises

  • OuterSpace – This class is the simplest possible class (and, coincidentally, one of the most useless).
  • AnsweringMachine – The AnsweringMachine class provides a method to give an answer to an incoming phone call.
    Driver – This class is the driver for the AnsweringMachine class.
  • Salutation – This is the Salutation class that was previously demonstrated.
    HelloWorld_Driver – This class is the driver for the Salutation class.

OuterSpace

This class is the simplest possible class (and, coincidentally, one of the most useless).

Problem Statement:

Create a class called OuterSpace. This class has no members at all (it is empty, like outer space). Add a multi-line comment at the beginning that should include your full name as the author of the class. Also make an XML comment for the class as a whole.

AnsweringMachine

The AnsweringMachine class provides a method to give an answer to an incoming phone call.

Problem Statement:

Create the AnsweringMachine class so that it can provide a default answer for an incoming phone call as well as a customizable answer. The methods should be named answer and they should return a String. There should be two methods in total, and both of them should be declared as static. The actual content of the default message can be whatever you choose, while the customizable method will receive a single String argument that has the name of the person receiving the message. Also create a driver that demonstrates the AnsweringMachine in a Console environment.

Use the following details to guide your solution.

Method Detail

  • public static string Answer()
    Provides a standard default message for the answering machine.

    Returns:
    A String that instructs the caller to leave a message.

  • public static string Answer(string name)
    Provides a customizable message for the answering machine.

    To use this method, supply a person’s name, and this name will be incorporated into the message that is returned. For example,
    System.Console.WriteLine(AnsweringMachine.Answer("Bob");

    Parameters:
    name - A String that is the name of the person being called.
    Returns:
    A String that instructs the caller to leave a message for the person with the supplied name.

Salutation and HelloWorld_Driver

In each of the single-line comments above a line of code, enter a phrase or sentence in English that describes what is done in that line of code.

Salutation

 1 // Instructions: Enter comments in each blank to describe the following code
 2 // _____________________________________________________
 3 namespace Topic.A.Exercises
 4 {
 5 
 6     // _____________________________________________________________________
 7     public class Salutation
 8     {
 9         // _________________________________________________________________
10         public static string Greeting()
11         {
12             // _____________________________________________________________
13             return "Hello World!";
14         } // end of Greeting()
15 
16         // _________________________________________________________________
17         public static string Greeting(string name)
18         {
19             // _____________________________________________________________
20             return "Hello " + name;
21         } // end of Greeting(string)
22 
23         // _________________________________________________________________
24         public static string Farewell()
25         {
26             // _____________________________________________________________
27             return "So long!";
28         } // end of Farewell()
29     } // end of Salutation class
30 }

HelloWorld_Driver

 1 // Instructions: Enter comments in each blank to describe the following code
 2 // _____________________________________________________
 3 namespace Topic.A.Exercises
 4 {
 5     // _____________________________________________________
 6     public class HelloWorld_Driver
 7     {
 8         // __________________________________________________________________
 9         public static void main(string[] args)
10         {
11             // ______________________________________________________________
12             System.Console.WriteLine(Salutation.Greeting());
13             // ______________________________________________________________
14             System.Console.WriteLine(Salutation.Greeting("Bob"));
15             // ______________________________________________________________
16             System.Console.WriteLine(); // print a blank line
17             // ______________________________________________________________
18             System.Console.WriteLine(Salutation.Farewell());
19         }
20     }
21 }