Download SectionLoops

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Section: While Loop with a Scanner
1) In the current project, consider method firstOf3Strings:
public String firstOf3Strings(String a, String b, String c)




Specifically, how do you compare Strings with no > or < for String objects?
Is “A”.compareTo(“B”) < 0?
Is “A”.compareTo(“a”) < 0?
Is “abc”.compareTo(“abc ”) < 0?
2) In the current project, consider method maximumInScanner:
public double maximumInScanner(Scanner stream)





You need a scanner argument constructed with a String argument.
Need Scanner methods hasNextDouble and nextDouble.
Need to consider each number twice in the loop.
Realize that once a Scanner has been iterated over, there are no more tokens to visit.
Consider the following unit test and class OneMethod. There is a bug. Run it for the red bar.
Take a minute to consider how to fix the bug. Fix the bug and see the assertion pass.
import static org.junit.Assert.*;
import java.util.Scanner;
import org.junit.Test;
public class OneMethodTest {
@Test
public void testSumOfEvens() {
OneMethod om = new OneMethod();
Scanner input = new Scanner("1 4 2 3");
assertEquals(6, om.sumOfEvens(input));
// The method changes the input. Input has been scanned.
assertFalse(input.hasNextInt());
}
}
import java.util.Scanner;
public class OneMethod {
public int sumOfEvens(Scanner scanner) {
int sum = 0;
while(scanner.hasNextInt()) {
if(scanner.nextInt() % 2 == 0)
sum = sum + scanner.nextInt();
}
return sum;
}
}
3. Go to 228 Gould Simpson and complete a different method (which is very similar to the example above) and
turn it into WebCat. Take this page with you as your two classes will look very much like the two above.
The link to the next assignment is on the Assignments page https://sites.google.com/site/csc227arizona/assignments-2