Claremont McKenna College Computer Science CS 51 Handout 4: Problem Set 4 February 10, 2011 This problem set is due 11:50pm on Wednesday, February 16. As usual, you may hand in yours until I make my solutions available. To solve the problems below, you will be implementing classes. There is plenty of room for creativity in this course, but when I specify something, follow the specification exactly. It makes grading much easier for us. Each method that you write must be properly commented and attractively formatted. Be sure to include a comment at the top of each file submitted that gives your name, your student ID number, and your email address. Your submission should eventually include the following (do not hand in.class files or any other that I did not ask for.): Date.java Digits.java Pascal.java Slot.java Follow the instructions on how to submit your homework electronically which you can find on our class web page.
A Piece of Advice This is what I said in the FAQ for a problem set around this time of the semester the last time I taught this class. It might be helpful for you to read. I can see what you are doing. You are in a hurry to finish these programs. So, you try to write the whole program that would solve the entire problem quickly. Think again. Remember I have been telling you to solve them incrementally? Try a partial solution that works and little by little improve it toward the ultimate solution. I see these long programs that do not work that you are sending to me by email and ask me to take a look and tell you why they are not working. There are so many problems with these programs that you are sending to me that it s hard to know where to begin. Waiting until the last minute to solve these problems isn t a good strategy and problems in the future assignments will not get any easier. Those who are keeping up the pace with the class seem to be enjoying the class. We are just getting into the phase where we start experiencing some exciting programming. Try to take advantage and enjoy! I was going to add the diamond problem that we talked about in class, but decided not to. However, I still recommend that you complete the Tree.java program as an exercise. Start early - don t wait until two or three days before the due date. Problem 1 Implement a class named Date in a file named Date.java. Implement the following function in the class: public static String alphatonum (String m, int d, int y); where m is for month, d for day, and y for year. You may assume that you only get valid actual arguments. That is, you do not have to worry about handling error situations due to invalid input values. You may assume that values will be of correct type and in a valid range. Below is an example of a call to the method and its return value. alphatonum("february", 10, 2011) = "2/23/2011" Include at least three calls to alphatonum in your main to test your implementation. Here is an example of main calling it once. Note how I used print and println. public static void main(string[] args) { System.out.print("alphaToNum(\"February\", 10, 2011) should be 2/10/2011"); System.out.println(" : " + alphatonum("february", 10, 2011)); Hand in your Date.java. Hint: You will find a multi-branched if statement useful for this problem. Also consider introducing an auxiliary method with the following signature: // Examples: month("february") returns 2 // month("july") returns 7 public static String month (String m); 2
Problem 2 Implement a class named Digits in a file named Digits.java. Your program is to read a positive integer from keyboard and print the number of even digits in the number, the number of odd digits in the number, and the number of zeros in the number that you read in. As you design your program, implement the following functions in the class: public static boolean iseven (int n); public static int numevens (int n); public static int numodds (int n); public static int numzeros (int n); iseven returns true if n is even; false otherwise. numevens counts the number of even digits in n and returns the count. Similarly for the number of odd digits and zeros with the other two functions. Your main function should read a positive integer from keyboard and use these three functions to produce the desired output. For example, if the number that your program read in is 1073740804, your program should generate an output that looks like the following: 1073740804 contains 3 evens, 4 odds, and 3 zeros. Notes: As you design your program, you may feel that the whole thing could easily be done in one function, namely main without introducing any of the functions that are mentioned above. Well, that may be the case, but more importantly I would like you to learn how to use functions to manage complexity of your program. Hand in your Digits.java. Problem 3 In this problem you will solve the problem that you solved in Problem 2 once more but differently. Again your program is to read a positive integer from keyboard and print the number of even digits in the number, the number of odd digits in the number, and the number of zeros in the number that you read in. In this new design you will implement the following function: // numdigits will return the number of even digits in n if // which is 2, the number of zero digits in n if which is 0, // and the number of odd digits in n if which is 1. // public static int numdigits (int n, int which); You will add this function to Digits from Problem 2. As you implement numdigits, you may use iseven from Problem 2, but you may not use the other three functions from Problem 2. The main function from Problem 2 can now be extended to call numdigits as many times as you need to generate the desired output using the same number that you read in in Problem 2. Using the same input number 1073740804, your program should generate the same output that your program generated in Problem 2, namely: 1073740804 contains 3 evens, 4 odds, and 3 zeros. Hand in your Digits.java which now contain your solutions for both Problems 2 and 3. Problem 4 3
Implement a class named Pascal in a file named Pascal.java. Implement the following function in the class: public static void drawpascal (int n); where n is a positive integer. This function displays the Pascal triangle of size n. If you have to use any loop in this problem, you may only use while loops. A sample run of your program with n = 3 should produce the following. 1 1 1 1 2 1 1 3 3 1 Here is another run with n = 7 this time: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 The Pascal triangle can be obtained by arranging only the binomial coefficients in a triangle as we did in the sample output above. That is, we only include the binomial coefficients in the triangle while ignoring the a s and b s. Binomial coefficients can be computed using the Binomial Theorem: If n is a positive integer and if a and b are any two numbers, then ( ) ( ) ( (a + b) n n = a n n + a n 1 n b +... + 0 1 j ) a n j b j +... + ( n n ) b n where with n j 0. ( n j ) = n! (n j)!j! For example, if n = 0, you would get only one term, i.e., 1. If n = 1, you would get 1a + 1b thus 11 ignoring a and b. You can continue up to the given n for each call to drawpascal. Be sure that your program produces exactly the form of output shown above. Make sure that you use the same indentation and white spaces as shown in the sample output. Here is a sample main that you may use. public static void main (String[] args) { drawpascal(3); drawpascal(7); Hand in Pascal.java. 4
Hints: Try to define some auxiliary functions rather than trying to do everything in one function. For example you may define one that computes factorial, and another that computes the following: ( ) n n! = j (n j)!j! which would use the factorial function. Problem 5 Implement a class named Slot in a file named Slot.java. In this problem your program will simulate a simple slot machine in which three numbers between 0 and 9 (inclusive) are randomly selected and printed side by side separated by a reasonable number of blank spaces. It will also print an outcome telling the user whether it was a triple, double, or single. Continue playing until the user chooses to stop. When the user chooses to stop, print out some statistics much like my solution does (see below). Try to introduce a function whenever a specific task can be isolated as a separate function that can be called by another function. Hand in Slot.java. Note that you will need to use random numbers. Here is a sample interaction (generated by my solution): int-168-217:sol alee$ java Slot The numbers you drew are: 4, 9, 1 The numbers you drew are: 2, 3, 4 The numbers you drew are: 1, 6, 1 wow, double luck! The numbers you drew are: 7, 3, 2 The numbers you drew are: 3, 4, 4 wow, double luck! The numbers you drew are: 0, 6, 4 The numbers you drew are: 2, 6, 8 The numbers you drew are: 6, 5, 4 The numbers you drew are: 9, 5, 6 The numbers you drew are: 0, 5, 8 The numbers you drew are: 2, 2, 2 wow, triple luck! 5
The numbers you drew are: 8, 8, 0 wow, double luck! The numbers you drew are: 5, 8, 0 The numbers you drew are: 8, 7, 2 Continue? (enter 1 to continue 2 to stop): 2 Out of 14 hands, you had 1 triple jack pots (7%), 3 doubles (21%), and 10 singles (71%) today!!! Come again... I said Continue playing until the user chooses to stop. Well, we don t quite know how to do that. Here is some extra information for doing that. The idea is to use an infinite loop - we know how to create an infinite loop. The question is how to break out of an infinite loop when you need to. Well, you could have guessed. We use break to break out of a loop. Let me give you an example: import java.util.scanner; public class BreakTest { public static void main (String[] args) { while (true) { Scanner scan = new Scanner(System.in); System.out.print("Enter an integer: "); int n = scan.nextint(); System.out.println("You typed in: " + n); if (n == 22) { break; System.out.println("Done."); In the above code, if the number that was read in, namely n, is equal to 22, it will leave the while loop. Otherwise it will continue asking the user to type in a new integer. Remember that we can create an infinite loop using for right? Just in case you want to use a for loop instead of a while loop. If you don t remember, see the lecture notes. 6