RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw11
loops
exam re-do; short-answer, short-programs

All work due Apr.16 (Wed).
  1. Re-do of the problems you missed on exam2:

  2. (3pts) What is the syntax of a while loop?
                     (                  ) {
                       
      }
    
  3. (1pt) true or false?: even if a return statement is used inside a for-each loop, every element of the list is processed.
  4. (2pts) What does the following code fragment print?
    (Hint: walk through the code by hand, keeping track of what each variable is. Your answer will be several lines long.)
    int i = 2;
    int ssf = 0;
    while (i < 10) {
      ssf = ssf + i;
      System.out.println( "i=" + i + "; ssf = " + ssf );
      i = i+3;
      }
    
  5. For this problem, assume that deadlines is of type java.util.LinkedList<Date>, and refers to a non-empty list.

          Date mysterySoFar = deadlines.get(0);
          int i = 0;
          while (i < deadlines.size() ) {
            Date d = deadlines.get(i);
            if (d.comesBefore(mysterySoFar)) {
              mysterySoFar = d;
              }
            i = i+1;
            }
        

    1. (2pts) Suppose that deadlines contained four Dates in the following order: new Date( 2007, 1, 1), new Date( 2008, 1, 15), new Date( 1988, 9, 10), new Date( 2008, 1, 15). After the loop runs, what will mysterySoFar be equals to?
    2. (3pts) In general, what does the loop compute? (4-5 words of English.) What would be a more descriptive name for mysterySoFar?

    3. (4pts) Rewrite the fragment to use a for-each loop.

                
      
      
      
      
      
      
                  

  6. (2pts) What does the following code fragment print?
    int ssf = 0;
    for ( int i=0;  i < 12; i = i+4 ) {
      ssf = ssf + i;
      System.out.println( "i=" + i + "; ssf = " + ssf );
      }
    
    (We will talk about for loops in Monday's lecture. This is not a for-each like we've used previously!)
  7. (4pts) Write a loop to compute 1³+2³+3³+...+1000³.
    (This is simpler than computing the number of oranges in any sort of pyramid.)

For the remaining questions, you'll type in methods, and submit in your program-source only. Javadoc is required (although most of it is provided), but test cases are not, beyond what is indicated. (That's because test cases for random methods are inherently problematic.)

  1. (6pts) Implement the following static method:
      /** Return a String concatenated to itself multiple times.
       * @param n the number of copies to make.
       * @param word the String to copy.
       * @param a String containing `n` copies of `word`.
       * Examples:
       *     Linguistics.stringTimes( 3, "ho" ) = "hohoho"
       *     Linguistics.stringTimes( 1, "unary" ) = "unary"
       *     Linguistics.stringTimes( 0, "turkey" ) =                 
       *     Linguistics.stringTimes( 4, "" ) =                 
       */
      public static String stringTimes( int n, String word ) {
        
    
    
        
    
    
            
        }
        
    Hint, for figuring out your accumulator variable: If somebody calls stringTimes( 1000, "hi" ), what partial result will you have built up after going through the loop 500 times? (It should be the same type as the final answer you're trying to compute!) How will you update that value the 501st time through?
  2. (4pts) Write a method randLetter which returns a random char between 'a' to 'z' inclusive1 (You can use class Die from previous labs, or you can just use java.util.Random methods directly.)

      /** Return a random lower-case letter.
       * @return a random lower-case letter.
       */
      char randLetter() {
        // your code here
        }
      

    Hint: the expression ((char) ('a'+2)) evaluates to 'c'. 2 One can find various tables which list Java's notion for equating characters with numbers.

    How can you tell if your function is working correctly? You might try calling it 50 times, but if you don't see a 'z' maybe that's just slightly bad luck. So, a better way to test this method is to replace the number 26 with something like 3, and make sure that repeated calls do eventually return each of {'a', 'b', 'c'}, but never 'd'.
    (You did remember to used a named-constant instead of 26, right?)

  3. If you call randLetter 26000 times, one would expect that the letter 'z' gets returned 1000 times.

    Confirm this experimentally:

  4. (4pts) Write a method which returns a random “word”, like "bzxtq", each time it is called. The word should be between 3 and 7 characters long (inclusive, with each length being equally-likely).
      /** Return a random 'word' (a string of letters, probably gibberish).
       * @return a random 'word' (a string of letters, probably gibberish).
       */
      
  5. Extra credit: Experimentally, what is the approximate probability that a random word contains 1 or more 'z's? Show the code you used to make this estimate. (If you have experience with probability, you can try to derive the exact answer.)
  6. (4pts) Write a method which returns a list of n random “words” (for any given n).
      /** Return a new list containing n 'words' (strings of letters).
       * @param n The number of words in the returned list.
       * @return a new list containing n 'words' (strings of letters).
       */
      

1In Java, char literals are enclosed in single-quotes. Double-quotes are only for Strings; a char is not considered the same as String-of-length-one.      

2This is an odd expression. Before adding two, Java is auto-converting 'a' into a numeric value. Then, we are explicitly casting the result with “(char)”. (Remember that in casting, the parentheses around the type are required; it is the only time we'll ever mention a type in Java that is not part of some declaration (declaring a local variable, parameter, field, or signature).)      

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Apr.15 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme