01 iec t1s1 oops session 01

Upload: caininme

Post on 30-May-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    1/45

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    2/45

    Slide 2 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    In this session, you will learn to:

    Explain features of the object-oriented methodology

    Describe the phases of the object-oriented methodology

    Define classes in C#

    Declare variablesWrite and execute C# programs

    Objectives

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    3/45

    Slide 3 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Object orientation is a software development methodology

    that is based on modeling a real-world system.

    An object oriented program consists of classes and objects.

    Let us understand the termsclass and objects

    Object-Oriented Methodology

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    4/45

    Slide 4 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Toyota CamrySuzuki Reno Honda Acura

    Objects

    Class

    Object-Oriented Methodology (Contd.)

    Car

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    5/45

    Slide 5 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    An object means a material thing that is capable of being

    presented to the senses.

    An object has the following characteristics:

    It has a state

    It may display behaviorIt has a unique identity

    Objects interact with other objects through messages.

    Let us understand these concepts.

    The Foundation of Object Orientation

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    6/45

    Slide 6 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Car positioned at one place defines its State

    Movement of car defines its Behavior

    Car number XX 4C 4546 shows

    the Identity of the car

    The Foundation of Object Orientation (Contd.)

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    7/45Slide 7 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Car is flashing the lights to pass

    the message to the other car

    The Foundation of Object Orientation (Contd.)

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    8/45Slide 8 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Just a minute

    Identify the possible states of the following objects:

    1. A cell phone

    2. A stereo

    Solution:1. States of a cell phone: Off, Ring, Vibrate, and Call

    2. States of a stereo: Play, Pause, Rewind, and Forward

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    9/45Slide 9 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Just a minute

    Dr. James and Mr. Hyde went to the railway station to book

    tickets for 3rd December. At the railway station, they requested

    the clerk at the ticket counter to book two tickets for the Flying

    Express in the first class. Identify the following:

    1. The possible receiver of the message in this situation.

    2. The possible method that the receiver can use.

    Solution:

    1. The receiver of the message in this case will be the clerk at

    the ticket counter.2. The clerk will check if two tickets are available on the

    requested train in the desired class and for the desired date. If

    the tickets are available, the clerk will enter the details (name,

    age, departure date, and seat), confirm the reservation, and

    collect the required fare.

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    10/45Slide 10 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Characteristics of the Object-Oriented Approach

    Realistic modeling

    Reusability

    Resilience to change

    Existence as different forms

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    11/45Slide 11 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Just a minute

    State whether the following situations demonstrate

    reusability:

    1. Recycling paper

    2. Pump reusability (same pump is used in a well and in a fuel

    station)

    Solution:

    1. It does not represent reusability because the unusable paper

    is destroyed before paper is recycled for use. The unusable

    paper loses its identity and cannot be considered the same as

    recycled paper.2. It represents reusability because a pump can be used for

    suction of water as well as petrol. It is not necessary to use

    the same pump in both the cases. Two separate machines

    can be used because both belong to the Pump class.

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    12/45Slide 12 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    The following phases are involved in the software

    development:

    The Analysis phase

    The Design phase

    The Implementation phase

    Let us discuss the process of constructing a building.

    Phases of Object Orientation

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    13/45Slide 13 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Just a minute

    As a member of a team that is developing software for

    DialCom Telecommunications, Inc., you have been

    assigned the task of creating a software module that

    accepts and displays customer details such as name, age,

    and phone number. Identify the class that you will create

    and the methods of the class.

    Solution:

    As per the problem statement, the class required is:

    Customer

    The class should have the methods to:

    Accept customer details

    Display customer details

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    14/45Slide 14 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    A program is a set of instructions to perform a specific task.

    Programming languages use programs to develop software

    applications.

    A compiler is a special program that processes the

    statements written in a particular programming languageand converts them into a machine language.

    This process of conversion is called compilation.

    Introducing C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    15/45Slide 15 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    C#, also known as C-Sharp, is a programming language

    introduced by Microsoft.

    C# is specially designed to work with the Microsofts .NET

    platform.

    Let us understand the structure of a C# program.

    Introducing C# (Contd.)

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    16/45Slide 16 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Consider the following code example, which defines a class:

    public class Hello

    {

    public static void Main(string[] args)

    {System.Console.WriteLine("Hello, World!

    \n");

    }

    }

    Classes in C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    17/45Slide 17 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    public class Hello

    {

    public static void

    Main(string[] args)

    {

    System.Console.WriteLine("He

    llo, World! \n");

    }

    }

    Classes in C# (Contd.)

    The class Keyword

    Is used to declare a

    class

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    18/45Slide 18 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    public class Hello

    {

    public static void

    Main(string[] args)

    {

    System.Console.WriteLine("He

    llo, World! \n");

    }

    }

    The class Name

    Is used as an identifier

    for a class

    Classes in C# (Contd.)

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    19/45Slide 19 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    public class Hello

    {

    public static void

    Main(string[] args)

    {

    System.Console.WriteLine("He

    llo, World! \n");

    }

    }

    The Main() Function

    Is the entry point of an

    application

    Is used to create

    objects and invokemember functions

    Classes in C# (Contd.)

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    20/45Slide 20 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    public class Hello

    {

    public static void

    Main(string[] args)

    {

    System.Console.WriteLine("He

    llo, World! \n");

    }

    }

    System.Console.WriteLine()

    Displays the enclosed text on

    the screen

    Classes in C# (Contd.)

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    21/45Slide 21 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    public class Hello

    {

    public static void

    Main(string[] args)

    {

    System.Console.WriteLine("He

    llo, World! \n");

    }

    }

    The Escape Character

    Displays New line

    character. Other special

    characters can also be

    displayed such as \t, \b

    and \r

    Classes in C# (Contd.)

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    22/45Slide 22 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Demo: Creating Classes

    Problem Statement:

    As a member of a team that is developing toys for JoyToys,

    Inc., you have been assigned the task of creating a bike

    module that accepts and displays bike details. Declare the Bike

    class and its member functions. The member function that

    accepts bike details should display the message AcceptingBike Details. Similarly, the member function to display bike

    details on the screen should display the message Displaying

    Bike Details.

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    23/45

    Slide 23 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    A variable is a location in the memory that has a name and

    contains a value.

    A variable is associated with a data type that defines the

    type of data that can be stored in a variable.

    Declaring Variables

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    24/45

    Slide 24 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    You can declare and initialize variables by using the

    following syntax:

    =;

    Declaring and Initializing Variables

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    25/45

    Slide 25 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Consider the followingexample of declaring andinitializing a variable:

    int class_rank=2;

    Declaring and Initializing Variables (Contd.)

    Data Types in C#

    Represents the kind of

    data stored in a variable

    C# provides you with

    various built-in datatypes, such as:

    char

    int

    float

    double

    bool

    string

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    26/45

    Slide 26 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Data Types in C#

    Let us now understand the various data types with the help

    of examples.

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    27/45

    Slide 27 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Name

    Marks

    Age

    Vowel

    string

    float

    int

    char

    = Peter

    = 83.56

    = 23

    = a

    Data Types in C# (Contd.)

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    28/45

    Slide 28 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Consider the following

    example of declaring and

    initializing a variable:

    int class_rank=2;

    Data types in C#

    The following types of

    data types are supported

    by C#:

    Value types

    Num

    Memory allocated

    Variable declared and Initialized

    int Num;

    Num=5; 5

    Data Types in C# (Contd.)

    Memory Allocation in Value Type

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    29/45

    Slide 29 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Consider the following

    example of declaring and

    initializing a variable:

    int class_rank=2;

    Declaring and Initializing Variables

    Data types in C#

    The following types of

    data types are supported

    by C#:

    Reference types

    string Str=Hello;

    Str

    0 1 2 3 4

    H E L L O

    Address

    Memory Allocation of the String Type Variable

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    30/45

    Slide 30 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Consider the following

    example of declaring and

    initializing a variable:

    int class_rank=2;

    Declaring and Initializing Variables (Contd.)

    Naming variables in C#

    The following rules are used

    for naming variables in C#:

    Must begin with a letter or

    an underscore Should not contain any

    embedded spaces or

    symbols

    Must be unique

    Can have any number ofcharacters

    Keywords cannot be used

    as variable names

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    31/45

    Slide 31 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Consider the following

    example of declaring and

    initializing a variable:

    int class_rank=2;

    Declaring and Initializing Variables (Contd.)

    Examples and non-examples

    of Naming Variables

    Name

    #Score

    Age

    2Strank

    Family_Size

    Gender

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    32/45

    Slide 32 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Consider the

    following example of

    declaring and

    initializing a variable:

    int class_rank=2;

    Declaring and Initializing Variables (Contd.)

    Initializing Variables in C#

    Specifies the value that

    needs to be stored in a

    variable. The value could be

    an integer, a decimal, or a

    character.

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    33/45

    Slide 33 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    To understand how to accept

    value in a variable, let us

    consider the following code

    snippet:

    int Number;

    Number=

    Convert.ToInt32(Console.Rea

    dLine());

    Accepting and Storing Values in Member Variables

    Console.ReadLine()

    Is used to accept input

    from the user and store it

    in the variable

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    34/45

    Slide 34 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    To understand how to accept

    value in a variable, let us

    consider the following code

    snippet:

    int Number;

    Number=

    Convert.ToInt32(Console.Rea

    dLine());

    Accepting and Storing Values in Member Variables(Contd.)

    Convert.ToInt32()

    Converts the value

    entered by the user to

    the int data type

    Obj O i d i i C

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    35/45

    Slide 35 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Let us know learn to write, compile, and execute a C#

    program.

    Writing and Executing a C# Program

    Obj O i d P i U i C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    36/45

    Slide 36 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    A C# program can be written by using an editor like

    Notepad. Consider the following code, which declares a

    class Car and also creates an object MyCar of the same

    class:

    using System;

    class Car

    {

    //Member variables

    string Engine;

    int NoOfWheels;

    //Member functionsvoid AcceptDetails()

    Creating a Sample C# Program

    The using keyword is used to include

    the namespaces in the program.

    Comments are used to explain the

    code and are represented by //

    symbols.

    Member variables are used to store

    the data for a class.

    Member functions are declared insidethe class that are used to perform a

    specific task.

    Obj t O i t d P i U i C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    37/45

    Slide 37 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    {

    Console.WriteLine("Enter the Engine Model");

    Engine = Console.ReadLine();

    Console.WriteLine("Enter the number of Wheels");

    NoOfWheels = Convert.ToInt32(Console.ReadLine());

    }public void DisplayDetails()

    {

    Console.WriteLine("The Engine Model is:{0}",

    Engine);

    Console.WriteLine("The number of wheels are:{0}",

    NoOfWheels);

    }

    }

    Creating a Sample C# Program (Contd.)

    Obj t O i t d P i U i C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    38/45

    Slide 38 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    //Class used to instantiate the Car class

    class ExecuteClass

    {

    public static void Main(string[] args)

    {

    Car MyCar = new Car();MyCar.AcceptDetails();

    MyCar.DisplayDetails();

    }

    }

    Creating a Sample C# Program (Contd.)

    The Execute class is used as a class from

    where the Car class can be instantiated.

    Obj t O i t d P i U i C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    39/45

    Slide 39 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    After writing the program in a Notepad, you need to compile

    and execute it to get the desired output.

    The compiler converts the source code that you write into

    the machine code, which the computer can understand.

    The following steps are needed to compile and execute aC# program.

    1. Save the code written in the Notepad with an extension .cs.

    2. To compile the code, you need to go to the Visual Studio

    2005Command Prompt window. Select StartAllProgramsMicrosoft Visual Studio 2005Visual StudioToolsVisual Studio 2005 Command Prompt. The VisualStudio 2005 Command Prompt window is displayed to

    compile the program.

    3. In the Visual Studio 2005 Command Prompt window, move

    to the location where the programs file is saved.

    Compiling and Executing C# Program

    Obj t O i t d P i U i C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    40/45

    Slide 40 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    4. Compile the program file by using the following command:

    csc ExecuteClass.cs

    5. To execute the code, type the following in the command

    prompt:

    ExecuteClass.exe

    Compiling and Executing C# Program (Contd.)

    Obj t O i t d P i U i C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    41/45

    Slide 41 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Problem Statement:

    David is the member of a team that is developing the

    Automatic Ranking software for a tennis tournament. You have

    been assigned the task of creating a program. The program

    should accept the following details of a tennis player and

    display it:Name, containing a maximum of 25 characters

    Rank as an integer

    Winning average as a decimal value

    Help David to create the program.

    Demo: Creating a C# Program

    Object Oriented Programming Using C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    42/45

    Slide 42 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    In this session, you learned that:

    According to the object-oriented approach, systems consist of

    component objects that interact with each other.

    An object is an entity that may have a physical boundary.

    However, it should have the following characteristics:

    State

    Behavior

    Identity

    A class consists of a set of objects that share a common

    structure and behavior.

    If an object desires an action from another object, it sends amessage to that object.

    The object that receives the message is called the receiver, and

    the set of actions taken by the receiver constitutes the method.

    Summary

    Object Oriented Programming Using C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    43/45

    Slide 43 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    The features of the object-oriented approach are:

    Realistic modeling

    Reusability

    Resilience to change

    Existence as different forms

    A model of a system is built in the stages of analysis, designand implementation.

    The purpose of the model is to help developers understand the

    reality that they are trying to imitate.

    In C#, a class is created by using the keyword class. It is

    identified by a name called the class name.The Console.WriteLine() method is used to display text on the

    screen.

    Main() is the first function which is executed in a C# program.

    Summary (Contd.)

    Object Oriented Programming Using C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    44/45

    Slide 44 of 45Session 1Ver. 1.0

    Object-Oriented Programming Using C#

    Escape characters are used to display special characters such

    as the newline character.

    A variable is a named location in the memory, which contains a

    specific value.

    A datatype defines the type of data that can be stored in a

    variable.The two types of data type are Value type and Reference type.

    The ReadLine() method is used to accept inputs from the

    user.

    The using keyword is used to include the namespaces in the

    program.A namespace contains a set of related classes.

    Member variables are declared inside the class body.

    Summary (Contd.)

    Object Oriented Programming Using C#

  • 8/14/2019 01 Iec t1s1 Oops Session 01

    45/45

    Object-Oriented Programming Using C#

    Comment entries are notes written by a programmer in the

    code so that others reading that code can understand it better.

    An object is an instance of a class.

    The compiler software translates a program written in a

    language like C# into the machine language.

    Summary (Contd.)