CS 161 Lab #11

April 18th

Goals:

For this final lab you'll get some practice using a Scanner — first to read from the keyboard, then from a text file. You might want to refer to the code from class for reading from the keyboard and files.

Getting Started

  1. Take a moment to introduce yourself to your partner(s). After social pleasantries are complete, pick one member of the team to be the "typer". They'll share their screen while editing the lab code in BlueJ. (I think BlueJ works better for these interactions than Eclipse. It's easier to see when sharing screens, and makes it easier to quickly test individual methods than Eclipse does.) Group members should contribute equally while working through the problems below and discuss all code to be written, though only the "typer" will be able to edit code. Resist the temptation to have both members work simultaneously in BlueJ — you're much more likely to "drift apart" over the course of the lab if you do so. The goal here is to have a partner who's engaged on exactly the same step of the lab as you are.

    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.

Directions:

  1. Download the InputLab project and extract its contents, then start BlueJ and open the project.
  2. Before writing anything, let's first play around with some existing code. Start by reading through the code in the 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?
  3. Next, you'll write a method that prompts the user for multiple inputs and suggests a username for them. As shown in the interactions below, the username will be the first letter of their first name concatenated with their full last name, and twice their favorite number. I've left you a "to-do list" in the body of the method, and you can borrow the pieces you need from the 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
    
  4. Review the 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.

  5. Now let's get more practice reading from files: Fill in the body of the 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.
  6. Next try writing 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.)
  7. If time permits, try writing 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.)


Brad Richards, 2024