Lesson # 16. C# Classes

Дата изменения: 27 мая 2020
Programming on c sharp in Microsoft Visual Studio. C# Classes

Содержание:

Theory

  • C# classes encapsulate data and associated functionality into an object;
  • Encapsulation encloses data and functionality into a single unit (called a class);
  • data and functionality are separated into two separate protections public and private:
  • Public members can be accessed by client code.
  • Private members cannot be accessed by client code (only used within the class itself)
  • The objects of a class have three things:
    First, objects have state. It’s the current characteristics of that object and those characteristics might and probably will change within the program. The second thing, all objects have is behavior. Finally, all objects have identity. If our system is a bunch of interacting objects, then we have to be able to tell them apart. And so, each object has their own identity. And it turns out that in practice, the identity of the object is the memory address of that object.

  • state of an object is stored in fields in C-sharp;
  • behavior in C-sharp is implemented in a method.
Class definition
  • private modifier – is the default modifier for members

  • Class definition sample:

    public class Person
    {
    	private string Name; // state or field
    	private int Age;
     
    	public Person(string Name, int Age) // constructor
    	{
               // to initialize class fields is the main role of a constructor
    		this.Name = Name; 
    		this.Age = Age;
    	}
    	public void Print() // behavior or method
    	{
    		Console.WriteLine($"{Name} {Age}");
    	}
    }
  • When an instance of a class is created, the class constructor sets up the initial state of the class.
  • The simplest constructor we can provide is a custom default constructor that specifies the state of the object when the object is constructed.
  • Working with class within the Main function:

    class Program
    {
    	static void Main(string[] args)
    	{
              // how to create an instance of the class?
              // 1 way:
    		Person p1 = null;
    		p1 = new Person("Name1", 20); // an instance of the class
              // 2 way:
    		Person p2 = new Person("Name2", 21);
              // 3 way:
    		var p3 = new Person("Name3", 19);
              // to print out:
    		p1.Print(); p2.Print(); p3.Print();
    	}
    }
  • Properties allow to restrict access to the fields:
How to copy and compare objects
  • Variable of a class type is a reference to an object
  • public class Person
    {
    	...
    	public Person(string Name, int Age)
    	{
    		this.Name = Name;
    		this.Age = Age;
    	}
    }
    Person p;
    p = new Person("Name",18);
     
    var p1 = p; // Assignment of references
     
    if (p1==p) ... // Comparison of references
     
    p = null; // Garbage collection
    void Print(Person p) {}

Labs and Tasks

Lab 1.

Perform tasks in the Main function with a comment on which item of the task is being performed.

