Consider letting the less experienced member of the group do the typing if it seems like there's a mismatch in experience or comfort levels. That will help make sure they don't get left behind. Worst case, flip a coin to see who does the typing. Or have Java generate a random boolean value. (Type "(new java.util.Random()).nextBoolean()
" in the codepad without the double quotes.)
At the conclusion of the lab, make arrangements for the typer to share a copy of the code with the other member(s) of the group if desired. (E.g. email it, or put it on a shared Google drive, etc.) My solutions to the lab will get posted as well.
readTwoInts
method. It tries to read two integers from the keyboard and print them, but it makes no effort to verify that the user has really typed in integers. Run it enough times to get a sense for how the nextInt
method in the Scanner
class behaves. Try entering the integers on separate lines, on the same line, with some extra space before the inputs, after the inputs, etc. Make sure you experiment with non-numeric characters too, to see how it behaves when the inputs aren't (all) digits. What if the first input is a word and the second is an integer, for example?
readTwoInts
method above and/or code from class. (Hint: You don't need to use substring
to find the first name — just use an appropriate Scanner
method to read the first name, and then the last name.)
Please enter your first and last names: Brad Richards Thanks, Brad. What's your favorite number? 256 Your suggested user name is BRichards512
printAllLines
method we wrote in class — it takes a filename as its input and reads it line-at-a-time, printing it to the screen. (There's a copy of it in the lab project.) Call it and have it print "numbers.txt", or "Input.java" to verify it works before going to the next step. The "numbers.txt" file is inside the project folder, though older versions of BlueJ won't display it since it's not Java code. If you can't see it in BlueJ, look here to see a copy of its contents.
When you call the method, BlueJ notices that it requires a String as its input, and pops up the dialog box that prompts you for an input. We could get one step closer to running without BlueJ's help if we wrote a method that asked the user for the filename, read it in from the keyboard using a Scanner, then called printAllLines
for us. Finish the definition of printContents
, which aims to do that. Once you finish it you'll get an error message about FileNotFoundException
. Since printContents
calls printAllLines
, and printAllLines
has the "warning label" involving "throws", we need the same warning label here on printContents
. Copy the "throws" part from printAllLines
to this new method and all should be well.
printPositiveValues
method. It should print out all of the integers in the specified input file that are greater than zero. You can refer to the printAllLines
method for inspiration. That method read lines as strings though, and you'll need to read integers for this problem so that you can inspect the values and only print the positive ones. To test your code, call the method with "numbers.txt"
as its input.
sumUntilNegative
. It should add up all of the values from the file, but stop when you read a negative number from the file (or when you hit the end of the file, if there are no negative numbers in it). You can create another sample input file containing only non-negative numbers if you want to test your code thoroughly. (Hint: You might consider reading the first value before you get to the loop.)
findIdenticalNeighbors
. It should work its way through the file, watching for situations where two consecutive integers have the same value and printing a message as they're found. (It doesn't happen often in the sample file, but there are a couple of places where there are repeated values.)