Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COS 170 : Lab 1 - Using Eclipse COS 170 : Lab 1 - Using Eclipse Objectives The objective of this first lab is to familiarize you with the Eclipse Integrated Development Environment (IDE), the application in which programs for this course will be developed. Preparation if working from home You will first need to install the course software on your home computer. Follow these instructions: Installing Java And Eclipse Notes if working from a USM lab If you are working in a USM lab, we suggest that you bring a USB drive to save your work. Alternatively you could email files to yourself or store them to google drive. We overbook the lab so there may be more students than computers. We request that you work in teams of 2. A team can share a single computer, but if you have a laptop or there are unused computers, it can be helpful for a team to have a 2nd computer on which to view the lab instructions. Log into a lab computer with your UMaine System (UMS) account. If you haven't yet activated your account, go to http://mail.maine.edu and choose Account Activation. If you do not have your account activation key, please visit the HelpDesk in the Portland computer center (first floor of Luther Bonney Hall) or call them at 780-4029. Lab Solutions Document If you are working in a team, only one solution document should be turned in per team. All labs must be neatly typed and organized as a single document. Use a word processor such as Microsoft Word. You can also use Pages or Google Docs, but you will need to save your document as a PDF file before submitting it through Brightspace. Before you start the lab exercises, create the solution document (e.g., Lab1Solutions.doc) into which you will paste your solutions. On the first line put:     COS170  Lab1 by [names of all team members] Instructions for Teams of 2 When working as a team, the lab procedure is based on a professional programming practice called Extreme Programming. One team member is designated the driver and sits at the keyboard.  The other member is called the navigator and fills all of the supportive roles: looking for problems, offering suggestions, keeping track of issues that need to be addressed and checking that the exercise is being correctly and fully solved.  An effective team is very active and communicates frequently to confirm that each partner understands and agrees with what is being done. The driver and navigator roles should be swapped after each exercise. In your solutions document include the lab exercises in order and well labeled.  Create the structure of your document now with labels for each exercise and who the driver will be.  This will help remind you to switch roles.  For example this lab has 6 exercises, so your solutions document might look like: COS 170 Lab 1 by Bart Simpson & Jessica Simpson Exercise 1 - Bart   Paste the results of Exercise 1 here Exercise 2 - Jessica   ... Exercise 3 - Bart   ... Exercise 4 - Jessica   ... Exercise 5 - Bart   ... Exercise 6 - Jessica   ... As you complete each exercise cut and paste your answers into the solutions document. If you save your solutions document to your USB drive now, it will keep you from accidentally saving it to the local machine later. Points may be deducted for messy work! You should feel free to experiment, but only submit the requested items. We hope the labs are clearly explained, but please ask the instructor if you have any questions. Exercise 1: (2 points) First Program Now you will write your first program. Open Eclipse (on a USM Windows computer: Start > All Programs > Programming Applications > Eclipse...) (on a USM mac: Applications Folder > Eclipse.app). Eclipse will ask you for a workspace folder. This will be a main folder to hold all of your projects for this course. By default it will be created on the desktop, but if you have a USB flash drive, choose its location and add the name "workspace" for the folder. (Beware that if you use the Desktop on a USM lab machine, it will be erased when you logout.) When eclipse starts you will see a "Welcome" tab, click the X on the Welcome tab next to the word "Welcome" to close it. Select File > New  > Java Project    (make sure you select Java Project!) For Project Name type: Lab1 For Use an execution enivronment JRE: make sure is says JavaSE-11 (you may already have other versions of java on your computer) For Project Layout choose  "Use project folder as root for sources and class files".  (this may save you from cutting and pasting files to the wrong place, and other frustrations) We don't use Modules, so "Create module-info..." should not be selected. Click Finish and your new java project folder will be created. Click OK to ignore the "No source file exists in the project." warning. (If you forgot the Project Layout step, you will get a message about modules, choose "Don't Create".) Every Java program is stored in a class file, so next we create that. Right click on the Lab1 project. (Ctrl-click on a mac) Select New > Class. Verify the Source folder is really what you intended. Verify the Package is set to (default). If eclipse filled in a Package name, clear it. Ignore the message "The use of the default package is discouraged." Enter HelloWorld as the Name (do not put a space in the name) Check the box labeled  public static void main(String[] args). Click Finish and your new java class file will be created. You should now see this skeleton for the HelloWorld program you are about to write. Make the program look as follows (but with your names).  Run the program by clicking the  in the toolbar. The output should appear in the console tab at the bottom. Copy the complete program and the output from the Console tab and paste them into your solutions document as the result of Exercise 1. Exercise 2: (2 points) Errors In the HelloWorld program change the word static to status and remove the ;. You should see a red circle with an x in it to the left of each statement and red underlining where Eclipse thinks the error is. All errors need to be fixed before the program can be run. Hover over each x and read what it says. Type the error messages into your solutions document Exercise 2 section, but do not fix the errors yet. Try to Run the program as you did in Exercise 1.  Type any messages into your solutions document. Now fix the errors and make sure it runs. Note: When you hover the mouse over the red markings it will tell you information about what the compiler thinks the error is, and sometimes it even has quick fix suggestions about how to fix the problem. I suggest you do not use the quick fixes unless you are 100% sure they are what you want to do! It often has good suggestions, but it only understands the rules for correct syntax. You are the only one who truly understands what the code is supposed to do. Exercise 3: (2 points) Copy and Pasting Example Programs In many of the labs we will give you starting code to work with. In this exercise you are going to learn how to copy and paste a program from this lab document into Eclipse. You will need this lab's web page and Eclipse both open on the same machine. Select everything in the yellow box below and copy it. (Windows: Ctrl+C) (Macs: Command-C) public class LoopExample {     public static void main(String[] args) {         int x;         for (x = 1; x <= 5; x++) {             System.out.print("Java ");         }     } } Now switch to Eclipse. In the Package Explorer panel on the left side, click on Lab1 to select your project, and then (on Windows: Ctrl+V) (on Macs Command-V) to paste it into the project. Eclipse reads what your are pasting, it sees that it is code for a class named LoopExample, and it automatically creates a file name LoopExample.java and inserts it into your project. Run the program and copy its console output to your solutions document. Exercise 4: (2 points) You may not have learned about variables and loops yet, but look over the LoopExample program and try to guess how it works. "x" is a variable that is used here to count from 1 to 5. Many programmers use short variable names because they are quicker to type, but a better variable name would be "count" because that is what the variable really represents. In this exercise you are going to use find & replace to change the variable name. It works just like in word processors. Move the cursor to the start of the code, and press the shortcut key (on Windows: Ctrl-F) (on Macs: Command-F) for "find". For Find: put x and for Replace with: put count. Now use the buttons at the bottom to replace all x's with count, however be careful NOT to replace the letter "x" in the class name "LoopExample". Run your code to make sure it still works. Copy your modifed program code into the solutions document. Exercise 5: (1 point) Eclipse Perspective Eclipse is highly customizable. The various panels that you see were chosen and arranged to be helpful when writing Java programs. You can minimize or maximize each panel or drag the edges between the panels to change their sizes. Since I never use the "Task List" panel or the "Outline" panel, I get rid of them by clicking the close icon on those panels. This will give you more room to show your code. Most of the time you wont be actively be using the Package Explorer. You can minimize it with the minimize icon and restore it when you need it with the restore icon that appears along the left edge of the window. For this exercise play around with changing the look of Eclipse. Try all of the things mentioned above. Make a screen shot of your modified Eclipse window (on Windows: Alt+PrntScr creates a screen shot on the clipboard, then paste it into your solutions document) (on Macs: Command+Shift+4 then drag to select a region, this creates an image file on your desktop,  then drag and drop that file into your solutions document). Your changes will be saved automatically in your workspace when you exit eclipse. The organization of panels is called the "Java Perspective". You can restore the original layout of the perspective by clicking Window > Perspective > Reset Perspective Exercise 6: (1 point) Eclipse Preferences As you enter code into the eclipse editor, it automatically indents your code to help show program structure. Automatic indentation is an important feature of editors designed for writing programs. Proper indentation makes programs more readable because it visually shows the high-level structure of a program. When you learn about loops and if-statements, there will often be several levels of nested indentation. By default Eclipse performs indentation by inserting tab characters into your code. One tab for each level of indentation, and each tab character is displayed as 4 blank spaces, which causes the 4 space indentation that you see in your code. Unfortunately other editors such as Microsoft Word, which you are probably using for your solutions document, treat tab characters differently. In Microsoft Word, tabs are 1/2 inch by default. This would probably be okay except that since tabs and spaces are essentially invisible, they sometimes get mixed together and what looks nice and neat in Eclipse, looks disorderly when pasted into Microsoft Word. We are going to fix this problem by setting Eclipse preferences to always just use spaces. (on a Windows computer) In Eclipse click Window > Preferences (on a Mac computer) In Eclipse click Eclipse > Preferences From the preferences menu expand [> Java > [> Code Style > Formatter You will now create a new Formatter profile, click New..., give it the Profile Name: AllSpaces and click OK. On the Profile configuration window that comes up, expand [> Indentation For Tab policy: select Spaces Only from the choice list. Click OK then Apply and Close. Another preference that you may want to change is the font size. From the preferences menu expand [> General > [> Appearance > Colors and Fonts > [> Basic > scroll down to and select "Text Font" > Edit You will want to keep a fixed width font like: Consolas, Courier New, Menlo, or Monaco, but change the font size to something a little larger. When you exit Eclipse, it will store these preferences in a hidden file in your Workspace folder. If it is on your USB drive, it will then reload them every time you start Eclipse.     There is nothing to show in your solutions document, so just write "I changed the preferences." YOU ARE DONE! These labs are designed to be finished during the lab period. If you need more time, make arrangements with your lab partner to meet up to finish the lab before the due date. Labs are turned in through Brightspace > Assessments > Assignments. If you used Microsoft Word to write you lab report, you can submit the .doc or .docx file. For any other editor, export your report as a PDF file. Only one student needs to submit the report, but make sure that both names are listed at the top of the report. Please remember all student files are removed from lab machines. Save your work to your USB drive, your google drive, or email it to yourself.      Exit Eclipse before removing your USB drive. Saving a copy for your partner All of this work has presumably been done on one partner's USB drive. Each partner may want to have their own copy. Also, if the lab was not finished and the partners can not get together to finish it, they can each finish it on their own and each hand in their own solutions document. If you are still in Eclipse you can just drag and drop the .java files to the partner's USB drive. Otherwise find the .java files on the first USB drive in something like workspace/Lab1. (There may be extra nested subdirectories if you didn't set up the project like we suggested.) Do not try to copy the subdirectories! Later the second student should use Eclipse to create the workspace and project folders and then finish by dragging and dropping the .java files into the project folder shown in Eclipse. You may notice other file such as: .class, .metadata, .classpath, and .project. Don't copy these. Eclipse creates these when it compiles your code and also to keep track of project organization and preferences.