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!
- The hw instructions from hw1 all apply here.
In particular:
- This is not a lab and this is not
collaborative!
- You must work alone on this assignment
(except for help from CA's or the course instructor, who you are always
welcome to consult).
- Submit all your source (.java) file(s) all in a single zipped folder,
hw5-<andrewId>.zip, sent by email as an attachment to your CA. For
example, I would submit hw5-koz.zip (you should replace "koz" with your
andrewId).
- Do not submit .java~ files (with the tilde
(~) at the end). These are DrJava backup files. They are not
useful.
- Do not submit .class files. These are
compiled bytecode files. They also are not useful.
- Do not submit project files or any other
files. Just .java files
and your one free-response file.
Programming guidelines:
- Style counts!!! Use well-named variables,
proper indenting, reasonable commenting, etc.
- Write test methods for every non-graphical
method you write!!!
- You may now use any Java techniques as
appropriate. That said, the point of this assignment is to
study loops and arrays (and Monte Carlo methods), so you should expect to
use loops and arrays in nearly every solution (other approaches may work, but they
wouldn't be as appropriate, and so would not receive full credit).
- Start by copying this file:
Hw5.java
- For each method in Hw5.java, a test method is
already provided for you Even so, you should add any additional test
cases that you think may be needed for completeness.
- count
- hasDuplicates
- removeOdds
- reverse
- sumOfDiceOdds
- Array-based Animation
- 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.
- 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.
- 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".
- 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).
- 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!
- 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!