PCP − Lecture 06

Fall 2020

Clément Aubert

September 12, 2020

Last Time - Operations, conversions and reading from a user

Unified Modeling Language

UML is a specification language with multiple benefits:

A class is represented as follows:

ClassName
- attribute: int
+ SetAttribute(attributeParameter: int): void
+ GetAttribute(): int

Note that void is optionnal. For our Rectangle class, this gives:

Rectangle
- width: int
- length: int
+ SetLength(lengthParameter : int): void
+ GetLength(): int
+ Setwidth(widthParameter: int): void
+ GetWidth(): int
+ ComputeArea(): int

More Methods for the Rectangle Class

Last time, in lab, you were asked to write additional methods. They look like this:

    public int ComputePerimeter()
    {
        return length*2 + width*2;
    }

    public void DoubleRectangle()
    {
        width *= 2;
        length *= 2;
    }

    public void Swap()
    {
        int temp;
        temp = length;
        length = width;
        width = temp;
    }

We could also write a method that multiply the length and width of a rectangle by a particular factor given in argument:

    public void MultiplyRectangle(int factor)
    {
        width *= factor;
        length *= factor;
    }

Note that this method is more general than DoubleRectangle, which can be “emulated” using MultiplyRectangle(2).

Variables and Methods Name and Conventions

Variable Scope

A variable exists at a particular time and place in a program, that defines its scope.

Time

You cannot use a variable before declaring it! The following would return an error:

a = 3;
int a;

Space:

  • One project can not access the variable of another project!
  • Rectangle.cs’s variables are not directly accessible in Program.cs (you have to use accessors).
  • The variable in a method are not accessible from the other methods.

Renaming

Identifiers can be uniformly renamed.

int a = 3;
a += 2;

is the same as

int myVar = 3;
myVar += 2;

We will use this example to discuss the scope:

class MyClass{
    private int attribute;
    public void SetAttribute(int attributeParameter){…}
}

MyClass, attribute, SetAttribute and attributeParameter can be changed, those are the identifiers in this class.

Conventions

We can change all the identifiers in the classes if we want: class names, method names, etc. But it’s good to have conventions.

“Hard” convention:

Variations / Conventions for instances variables and argument names:

Named Constant

A constant is a variable whose value cannot change.

const int MONTHS = 12;
const double AVOGADRO = 6.0220e23; // Avogadro Number. Units  1/mol 
const double PI = 3.14159265358979;
const double MILES_TO_KM = 1.60934;

For instance, π is defined in the Math class and can be accessed as follows:

Console.WriteLine(Math.PI);

Format Specifiers

We can use interpolation to display more nicely numerical values. There are four important format specifiers in C#.

Format specifier Description
N or n Formats the string with a thousands separator and a default of two decimal places.
E or e Formats the number using scientific notation with a default of six decimal places.
C or c Formats the string as currency. Displays an appropriate currency symbol ($ in the U.S.) next to the number. Separates digits with an appropriate separator character (comma in the U.S.) and sets the number of decimal places to two by default.
P or p Print percentage
        Console.WriteLine(
              "\n" + $"{1234.567:N}" // 1,234.57
            + "\n" + $"{1234.5:N}"   // 1,234.50
            + "\n" + $"{1234.567:E}" // 1.234567E+003
            + "\n" + $"{1234.567:C}" // $1,234.57
            + "\n" + $"{1234.5:C}"   // $1,234.50
            + "\n" + $"{.5:P}"       // 50.00%
    );