Monday, January 28, 2013

6.1. Why objects?


So far we have looked for algorithms and we have tried to turn anything into a function to use in our programs. When we know which steps we must take, we look for the variables required for such steps.

Let's suppose we have to store data about a door in a simulation program. Do we create functions OpenDoor and CloseDoor? At least, we should also go to the area of ​​variable declarations, to keep other data such as size, color, etc.

Not bad, but it is "not natural": a door is a set: we cannot take apart its color from its size, or the fact that it opens or closes. It has both physical features (which so far we have called variables) and a behavior under different circumstances (which were the functions). All this goes together, forming an "object".

On the other hand, if we have to explain what a garage door is to someone who has not seen it before, but who knows how is the door of his house, we can say "it looks like a door of a house, but it is larger to accommodate cars, it is made of metal instead of wood ..." That is, we can describe some objects from what we know of others.

Finally, we should remember that "open" does not only refer to doors. We can also talk about opening a window or a book, for example.

With this, we have discussed about the three most important features of Object-Oriented Programming (OOP):

Ø  Encapsulation: We can not separate the behavior of the characteristics of an object. The behaviors are functions, called methods in OOP. The characteristics of an object will be variables, as the ones we have used before (we'll call them attributes). The appearance of an object in C#, as we will see a little later, is similar to a "struct".

Ø  Inheritance: Objects can inherit methods and data from others. This makes it easy to define new objects from others we had previously (as with the garage gate and the house door) and facilitates rewriting programs, as we will be able to reuse a big part of the previous ones... if they were well designed.

Ø  Polymorphism: The same method name can refer to different behaviors (such as opening a door or a book). The same idea applies for the data: both the weight of a gate and the weight of a door have the same name and refer to the same physical magnitude, but obviously have different ranges of values.

Another important concept is that of a "Class": A class is a set of objects that have common characteristics. For example, both my door and my neighbor's are doors, ie they are objects that belong to the class "door." Similarly, both a Ford Focus, a Honda Civic or a Toyota Corolla are objects that belong to the class "car".

Friday, January 25, 2013

5.11. Parameters and return value for "Main"


It is usual than a "command line program" accepts options that the user indicates as arguments. For example, under Linux or other Unix-like operating system, we can see a detailed list of files ending in .c by

lsl *.c

In this case, the command is "ls", and the two options (arguments or parameters) are "-l" and "*. c".

The equivalent command in MSDOS and in Windows command line would be:

dir *.c

Now the command is "dir", and the parameter is "*. c".

Those options that are passed when calling the program can be read from C#. It is a special parameter for Main, an array of strings:

static void Main(string[] args)

To know the values for these parameters we would proceed in the same way as for any other array: using a "for" sentence, ending on the length of the array:

for (int i = 0; i < args.Length; i++)
{
    System.Console.WriteLine("The parameter {0} is: {1}",
      i, args[i]);
}


On the other hand, if we want our program to be interrupted at some point, we can use the "Environment.Exit" command. It can be used like this:

Environment.Exit(1);

In brackets we indicate a certain code, which is usually (by convention) a 0 if there has been no error, or a different code if an there has been any error.

This value can be checked from the operating system. For example, in MS-DOS and Windows it can be read with "IF ERRORLEVEL", like this:

IF ERRORLEVEL 1 ECHO There was a problem

An alternative way to indicate errors to the operating system is not declaring Main as "void" but as "int", and then using the command "return":

public static int Main(string[] args)
{
  ...
  return 1; 
}

An example all of this might be:

/*---------------------------*/
/*  C# Example #58:          */
/*  example58.cs             */
/*                           */
/*  Parameters and return    */
/*  value of "Main"          */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example58
{

  public static int Main(string[] args)
  {
    Console.WriteLine("parameters: {0}", args.Length);
   
    for (int i = 0; i < args.Length; i++)
    {
      Console.WriteLine("The parameter {0} is: {1}",
        i, args[i]);
    }
   
    if (args.Length == 0)
    {
      Console.WriteLine("No parameters!");
      Environment.Exit(1);
    }
   
    return 0;
 
  }
 
}


Suggested exercises:
·       (5.11.1) Create a program called "sum", to calculate (and show) the sum of two numbers that are indicated as parameters. For example, if the user types "sum 2 3", the program must answer "5", and if he types "sum 2", it must answer "not enough data" and return an error code 1.
·       (5.11.2) Create a basic calculator, to add, subtract, multiply or divide the two numbers indicated as parameters. Examples of use: "calc 2 + 3" or "calc 5 * 60".
·       (5.11.3) Create a variant of the exercise 5.11.2, where Main returns the code 1 if the indicated operation is invalid or code 0 when it is an acceptable operation.
·       (5.11.4) Create a variant of the exercise 5.11.3, which also Main returns the code 2 if one of the two numbers to operate does not have a valid numeric value.