Download - JavaProgramming_SlideNotes1

Transcript
  • 7/31/2019 JavaProgramming_SlideNotes1

    1/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    JAVA

    PROGRAMMINGLANGUAGE

  • 7/31/2019 JavaProgramming_SlideNotes1

    2/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Simple Input/OutputOperation

    Objectives Simple Input Command

    Character

    String

    Simple Output Command

    Convert String to

    Integer Real

    Programming Exercises

  • 7/31/2019 JavaProgramming_SlideNotes1

    3/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Simple Input Command

    The form of input can be keyboardentry from the user.

    Values input by user can be numerical

    or non-numerial. Java has some standard input

    commands that allows the user to usethe commands to input values via thekeyboard.

    We shall look at the character andString input

  • 7/31/2019 JavaProgramming_SlideNotes1

    4/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Character Input Command

    Characters are read from the keyboardusing System.in.read() method.

    One of Javas console function.

    Accepts input from the keyboard.

    in object has access to method read()which retrieves data from keyboard.

    The main() method header had aphrase throws Exception added at theend of the line.

  • 7/31/2019 JavaProgramming_SlideNotes1

    5/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Character Input Command

    Exception is an error situation, bythrowing the exception, thecompiler will handle the differentsorts of errors.

    Programs accepting input fromkeyboard will not be able to

    compile successfully without thephrase throws Exception.

  • 7/31/2019 JavaProgramming_SlideNotes1

    6/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    String Input Command

    TheInputStreamReader() is used toread characters from the keyboard.

    The BufferedReader() is used, for it

    provides the readLine() methodtoallow programs to read from keyboarda line at a time.

    Java implements I/O operationsthrough streams.

    Represented by two abstract classes,InputStream and OutputStream.

  • 7/31/2019 JavaProgramming_SlideNotes1

    7/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    String Input Command

    These are found injava.iopackage.

    Thus, have to import this packageinto the program, which is placeat the beginning of the program.

  • 7/31/2019 JavaProgramming_SlideNotes1

    8/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Simple Output Command

    System.outis a PrintStream object thatallows output to be displayed.

    Two methods used, i.e.print() and

    println() method. print() method allows the cursor to be

    positioned on the next availableposition on the same line.

    println() method allows the cursor tobe positioned on the next line.

  • 7/31/2019 JavaProgramming_SlideNotes1

    9/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Convert String to Integer

    Converts String containingnumbers to numerical data toperform arithmetic calculation.

    Use teIntegerclass, which is partof thejava.lang package.

    parseInt() method from the

    Integerclass is used to convertString to integer.

  • 7/31/2019 JavaProgramming_SlideNotes1

    10/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Convert String to Real

    Use the Double class, which ispart of thejava.lang package.

    parseDouble() method from theDouble class is used to convertString to real numbers.

  • 7/31/2019 JavaProgramming_SlideNotes1

    11/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Programming Exercises

    Q1) Write a Java program to capture theusers name and age and displays thename and converts the age to aninteger and displays the age with ameaningful message.

  • 7/31/2019 JavaProgramming_SlideNotes1

    12/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Control Structures

    Objectives To write Selection statements

    To write Iteration statements

    Programming Exercises

  • 7/31/2019 JavaProgramming_SlideNotes1

    13/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Selection Statements

    Based on the user input, decision making willtake place.

    It involves choosing between two alternativecourses of action.

    Java supports two kinds of selection statements

    The if, if..else or nested ifstatement

    Theswitch statement

  • 7/31/2019 JavaProgramming_SlideNotes1

    14/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Selection Statements

    The ifstructure

    A single-alternate ifas it only performs anaction based on one alternative.

    The if..else structure A dual-alternate ifas it requires two

    options for the next course of action.

    Performs one action when a booleanexpression evaluates as true and another

    when the boolean expression evaluates asfalse.

  • 7/31/2019 JavaProgramming_SlideNotes1

    15/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Selection Statements

    The Nested ifstructure

    An ifwithin another ifstatement.

    Nested ifstatements are useful

    when two conditions must be metbefore some action is taken.

    Theswitch structure

    Another alternative for nested if

    statements. Useful when testing single variable

    against a series of exact integer orcharacter values.

  • 7/31/2019 JavaProgramming_SlideNotes1

    16/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example ofif statement#1

    class SampleIf{

    public static void main(String[] test) {

    int number = 5;

    if(number > 0){

    System.out.println(The number is positive.);

    }

    else

    System.out.println(The number is negative.);

    }}

  • 7/31/2019 JavaProgramming_SlideNotes1

    17/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example ofif statement#2

    class SampleIf {

    public static void main(String args[]) {

    System.out.print("Hello ");

    System.out.println(args.length);

    if (args.length > 0) {

    System.out.println(args[0]); }

    if (args.length

  • 7/31/2019 JavaProgramming_SlideNotes1

    18/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example ofswitchstatement

    class SampleIf {

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

    char ans , upans ;

    System.out.println("Enter A or B ");ans = (char) System.in.read();

    upans = Character.toUpperCase(ans);

    switch (upans) {

    case 'A :System.out.println(" First option");

    break;

    case 'B :System.out.println(" Second option");

    break; }

    } }

  • 7/31/2019 JavaProgramming_SlideNotes1

    19/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Iteration Statements

    A structure that allows a block of statementsto be executed repeatedly depending on theevaluation of the boolean expression.

    A loop that never ends is called an endless loop

    or infinite loop.

    A loop within a loop is called a nested loop.

    Java supports three kinds of iterationstatements

    The while loop The do..while loop

    The forloop

  • 7/31/2019 JavaProgramming_SlideNotes1

    20/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Iteration Statements

    The while loop

    Used to repeat a statement or block only ifthe condition is true.

    The do..while loop

    Used when we need to execute the loopbody at least once before checking thecondition.

    like the while loop, it will only repeat astatement or block if the condition is true.

  • 7/31/2019 JavaProgramming_SlideNotes1

    21/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Iteration Statements

    The testing of the condition is placed differently

    while loop test the condition first, thenexecute the body of statements.

    do.. while execute the body of statementsbefore testing the condition.

  • 7/31/2019 JavaProgramming_SlideNotes1

    22/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example ofwhile loop #1class SampleWhile{public static void main(String[] test) {

    int count = 1;

    System.out.println(Good Day!);while (count

  • 7/31/2019 JavaProgramming_SlideNotes1

    23/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example ofwhile loop #2

    class SampleWhile{

    public static void main(String[] test) {

    int num = 1;

    while (num < 5) {System.out.print(num*2);

    num = num + 1;

    }

    }

    }

  • 7/31/2019 JavaProgramming_SlideNotes1

    24/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example ofdo-while loop #1

    class SampleDoWhile{public static void main(String[] test) {

    int count = 1;

    System.out.println(Good Day!);do {System.out.print(INFORMATICS);count = count + 1;

    } while (count

  • 7/31/2019 JavaProgramming_SlideNotes1

    25/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example ofdo-while loop #2

    class SampleDoWhile{

    public static void main(String[] test) {

    int num = 1;

    do {System.out.print(num*2);

    num = num + 1;

    } while (num < 5) ;

    }

    }

  • 7/31/2019 JavaProgramming_SlideNotes1

    26/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Iteration Statements

    The forloop

    Allows the statements in the loop body tobe executed a specific number of times.

  • 7/31/2019 JavaProgramming_SlideNotes1

    27/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example offor loop #1

    class SampleFor{

    public static void main(String[] test) {

    System.out.println(Good Day!);

    for(int count = 1; count

  • 7/31/2019 JavaProgramming_SlideNotes1

    28/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example offor loop #2class SampleFor {public static void main(String[] test) {

    System.out.println(ASCII");

    for (int count = 0 ; count

  • 7/31/2019 JavaProgramming_SlideNotes1

    29/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Example offor loop #3class SampleFor {

    public static void main(String[] test) {

    System.out.println("Graphics ");

    for (int x = 1; x

  • 7/31/2019 JavaProgramming_SlideNotes1

    30/30

    CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

    Programming Exercises

    Write a Java program to capture the users nameand age

    and display a message based on the followinginformation:

    if age > 65 display retired

    if age is between 22 to 65 display working

    if age is between 7 to 21 display studying

    if age < 7 display playing

    The user will be prompted to enter another nameand age.

    If the user decides not to enter another name, the

    program will terminate.