Programming Fundamentals 1

Embed Size (px)

Citation preview

  • 8/9/2019 Programming Fundamentals 1

    1/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 11

    Programming FundamentalsProgramming Fundamentals

    ICS 112 / IT 112ICS 112 / IT 112

    This presentation follows the introductoryThis presentation follows the introductorylessonlesson

    The introductory lesson covered generalThe introductory lesson covered general

    concepts about programming (See Courseconcepts about programming (See CourseNotes 1, 2 and 3)Notes 1, 2 and 3)

  • 8/9/2019 Programming Fundamentals 1

    2/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 22

    In Java, a program is a classIn Java, a program is a class

    /**/**

    * Sample java program* Sample java program

    */*/

    public class Hellopublic class Hello{{

    public static void main(String[] args)public static void main(String[] args) {{

    //prints the string "Hello world" on screen//prints the string "Hello world" on screenSystem.out.println("Hello world!");System.out.println("Hello world!");

    }}

    }}

  • 8/9/2019 Programming Fundamentals 1

    3/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 33

    A class must have the main method for it toA class must have the main method for it to

    be an executable programbe an executable program

    /**/**

    * Sample java program* Sample java program

    */*/

    public class Hellopublic class Hello{{

    public static void main(String[] args)public static void main(String[] args) {{

    //prints the string "Hello world" on screen//prints the string "Hello world" on screenSystem.out.println("Hello world!");System.out.println("Hello world!");

    }}

    }}

  • 8/9/2019 Programming Fundamentals 1

    4/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 44

    Coding Guidelines in JavaCoding Guidelines in Java

    Your Java programs should always end with theYour Java programs should always end with the

    .java.java extension.extension.

    Filenames shouldFilenames shouldmatchmatch the name of yourthe name of your

    public class.public class.

    For example, if the name of your public class isFor example, if the name of your public class is Hello,Hello,

    you should save it in a file calledyou should save it in a file calledHello.javaHello.java..

    You should write comments in your codeYou should write comments in your code

    explaining what a certain class does, or what aexplaining what a certain class does, or what a

    certain method do.certain method do.

  • 8/9/2019 Programming Fundamentals 1

    5/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 55

    Java CommentsJava Comments

    Comments are notes included in a codeComments are notes included in a code

    for documentation purposes.for documentation purposes. The comments are not part of the programThe comments are not part of the program

    and they do not affect the flow of theand they do not affect the flow of theprogram.program.

  • 8/9/2019 Programming Fundamentals 1

    6/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 66

    Single Line CommentsSingle Line Comments

    Single line comments starts with //.Single line comments starts with //.

    All the text after // are treated asAll the text after // are treated ascomments.comments.

    For example,For example,

    // This is a single line comment// This is a single line comment

  • 8/9/2019 Programming Fundamentals 1

    7/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 77

    Multiline CommentsMultiline Comments

    Multiline comments starts with aMultiline comments starts with a /*/* and ends withand ends withaa */*/..

    All text in between the two delimeters (All text in between the two delimeters (/*/* andand */*/ ))are treated as comments.are treated as comments.

    Unlike single line comments, the multilineUnlike single line comments, the multilinecomment can span multiple lines.comment can span multiple lines.

    For example,For example,/* this is an example of/* this is an example of

    multiline comments */multiline comments */

  • 8/9/2019 Programming Fundamentals 1

    8/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 88

    Special Javadoc CommentsSpecial Javadoc Comments

    Special Javadoc comments are used for generating an HTMLSpecial Javadoc comments are used for generating an HTMLdocumentation for your Java programs.documentation for your Java programs.

    You can create javadoc comments by starting the line withYou can create javadoc comments by starting the line with /**/** andandending it withending it with */*/..

    Like multiline comment, the special javadoc comment can spanLike multiline comment, the special javadoc comment can spanlines. It can also contain certain tags to add more information to yourlines. It can also contain certain tags to add more information to yourcomments. For example,comments. For example,/**/**This is an example of special java doc comments used forThis is an example of special java doc comments used for \\nngenerating an html documentation. It uses tags like:generating an html documentation. It uses tags like:

    @author Francisco Balagtas@author Francisco Balagtas@version 1.2@version 1.2*/*/

  • 8/9/2019 Programming Fundamentals 1

    9/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 99

    Java Statement and BlockJava Statement and Block

    AA statementstatement is one or more lines of codeis one or more lines of codeterminated by a semicolon.terminated by a semicolon.

    Example of a single statementExample of a single statement

    System.out.println(Hello world);System.out.println(Hello world);

  • 8/9/2019 Programming Fundamentals 1

    10/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 1010

    AA blockblock is one or more statements bounded byis one or more statements bounded bya pair of an opening curly brace and closinga pair of an opening curly brace and closingcurly brace that groups the statements as onecurly brace that groups the statements as oneunit.unit.

    Block statements can be nested indefinitely.Block statements can be nested indefinitely. Any amount of white space is allowed.Any amount of white space is allowed. Example of a blockExample of a block

    public static void main( String[] args )public static void main( String[] args ) {{System.out.println("Hello");System.out.println("Hello");System.out.println("world");System.out.println("world");

    } // end of main} // end of main

  • 8/9/2019 Programming Fundamentals 1

    11/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 1111

    Coding GuidelineCoding Guideline

    In creating blocks, you can place the opening curlyIn creating blocks, you can place the opening curly

    brace in line with the statement, for example,brace in line with the statement, for example,public static void main( String[] args )public static void main( String[] args ){{

    } // end of main} // end of main

    or you can place the curly brace on the next line,or you can place the curly brace on the next line,

    for examplefor example

    public static void main( String[] args )public static void main( String[] args ){{

    } // end of main} // end of main

  • 8/9/2019 Programming Fundamentals 1

    12/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 1212

    Coding GuidelineCoding Guideline

    You should indent the next statements afterYou should indent the next statements afterthe start of a block, for example,the start of a block, for example,

    public static void main( String[] args ){public static void main( String[] args ){

    System.out.println("Hello");System.out.println("Hello");

    System.out.println("world");System.out.println("world");

    } // end of main} // end of main

  • 8/9/2019 Programming Fundamentals 1

    13/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 1313

    Java IdentifiersJava Identifiers

    Identifiers are tokens that representIdentifiers are tokens that representnames of variables, methods, classes, etc.names of variables, methods, classes, etc.

    Examples of identifiers :Examples of identifiers :

    Hello,Hello,

    main,main,

    System,System,

    outout

  • 8/9/2019 Programming Fundamentals 1

    14/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 1414

    Java IdentifiersJava Identifiers

    Java identifiers areJava identifiers are casecase--sensitivesensitive. This. Thismeans that the identifier:means that the identifier: HelloHello is not theis not thesame assame as hellohello..

    Identifiers must begin with either a letter,Identifiers must begin with either a letter,an underscore _, or a dollar sign $.an underscore _, or a dollar sign $.

    Letters may be lower or upper case.Letters may be lower or upper case.

    Subsequent characters may use numbersSubsequent characters may use numbers0 to 9.0 to 9.

    Note: No space character in the identifier!Note: No space character in the identifier!

  • 8/9/2019 Programming Fundamentals 1

    15/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 1515

    Java IdentifiersJava Identifiers

    Java keywords, like class, public, void, etc.Java keywords, like class, public, void, etc.

    cannot be identifierscannot be identifiers

    Keywords are predefined identifiersKeywords are predefined identifiersreserved by Java for a specific purpose.reserved by Java for a specific purpose.

  • 8/9/2019 Programming Fundamentals 1

    16/45

  • 8/9/2019 Programming Fundamentals 1

    17/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 1717

    Coding GuidelinesCoding Guidelines

    For names of classes, capitalize the first letterFor names of classes, capitalize the first letterof the class name.of the class name.

    ExampleExample

    TThishisIIssAAnnEExamplexampleOOffCClasslassNNameame

    For names of methods and variables, the firstFor names of methods and variables, the firstletter of the word should start with a smallletter of the word should start with a smallletter.letter.

    ExampleExample tthishisIIssAAnnEExamplexampleOOffMMethodethodNNameame

  • 8/9/2019 Programming Fundamentals 1

    18/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 1818

    Coding GuidelineCoding Guideline

    In case of multiIn case of multi--word identifiers that areword identifiers that arenot intended as names of classes, usenot intended as names of classes, usecapital letters to indicate the start of thecapital letters to indicate the start of the

    word except the first word.word except the first word.

    Example:Example:

    charArray,charArray,fileNumber,fileNumber,

    firstName.firstName.

  • 8/9/2019 Programming Fundamentals 1

    19/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 1919

    Coding GuidelineCoding Guideline

    Avoid using underscores at the start ofAvoid using underscores at the start of

    the identifier such as _read or _write.the identifier such as _read or _write.

  • 8/9/2019 Programming Fundamentals 1

    20/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 2020

    Java LiteralsJava Literals

    Literals are tokens that have values thatLiterals are tokens that have values that

    do not change or are constant.do not change or are constant. The different types of literals in Java are:The different types of literals in Java are:

    Integer Literals, FloatingInteger Literals, Floating--Point Literals,Point Literals,Boolean Literals, Character Literals andBoolean Literals, Character Literals andString Literals.String Literals.

  • 8/9/2019 Programming Fundamentals 1

    21/45

  • 8/9/2019 Programming Fundamentals 1

    22/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 2222

    FloatingFloating--Point LiteralsPoint Literals

    Floating point literals represent decimals withFloating point literals represent decimals withfractional parts. An example is 3.1415. Floatingfractional parts. An example is 3.1415. Floatingpoint literals can be expressed in standard orpoint literals can be expressed in standard orscientific notations. For example, 583.45 is inscientific notations. For example, 583.45 is instandard notation, while 5.8345e2 is in scientificstandard notation, while 5.8345e2 is in scientificnotation.notation.

    Floating point literals default to the data typeFloating point literals default to the data typedoubledouble which is a 64which is a 64--bit value. To use a smallerbit value. To use a smallerprecision (32precision (32--bit)bit) floatfloat, just append the f or F, just append the f or Fcharacter.character.

  • 8/9/2019 Programming Fundamentals 1

    23/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 2323

    Boolean LiteralsBoolean Literals

    Boolean literals have only two values,Boolean literals have only two values,

    truetrue ororfalsefalse..

  • 8/9/2019 Programming Fundamentals 1

    24/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 2424

    Character LiteralsCharacter Literals

    Character Literals represent single Unicode characters.Character Literals represent single Unicode characters.A Unicode character is a 16A Unicode character is a 16--bit character set thatbit character set thatreplaces the 8replaces the 8--bit ASCII character set. Unicode allowsbit ASCII character set. Unicode allows

    the inclusion of symbols and special characters fromthe inclusion of symbols and special characters fromother languages.other languages. To use a character literal, enclose the character in singleTo use a character literal, enclose the character in single

    quote delimiters. For example, the letterquote delimiters. For example, the letteraa, is, isrepresented asrepresented as aa..

    To use special characters such as a newline character, aTo use special characters such as a newline character, abackslash is used followed by the character code. Forbackslash is used followed by the character code. Forexample, example, \\n for the newline character, n for the newline character, \\r for ther for thecarriage return, carriage return, \\b for backspace.b for backspace.

  • 8/9/2019 Programming Fundamentals 1

    25/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 2525

    String LiteralsString Literals

    String literals represent multiple charactersString literals represent multiple characters

    and are enclosed by double quotes.and are enclosed by double quotes.An example of a string literal is,An example of a string literal is, HelloHello

    World.World.

  • 8/9/2019 Programming Fundamentals 1

    26/45

  • 8/9/2019 Programming Fundamentals 1

    27/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 2727

    LogicalLogical -- booleanboolean

    A boolean data type represents two states:A boolean data type represents two states:

    truetrue andand falsefalse.. Example is,Example is,

    boolean result = true;boolean result = true;

    The example declares a variable namedThe example declares a variable namedresultresult asas booleanboolean type and assigns it atype and assigns it avalue ofvalue oftruetrue..

  • 8/9/2019 Programming Fundamentals 1

    28/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 2828

    TextualTextual charchar

    A character data type (char), represents a singleA character data type (char), represents a singleUnicode character. It must have its literalUnicode character. It must have its literalenclosed in single quotes( ). For example,enclosed in single quotes( ). For example, aa //The letter a//The letter a \\tt //A tab//A tab

    To represent special characters like ' (singleTo represent special characters like ' (singlequotes) or " (double quotes), use the escapequotes) or " (double quotes), use the escape

    charactercharacter \\. For example,. For example, \\'''' //for single quotes//for single quotes ''\\""'' //for double quotes//for double quotes

  • 8/9/2019 Programming Fundamentals 1

    29/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 2929

    The StringThe String

    A String represents a data type thatA String represents a data type thatcontains multiple characters.contains multiple characters.

    It isIt is nota primitived

    atatype, it isanota primitived

    atatype, it isaclass.class.It has its literal enclosed in doubleIt has its literal enclosed in doublequotes().quotes().

    Example,Example,

    String message=Hello world!String message=Hello world!

  • 8/9/2019 Programming Fundamentals 1

    30/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3030

    IntegralIntegral byte, short, int & longbyte, short, int & long

    Integral data types in Java uses three formsIntegral data types in Java uses three forms decimal, octal or hexadecimal. Examples are,decimal, octal or hexadecimal. Examples are,

    22 //The decimal value 2//The decimal value 2

    077077 //The leading 0 indicates an octal value//The leading 0 indicates an octal value

    0xBACC0xBACC //The leading 0x indicates a hexadecimal value//The leading 0x indicates a hexadecimal value

    Integral types hasIntegral types has intint as default data type.as default data type. You can define itsYou can define its longlong value by appending thevalue by appending the

    letter l or L.letter l or L.

  • 8/9/2019 Programming Fundamentals 1

    31/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3131

    Integral types and their rangesIntegral types and their ranges

    Integer lengthInteger length Name ofName oftypetype

    RangeRange

    8 bits8 bits bytebyte ((--2277) to (2) to (277-- 1)1)

    16 bits16 bits shortshort ((--221515) to (2) to (21515--1)1)

    32 bits32 bits intint ((--223131) to (2) to (23131-- 1)1)

    64 bits64 bits longlong ((--226363) to (2) to (26363--1)1)

  • 8/9/2019 Programming Fundamentals 1

    32/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3232

    Coding GuidelinesCoding Guidelines

    In defining a long value, a lowercase L isIn defining a long value, a lowercase L is

    not recommended because it is hard tonot recommended because it is hard todistinguish it from the digit 1.distinguish it from the digit 1.

  • 8/9/2019 Programming Fundamentals 1

    33/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3333

    Floating PointFloating Point float and doublefloat and double

    Floating point types hasFloating point types has doubledouble as default data type.as default data type. FloatingFloating--point literal includes either a decimal point or one of thepoint literal includes either a decimal point or one of the

    following,following, E or e //(add exponential value)E or e //(add exponential value)

    F or f //(float)F or f //(float) D or d //(double)D or d //(double)

    ExamplesExamples 3.143.14 //A simple floating//A simple floating--point value (a double)point value (a double) 6.02E236.02E23 //A large floating//A large floating--point valuepoint value 2.718F2.718F //A simple float size value//A simple float size value

    123.4E+306D123.4E+306D //A large double value with redundant D//A large double value with redundant D In the example shown above, the 23 after the E in the secondIn the example shown above, the 23 after the E in the second

    example is implicitly positive. That example is equivalent toexample is implicitly positive. That example is equivalent to6.02E+23.6.02E+23.

  • 8/9/2019 Programming Fundamentals 1

    34/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3434

    Floating point types and theirFloating point types and their

    rangesranges

    Float lengthFloat length Name/TypeName/Type RangeRange

    32 bits32 bits floatfloat ApproximatelyApproximately3.4e+38 with 73.4e+38 with 7significant digitssignificant digits

    64 bits64 bits doubledouble ApproximatelyApproximately1.7e+308 with 151.7e+308 with 15significant digitssignificant digits

  • 8/9/2019 Programming Fundamentals 1

    35/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3535

    VariablesVariables

    AA variablevariable is an item of data used to storeis an item of data used to storestate of objects.state of objects.

    A variable has aA variable has a datatypedatatype andand anameaname..

    TheThe datatypedatatypeindicates the type of valueindicates the type of valuethat the variable can hold. Hence, thethat the variable can hold. Hence, thedata type defines the possible values fordata type defines the possible values for

    the variable.the variable. TheThe variablenamevariablename must follow rules formust follow rules for

    identifiers.identifiers.

  • 8/9/2019 Programming Fundamentals 1

    36/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3636

    Declaring and InitializingDeclaring and Initializing

    VariablesVariables

    Syntax for declaring a variableSyntax for declaring a variable

    [=initial value]; [=initial value];

    Values enclosed in are required values,Values enclosed in are required values,while those values enclosed in [] arewhile those values enclosed in [] areoptional.optional.

  • 8/9/2019 Programming Fundamentals 1

    37/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3737

    Examples ofVariable DeclarationExamples ofVariable Declaration

    /*declareadatatype with variablename/*declareadatatype with variablename

    resultand booleandatatype*/resultand booleandatatype*/

    booleanresult;booleanresult;

    /*declareadatatype with variablename/*declareadatatype with variablenameoptionand chardatatype*/optionand chardatatype*/

    charoption;charoption;

    option = 'C';option = 'C'; //assign'C'to option//assign'C'to option

    /*declareadatatype with variablename/*declareadatatype with variablename

    grade,doubledatatypeand initializedto 0.0*/grade,doubledatatypeand initializedto 0.0*/

    doubledouble grade = 0.0;grade = 0.0;

  • 8/9/2019 Programming Fundamentals 1

    38/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3838

    Coding Guidelines:Coding Guidelines:

    It always good toIt always good to initializeinitialize your variables as you declare them.your variables as you declare them.

    UseUse descriptivedescriptive names for your variables. Like for example, ifnames for your variables. Like for example, ifyou want to have a variable that contains a grade for a student,you want to have a variable that contains a grade for a student,name it as,name it as, gradegrade and not just some random letters you choose.and not just some random letters you choose.

    Declare one variable per line of code.Declare one variable per line of code. For example, the variable declarations,For example, the variable declarations,

    double exam=0;double exam=0;

    double quiz=10;double quiz=10;

    double grade = 0;double grade = 0;

    is preferred over the declaration,is preferred over the declaration, double exam=0, quiz=10, grade=0;double exam=0, quiz=10, grade=0;

  • 8/9/2019 Programming Fundamentals 1

    39/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 3939

    Reading values from theReading values from the

    keyboardkeyboard

    For some applications, values to be assigned toFor some applications, values to be assigned tovariables are entered through the keyboard duringvariables are entered through the keyboard duringprogram execution. Java provides a mechanism forprogram execution. Java provides a mechanism for

    connecting the keyboard to the program. Theconnecting the keyboard to the program. Themechanism involves the so called Stream classes( to bemechanism involves the so called Stream classes( to bestudied later.).studied later.).

    In the mean time, the Keyboard class may be used.In the mean time, the Keyboard class may be used.

    By using the class Keyboard, values may be assigned toBy using the class Keyboard, values may be assigned toa variable.a variable.

    NoteNote:: Copy and save the Keyboard.java program in theCopy and save the Keyboard.java program in thedirectory where you are saving your programsdirectory where you are saving your programs..

  • 8/9/2019 Programming Fundamentals 1

    40/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 4040

    Using the KeyboardReader classUsing the KeyboardReader class

    Suppose age is assigned an integer value:Suppose age is assigned an integer value:age = Keyboard.readInt();age = Keyboard.readInt();

    Suppose rate is assigned a floating point value( i.e.Suppose rate is assigned a floating point value( i.e.number with decimal digits):number with decimal digits):

    rate = Keyboard.readFloat();rate = Keyboard.readFloat();ororrate = Keyboard.readDouble();rate = Keyboard.readDouble();

    Suppose choice is assigned a character value:Suppose choice is assigned a character value:choice = Keyboard.readChar();choice = Keyboard.readChar();

    Suppose name is a string value(i.e. sequence ofSuppose name is a string value(i.e. sequence ofcharacters):characters):

    name = Keyboard.readString();name = Keyboard.readString();

  • 8/9/2019 Programming Fundamentals 1

    41/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 4141

    OutputtingVariable DataOutputtingVariable Data

    int value = 10;int value = 10;

    char x;char x;

    x = A;x = A;System.out.println( value );System.out.println( value );

    System.out.println( The value of x= + x );System.out.println( The value of x= + x );

  • 8/9/2019 Programming Fundamentals 1

    42/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 4242

    Formatting a Number OutputFormatting a Number Output

    long n = 461012;long n = 461012;System.out.printf("%d%n", n);System.out.printf("%d%n", n);System.out.printf("%08d%n", n);System.out.printf("%08d%n", n);System.out.printf("%+8d%n", n);System.out.printf("%+8d%n", n);System.out.printf("%,8d%n", n);System.out.printf("%,8d%n", n);System.out.printf("%+,8d%n%n", n);System.out.printf("%+,8d%n%n", n);double pi = Math.PI;double pi = Math.PI;System.out.printf("%f%n", pi);System.out.printf("%f%n", pi);

    System.out.printf("%.3f%n", pi);System.out.printf("%.3f%n", pi);System.out.printf("%10.3f%n", pi);System.out.printf("%10.3f%n", pi);System.out.printf("%System.out.printf("%--10.3f%n", pi);10.3f%n", pi);

  • 8/9/2019 Programming Fundamentals 1

    43/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 4343

    Formatting a Number OutputFormatting a Number Output

    long n = 461012;long n = 461012;System.out.format("%d%n", n);System.out.format("%d%n", n);System.out.format("%08d%n", n);System.out.format("%08d%n", n);System.out.format("%+8d%n", n);System.out.format("%+8d%n", n);System.out.format("%,8d%n", n);System.out.format("%,8d%n", n);System.out.format("%+,8d%n%n", n);System.out.format("%+,8d%n%n", n);double pi = Math.PI;double pi = Math.PI;System.out.format("%f%n", pi);System.out.format("%f%n", pi);

    System.out.format("%.3f%n", pi);System.out.format("%.3f%n", pi);System.out.format("%10.3f%n", pi);System.out.format("%10.3f%n", pi);System.out.format("%System.out.format("%--10.3f%n", pi);10.3f%n", pi);

  • 8/9/2019 Programming Fundamentals 1

    44/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 4444

    Formatted OutputFormatted Output

    461012461012

    0046101200461012

    +461012+461012

    461,012461,012+461,012+461,012

    3.1415933.141593

    3.1423.142

    3.1423.142

    3.1423.142

  • 8/9/2019 Programming Fundamentals 1

    45/45

    8/21/20108/21/2010 Prepared by Dale D. Miguel SLU CICSPrepared by Dale D. Miguel SLU CICS 4545

    Formatting a Number OutputFormatting a Number Output

    long n = 461012;long n = 461012;System.out.format("%d%n", n);System.out.format("%d%n", n); //461012//461012System.out.format("%08d%n", n);System.out.format("%08d%n", n); // 00461012// 00461012

    System.out.format("%+8d%n", n);System.out.format("%+8d%n", n); // +461012// +461012

    System.out.format("%,8d%n", n);System.out.format("%,8d%n", n); // 461,012// 461,012

    System.out.format("%+,8d%n%n", n);System.out.format("%+,8d%n%n", n); // +461,012// +461,012

    double pi = Math.PI;double pi = Math.PI;

    System.out.format("%f%n", pi);System.out.format("%f%n", pi); //// 3.1415933.141593

    System.out.format("%.3f%n", pi);System.out.format("%.3f%n", pi); ////3.1423.142System.out.format("%10.3f%n", pi);System.out.format("%10.3f%n", pi); //// 3.1423.142System.out.format("%System.out.format("%--10.3f%n", pi);10.3f%n", pi); //// 3.1423.142