CSCI E-50b: HOMEWORK 5 HINTS

HOME | JAVA | HELP | VIDEOS | RESOURCES


Homework 5 Part 1

GENERAL INFO:

  • ALL PROGRAMS should be written with methods unless otherwise specified. Please use good judgement in deciding when to add methods; try to identify them by breaking down tasks with stepwise refinement. A single method should not be longer than 1/2 - 3/4 of a page; at the same time, you should not be writing several methods with only 2-4 lines of code each in a single program. Main should have the minimum number of lines of code; the bulk of your processing is done in methods.
       
       
  • BE SURE YOU WRITE WHAT THE PROBLEM ASKS FOR! We do grade based very specifically on the problem set, so if a problem tells you to return a particular type of value, be certain that you return it. If it says to pass certain arguments to a method, do so. Look carefully at any method signatures given, or examples included in the problem.
       
  • When you are given sample output from a program, do your best to have your program produce output that is as close as possible to that sample. It's there for a reason! ;-)
       
  • ERROR-CHECK ALL USER INPUT! Although you may think users are nice people, often they will deliberately try to subvert your programs to do things they shouldn't do. Unfortunately, your TAs are among the worst, and will try to cause crashes when input isn't tested to be certain it's what the program expects. Make it a habit to always check any data that you get from the user!
       
  • Comments and style count on every complete program (you do not need to routinely comment pencil-and-paper exercises). You should be using standard Javadoc commenting style, for both the program description and all methods except main, and including @tags when appropriate. Use meaningful variable names, to make your code easier to read. Indent correctly, and use white space on your pages for readability also. We WILL be checking whether you are writing correct comments when we read your printouts, and will mark comments based on that.
       
How about hints on the Pencil and Paper problems?
  Problem 1 will require you to use String methods; you may look at the custom text for more on Strings; you can also look at the String API in Sun's documentation. Your answers don't need to be elaborate, but do be sure you indicate every character that's included in the result.

For Problem 2, you can review arrays if needed in the 50a lectures on the videopage. Think about how arrays are passed to methods, and tell us what the values in array a1 are after the code executes.

For Problem 3, NOTE that you are to change the method's declaration to accept a Months parameter and return an int representing the number of days in that particular month (e.g., Months.JAN would return 31). Your task is to figure out how to switch on the enums to return the correct value.
 
For Problem 4, I don't remember this??
  The example was briefly discussed during the final lecture in CSCI E-50a (remember, this IS on the Video page!). While we can write the powers program recursively as shown, exponents can also be split in half when they are even. So, for example, 2 raised to the 8th power (256) is the same as 2 raised to the 4th power (16) and the result raised to the 2nd power (16 * 16 = 256). Keep in mind that, when n is odd, you can easily use the existing method to get n to an even number (e.g., n to the 5th power is the same as n * n to the 4th power). This is how we'd like to have you rewrite the solution. When you have done so, see if you can figure out how many recursive calls are made for the two computations listed.
 
I'm lost on Problem 5 --- HELP??
  We are changing the requirement for this problem, as noted on the home page. If you have already done it with only the array as a parameter, that's fine. However, if you are having difficulty, you may use TWO parameters - the array and an int for the size. Remember to think about the base case - when have all the values in the array been summed up? Then think about how to go from a larger size toward that base case - this is your recursive case.
 
Recursion is really confusing - how do I approach the Rabbits program?
  Keep in mind that for recursion, you need to have a base case or cases - the point where you KNOW the answer and can simply return it, rather than needing to do calculations. When you then write your recursive method, be sure that you are writing it exactly as described in the problem; it should take one argument (for the month) and return one value (the number of rabbit pairs for that month). The method should test your base case first, then handle other cases. Put your for loop in main to get the rabbit numbers for 12 months (you do not need to get user input), and print your results in main.
 
We haven't talked a lot about arrays... What about Problem 7?
  Although we haven't said much about arrays in lecture, the homework shows you how Arrays.toString( a ) can be used to print an array. We would like you to write your own code to do this. Note that your array only needs to handle ints, and should output any array of ints as shown in the problem. It should also be able to handle an empty array!
 
How about suggestions on number 8?
  You might think of this as a "check-writing" program - when you write a check, you first list the payment amount in numbers, then rewrite it in words. Remember to first identify your base case or cases, then begin to work up to the more complicated situations. This problem can be much easier to write if you use final String arrays to store some of the various numeric words you'll need; you may even find a clever way to align the words (e.g., "one"), and the array indices. Think about which end of the number is easiest to break apart for processing!
   
When you write main, it should request an integer from the user (using the Scanner methods and error-checking), and pass it to your method. The method can write the text translation of that number - you do not have to return the text. NOTE that all your processing should be done in the method recursively - you do NOT need any class-scope static variables to hold or track parts of the number!!!
 
What is the formula for GCD? I don't know it...
  Go to the Video page, and take a look at the first hour of lecture (CSCI E-50a) from December 8th - in that class, we wrote a Rational number class, and we discussed Euclid's GCD and how to calculate it. The formula is given there. For this program, you should take that formula, and convert it into a recursive solution - what will your base case be (i.e., when should the method stop)? What is the recursive case?
 
And the last problem? Pascal's Triangle?
  You are given the formula at the top of the problem - can you convert these rules into your base case(s) and recursive case? Note that you will read in the number of rows from the user using keyboard input. You will then need to call your method passing it each row/column combination as parameters (what control structure do we use to manage row and column patterns?), to calculate the correct value for that location. We recommend using printf() to nicely format your output.
 
TOP | HOME


© 2008 The President and Fellows of Harvard College
Please send comments to The Webmaster
Last modified: Monday, 09-Feb-2009 14:57:40 EST
URL: http://www.fas.harvard.edu/~libe50b/homework51.html