Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS 161 Lab #12 CS 161 Lab #12 April 28th 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 In each lab this semester you will work with a randomly assigned partner. (I'll have Zoom randomly set up breakout rooms.) Please be kind in your interactions with your partners! Keep in mind that students in this class have a range of previous programming experience, and that some have been college students for longer than others. We're all in this together, and you have something to learn from your partner, no matter who they are or what their previous experiences have been. I expect that group members will collaborate and work together on each step of the lab. 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: Download the InputLab project and extract its contents, then start BlueJ and open the project. 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? 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 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 and/or code from class. Please enter your first and last names: Brad Richards Thanks, Brad. What's your favorite number? 256 Your suggested user name is BRichards512 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 BlueJ doesn't display it since it's not Java code. You can use any text editor to view the file (from the desktop), or 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. 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. 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.) Which of the methods in the Input class could be made static? Test your predictions by trying to make them static and testing to see if there are any unintended consequences. 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, 2022