Computer Science 15-110, Lecture 9 (Sections M-Q), Fall 2009
Homework 5
Due:  Tue 13-Oct-2009 at 10:00pm (email copy to your CA)
(no late submissions accepted).


Read these instructions first!



Programming guidelines:

  1. count
  2. hasDuplicates
  3. removeOdds
  4. reverse
  5. sumOfDiceOdds
  6. Array-based Animation

  1. count
    In the file Hw5.java (which you should first download from here), write the method:
       public static int count(int[] a, int x)
    This method takes an int array "a" and an int "x", and returns the number of times the value "x" occurs in the array "a". The method returns 0 if "x" does not occur, or if "a" is null.
     
  2. hasDuplicates
    In the file Hw5.java, write the method:
       public static boolean hasDuplicates(int[] a)
    This method takes an int array "a" and returns true if that array contains any duplicates -- that is, if any numbers occur more than once in the array -- and false otherwise. The method returns false if "a" is null.
    Note that you do not have to be especially efficient in your solution here. For example, it is acceptable to solve this using the "count" method you just wrote in some reasonably straightforward manner, even though that may not be the most efficient approach.
     
  3. removeOdds
    In the file Hw5.java, write the method:
       public static int[] removeOdds(int[] a)
    This method takes an int array "a" and returns a new int array that is the same as "a" but with all the odd values removed, and all the even values occurring in the same order. If "a" is null, the method returns null.
    Hint: you may want to use a helper method named "countOdds".
     
  4. reverse
    In the file Hw5.java, write the method:
       public static int[] reverse(int[] a)
    This method takes an int array "a" and returns a new int array that is the same as "a" but with all the values in reverse order. Note:  in this method, if "a" is null, the method returns an empty array (a non-null array of size zero) rather than a null array (this is less common, but not unheard of, and you should be able to implement and/or use methods using either approach).
     
  5. sumOfDiceOdds
    In the file Hw5.java, write the method:
       public static double sumOfDiceOdds(int dice, int targetSum)
    This method basically confirms the data in this table:  http://wizardofodds.com/gambling/dice2.html.  That table gives the odds of a given sum when rolling various numbers of 6-sided dice.

    Specifically, this method takes two parameters -- the number of 6-sided dice and the desired sum -- and it returns as a double value the probability of rolling that sum with that many dice. The method does this using Monte Carlo methods, using 1 million trials where each trial consists of rolling the given number of 6-sided dice one time and succeeding if they produce the given sum.

    Note: Our test method's version of almostEqual (to compare your results against the expected results) will use a "large" epsilon of 0.001 since Monte Carlo methods are only approximate, and with only one million trials, we will not get very precise results.

    Also note:  given the nature of Monte Carlo methods, there is some small (perhaps very small) chance that your code would "work" and yet occasionally fail a test.  So, if your code "almost always" passes the tests, then consider it correct!
     
  6. Array-based Animation
    In the file Hw5Animation.java (which is not provided for you), write this simple array-based animation:
         Hw5Animation.jar  (also available in Hw5Animation.zip)
    Download and run the demo program (Hw5Animation.jar), which shows how this program should work once it is completed.
    Hint:  the x coordinates of the balls should probably be stored in an instance variable as an array of int's.
    Hint:  the y coordinates of the balls do not need to be stored at all -- they can be computed in paint based on the number of balls and the height of the window.
    Have fun!!!

Carpe diem!