Computer Science 15-110, Lecture 9 (Sections M-Q), Fall 2009
Lab 5
Due: Tue 6-Oct-2009 at
10pm (email copy to your CA)
(no late submissions accepted).
Read these instructions first!
| Country | Gold | Silver | Bronze | Total |
| China | 51 | 21 | 28 | 100 |
| USA | 36 | 38 | 36 | 110 |
| Russian Fed. | 23 | 21 | 28 | 72 |
| Great Britain | 19 | 13 | 15 | 47 |
| ... | ... | ... | ... | ... |
The question we will be addressing is: which country won overall?
This depends on how you weight the gold, silver, and bronze medals.
For example, if you only count gold medals (give them a weight of 1 each,
with silvers and bronzes weighted 0 each), then China clearly won. But
if you weight each medal equally (all with a weight of 1, say), then the
United States (USA in the chart) won. There are other weightings, too,
of course. For example, if you think gold medals and silver medals are
each worth 1, but bronze medals are worth -3 (peculiar, yes, but also quite
legal), then there turns out to be a 3-way tie between the Czech Republic,
Poland, and Spain (all with 6 total points by those weights).
Based on this description, write a method, beijingOlympicsWinner, that takes 3 (possibly-negative,
possibly-zero) integers, the weightings for gold, silver, and bronze medals
(in that order), and a fourth parameter -- a String containing the path to
the data file -- and returns a string containing the name of the overall
winning country (exactly as the name appears in the first column). If
there is a tie, then return all the winning country names in a single
string, separated by vertical bars ("|"), ordered in the same order they
appear in the file. So, with the weightings of 1, 1, and -3 from
above, you would return "Spain|Poland|Czech Republic" (as they appear in
that order when listed by total medals, which is how the file is arranged).
Note: when we test your method, we will use a different data file --
same format, but different data.
Hint: you might want to tell the scanner to use a comma as a
delimiter!
Hint: The autograder is very picky. Be sure to name your method
exactly as specified, with exactly the same parameters and return
type!
Pittsburgh, PA
Current Local Conditions at:
Pittsburgh International Airport
Lat: 40.51 N Lon: 80.22 W Elev: 1150 ft
Last Update: 10/02/09, 04:51 PM EDT
Weather: Overcast
Temperature: 58°F (14°C)
Humidity: 90 %
Wind Speed: S 7 MPH
Barometer: 29.66 in. (1005.1 mb)
Dewpoint: 55°F (13°C)
Visibility: 10.00 mi.
Based on this description, write a method, miamiHeat, that takes no
parameters and returns a single integer -- how many degrees Fahrenheit
warmer it is at this time in Miami, FL, as compared to Pittsburgh, PA.
For example, at the same time the data above was read, the temperature in
Miami, FL, was 75 degrees Fahrenheit (sigh...), so the difference is 75-32,
or 43 degrees. So the method would return 43. Of course, this
value changes constantly, and your method must return the current
difference, whatever it is. Also, if Pittsburgh is warmer than Miami
(not likely in the next few months), then the method would return a negative
value.
Note: as should be apparent, to complete this problem you will
have to follow the steps given above to find the URL for Miami's current
weather conditions.
Further note: it is very hard to write a good test method for
real-time varying data. Anything remotely reasonable will be accepted.
Hint: Once again... The autograder is very picky. Be sure to
name your method exactly as specified, with exactly the same
parameters and return type!
Note:
For user responses to the yes/no questions, you can presume the user types
"y" or "n", and you do not have to deal with other cases.
Hint: Remember that your output must exactly match the examples
above. The autograder requires this!
public static void testPi() {
System.out.print("Testing pi... ");
assert(pi(1) == 0);
assert(pi(2) == 1);
assert(pi(3) == 2);
assert(pi(4) == 2);
assert(pi(5) == 3);
assert(pi(100) == 25); // there are 25 primes in the range [2,100]
System.out.println("Passed all tests!");
}
public static void testH() {
System.out.print("Testing H... ");
assert(almostEqual(h(0),0.0));
assert(almostEqual(h(1),1/1.0 )); // h(1) = 1/1
assert(almostEqual(h(2),1/1.0 + 1/2.0 )); // h(2) = 1/1 + 1/2
assert(almostEqual(h(3),1/1.0 + 1/2.0 + 1/3.0)); // h(3) = 1/1 + 1/2 + 1/3
System.out.println("Passed all tests!");
}
Note: here we use "almostEqual" rather than "==" because this
method returns a double.
public static void testEstimatedPi() {
System.out.print("Testing estimatedPi... ");
assert(estimatedPi(100) == 27);
assert(estimatedPi(200) == 46);
assert(estimatedPi(300) == 63);
System.out.println("Passed all tests!");
}
public static void testEstimatedPiError() {
System.out.print("Testing estimatedPiError... ");
assert(estimatedPiError(100) == 2); // pi(100) = 25, estimatedPi(100) = 27
assert(estimatedPiError(200) == 0); // pi(200) = 46, estimatedPi(200) = 46
assert(estimatedPiError(300) == 1); // pi(300) = 62, estimatedPi(300) = 63
assert(estimatedPiError(400) == 1); // pi(400) = 78, estimatedPi(400) = 79
assert(estimatedPiError(500) == 1); // pi(500) = 95, estimatedPi(500) = 94
System.out.println("Passed all tests!");
}
Aside: as you can see, the estimatedPi function is an
amazingly accurate estimation of the pi function. For example, the
estimatedPi function predicts that there should be 94 prime numbers up
to 500, and in fact there are 95 prime numbers in that range. And so we
must conclude that there is some kind of very deep relationship
between the sum of the reciprocals of the integers (1/1 + 1/2 + ...) and
the number of prime numbers. This relationship has been explored in
great detail by many mathematicians over the past few hundred years,
with some of the most important results in modern mathematics relating
to it.


Carpe diem!