Homework 1
CS202, Summer 2004
Due: Thursday, July 1
Please email your solutions to csun@calstatela.edu,
and make sure to use CS202 Summer 04
HW1 as the subject of the email. The email should include your
name, and the .java files for all the exercises as attachments. For all homework assignments and projects, failing to comply with the required
code conventions (see Review of Language Basics, slide #7) will incur a
credit penalty up to 10% of the total credit for the assignment.
1. The number of different combinations that can be made by choosing m objects out of n objects is usually denoted as C(n,m), and can be calculated with the formula C(n,m) = n! / ( m! * (n-m)! ). For example, C(4,2) = 4! / (2! * (4-2)!) = 4 * 3 * 2 * 1 / ( 2 * 1 * 2 * 1 ) = 6. Write a Java program which does the following:
- (10pt) get two positive integer numbers n and m from user input. You may use either ConsoleReader or JOptionPane for input.
- (15pt) check whether the input is correct, namely, whether both numbers are greater than 0 and n is no less than m.
If the input is not correct, output an error message, and ask the user
to re-enter the two numbers. Repeat this process if necessary until
the program gets the right input.
- (15pt) implements a method to calculate factorial, and use this method to calculate C(n,m)
- (5pt) output the result in the form "C(n,m) = value", for example, C(4,2) = 6 if n is 4 and m is 2.
2. (25pt) Implement a recursive binary search method, and write a Java program to test your method.