Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes:  Review Methods, If Statements, and For Loops


Logistics

  1. Schedule
    1. Quiz 4 returned today
    2. Hw6 due tomorrow
    3. Quiz 5 on Tuesday
  2. Reading:
    1. No new reading for today

Topic Outline:

  1. Some misconceptions on Quiz 4
    1. Scanning input rather than just using a method's parameter
    2. Printing output rather than just returning the method's result
    3. How to use String.format and/or System.out.printf
    4. The difference between String.format and System.out.printf (aka System.out.format)
    5. How to include a newline in a String
    6. How to handle the null case
    7. How to round to the nearest 10th, 100th, ...
    8. How to alternate signs in a sum (covered via email)
       
  2. Practice

    You should be able to write each of these methods using only "for" loops, then only "while" loops, then only "do" loops.  Of course, some of these may be a nuisance, as not every loop is a good fit for every problem.
     
    1. Write a method that takes two ints, r and c, and returns a String that, when printed, would print out an r-by-c rectangle of asterisks, where there are r rows and c columns.  If either of r or c is non-positive, the method should return null.
       
    2. Write a method that takes two ints, r and c, and returns nothing, instead directly printing out an isosceles triangle pointing to the [left, right, up, or down], so that the triangle fills an entire r-by-c rectangle, where r are the rows and c are the columns.  If either r or c is non-positive, the method should do nothing.
       
    3. Write a method that takes a string which is a file name, and returns nothing, instead printing out all the strings that occur more than once in the given file.  Note:  this method will have to be declared with "throws Exception" so that it can create the scanner to access the given file.
       
    4. Write a program that creates a hypocycloid (as in the Steelers insignia) by drawing coordinate axes, placing n points on each axis in each quadrant, then drawing lines connecting the points in reverse order between each quadrant, so, for example, the ith point from the left in on the positive X axis will be connected to the ith point from the bottom of the positive Y axis.  The larger n gets, the closer this gets to a hypocycloid.
       
  3. More Practice

    What will the following code fragments print out? Explain each of your answers.
    (a)  int i = 0, j = 0;
         while (i <= 10) {
             j++;
             i++;
         }
         System.out.println(i + "," + j);
    
    (b)  int i = 0, j = 0;
         while (i++ <= 10) {
             j++;
             i++;
         }
         System.out.println(i + "," + j);
    
    (c)  int i = 0, j = 0;
         do {
             j++;
             i++;
         } while (i <= 10);
         System.out.println(i + "," + j);
    
    (d)  int i = 0, j = 0;
         do {
             j++;
             i++;
         } while (i++ <= 10);
         System.out.println(i + "," + j);
    
    (e)  int i = 0, j = 0;
         do {
             j++;
             i++;
         } while (++i <= 10);
         System.out.println(i + "," + j);
    
    (f)  int i = 0, j = 0;
         for (i = 1; i <= 20; i += i)
             j++;
         System.out.println(i + "," + j);
    
    (g)  int i = 0, j = 0, k = 0;
         for (i = 1; i <= 20 ; i += 5) {
             for (j = 3; j <= i; j += 3) {
                 k++;
             }
         }
         System.out.println(i + "," + j + "," + k);
    

  • carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem