02 programming concepts

Upload: dammam-jee

Post on 06-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 02 Programming Concepts

    1/31

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Programming Concepts

    2140101 Computer Programming for International Engineers

  • 8/3/2019 02 Programming Concepts

    2/31

    22140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Objectives

    Students should: Know the steps required to create programs

    using a programming language and related

    terminology. Be familiar with the basic structure of a Javaprogram.

    Be able to modify simple java to obtain desired

    results

  • 8/3/2019 02 Programming Concepts

    3/31

    32140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Programming Languages

    Program a set or sequence of instructionsthat tell a computer what to do

    Instructions described using

    programming languages

    We execute a program to carry out theinstruction listed by that program.

  • 8/3/2019 02 Programming Concepts

    4/31

    42140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    High-level Programming Languages

    High-level programming languages:

    rather natural for people to write.

    Java, C, C++,

    C#, VisualBasic, Pascal,Delphi,

    FORTRAN,COBOL.

  • 8/3/2019 02 Programming Concepts

    5/31

    52140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Machine Languages

    Machine language or object code:

    Most primitive to the machines.

    A set of binary code that is unique to the typeof CPU.

    Each instruction = a fundamental operation ofthe CPU.

    Writing object code directly is tedious and

    error-prone.

  • 8/3/2019 02 Programming Concepts

    6/31

    62140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Machine Languages

    Binary codesrepresented in

    Hexadecimal formatMany instructions ofmachine code are

    usually required forone line of high-levellanguage.

  • 8/3/2019 02 Programming Concepts

    7/31

    72140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Traditional Compiled Programs

    Your Code

    Compiler

    (Windows)

    Compiler(Linux)

    Compiler(Mac OS)

    executable file

    executable file

    executable file

  • 8/3/2019 02 Programming Concepts

    8/31

    82140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Running a Java Program

  • 8/3/2019 02 Programming Concepts

    9/31

    92140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Write once, run everywhere

  • 8/3/2019 02 Programming Concepts

    10/31

    102140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Programming Cycle

  • 8/3/2019 02 Programming Concepts

    11/31

    112140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    First Java Program

  • 8/3/2019 02 Programming Concepts

    12/31

    122140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Look Inside a Program

    MyFirstProgram.java

    method

    class

  • 8/3/2019 02 Programming Concepts

    13/31

    132140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Syntax, Keywords, & Identifiers

    Syntax rules of the language, very strict

    Keywords words that reserved for somepurpose. You cannot use keywords as an

    identifier.

    Identifiers names that given to classes,methods, and variables

  • 8/3/2019 02 Programming Concepts

    14/31

    142140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Java Keywords

    whilesupernativefloatconst*

    volatidestrictfplongfinallyclass

    voidstaticinterfacefinalchar

    tryshortintextendscatch

    transientreturninstanceofenumcase

    throwspublicimportelsebyte

    throwprotectedimplementsdoublebit

    thisprivateifdoboolean

    synchroniz

    edpackagegoto*defaultassert

    switchnewforcontinueabstract

    * not used

  • 8/3/2019 02 Programming Concepts

    15/31

    152140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Comments

    Comments are ignored by the

    compiler. They are used byprogrammers for:

    Explaining the code.Making notes.

    Other purposes.

  • 8/3/2019 02 Programming Concepts

    16/31

    162140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Comments

    Block commentBlock comment

    Single line commentSingle line comment

  • 8/3/2019 02 Programming Concepts

    17/31

    172140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Readability

    Indentation and new lines are keys to

    a high level of program readability.

  • 8/3/2019 02 Programming Concepts

    18/31

    182140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Readability

    Imp

    ro

    vedre

    adability

  • 8/3/2019 02 Programming Concepts

    19/31

    192140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    On-screen Display

  • 8/3/2019 02 Programming Concepts

    20/31

    202140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    println() andprint()

    println() adds a new line afterdisplaying the message, whileprint() does not.

    println() adds a new line afterdisplaying the message, whileprint() does not.

  • 8/3/2019 02 Programming Concepts

    21/31

    212140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Escape Sequences

    An escape sequence is a special character sequencethat represents another character. Each of thesespecial character sequences starts with a backslash,which is followed by another character.

  • 8/3/2019 02 Programming Concepts

    22/31

    222140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Escape Sequences

    \t Tab

    \n Newline

    \ double quote

    \ single quote

    \\ backslash

  • 8/3/2019 02 Programming Concepts

    23/31

  • 8/3/2019 02 Programming Concepts

    24/31

    D t t f C t E i i F lt f E i i Ch l l k U i it

  • 8/3/2019 02 Programming Concepts

    25/31

    252140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Naming Rules for Java Identifier

    Cannot be a Java reserved word.

    Case-sensitive

    Unlimited length of sequence of Unicodeletters and digits

    Must begin with a letter, underscore (_), ora dollar sign ($).

    White space not allowed

    Department of Computer Engineering Faculty of Engineering Chulalongkorn University

  • 8/3/2019 02 Programming Concepts

    26/31

    262140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Naming Convention

    Use meaningful names

    For compound words use camelCase.

    Class names begin with an Uppercase letter.Account, DictionaryItem, FileUtility, Article

    Variable names and method names begin with alowercase letter:

    Height, speed, filename, tempInCelcius,imcomingMsg, textToShow.

    (method names usually are verbs) locate, sortItem,findMinValue, checkForError

    For a constant use all uppercase letters and underscore(_) to separate words in compound names.SOUND_SPEED, KM_PER_MILE, BLOCK_SIZE

    Department of Computer Engineering Faculty of Engineering Chulalongkorn University

  • 8/3/2019 02 Programming Concepts

    27/31

    272140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Expressions

    An expression is a value, a variable, amethod, or one of their combinations that

    can be evaluated to a value.3.875

    a + b 10

    8 >= xp || q

    go

    System.out.print(go)

    Math.sqrt(2)

    (x + 3 > y) && (x 3 < z)

    Department of Computer Engineering Faculty of Engineering Chulalongkorn University

  • 8/3/2019 02 Programming Concepts

    28/31

    282140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Statements

    Complete sentence that causes some action to occur. ends with a semicolon (;)

    int k;

    int j = 10;

    double d1, d2, d3;

    k = a + b 10;

    boolean p = (a >= b);

    System.out.println(go);

    sqareRootTwo = Math.sqrt(2);

    block of statements are the statements withing a block of curlybraces ({ })

    {

    int m, n;m = 5;

    n = m * m;

    }

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

  • 8/3/2019 02 Programming Concepts

    29/31

    292140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

    Simple calculation

    Arithmetic operators

    add (+)

    subtract (-)

    multiply (*)

    divide (/) modulo (%) : remainder after divide

    Assignment operator (=) is used to assign

    a value or the result from the calculationto a variable

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

  • 8/3/2019 02 Programming Concepts

    30/31

    302140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    p p g g, y g g, g y

    A Simple Calculation Program

    Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University

  • 8/3/2019 02 Programming Concepts

    31/31

    312140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY

    Another Calculation

    Value of the variable avgValue of the variable avg