15-110 FALL 2009 (CORTINA)

HOMEWORK 2 - due Friday, September 11, 2009 in class

Please remember that homework must be handed in on time. No late homework will be accepted.

Review the readings from chapters 2 and 3 of your textbook and answer the following questions based on your textbook and the course lecture material.

  1. (1 pt) Let the variables a and b represent two positive integers. Show how to print out the value of c from Pythagorean's Theorem below:

    c2 = a2 + b2

    Will the output be an int or a double? Explain.

  2. (1 pt) Let s be a variable representing a String. Suppose we want to output the length of the string s. Explain what is wrong with the following instruction and correct it.

    System.out.println(String.length(s));

  3. (2 pts) Let String months = "JanFebMarAprMayJunJulAugSepOctNovDec". Show how to use the String variable months and the methods of the String class to print out the strings below. For example: If the desired output is J, we could write the command: System.out.println(months.charAt(0));

    1. Jan

    2. Dec

    3. j*nfebm*r*prm*yjunjul*ugsepoctnovdec

    4. JUNJULNOVDEC

  4. (2 pts) Using the random method of the Math class, show how to store a psuedo-random number into the variable number that satisfies each of the following descriptions:

    1. A random floating point value in the range [0,50) (i.e. the range is between 0 and 50, including 0 but excluding 50).

    2. A random floating point value in the range [100,200).

    3. A random integer from the set {0, 1, 2, ... , 15}.

    4. A random integer from the set {8, 10, 12, 14, 16, 18}.

    (You may assume that number is already declared to be of the correct data type.)

  5. (1 pt)

    Consider the following Java code fragment:

    Scanner scan = new Scanner(System.in); 
    System.out.print("Input your birthdate day: "); 
    int day = scan.nextInt(); 
    System.out.print("Input your birthdate month: "); 
    String month = scan.nextLine(); 
    System.out.println("Happy birthday on " + month + " " + day); 
    

    When this code is executed, the user never gets a chance to enter the birthdate month as shown below (user input is in italics). Why? Correct the code so the user can enter the day as an int and then the month as a String in that order successfully.

    Input your birthdate day: 24 
    Input your birthdate month: Happy birthday on  24 
    

    HINT: Try the Java code on the computer and when prompted for the day, enter
    24 Purple Monkey Dishwasher
    and watch what happens.

  6. (1 pt) What is the meaning of the following import statement? When do you need to use it?

    import java.util.*;

  7. (2 pts) A website assigns a default password for a new user based on the user's first and last name. The default password is the first letter of the user's first name followed by up to the first 7 letters of the user's last name. If the user's last name has less than 7 letters, then additional dollar signs ($) are added to bring the last name up to 7 characters. For example, the following users would have these passwords:

    NAME                    WEBSITE PASSWORD 
    homer simpson           hsimpson 
    apu nahasapeemapetilon  anahasap 
    montgomery burns        mburns$$
    lionel hutz             lhutz$$$
    

    Write a simple Java program with one class named PasswordGenerator. It should have a main method that prompts for the user's first name and the user's last name, and prints out the user's password in lowercase letters. You may assume that the user's input will be entirely lowercase letters and each name has no spaces or other punctuation. You may also assume that each input name has at least one letter. A sample run of the program might be (user input is in italics):

    What is your first name? homer 
    What is your last name? simpson 
    YOUR WEBSITE PASSWORD IS: hsimpson
    

    HINT: Concatenate 6 dollar signs on to the last name before you display the first 7 characters of the last name.

    NOTE: You do not have to write this program in Eclipse, although you may do so, of course. Keep in mind that you will be required to write Java code on your written exams when you won't have the compiler to correct your errors. So you should try to learn to write some code by hand and work through the process on paper. Most professional programmers spend a good deal of time planning out their program code on paper before they go to the keyboard to type it in and test it.