Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
BSU CS121 Labs { CS121 } Summer 2018 Toggle navigation Home (current) Schedule Projects Lab Resources CS Tutoring Center Setting up your own machine Program Examples Java 8 API Docs Local Linux Guide MobaXTerm - SSH/SCP Client for Windows Users Video: 5 Tips for Computer Science Students Tutoring Hours Office Hours Toggle navigation Labs Lab Overview 01 Basic Skills Walkthrough 02 Time Conversion 03 Project 1 Startup 04 Color Blob Painter 05 Bouncy Ball 06 Going Loopy 07 ArrayLists and Gradients 08 Shipping Boxes 09 A Game of Chance 10 Word Wrapping 11 Deck of Cards 12 Lite-Brite 13 More GUIs 14 MineWalker GUI Logic 15 Testing and Debugging Pre-lab Homework Bring proof that you completed your CS121 and CS121L course evaluations. We appreciate feedback to help us improve the course. To help put the numbering system they use into perspective, a 5 is like giving us an 'A', a 4 is a 'B',...etc...and a 0 means we get an 'F'. Lab Description Overview A software bug is an error, flaw, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Read the introductory section on software bugs on Wikipedia. Read also the etymology section that describes the origin of the usage of the word bug for software. A debugger is a computer program that is used to test and debug other programs. You may then naturally ask the question: if the debugger is a program, can we debug a debugger using itself? (Read this for a not so serious answer: debugging myself) Objectives Introduce you to the Eclipse debugger for Java. Getting Started Create a lab15 project in Eclipse. Download the five java files available here: http://cs.boisestate.edu/~cs121/lab/15/ DebuggerTest1.java, DebuggerTest2.java, Car.java, FindRoute.java, and Direction.java Import them into Eclipse. Create a README file where you will journal how you used the debugger to find the bugs. Setup If you are working on CentOS 7 Linux (installed in our labs) you must complete the steps outlined in the document below before using the debugger. There are some issues with the Oxygen KDE Theme that cause the Eclipse debugger to crash when expanding variables. The problem is not with Eclipse itself, so if you are on your own computer, you do not need to make the changes. Eclipse Debugger Workaround: Change KDE Theme Part 1: DebuggerTest1 Open the class DebuggerTest1.java in Eclipse and take a minute to browse it. This class doesn't have any bugs in it. We will use it to explore the features of the debugger like stepping through the code and inspecting the values of variables, arrays and objects. Start running it in the debugger using the steps shown in the following video: Below we summarize the steps shown in the video. We show keyboard shortcuts, in parentheses, for most of the actions: Go to Run --> Debug Configurationsand then select Stop in mainoption. Start the debugger with Run --> Debug(F11). It will open a new perspective and rearrange the windows in Eclipse. The debugger will start and stop at the first line in the main method. Here are some of the common things you can do inside a debugger. You can Step Into (F5) your program one line at a time. This will go into methods that you encounter, which isn't what you always want. You can Step Over (F6) your program one line at a time. This will execute any method encountered in its entirety. If you enter a method and want to simply finish it until it returns, then use Step Return (F7). To resume running your program, use the resume button (F8). You can terminate the debug session anytime by pressing the red Terminate button (Ctrl+F2). You can set a breakpoint anywhere in your program by right clicking on the blue side bar in the code window (or use Shift+Ctrl-B). The program will stop when it encounters a breakpoint. Breakpoints can be conditional where you specify a boolean expression to specify when the debugger to stop at a specific breakpoint. Examine a variable, including arrays and objects in detail in the top-right variables window. You may also hover the mouse over a variable to examine the value of a variable. If your debugging windows disappear or are not where you expect them, you can reset your layout to the default view with Window -> Reset Perspective Part 2: DebuggerTest2 The DebuggerTest2 class throws an ArrayIndexOutOfBounds exception. Use the debugger to determine the cause and fix it. Part 3: A Buggy FindRoute The FindRoute class, along with the supporting classes Car and Direction, finds and prints the route for a car on a grid from a starting point to a destination. The classes provided to you have a couple of bugs: one in FindRoute class and another in the Car class. First, let us show you what the program output looks like if it was correct. Note that we provide two sample inputs. The parameters for the car are provided via the command line. Ask a lab tutor if you aren't sure about how to use command line arguments. java FindRoute car1 0 0 0 4 Car [id = car1, location = [x=0, y=0], destination = [x=0, y=4]] NORTH NORTH NORTH NORTH java FindRoute car1 0 0 4 4 Car [id = car1, location = [x=0, y=0], destination = [x=4, y=4]] EAST EAST EAST EAST NORTH NORTH NORTH NORTH The first bug in FindRoute The first bug in FindRoute occurs when we run the FindRoute class with the arguments car1 0 0 4 4. The program outputs the following and then there is no more output. Car [id = car1, location = [x=0, y=0], destination = [x=4, y=4]] Try it in Eclipse. Note that the red terminate button is on so the program is running but not producing any output. Turns out that it goes into an infinite loop. Terminate the program with the red terminate button. Now start it again in the debugger and go step by step in the FindRoute class until you find the bug! The second bug in FindRoute Make sure you have removed the first bug from the previous section before working on this section. Now when we run the FindRoute for the same input car1 0 0 4 4 we get a bad output because it goes for too long. Actually, we have another infinite loop but we have added some code to check if the car is driving too long and we break out of the loop. java FindRoute car1 0 0 4 4 Car [id = car1, location = [x=0, y=0], destination = [x=4, y=4]] EAST EAST EAST EAST EAST EAST EAST EAST EAST EAST EAST EAST EAST EAST EAST EAST EAST Oops!!! there is an error in the car's driving...it is taking too long It also fails for the arguments car1 0 0 4 0. Try them as well. However, the program works correctly for the following two cases: java FindRoute car1 0 0 0 4 Car [id = car1, location = [x=0, y=0], destination = [x=0, y=4]] NORTH NORTH NORTH NORTH java FindRoute car1 0 0 0 -4 Car [id = car1, location = [x=0, y=0], destination = [x=0, y=-4]] SOUTH SOUTH SOUTH SOUTH Use the debugger to find the bug and fix it. Hint: the bug is in the Car class so you will need to set a breakpoint in the Car class and debug the nextMove() method in the Car class. Submitting the Lab You will follow the same process for submitting each lab. Open a console and navigate to the lab directory containing your source files, Remove all the .class files using the command, rm *.class. In the same directory, execute the submit command for your lab section as shown in the following table. Look for the success message and timestamp. If you don't see a success message and timestamp, make sure the submit command you used is EXACTLY as shown. Required Source Files DebuggerTest1.java DebuggerTest2.java Car.java Direction.java FindRoute.java README Section Instructor Submit Command After submitting, you may check your submission using the "check" command. In the example below, replace X with your lab section. Make sure you log out before you leave the lab! The logout option is in the Fedora Linux menu on the main panel at the bottom left. Be sure the login screen is visible before you leave the lab. There are gremlins in the labs who have been known to play tricks on people who forget to log out before leaving.... CS121 is maintained by Marissa Schmidt.