Computer Science 15-100 (Sections S-V), Fall 2008
Homework 9
Due:  Thu 6-Nov-2008 at 11:59pm (email copy and identical physical copy)
(no late submissions accepted).


Read these instructions first!


  1. ch2Exercises
  2. ch3Exercises
  3. SCALES Practice
    1. FootballStandings
    2. Mystery Methods
  4. Tetris (Part 1 of 2)

  1. ch2Exercises
    Read and study Chapter 2.  Then, do the following Exercises from Chapter 2 (pp 106-108):
    Exercises 2.5, 2.9, 2.17, 2.18
     
  2. ch3Exercises
    Read and study Chapter 3, sections 3.1 through 3.8 (note:  you are not responsible for DecimalFormat in section 3.6, nor any material in sections 3.9 through 3.11, though you may find it interesting reading in any case).  Then, do the following Exercises from Chapter 3 (pp 154-157):
    Exercises 3.4, 3.5, 3.6, 3.7ace (skip 3.7bdf).
     
  3. SCALES Practice
    1. FootballStandings
      In a file named Hw9FootballStandings.java, write a program that reads data from this ESPN web page:
         http://mobileapp.espn.go.com/nfl/mp/redesign/standings?markupType=XHTML&conference=afc
      This page contains the current football standings in the AFC (the Steelers conference).  For example, as of today, the page looks like this (just the part we care about is shown):

      The standings are listed by division (East, North, South, West), where each row contains a team identifier (where PIT is the Pittsburgh Steelers, for example) followed by that team's wins (W), losses( L), and ties (T).  You can ignore GB (which means "Games Behind", or how far out of first place a team is in its division).

      To solve this problem, your program must do the following:
       
      1. Create a Java class called FootballTeam (in the Hw9FootballStandingsFile, placed after the Hw9FootballStandings class).  This class must store the team's 3-letter identifier (like "PIT"), its wins, its losses, and its ties.  You may ignore games-behind, as noted.
         
      2. In the Hw9FootballStandings class, create the following method:
           public static FootballTeam[] getCurrentAfcStandings()
        This method that takes no parameters and returns an array of type FootballTeam[], containing the current standings of the 16 teams in the AFC (as listed on that ESPN web page).  To do this, the method first downloads the current AFC standings from the web page above, and creates 16 instances of the FootballTeam class, one for each team listed on the web page, and store these instances in the "standings" array.  Next, the method must sort the standings array by calling Arrays.sort on it.  You must define the FootballTeam class so that teams are sorted by their winning percentage (wins divided by the sum of wins and losses, where ties are ignored for this calculation), with ties in the standings decided alphabetically.  So, for example, NYJ, BAL, and DEN all have 4 wins and 3 losses, so they would be sorted alphabetically as BAL, DEN, NYJ.
         
      3. In the Hw9FootballStandings class, create the following method:
           public static void printCurrentAfcStandings()
        This method (which takes no parameters and returns nothing) calls getCurrentAfcStandings and then prints out the results in a tabular format, adding one more column, "PLACE", which lists where in the standings each team occurs, where tied teams all have the same place and only the first of each set of tied teams actually displays its place.  For example, here is the output for the today's data as given above.
        ----------------------------
        CURRENT AFC STANDINGS
        PLACE TEAM W  L  T
          1   TEN  7  0  0
          2   BUF  5  2  0
              NWE  5  2  0
              PIT  5  2  0
          5   BAL  4  3  0
              DEN  4  3  0
              NYJ  4  3  0
          8   CLE  3  4  0
              HOU  3  4  0
              IND  3  4  0
              JAC  3  4  0
              MIA  3  4  0
         13   SDG  3  5  0
         14   OAK  2  5  0
         15   KAN  1  6  0
         16   CIN  0  8  0
        ----------------------------
        Note that BAL, DEN, and NYJ are all tied in 5th place, yet only BAL displays 5 in the PLACE column.  Also, note that with 3 teams all tied in 5th place, there is no 6th or 7th place, and so the next team (or teams, in this case) would be in 8th place.
         
      4. Have your main method simply call printCurrentAfcStandings, so any time you want to know the current AFC standings, you merely need to run this program!
         
    2. Mystery Methods
      In the written portion of your submission, answer the following questions in general, and in just a few words of plain English.
       
      1. In general, when will the following method return true?
          public static boolean f(int[] a, int[] b) {
            if ((a == null) || (b == null) || (a.length != b.length))
              return false;
            int n = a.length;
            for (int i=0; i<n; i++) {
              boolean ok = true;
              for (int j=0; j<n; j++)
                ok = ok && (a[j] == b[(i+j)%n]);
              if (ok)
                return true;
            }
            return false;
          }
      2. In general, what does the following method do?
          public static void g(int h) {
            if (h % 2 == 0) h--;
            for (int i=0; i<h; i++) {
              int s = Math.abs(h/2-i);
              for (int j=0; j<h-s; j++)
                System.out.print((j < s) ? " " : "*");
              System.out.println();
            }
          }
        
      3. In general, what does the following method do?
          public static char[][] h(String s, int n) {
            char[][] result = new char[n][n];
            for (int i=0; i<n*n; i++)
              result[i/n][i%n] = s.charAt(i%s.length());
            return result;
          }
      4. In general, what does the following method do?
          public static double m(double d) {
            double q = 0;
            double r = 0.0001;
            double t = Math.abs(d);
            double s = t/2;
            while (Math.abs(d-s*s)>r) {
              if (s*s>d) t=s;
              else q=s;
              s = (q+t)/2;
            }
            return s;
          }
  4. Tetris (Part 1 of 2)
    Follow steps 1-4 in the notes for "Tetris for Intro/Intermediate Programmers".  Place the results in a file named Hw9Tetris.java.  You must follow the design spec as described in the document.  You should also add test methods wherever they are appropriate.

    Note:  we are only completing the first half of Tetris this week.  Next week, we will finish Tetris.  Your submission this week should do the following:
      * Create and draw the empty board
      * Create and draw the "falling piece"
      * Choose the falling piece randomly from the 7 legal Tetris piece types
      * Paint the falling piece in the appropriate color for that piece type
      * Move the falling piece left/right/down
      * Not allow the falling piece to move illegally
      * Replace the falling piece with a new falling piece, centered at the top,
         in response to keypresses besides left/right/down
      * Provide test methods as appropriate.

    Note that you should not submit a solution that goes beyond step 4 this week, even if you continue beyond that point (which is encouraged).  In particular, your submission for Hw9Tetris should not rotate the falling piece, and definitely should not drop the piece or respond to timer events.  That is for next week.
     
  5. Bonus
    There is no bonus this week.  Instead, you are encouraged to forge ahead with Tetris, seeing as only parts 1-4 are assigned this week and parts 5-8 will be assigned next week.  Part 8, in particular provides ample bonus opportunities.  However, again, if you do this (and you should!), be sure to submit just those portions that are due this week!

Carpe diem!