To do:
1. Create the Cow class with two fields: name and weight.
2. Provide class constructor with two parameters, they are a non-empty name and a positive weight; and also the Print/Println method. Don’t forget to check whether the field values are correct.
3. Within the Main program initialize two instances of the Cow class object, call the Println method for them.
4. Add the read and write properties for all fields to the Cow class. Don’t forget to check the correct weight and name values (Check in the Main program).
5. Create two external procedures or methods (TestCowValue and TestCowVar) with a single parameter of Cow type. Within the first one the parameter by value must be provided, and within the second the parameter by reference must be provided.

  • Assign a new object to the parameter inside the procedures (any object, since the constructor must be called).
  • Inside the Main program, create an object of the Cow class and call the Println method for the created object.
  •   
    6. Create an array of objects of the Cow class with four elements inside it. Initialize the elements with values. Create a method to print the array out.

    The resulting example:

    Name: Lovely
    Weight: 56
    Name: Bunny
    Weight: 90
    cow3 before reassigning:
    Name: Lucy Weight: 500
    cow3 after reassigning a property by the value :
    Name: Lucy Weight: 500
    cow4 before reassigning:
    Name: Laserda Weight: 400
    cow4 after reassigning a property by the reference:
    Name: Stark Weight: 325
    The array:
    Name: Lassie Weight: 200 Name: Bern Weight: 325 Name: Emerald Weight: 333 Name: Sindy Weight: 256
    

    [Solution and Project name: Lesson_16Lab1, file name CowsClass.cs]


    ✍ How to do:

    • Create a new project with the name and file name as it is specified in the task.
    • Include all the namespaces and classes that we’ll need for our project. In order not to type always Diagnostics namespace, that is used for debugging process, and the Console class, we’ll include them in our project. Enter the code in the including section at the top of the screen:
    • using System.Diagnostics;
      using static System.Console;

      1. Create a Cow class with two fields: name and weight.

       class Cow
              {
                  private string name;
                  private double weight;
               ...
              } // end of class Cow

      2. Create a class constructor with two parameters, they are a non-empty name and a positive weight; and also the Print/Println method. Don’t forget to check whether the field values are correct.

    • The class constructor has to be with Public access modifier. Add the following code instead of ...:
    • public Cow(string name, double weight)  // constructor
          {
               ...
           }// end of constructor
    • To check whether the field name is empty, you need to use an ArgumentException class, to throw an exception error in the case if the name is empty. Place the code inside the constructor:
    •  if (name=="")
                throw new System.ArgumentException("The name can't be empty");
    • Check the value of the weight field in the same way:
    •  if (weight <= 0)
                throw new System.ArgumentOutOfRangeException("The weight must be > 0");
    • Class constructor has to specify the state of the object when the object is constructed. So you need to set up the initial state of the class fields. Add the following code into the constructor after if statements:
    •  this.name = name;
       this.weight = weight;
    • Create a print method to print out the fields values within a single line. Use a short syntax. Create printLn method to print them out within two lines. Use a usual syntax. Add the code to the class Cow :
    • public void Print() => Write($"Name: {name} Weight: {weight} ");
      public void Println()
                  {
                      WriteLine($"Name: {name}");
                      WriteLine($"Weight: {weight}");
                  }
      // don't type the curly brace, you already have it:
      }// end of class Cow

      3. Within the Main program initialize two instances of the Cow class object, call the Println method for them.

      var cow1 = new Cow("Lovely", 56);
      var cow2 = new Cow("Bunny", 87);
      cow1.Println();
      cow2.Println();
    • Run the application and check the output.
    • The new keyword will arrange for us that when the function constructor creates an empty object and then sets it to be a cow, it will go ahead and return that object and store it in whatever variable that we’re providing for it. We’ve provided cow1 and cow2, they are the instances of the cow object.
    • But if we try to print out the name of cow1, using WriteLine(cow1.name); statements, we’ll have an error («name is inaccessible due to its protection level»). So, we need to create public properties for the fields of our Cow class.
    • 4. Add the read and write fields to the Cow class. Don’t forget to check the correct weight and name values.

    • First, let’s create a read and write property for the name field. Use get keyword to return the field value and set keyword — to assign a new value to it:
    • public string Name
              {
                  get { return name; }
                  set
                  {
                      if (value.Length > 0)
                          name = value;
                      else
                          throw new System.ArgumentException("The name can't be empty");
                  }
              }// end of property Name
    • A similar code has to be created for the weight field:
    • public double Weight
                  {
                      get { return weight; }
                      set
                      {
                          if (weight > 0)
                              weight = value;
                          else
                              throw new System.ArgumentOutOfRangeException("The weight has to be > 0");
                      }
                  }// end of property Weight
    • Go back to the Main function and assign a new value for cow2 weight before printing the results. Run the application and check the result: the name of cow2 has to be set to the new one:
    • cow2.Weight = 90;
      cow2.Println(); // you already have this

      5. Create two external procedures or methods (TestCowValue and TestCowVar) with a single parameter of Cow type. Within the first one the parameter by value must be provided, and within the second the parameter by reference must be provided.

    • First, let’s create a method TestCowValue that takes one parameter by the value:
    •  static void TestCowValue(Cow myCow)
              {
                  myCow = new Cow("Star", 325);
              }
    • Within the Main function initialize a new instance of the Cow class and print out the values of its fields. Call the created method for it and print the fields again:
    •  // Task 5
       Cow cow3 = new Cow("Lucy", 500);
       WriteLine("Before reassigning:");
       cow3.Print();
       WriteLine();
       WriteLine("After reassigning a property by the value :");
       TestCowValue(cow3); //can't change the value
       cow3.Print();
    • Run the application and check the output.
    • The values of the fields were not changed, because the TestCowValue method didn’t return new values — it takes the parameter by value.
    • Let’s create the TestCowVar method that takes a parameter by reference:
    • static void TestCowVar(ref Cow myCow)// parameter by ref
              {
                  myCow = new Cow("Star", 325);
              }
    • Within the Main function initialize a new instance of the Cow class and print out the values of its fields. Call the created method for it and print the fields again:
    • WriteLine();
      WriteLine("cow4 before reassigning:");
      Cow cow4 = new Cow("Laserda", 400);
      cow4.Print();
      WriteLine();
      WriteLine("cow4 after reassigning a property by the reference:");
      TestCowVar(ref cow4);
      cow4.Print();
      ReadKey();

      6. Create an array of objects of the Cow class with four elements inside it. Initialize the elements with values. Create a method to print the array out.

    • Within the Main function initialize the single-dimentional array of the Cow class:
    • // Task 6
      Cow[] cows = new Cow[] {
      	new Cow("Lassie", 200),
      	new Cow("Bern", 325),
      	new Cow("Emerald", 333),
      	new Cow("Sindy", 256)
      };
    • Create a PrintCows method to print out the elements of the array:
    • public void PrintCows(Cow[] cows)
      	{
      		foreach (Cow cow in cows)
      		{
      			cow.Print();
      		}
      	} // end of the PrintCows method
    • Return to the Main function and call the method:
    • WriteLine("The array:");
      PrintCows(cows);
    • Run the application again and check the output.
    • Save and upload the file into the moodle system.

    Task 1:

    Perform tasks in the Main program file with a comment on which item of the task is being performed.

    To do:
    1. Create the TransportCompany class with three fields: for the company name, for the planned number of product containers sent, and for the actual number of product containers sent.
    2. Create auto read- and write- properties for all the fields.
    3. Define a constructor with the fields, and add the Print/Println methods for the class.
    4. Within the Main function initialize two instances of the class. Print out the information about them.
    5. Create a PlanIsDone method (returns true if the plan is done, that is the number of sent containers grater or equal to the number of planned contaiers). The method has one parameter — the instance of the created class object. Call the method for one of the companies within the Main function.
    6. Create and fill in a dynamic array of objects with data about several companies: the company name, the number of containers planned to be sent and the number of containers actually sent.
    7. Output all elements of the array (create a new method to do it).
    * extra task 8. Create a function that returns an array of companies’ names that completed the plan. Print the names using a another method.

    Note: Specify the meaningful names for the class, its fields, and methods / functions.

    The resulting example:

    Company name: BMW. Was sent: 2, Was planned: 3
    Company name: Lada. Was sent: 4, Was planned: 3
    The plan for the BMW company was done: False
    # 7:
    The companies:
    Company name: Mazda. Was sent: 25, Was planned: 20
    Company name: Opel. Was sent: 30, Was planned: 32
    Company name: Honda. Was sent: 33, Was planned: 33
    Company name: Nissan. Was sent: 20, Was planned: 25
    # 8:
    Successful companies:
    Company name: Mazda. Was sent: 25, Was planned: 20
    Company name: Honda. Was sent: 33, Was planned: 33
    

    [Solution and Project name: Lesson_16Task1, file name L16Task1.cs]


    Task 2:

    Perform tasks in the Main program file with a comment on which item of the task is being performed.

    To do:
    1. Create a Student class with four fields: name, course number, group number, and number of classes of absence.
    2. Define a constructor with the fields, and add the Print/Println methods for the class. Add auto read and write properties for all fields.
    3. Fill in an array of students.
    4. Create a method to print out the names of students from the array who belong to a particular course and group, and missed more than N classes.

    Note: Specify the meaningful names for the class, its fields, and methods / functions.

    The resulting example:

    The array of students: 
    Ivanov, course 2, group 23, was absent 2 classes
    Petrov, course 1, group 9, was absent 5 classes
    Sidorova, course 2, group 23, was absent 0 classes
    Ivanov, course 2, group 23, was absent 2 classes
    
    Enter the course number, group number and missed classes:
    1  9  3
    Petrov
    

    [Solution and Project name: Lesson_16Task2, file name L16Task2.cs]

    Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *

    *
    *


    Вставить формулу как
    Блок
    Строка
    Дополнительные настройки
    Цвет формулы
    Цвет текста
    #333333
    Используйте LaTeX для набора формулы
    Предпросмотр
    \({}\)
    Формула не набрана
    Вставить