CS21 Week2 Tuesday In-Class Exercises

  1. Create a week2 subdirectory in your cs21 subdirectory, and from your week2 subdirectory, copy over my week2 files:
    % cd                # remember you can use ls and pwd to figure out where you are in the directory tree
    % cd cs21
    % mkdir week2
    % cd week2
    % pwd                # should list /home/yourusername/cs21/week2
    
    % cp ~newhall/public/cs21/week2/* .   
    % ls
      Add.java  Round.java 
    
  2. We will start by opeing up Add.java in vim (or emacs), and looking at it together.
    % vim Add.java 
    

  3. Next, open up Round.java and lets look at that together.

    1. Try compiling and running this code. There is a bug in it. Can you find it? Can you fix it?
    2. Modify the program to sum 10 floats and round to the nearest int. Can your program be easily modified to sum 100 floats? If not, think about how to re-design your solution so that changing the number of floats to sum would be trivial.
    3. Write a program that reads in a list of floats until the user enters the value -1 as a sentinel. At that point, the program should display the sum rounded to the nearest int. Your program should be able to duplicate the following sample execution:
      This program sums a list of floats and 
      rounds their sum to the nearest int.
      Enter -1 to signal the end of the list.
      Enter next value: 95.2
      Enter next value: 100.7
      Enter next value: 34.6 
      Enter next value: -1
      The closest int to their sum is 230