《斯坦福大学CS106A课程讲义》由会员分享,可在线阅读,更多相关《斯坦福大学CS106A课程讲义(8页珍藏版)》请在金锄头文库上搜索。
1、- 1 - CS106A Summer 2013-2014 Handout #02 June 24, 2014 Using Karel in Eclipse _ Based on a handout by Eric Roberts and Mehran Sahami Once you have downloaded a copy of Eclipse as described at http:/cs106a.stanford.edu/eclipse.html, your next task is to understand how to write Karel programs using t
2、he Eclipse framework. Although it is not all that hard to create new Eclipse projects from scratch, it certainly reduces the complexity of assignments if we provide starter projects to get you going. That way, you can ignore all the mechanical details of making new projects and focus instead on the
3、problem-solving aspects of the assignments. Downloading starter projects The first step in working with any Karel assignment is to download the starter project for that assignment. If you go to the assignments section on the CS106A page, youll see a display that looks something like this, though wit
4、h the dates updated: If you click on the link for Starter Files, your web browser will download the starter folder. In some cases, the browser will also unzip/extract the folder automatically, assuming that you have the appropriate software for expanding files from a ZIP archive. The unzipped conten
5、ts of the ZIP file is a directory named Assignment1 that contains the project. Move that folder to some place on your file system where you can keep track of it when you want to load the project. Importing projects into the workspace From here, your next step is to start up Eclipse, which will bring
6、 up the Eclipse window shown on the last page of Handout #5. Find the small icon in the toolbar that looks like: This button is the Import Project button and is used to load a project folder into Eclipse so that you can work with it. Click on this button and then click the Browse. button to find and
7、 highlight (click on) the Assignment1 folder, then click OK. (Note that if you see an Assignment1 folder inside another folder named Assignment1, you want to select the innermost Assignment1 folder.) Now, make sure that the check box labeled “Copy projects into work space” is not checked (if the box
8、 is checked, just click on it to uncheck it). Then click the Finish button. When you do so, Eclipse will load the starter project and display its name in the Package Explorer window (on the left-hand side of the Eclipse application) like this: - 2 - The small triangle-sign (plus on Windows) to the l
9、eft of the folder name indicates that you can open it to reveal its contents. Clicking on the plus-sign/triangle exposes the first level of the package: At this point things look a more promising there is something about worlds listed there. Things get more interesting when you open the (default pac
10、kage), which is where the code you will write this quarter will go. Opening this package reveals: Now things have gotten much more exciting. Thereright on the screenare the Java files for each of the assignments. You can open any of these files by double-clicking on its name. If you double-click on
11、CollectNewspaperKarel.java, for example, you will see the following file appear in the editing area in the upper middle section of the Eclipse screen: - 3 - Note that the comments at the top of the file many not display initially and may need to be expanded by clicking the small + sign next to the c
12、omment header line. As you might have expected, the file we included in the starter project doesnt contain the finished product but only the header line for the class. The actual program must still be written. If you look at the assignment handout, youll see that the problem is to get Karel to colle
13、ct the “newspaper” from outside the door of its “house” as shown in this diagram: Suppose that you just start typing away and create a run method with the steps below: - 4 - public void run() move(); turnRight(); move(); turnLeft() move(); pickBeeper(); The bug symbol off to the side lets you know t
14、hat this program isnt going to do exactly what you want, but it is still interesting to see what happens. Eclipse compiles your program file every time you save it and then tells you abut any errors it found. In this case, saving the file generates the following information in the two windows (the f
15、irst in the upper middle part and the second along the bottom of the Eclipse window): The Problems screen shows the error messages, which are also highlighted with the symbol in the editor window. Clicking on the the small + sign next to line that says Errors, lists out the errors that Eclipse has d
16、etected in your program, as shown on the next page. - 5 - Here, the error messages are clear. The first is that there is a missing semicolon at the end of the indicated line. This type of error is called a syntax error because you have done something that violates the syntactic rules of Java. Syntax errors are easy to