tutorial for ict students

Upload: kasun-chamara-perera

Post on 01-Nov-2015

219 views

Category:

Documents


0 download

DESCRIPTION

A simple tutorial for ICT students on java programming

TRANSCRIPT

  • 1

    Tutorial 02

    1. Write the result of the following code segment.

    String str = "Java is cool!";

    System.out.println(str.charAt(2));

    System.out.println(str.charAt(5));

    2. Find the errors in following program written in the file ifElsedemo.java. What will

    be the output after correcting the errors?

    Class ifElsedemo {

    public static void [String() gument] (

    integer testscore = 76;

    char grade=0;

    If (testscore >= 90) {

    grade == A;

    } else if (testscore >= 80) (

    grade == 'B'

    } else if (testscore >= 70) {

    grade == 'C';

    } else if (testscore >= 60) {

    grade == 'D';

    ) Else {

    grade = 'F';

    }

    system.out.println("Grade = " ++ grade);

    }

    }

    3. Write a program called KeyboardScanner to prompt user for an int, a double, and a

    String. The output shall look like (the inputs are shown in bold)

    Enter an integer: 12

    Enter a floating point number: 33.44

    Enter your name: Nuwan

    Hi! Nuwan, the sum of 12 and 33.44 is 45.44

    4. The following is a segment of a program. x = 1;

  • 2

    y = 1;

    if (n>0)

    x = x +1;

    y = y +1;

    What will be the value of x and y if n assumes a value of (a) 1 and (b) 0.

    5. Write a program to input a number and print a message whether the inserted number is an

    even or odd.

    6. A man has found that the following rules enable him to travel to work comfortably in all

    weathers.

    If the barometer indicates stormy he takes his umbrella and wears his overcoat.

    If rainy is indicated then he takes his umbrella and wears his raincoat.

    If change is indicated, he behaves as for dry if it rained yesterday and as for rainy if

    it did not.

    If dry is indicated he simply takes his umbrella.

    If very dry is indicated than he takes neither coat nor umbrella.

    Design and write a program to input the barometer reading and print the type of clothing

    to take.

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