![]() |
![]() |
|
The remaining weeks' topics, jfyi:
Shorthand notation:
,java.util.Scanner
, (especially because we use the full name to the type when declaring a local-variable, the type when declaring a parameter, as well as to call the constructor).java.util.Random
import:
import java.util.Scanner;
import java.util.Random;
import java.util.List;
/** Comments for MyClass must immediately precede the class, as always.
* (Don't put `import` statements *between* the comment and what it's describing).
* @author Me McMyself
*/
class MyClass {
static |
Consider the following function:
static String mystery( String s, int a, int b ) {
String somethingSoFar = "";
for ( int i=a; i < b && i < s.length(); i += 1 ) {
somethingSoFar += s.charAt(i);
}
return somethingSoFar;
}
|
We will go through some of these in lecture;
on your own work through the remaining problems
(seeing me or a peer instructor if you have difficulties/questions).
(These are all eminently reasonable questions for an exam!)
In all cases, come up with at least 3 test cases.
If we finish these examples, we will talk about:
Recall: to read numbers from the keyboard repeatedly,
we could use the
import java.util.Scanner;
class Lect10c {
void echoNumbers() {
Scanner s = new Scanner( System.in ); // s will gather chars from the keyboard
System.out.println( "Please type in numbers, followed by a word (or, ^D)." );
while (s.hasNextInt()) {
System.out.println( "Read " + s.nextInt() );
// Usually we update our index variable, to update
// our notion of where we are in the process;
// but in this case calling 'nextInt' already udpated
// what characters had been consumed from the input stream.
}
System.out.println( "There isn't an int to read next." );
}
}
|
Task:
Read numbers from keyboard (as long as
| input typed by the user | expected result of calling |
| 3 17 9 2 21 bye-bye | 21 |
| -3 -17 -9 -2 -21 stoppity-stop | -2 |
| 3 bye-bye | 3 |
| bye-bye | no conceivable answer -- a run-time error perhaps? |
// ATTEMPT 1 -- does not work correctly
// Read the maximum integer typed at the keyboard.
// Keep reading until some non-integer gets typed.
// THERE MUST BE AT LEAST ONE INTEGER TYPED else our program will cause an error.
//
int maxFromKeyboard() {
Scanner s = new Scanner( System.in );
int maxSoFar;
while (s.hasNextInt()) {
if (maxSoFar < s.nextInt()) {
maxSoFar = s.nextInt();
}
// else do nothing.
}
return maxSoFar;
}
|
One problem is that we don't initiailze our accumulator
However, it's not clear what to initialize it to?
Clearly, 0 won't work: our second test case wouldn't pass. (Why not?)
Something like negative infinity would work in this case,
but the technique doesn't generalize (suppose you wanted to
find the minimum-length string that is typed in —
there is no 'infinite-string' value).
A clever solution (possibly inspired by how you'd solve this problem in real life):
Use the very first value, as your
// ATTEMPT 2 -- compiles, but still does not work correctly
// Read the maximum integer typed at the keyboard.
// Keep reading until some non-integer gets typed.
// THERE MUST BE AT LEAST ONE INTEGER TYPED else our program will cause an error.
//
int maxFromKeyboard() {
Scanner s = new Scanner( System.in );
int maxSoFar = s.nextInt();
// If there isn't an int to read, the scanner will blow up, but that's okay:
// our comment clearly states there must be at least one integer.
while (s.hasNextInt()) {
if (maxSoFar < s.nextInt()) {
maxSoFar = s.nextInt();
}
// else do nothing.
}
return maxSoFar;
}
|
The solution is pretty easy:
store the result of the first
// ATTEMPT 3 -- works!
// Read the maximum integer typed at the keyboard.
// Keep reading until some non-integer gets typed.
// THERE MUST BE AT LEAST ONE INTEGER TYPED else our program will cause an error.
//
int maxFromKeyboard() {
Scanner s = new Scanner( System.in );
int maxSoFar = in.nextInt();
// If there isn't an int to read, the scanner will blow up, but that's okay:
// our comment clearly states there must be at least one integer.
while (s.hasNextInt()) {
int currentNum = s.nextInt();
if (maxSoFar < currentNum) {
maxSoFar = currentNum;
}
// else do nothing.
}
return maxSoFar;
}
|
design principle: A method should either return a value or mutate state, but not both.
(Use two different methods — a getter and a mutator — if you want to do these two different things.)
while (s.hasNextInt()) {
maxSoFar = Math.max( maxSoFar, s.nextInt() );
}
|
early: Does a string contain the letter 'X'? Note that we still have an accumulator
so-farvariable and an index variable — but we can get fancier in our condition if we want. Does the program run much quicker, when we are fancy and stop earlier? (For what inputs is it a big win?)
gee, did I intend to leave the field uninitialized, or is there a latent bug hiding in this program?. ↩
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland |