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!
- Be sure to include your name, your Andrew ID, and your section clearly on the top of each file in your assignment.
- Also on the top of each file, include a timesheet logging all
the time you spent on that part of the assignment.
You will not be graded on your number of hours, but this information
will be helpful to the course staff.
- For non-programming problems
- Place all your solutions to the non-programming
problems in a single file named Hw9 (with whatever extension is appropriate
for the format you choose, such as Hw9.txt or Hw9.html, etc). You must
use a one of these file formats: plain text (txt), or RTF, or HTML, or
Word (doc, not docx), or PDF. No other file formats will be accepted.
- Show your work. Correct
answers without supporting calculations will not receive full credit.
- For programming problems:
- Place each solution in its own file named exactly as given below, and
with a class name that exactly matches the file name. So if the file
name is Hw9Foo.java, the main class in that file must be Hw9Foo.
- Use well-named variables, proper indenting, reasonable commenting,
etc.
- What to submit
- Either one zip file, Hw9.zip, containing all your files (this is
preferred), or all the files as attachments to a single email (do not send
one email per file!). It is recommended
that you "cc" yourself in that email, too, just to confirm that you properly
sent the email.
- ch2Exercises
- ch3Exercises
- SCALES Practice
- FootballStandings
- Mystery Methods
- Tetris (Part 1 of 2)
- 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
- 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).
- SCALES Practice
- 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:
- 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.
- 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.
- 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.
- 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!
- Mystery Methods
In the written portion of your submission, answer the following
questions in general, and in just a few words of plain
English.
- 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;
}
- 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();
}
}
- 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;
}
- 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;
}
- 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.
- 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!