Computer Science 15-100 (Lecture 18), Spring 2009
Homework 5
Due:  Thu 19-Feb-2009 at 11:59pm (email copy) and at Friday's class/recitation (identical physical copy)
(no late submissions accepted).


Read this first:


Read the instructions from hw4 first (changing "hw5" for "hw4" where appropriate).


  1. Hw4 Redux
  2. Beijing Olympics Winner (Read from File)
  3. Miami Heat (Read From Web)
  4. Math/Monte Carlo Methods
    1. Even Number of Sixes
    2. Triangular Cuts
  5. Math/Power Series
    1. Approximate Arctan
    2. Pi from Arctan
  6. Bonus: piFromEuler
  7. Bonus: Kepler's Polygons

  1. Hw4 Redux
    Redo hw4 (for 50% credit on hw5).  Carefully read the instructions above for details.
     
  2. Beijing Olympics Winner (Read from File)
    For this problem, you will analyze data from the Beijing 2008 Olympics by reading it from a file.  First, copy this file to your hard drive: beijingOlympics.txt.  The file contains a comma-separated list of each country that won at least one medal in these Olympics, and the number of gold, silver, and bronze medals they received, as such:
     
    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!
     

  3. Miami Heat (Read From Web)
    For this problem, you will use real-time weather data supplied by the National Weather Service in a format designed for mobile devices.  First, look at this page:  http://mobile.srh.noaa.gov/.  Then, in the "Local Weather Information by:" field, enter "Pittsburgh, PA" (without the quotes) and click Go.  On that next page, click Current Conditions.  At that point, you will be at this site:
       http://mobile.srh.noaa.gov/port_mp_ns.php?select=3&CityName=Pittsburgh&site=PBZ&State=PA&warnzone=PAZ021
    And you will see current weather conditions in Pittsburgh, something like this (where we have highlighted the temperature reading):
    Pittsburgh, PA
    Current Local Conditions at:
    Pittsburgh International Airport
    Lat: 40.51 N   Lon: 80.22 W   Elev: 1150 ft
    Last Update: 02/14/09, 05:51 PM EST
    Weather: Mostly Cloudy
    Temperature: 32°F (0°C)
    Humidity: 79 %
    Wind Speed: NW 10 MPH
    Barometer: 29.97 in. (1016.8 mb)
    Dewpoint: 26°F (-3°C)
    Wind Chill: 24°F (-4°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.
     

  4. Math/Monte Carlo Methods
    1. Even Number of Sixes
      According to this really interesting page from Drexel University's "Ask Dr. Math" Math Forum (http://mathforum.org/library/drmath/view/62874.html), if a six-sided die is thrown n times, then the probability that there were an even number of sixes among those n throws is 1/2 * [1 + (2/3)n].  Fascinating!  For this problem, you will use Monte Carlo methods to empirically confirm this result.

      Write a method, oddsOfAnEvenNumberOfSixes, that takes two integers -- n, the number of throws to make for each trial, and t, the total number of trials to run -- and returns a double value that is the estimate for this problem, which your method shall compute by running t trials, and for each trial throwing n dice, and checking if in fact there were an even number of sixes for that given trial, and finally returning the ratio of the total number of successful trials (those with an even number of sixes) divided by the total number of trials, t.  You may want to re-read that last sentence once or twice!

      Note:  you must not use the exact formula given above for your answer.  You must compute an estimate for the formula using Monte Carlo techniques (trials, etc).

      Note:  that said, you certainly may use the formula for your test method (what else would you do?  it is notoriously difficult to test Monte Carlo methods!).  Even using the formula, though, you have to have some notion of "tolerance", since we're dealing with probabilities and you will not get exact answers.  Of course, the larger the number of trials, the more accurate your answers will be.  In fact, you should play with the number of trials a bit to confirm your method works properly.
       
    2. Triangular Cuts
      Write a method, oddsOfTriangularCuts, that takes no parameters and uses Monte Carlo techniques (trials, etc) to return (as a double) an approximate answer to this problem:  if you take some line segment of length 1 and randomly choose 2 locations on that line segment and cut it at both places to form 3 line segments whose lengths sum to 1, what are the odds that those 3 segments can form a triangle?  Remember that 3 lengths can form a triangle if the sum of each pair of lengths is greater than the third length (this being the so-called "triangle inequality").

      Note:  it is possible to solve this problem exactly using math (of course), but you should not do that here!  Solve it using Monte Carlo methods.

      Hint:  the exact answer is a ratio of fairly small integers.  Your answer must be within 0.001 of the exact answer (so use enough trials so that this will reliably be the case).
       
  5. Math/Power Series
    1. Approximate Arctan
      The Maclaurin series for the arctan function (the inverse of the tangent, where the angle is measured in radians) is a formula which allows us to compute an approximation to arctan(x) as a polynomial in x.  If you do not understand the details, no problem.  You just need to know that the formula is this:
                   arctan(x)  =  x - x3/3 + x5/5 - x7/7 + x9/9 - x11/11 + . . .   (for |x|<=1)

      Based on this, write a method, approximateArctan, that takes two parameters -- a double x and a positive integer k -- and returns a double that is the partial sum from the first k terms in this series.  For example, if x equals 0.5 and k equals 3, your method should compute:
                   arctan(0.5)  ~=  (0.5) - (0.5)3/3 + (0.5)5/5 = 0.464583...
      As it turns out, this is a very good approximation, even with only 3 terms, as arctan(0.5) really equals 0.4636476...

      Note: the series only converges (ie, works) for |x|<=1.  But you do not have to worry about this -- if |x|>1, just solve the problem the same way and return your (bogus) answer.  Our test cases will be limited to |x|<=1.

      Note:  You may not use Math.atan (which computes the exact arctan value) in your method (it wouldn't really be useful there anyhow), but you may use it in your test method if you wish.

      Note:  We would expect this approximation to improve as k increases. Does it?  Try a few different values to see for yourself.
       
    2. Pi from Arctan
      Now we know:
         tangent(pi/4) = 1
      (because pi/4 radians is 45 degrees, and the tangent is basically the slope, and the slope at 45 degrees is 1).  Thus, taking the arctan of both sides:
         pi/4 = arctan(1)

      And finally, multiplying both sides by 4,
         pi = 4*arctan(1)
      Thus, we can use the preceding approximateArctan method to generate an approximate value for pi!  Cool!

      Based on this, write a method, piFromArctan, that takes a positive integer k and returns the result of approximating pi by first approximating the arctan of 1 using k terms (by calling the approximateArctan method you just wrote), and then multiplying this by 4.

      Note:  in your test method, you should confirm that it takes around 100 terms to get your approximation within 0.01 of the actual value of pi, and around 1000 terms to get with 0.001 of the actual value of pi, so it is converging, if rather slowly.
       
  6. Bonus: piFromEuler (smaller bonus)
    Leonard Euler, one of the greatest math minds ever, proved the following:
                 π2 = 6 (1/12 + 1/22 + 1/32 + 1/42 + 1/52 + ... )
    Astonishing!  This is also useful, since it converges to pi faster than the arctan series above.  To demonstrate this, write a method piFromEuler which takes a positive integer k and uses Euler's formula to approximate pi with the first k terms of this sum.  Be sure to multiply by 6 and then take the square root (since Euler's formula finds pi squared, not pi).  Finally, confirm that this converges faster than the arctan method.

     
  7. Bonus: Kepler's Polygons (bigger bonus)
    Among his lesser-known exploits, the great German mathematician and astronomer Johannes Kepler studied how polygons can tile the plane or portions of the plane.  For a fair bit of bonus, in a file named Hw5BonusKeplerPolygon.java, paint the following figure of Kepler's:

    Do not be put off by the apparent complexity.  Yes, it requires a clever combination of loops and trigonometry (sine and cosine), but you can do this!  At least you can do it well enough to get a fair bit of bonus for your effort.  Give it a try!!!!

    If you finish that shape, then -- for beaucoup bonus -- you can try one of these more complex shapes that Kepler also devised:

    Enjoy!!!

Carpe diem!