CompSci 100e Java Intro Exercises

Group members: name and NetID for each (max 3/group)

___________________     ___________________


___________________
  1. Give an algorithm to read a file and print the words together with counts of how often they occur.
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  2. Consider the following code from SimpleWordCount:
    import java.io.*;
    import java.util.*;
    
    
    public class SimpleWordCount {
        public static void main(String[] args) throws FileNotFoundException
        {
            Scanner s = new Scanner(new File("test.txt"));
            String text = s.useDelimiter("\\Z").next();
            String[] words = text.split("\\s+");
            Arrays.sort(words);
            
            String lastUnique = words[0];
            int count = 1;
            
            for (int k = 1; k < words.length; k=k+1)
            {
                String current = words[k];
                if (current.equals(lastUnique))
                { // same as previous one
                    count = count + 1;
                }
                else
                { // new word
                    System.out.println(lastUnique + "\t"+ count);
                    lastUnique = current;
                    count = 1;
                }
            }
            System.out.println(lastUnique + "\t"+ count);
            
        }
    }
    

    Questions? What does this code do?

    
    
    
    
    
    
  3. Do JavaBat problems
    1. sumDouble
    2. intMax
    3. arrayCount9
    4. arrayFront9
  4. From Ed Felten's Freedom to Tinker Blog:

    On December 31, 2008, some models of the Zune, Microsoft's portable music player, went dark. The devices were unusable until the following day. Failures like this are sometimes caused by complex chains of mishaps, but this particular one is due to a single programming error that is reasonably easy to understand. Let's take a look.

    Here is the offending code (reformatted slightly), in the part of the Zune's software that handles dates and times:

    year = 1980;
    
    while (days > 365) {
        if (IsLeapYear(year))  {
            if (days > 366)  {
                days -= 366;
                year += 1;
            }
         } else {
            days -= 365;
            year += 1;
        }
    }
    

    At the beginning of this code, the variable days is the number of days that have elapsed since January 1, 1980. Given this information, the code is supposed to figure out (a) what year it is, and (b) how many days have elapsed since January 1 of the current year. (Footnote for pedants: here "elapsed since" actually means "elapsed including", so that days=1 on January 1, 1980.)

    1. On December 31, 2008, days was equal to 10592. That is, 10592 days had passed since January 1, 1980. It follows that 10226 days had passed since January 1, 1981. Why?
      
      
      
    2. How does this code then calculate the correct year?
      
      
      
      
    3. What happened on December 31, 2008?
      
      
      
      
      

Jeffrey R.N. Forbes
Last modified: Wed Aug 26 16:06:28 EDT 2009