![]() |
![]() |
|
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
ejderrick v1.0
// 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. |
| # | 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 double[] convertAllLbsToKg( double[] numsLbs ) {
4 double[] numsKgs;
5 numsKgs = ;
6
7 for (int i=0; i < numsLbs.length; ++i) {
8 if (numsLbs[i] >= 0.0) {
9 numsKgs[i] = i * KG_PER_POUND;
10 }
11 else {
12 numsKgs[i] = 0.0;
13 }
14 }
15 return numsKgs;
16 }
|
| i | numsLbs[i] | initialized numsKgs[i] with: |
| 0 | ||
| 1 | ||
| 2 |
| i | numsLbs[i] | initialized numsKgs[i] with: |
| 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'. */ |
1 You may assume that the loop body does not modify the index variable i. ↩
2 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 |
![]() |