![]() |
![]() |
|
home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Re-do of the problems you missed on exam2:
If you realize you have a mistake, you can go ahead and fix it
as long as you have not looked at the solution set, notes,
etc. for half an hour.
(Again, the intent of this is to make sure that you understand the
material and how to fix it, rather than just regurgitating
something you read.)
( ) { } |
int i = 2; int ssf = 0; while (i < 10) { ssf = ssf + i; System.out.println( "i=" + i + "; ssf = " + ssf ); i = i+3; } |
For this problem, assume that deadlines is of type java.util.LinkedList<Date>, and refers to a non-empty list.
Date mysterySoFar = deadlines.get(0); int i = 0; while (i < deadlines.size() ) { Date d = deadlines.get(i); if (d.comesBefore(mysterySoFar)) { mysterySoFar = d; } i = i+1; } |
(3pts) In general, what does the loop compute? (4-5 words of English.) What would be a more descriptive name for mysterySoFar?
(4pts) Rewrite the fragment to use a for-each loop.
int ssf = 0; for ( int i=0; i < 12; i = i+4 ) { ssf = ssf + i; System.out.println( "i=" + i + "; ssf = " + ssf ); } |
For the remaining questions, you'll type in methods, and submit in your program-source only. Javadoc is required (although most of it is provided), but test cases are not, beyond what is indicated. (That's because test cases for random methods are inherently problematic.)
/** Return a String concatenated to itself multiple times. * @param n the number of copies to make. * @param word the String to copy. * @param a String containing `n` copies of `word`. * Examples: * Linguistics.stringTimes( 3, "ho" ) = "hohoho" * Linguistics.stringTimes( 1, "unary" ) = "unary" * Linguistics.stringTimes( 0, "turkey" ) = * Linguistics.stringTimes( 4, "" ) = */ public static String stringTimes( int n, String word ) { } |
(4pts) Write a method randLetter which returns a random char between 'a' to 'z' inclusive1 (You can use class Die from previous labs, or you can just use java.util.Random methods directly.)
/** Return a random lower-case letter. * @return a random lower-case letter. */ char randLetter() { // your code here } |
Hint: the expression ((char) ('a'+2)) evaluates to 'c'. 2 One can find various tables which list Java's notion for equating characters with numbers.
How can you tell if your function is working correctly?
You might try calling it 50 times,
but if you don't see a 'z' maybe that's just slightly bad luck.
So, a better way to test this method is to
replace the number 26 with something like 3,
and make sure that repeated calls do eventually return each of
{'a', 'b', 'c'},
but never 'd'.
(You did remember to used a named-constant instead of 26, right?)
If you call randLetter 26000 times, one would expect that the letter 'z' gets returned 1000 times.
Confirm this experimentally:
/** Return a random 'word' (a string of letters, probably gibberish). * @return a random 'word' (a string of letters, probably gibberish). */ |
/** Return a new list containing n 'words' (strings of letters). * @param n The number of words in the returned list. * @return a new list containing n 'words' (strings of letters). */ |
1In Java, char literals are enclosed in single-quotes. Double-quotes are only for Strings; a char is not considered the same as String-of-length-one. ↩
2This is an odd expression. Before adding two, Java is auto-converting 'a' into a numeric value. Then, we are explicitly casting the result with “(char)”. (Remember that in casting, the parentheses around the type are required; it is the only time we'll ever mention a type in Java that is not part of some declaration (declaring a local variable, parameter, field, or signature).) ↩
home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Apr.15 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |