Java Lect 02

Embed Size (px)

Citation preview

  • 8/17/2019 Java Lect 02

    1/13

    April 29, 2016April 29, 2016   11

     Java Java Tutotrial for [NLP-AI] Tutotrial for [NLP-AI]

    22 

    [email protected]@cse.iitb.ac.in

  • 8/17/2019 Java Lect 02

    2/13

     April 29, 2016  Java Tutorial series part2

    2

    Last AssignmentLast Assignment

    ?? Print the details in tabular formatPrint the details in tabular format

    - use tabs- use tabs \t\t

    !stem.out."rintln#!stem.out."rintln#$$%%$&an 'e "rint ($&an 'e "rint (%%%( 'ith%( 'ith

    !stem.out."rintln#) statement*!stem.out."rintln#) statement*%%$$$$ )+)+

    asgn,asgn

  • 8/17/2019 Java Lect 02

    3/13

     April 29, 2016  Java Tutorial series part2

    3

    toring datatoring data

    In Java /ata can be stored asIn Java /ata can be stored as NumbersNumbers

    0.01 2.223 0.01 2.223 &haracters&haracters

    44c5 4"5 4*5 45c5 4"5 4*5 45

    tringstrings

    66data7 68indi language7 69337data7 68indi language7 69337

    6'''.redi:.com76'''.redi:.com7

  • 8/17/2019 Java Lect 02

    4/13

     April 29, 2016  Java Tutorial series part2

    4

    ProgramProgram

    ;; this "rogram 'ill declare and "rint a number;; this "rogram 'ill declare and "rint a number

    class intclass int

    +

    !stem.out."rintln#$!our 'eight is $ ? 'eight)+!stem.out."rintln#$!our 'eight is $ ? 'eight)+

     

    ;;end of "rogram;;end of "rogram

    int,

    int

  • 8/17/2019 Java Lect 02

    5/13

     April 29, 2016  Java Tutorial series part2

    5

    &omments#double slash)&omments#double slash)

    Not eecutedNot eecuted Bor documentationBor documentation

    &an be "laced on an! line&an be "laced on an! line An!thing 'ritten afterAn!thing 'ritten after ;;;; is not eecutedis not eecuted C.g.C.g.

    ;; this is a comment;; this is a comment

    Dsed to e"lain;describe 'hat is done inDsed to e"lain;describe 'hat is done inthe "rogramthe "rogram

    Eood "racticeEood "ractice

  • 8/17/2019 Java Lect 02

    6/13

     April 29, 2016  Java Tutorial series part2

    6

     T!"es of numbers T!"es of numbers

    intint 

    Fhole numbers liGe 2 H1H -H etc.Fhole numbers liGe 2 H1H -H etc.

    doubledouble

    Numbers 'ith decimal "ointNumbers 'ith decimal "oint

    liGe ,.H . 2.222222liGe ,.H . 2.222222

    Koat,

  • 8/17/2019 Java Lect 02

    7/13

     April 29, 2016  Java Tutorial series part2

    7

     ariable declarationariable declaration Mefore using an! name it must be declaredMefore using an! name it must be declared

    #'ith its t!"e i.e int or double).#'ith its t!"e i.e int or double).

    Needed onl! once in one "rogramNeeded onl! once in one "rogram

    Eenerall! done initiall!Eenerall! done initiall! !nta!nta

    datatype name;datatype name;

    doubledouble total+total+ ;; stores the total value;; stores the total value

    intint inde+inde+

    intint ab c sum interest+ab c sum interest+

  • 8/17/2019 Java Lect 02

    8/13

     April 29, 2016  Java Tutorial series part2

    8

    AssignmentAssignment

    intint a+ ;;declaration needed oncea+ ;;declaration needed once

    a = ,2 + ;; assignment O declared abovea = ,2 + ;; assignment O declared above

     intint a = ,2+ ;; assignment and declaration togethera = ,2+ ;; assignment and declaration together

    ,2 = a +,2 = a + ;; not "ossible com"ilation error;; not "ossible com"ilation error

    Left hand side is al'a!s a variable for assignmentLeft hand side is al'a!s a variable for assignment

    intintH

    aStorage area

    ,2

  • 8/17/2019 Java Lect 02

    9/13

     April 29, 2016  Java Tutorial series part2

    9

    AssignmentOAssignmentO

    intint a b +a b +

    a = +a = +

    b = 1+b = 1+

    a = b+a = b+

    b = a+b = a+a = H+a = H+

    int

     ;; a =* b = *;; a =* b = *

    ;; a = b = *;; a = b = *

     ;; a = b = 1;; a = b = 1

     ;; a = 1 b = 1;; a = 1 b = 1

     ;; a = 1 b = 1;; a = 1 b = 1 ;; a = H b = 1;; a = H b = 1

  • 8/17/2019 Java Lect 02

    10/13

     April 29, 2016  Java Tutorial series part2

    10

    &haracter data&haracter data

    &haracters&haracters

    44a5 4A5 4c5 4*5 45 a5 4A5 4c5 4*5 45 4 54 5

    #last is the single s"ace)#last is the single s"ace)

    Cnclosed in single uotesCnclosed in single uotes

    &haracter variable declaration&haracter variable declaration

    charchar ch+ch+ &haracter assignment&haracter assignment

    ch = 4G5+ch = 4G5+

    char,

    char

  • 8/17/2019 Java Lect 02

    11/13

     April 29, 2016  Java Tutorial series part2

    11

    tring datatring data

    trings are seuence oftrings are seuence of char char acters enclosed in double uotesacters enclosed in double uotes /eclaration/eclaration

    tringtring name+name+

    tringtring address+address+

    tringtring line+line+ AssignmentAssignment

    name = 6ram7+name = 6ram7+

    line = 6this is a line 'ith s"aces7+line = 6this is a line 'ith s"aces7+

    name = 6a7+ ;; single character can be storedname = 6a7+ ;; single character can be stored

    name = 67+ ;; em"t! stringname = 67+ ;; em"t! string

     The seuence of characters enclosed in double uotes "rinted in The seuence of characters enclosed in double uotes "rinted in"rintln#) are also strings."rintln#) are also strings. C.g.C.g. !stem.out."rintln#!stem.out."rintln# 6Felcome Q 66Felcome Q 6 )+)+

    string,stringstring

  • 8/17/2019 Java Lect 02

    12/13

     April 29, 2016  Java Tutorial series part2

    12

    Practice "roblemPractice "roblem  Tr! to "rint these t'o Rgures on the screen using "rintln and least Tr! to "rint these t'o Rgures on the screen using "rintln and least

    number of stringsnumber of strings

      **************

      **********

      ******

      **  ******

      **********

     **************

    ********************

    * ** ** ** *

    * ** *

    ********************

  • 8/17/2019 Java Lect 02

    13/13

     April 29, 2016  Java Tutorial series part2

    13

    Thank youThank you