![]() |
![]() |
|
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
ibarland v1.01
// Class String: int length() // Return the length of this String. char charAt(int i) // Return the char at index `i` from this String. // (The first character is at index 0.) String substring(int start, int stop) // Return a String consisting of characters at index `start` up to // but not including index `stop` from this String. boolean contains( String target ) // Does this String include `target` as a substring? String toUpperCase() // Return this String converted entirely to upper-case. |
int size() // The number of items in this list. void add(SomeTypeT item) // Add `item` at the end of the list.1 SomeTypeT get(int i) // Return the item stored at index i. |
# | pts possible | pts |
1 | 10 | |
2 | 10 | |
3 | 10 | |
4 | 10 | |
5 | 10 | |
6 | 20 | |
7 | 20 | |
8 | 10 | |
Total |
/* @param weight An organism's weight, in kg. * @param faveFood An organism's favorite food. * @param maxSpeed An organism's maximum speed, in kph. * @return Our guess as to the organism's species. */ String mysteryMethod1( double weight, String faveFood, double maxSpeed ) { if (maxSpeed >= 80.0) { return "cheetah"; } else if (weight >= 1000 && !(faveFood.equals("krill")) { if (maxSpeed < 0.00001) { return "giant fungus colony"; } else { return "brontosaurus (a.k.a apatosaurus)"; } } else if (maxSpeed >= 15.0) { if (faveFood.equals("bugs") { return "african swallow"; } else if (weight < 0.01) { return "blue tail fly"; } else { return "pony"; } else { return "jackalope"; } } |
String mysteryMethod2( String s ) { String resultSoFar = ""; int i=0; while (i < s.length()) { resultSoFar = resultSoFar + s.charAt(s.length()-1-i); // What is the current value of resultSoFar? (Write into column 2) ++i; // What is the current value of i? (Write into column 1) } return resultSoFar; } |
i | resultSoFar |
0 | "" |
int csf = 0; // 'cubes-so-far' for ( int i=0; i < 77; ++i ) { csf = csf + i*i*i; } |
1 public static final double KG_PER_POUND = 1.0/2.2; 2 3 java.util.LinkedList<Double> convertAllLbsToKg( java.util.LinkedList<Double> nums ) { 4 java.util.LinkedList<Double> numsSoFar; 5 numsSoFar = ; 6 7 for (int i=0; i < nums.size(); ++i) { 8 if ( nums.get(i) >= 0.0 ) { 9 numsSoFar.add( i * KG_PER_POUND ); 10 } 11 } 12 return numsSoFar; 13 } |
i | nums.get(i) | added to numsSoFar: |
0 | ||
1 | ||
2 |
i | nums.get(i) | added tonumsSoFar: |
0 | ||
1 |
/** Return whether or not a String contains any 'Z's. * @param str A string to check for 'Z's. * @return whether or not `str` contains either 'Z' or 'z'. */ |
1Technically, the return type is boolean, but since true is always returned, it doesn't actually provide any information. ↩
2 You may assume that the loop body does not modify the index variable i. ↩
3 In fact, depending on your comfort with booleans as values, there is a plausible, well-indented version requiring only two lines of actual code. ↩
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
©2008, Ian Barland, Radford University Last modified 2008.Nov.21 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |