csc101_l2 - basic java

Upload: bryan-gallego

Post on 01-Jun-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 CSC101_L2 - Basic Java

    1/22

    LECTURE 2

    BASIC ELEMENTS OF JAVA

  • 8/9/2019 CSC101_L2 - Basic Java

    2/22

    programming

    process of planning and creating a program

    computer program

    a sequence of statements intended to accomplish a task

    programming language

    a set of rules, symbols, and special words used to construct pr

  • 8/9/2019 CSC101_L2 - Basic Java

    3/22

    A Java Program

    //********************************************************

    // This is a simple Java program. It displays three lines

    // of text, including the sum of two numbers.

    //********************************************************

    public class ASimpleJavaProgram

    {

    public static void main(String[] args)

    {

    System.out.println("My first Java program.");

    System.out.println("The sum of 2 and 3 = " + 5);

    System.out.println("7 + 8 = " + (7 + 8));

    }

    }

  • 8/9/2019 CSC101_L2 - Basic Java

    4/22

    Basics

    syntax rules

    tell you which statements (instructions) are legal, or acceptedprogramming language, and which are not

    a violation of the syntax, or grammatical rules, of a natural lanprogramming language

    semantic rules

    determine the meaning of the instructions

    a violation of the rules of meaning of a natural language or aprogramming language

  • 8/9/2019 CSC101_L2 - Basic Java

    5/22

    Basics

    1. Comment

    Single-line: //

    Multiple-line: /* and */

    2. Special Symbols

    considered a single symbol

    3. Reserved Words (Keyword

    int, float, do

    char, void, pu

    static, throws

    letters in a reserved

    always lowercase

    considered a single

  • 8/9/2019 CSC101_L2 - Basic Java

    6/22

    Basics

    4. Identifiers

    names of things, such as variables, constants, and methods, that appear in p

    consists of letters, digits, the underscore character (_), and the dollar sign ($

    begin with a letter, underscore, or the dollar sign

    can be made of only letters, digits, the underscore character (_), and the do

    other symbols are permitted to form an identifier

    Ex:

    Note: Java is case sensitive

    side Length sideLength

    angle! angle

    a+b ab

    2nd nd

  • 8/9/2019 CSC101_L2 - Basic Java

    7/22

    Data Types

    set of values together with a set of operations on tho

    Primitive Data Types

    fundamental data types in Java

    three categories

    1. integral2. floating-point

    3. boolean

  • 8/9/2019 CSC101_L2 - Basic Java

    8/22

    Arithmetic Operators and Operator Prece

    Java has 5 arithmetic operators:

    1. addition +

    2. subtraction

    3. multiplication *

    4. division /

    5. mod (modulus or remainder) %

  • 8/9/2019 CSC101_L2 - Basic Java

    9/22

    Ex:

    2 + 1 * 3

    2, 1, 3 - operands

    +,* - operators

    Unary operator

    an operator that has on

    operand.

    Ex: - 7

    Binary operator an operator that has tw

    Ex: 7+8

  • 8/9/2019 CSC101_L2 - Basic Java

    10/22

    Order of Precedence

    *, /, % (higher)

    +, - (lower)

    Ex:

    Evaluate 3 * 7 - 6 + 2

    Answer: 23

  • 8/9/2019 CSC101_L2 - Basic Java

    11/22

    Expressions

    integral expression

    floating-point or decimal expression

    mixed expression

  • 8/9/2019 CSC101_L2 - Basic Java

    12/22

    Two rules apply when evaluating a mixed expression

    1. When evaluating an operator in a mixed expression:

    If the operator has the same types of operands (that is, both are integer

    floating-point numbers), the operator is evaluated according to the type

    If the operator has both types of operands (that is, one is an integer and

    floating-point number), during calculation the integer is treated tempora

    floating-point number with the decimal part of zero, and then the operaThe result is a floating-point number.

    2. The entire expression is evaluated according to the prec

    rules.

  • 8/9/2019 CSC101_L2 - Basic Java

    13/22

  • 8/9/2019 CSC101_L2 - Basic Java

    14/22

    Evaluate the following expressions.

    a. 13 / 4

    b. 2 + 12 / 4

    c. 21 % 5

    d. 3 - 5 % 7

    e. 17.0 / 4

    f. 8 - 5 * 2.0

    g. 14 + 5 % 2 3

    h. 15.0 + 3.0 / 2.0

  • 8/9/2019 CSC101_L2 - Basic Java

    15/22

    Evaluate the following expressions.

    a. 13 / 4 = 3

    b. 2 + 12 / 4 = 5

    c. 21 % 5 = 1

    d. 3 - 5 % 7 = -2

    e. 17.0 / 4 = 4.25

    f. 8 - 5 * 2.0 = -2.0

    g. 14 + 5 % 2 3 = 12

    h. 15.0 + 3.0 / 2.0 = 16.5

  • 8/9/2019 CSC101_L2 - Basic Java

    16/22

    Type Conversion (Casting)

    (dataTypeName) expression

  • 8/9/2019 CSC101_L2 - Basic Java

    17/22

    Class StringStrings

    data values that contain more than one charactera sequence of zero or more characters

    enclosed in double quotation marks (not in single quotati

    as are the char data types)

  • 8/9/2019 CSC101_L2 - Basic Java

    18/22

    String and the operator +

    the operator +used to concatenate (or join) two strings as well as a string and a numeric va

    character.

    Ex:

    "Sunny" + " Day" evaluates to Sunny Day

    "Amount Due = $" + 576.35 evaluates to "Amount $576.35"

    "The sum = " + 12 + 26 evaluates to "The sum =

    "The sum = " + (12 + 26) evaluates to "The sum

  • 8/9/2019 CSC101_L2 - Basic Java

    19/22

    Increment and Decrement

    Ex:

    x = 7;

    y = ++x;

    x = 7;

    y = x++;

  • 8/9/2019 CSC101_L2 - Basic Java

    20/22

    Packages, Classes, Methods

    package

    a collection of related classes

    class

    used to create Java programs, either application or applet

    used to group a set of related operations

    used to allow users to create their own data types

    method

    set of instructions designed to accomplish a specific task

  • 8/9/2019 CSC101_L2 - Basic Java

    21/22

    Ex:

    class Math

    in java.lang package

    abs, cos, exp, max, atan, sqrt,

    etc

    class Scanner in java.util package

    next, nextInt, nextDouble,

    useDelimiter

    Importing

    import packageN

    import java.util

    import java.util

  • 8/9/2019 CSC101_L2 - Basic Java

    22/22

    Creating Java Application

    syntax of a class to create a Java application program