The BASE for JAVA LEARNING

by PRO FE S S OR


QUIZ for SECTION VII

To find an answer, click on the desired question.


QUESTION 1:

Write a program and use the 'if / else' construct to write a program to determine if a given year is leap. (Hint: Using the % modulo operator is useful here.)

Follow the Leap year algorithm:
  • If divisible by 400 is LEAP.
  • Else if divisible by 100 is NOT LEAP.
  • Else if dibisible by 4 is LEAP.
  • Else is NOT LEAP.
  • QUESTION 2:

    Write a program and use the 'for' loop and an array of characters, to populate the English alphabet (Use upper case only).

    QUESTION 3:

    Write a program and use the 'switch' construct to find out if a given letter is a vowel or a consonant. (Use only 5 cases, be concerned only with upper case).

    QUESTION 4:

    Write a program to reverse a 4 digit number, that is, if the number is 3245, the result will be 5423.

    QUESTION 5:

    Write a program and use the 'while' loop to display numbers from 12 down to 7, then skip 6, and then continues from 5 down to 1.

    QUESTION 6:

    Given three integer numbers, write a program that would display the three integers in ascending order.
























    ANSWER to QUESTION 1:

    class Quiz
    { public static void main(String[] args)
    { boolean leapYear = false;
    System.out.print("Enter a year : ");
    int year = Input.enterInt(); // Input.class needed to enter an 'int'
    if (year%400==0) leapYear = true; else if (year%100==0) leapYear = false; else if (year%4==0) leapYear = true; else leapYear = false;
    if (leapYear) System.out.println(year + " is Leap Year."); else System.out.println(year + " is NOT Leap Year.");
    }
    }
    This program requires
    Input.class in same directory to enter an 'int'. Use right mouse button and click on Input.class to download it.

    If you do not want to use the Input.class to enter the 'int' value from the keyboard, you can hard code it: int year = < a_year > ;



    Click your Back Button on your Browser to go back to the QUIZ Section.





















    ANSWER to QUESTION 2:

    class Quiz
    { public static void main(String[] args)
    { char[] alphabet = new char[26];
    char letter = 'A'; // beginning character

    for (int i=0; i < alphabet.length; i++)
    { alphabet[i] = letter++; }
    // for loop above populates the alphabet.

    for (int i=0; i < alphabet.length; i++)
    { System.out.print(alphabet[i] + " "); }
    // for loop above is to display each character of the alphabet.
    }
    }

    We could have had only one for loop, if we include the display method with the first for loop.


    Click your Back Button on your Browser to go back to the QUIZ Section.





















    ANSWER to QUESTION 3:

    class Quiz
    { public static void main(String[] args)
    { boolean vowel = false;
    System.out.print("Enter an alphabet letter (A through Z : ");
    char letter = Input.enterChar(); // Input.class needed to enter a 'char'

    switch(letter)
    { case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U': vowel = true;
    break;
    default: vowel = false;
    break;
    }

    if (vowel) System.out.println(letter + " is a vowel."); else System.out.println(letter + " is a consonant");
    }
    }

    This program requires
    Input.class in same directory to enter a 'char'. Use right mouse button and click on Input.class to download it.

    When executing this program, make certain you enter your vowels upper case as it would interpret the lower case letters as consonants.

    If you do not want to use the Input.class to enter the 'char' from the keyboard, you can hard code it: char letter = < a_letter >;



    Click your Back Button on your Browser to go back to the QUIZ Section.





















    ANSWER to QUESTION 4:

    You do it, and please send answer to Professor V.


    Click your Back Button on your Browser to go back to the QUIZ Section.





















    ANSWER to QUESTION 5:

    class Quiz
    { public static void main(String[] args)
    { int j=13;
    while(j>1)
    { j--;
    if (j==6) continue; System.out.print(j + " ");
    }
    }
    }


    Click your Back Button on your Browser to go back to the QUIZ Section.





















    ANSWER to QUESTION 6:

    class Quiz
    { public static void main(String[] args)
    { int x=0,y=0,z=0 ; // integers used for result

    System.out.print("Enter 1st integer: ");
    int a = Input.enterInt();
    System.out.print("Enter 2nd integer: ");
    int b = Input.enterInt();
    System.out.print("Enter 3rd integer: ");
    int c = Input.enterInt();

    // Integers a, b, c, will be placed in ascending order into integers x, y, z.

    if (a >= b && a >= c)
    { z = a ;
    if ( b > c)
    { x = c ;
    y = b ;
    }
    else
    { x = b ;
    y = c ;
    }
    }
    if ( b >= a && b >= c)
    { z = b ;
    if ( a > c)
    { x = c ;
    y = a ;
    }
    else
    { x = a ;
    y = c ;
    }
    }
    if ( c >= a && c >= b )
    { z = c ;
    if ( a > b)
    { x = b ;
    y = a ;
    }
    else
    { x = a ;
    y = b ;
    }
    }
    System.out.print("Integers in ascending order are: " + x + " " + y + " " + z);
    }
    }

    This program requires
    Input.class in same directory to enter the integers. Use right mouse button and click on Input.class to download it.

    Algorithm used above is not the bubble sort. Bubble sort is a popular method that can be used to arrange numbers in ascending or descending order.

    If you do not want to use the Input.class to enter the integers from the keyboard, you can hard code them: int a = < a_value >; , int b = < a_value > ; , and int c = < a_value > ;






    Click your Back Button on your Browser to go back to the QUIZ Section.





    Click here to go to the JAVA Mini-Course page