COS 126 Conditionals, Loops, Arrays |
Programming Assignment |
The goal of this assignment is to write five short Java programs to gain practice with loops, conditionals, and arrays.
% java Bits 0 % java Bits 8 0 4 % java Bits 1 % java Bits 16 1 5 % java Bits 2 % java Bits 1000 2 10 % java Bits 4 % java Bits -23 3 Illegal input
Remark: This computes the number of bits in the binary representation of N, which also equals 1 + floor(log2 N) when N is positive. This quantity arises in information theory and the analysis of algorithms.
% java Checkerboard 4 % java Checkerboard 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
% java BoyAndGirl Congratulations! You have 3 children. % java BoyAndGirl Congratulations! You have 6 children. % java BoyAndGirl Congratulations! You have 2 children.
java BoysAndGirls 2 Average: 6.0 children to get at least one of each sex. Number of families with 2 children: 0 Number of families with 3 children: 0 Number of families with 4 children: 0 Number of families with 5 or more children: 2 Most common number of children is 5 or more. % java BoysAndGirls 10 Average: 3.5 children to get at least one of each sex. Number of families with 2 children: 2 Number of families with 3 children: 3 Number of families with 4 children: 3 Number of families with 5 or more children: 2 Most common number of children is 3. % java BoysAndGirls 100 Average: 3.19 children to get at least one of each sex. Number of families with 2 children: 44 Number of families with 3 children: 24 Number of families with 4 children: 16 Number of families with 5 or more children: 16 Most common number of children is 2.
As T increases, we expect the average number of children per family to converge. Use BoysAndGirls to formulate a hypothesis as to what this average is. Run BoysAndGirls with T = 1, 10, 100, 100000 and 1000000 to watch it converge to a sufficiently accurate estimate.
Remark: a classic result from probability theory (central limit theorem) asserts that the shape of the resulting histogram is well-approximated by the ubiquitous bell curve (Gaussian distribution).
% java TenDice 1000 10: 11: 12: 13: 14: 15: 16: 17: 18: * 19: **** 20: 21: *** 22: ****** 23: ******** 24: **************** 25: ************* 26: ********** 27: ********************************* 28: **************************************** 29: ********************************* 30: *************************************************** 31: ***************************************************************** 32: ******************************************************** 33: ************************************************************************************** 34: *********************************************************** 35: ********************************************************************* 36: *********************************************************************************** 37: ************************************************************** 38: ***************************************************************** 39: *************************************** 40: ***************************************************** 41: ************************************ 42: **************************** 43: ************************ 44: ************************ 45: ********* 46: *********** 47: ******* 48: *** 49: ** 50: 51: 52: * 53: 54: 55: 56: 57: 58: 59: 60:
Submission.
Submit the files Bits.java, Checkerboard.java,
BoyAndGirl.java, BoysAndGirls.java, and TenDice.java.
Finally, submit a readme.txt
file and answer the questions.