Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CMPS 161     Lab 1 
Introduction to the JAVA Environment 
 
Task 1 (Create a Work Area) 
1.  Start by creating a work area which will be compatible with the Concurrent Versions System that we will be using.  Insert 
a formatted floppy disk or a USB flash drive and open a folder for that device by clicking Start->My Computer and then 
the icon for your floppy or flash drive. 
2.  You may already have a csvroot structure created from using the CVS system (if you have time, you may wish to pause 
here and go through the CVS Tutorial).  If so, skip to step7. 
3.  Create a folder called cvsroot in the top level (root) of your floppy disk or flash drive.  Open the cvsroot folder. 
4.  Create a folder called student_work in the cvsroot folder.  Open the student_work folder. 
5.  Create a folder for your class in the cvsroot folder.  If your class is CMPS161 section 1, name it cmps161-01.  If your 
class is CMPS161 section 2, name it cmps161-02.  Follow the same pattern for other classes.  For the purposes of the 
remainder of this lab, we will assume that your folder name is cmps161-01 (adjust to your situation as necessary).  Open 
the cmps161-01 folder. 
6.  Create a folder for your w-number in your class folder.  For the purposes of the remainder of this lab, we will assume that 
your w-number is w1234567, but you should actually use your correct w-number.  Open the w1234567 folder. 
7.  Create a folder for this lab called lab1.  You should now have a folder structure that looks something like this: 
 
F:\ 
 cvsroot 
  student_work 
   cmps161-01 
    w1234567 
     lab1 
8.  Open the lab1 folder.  This is your working area for this lab.  You should create a similar folder for each of your other labs 
and assignments in this class. 
This should be your w-number.
This should match your class.
This may be another drive letter or even a folder, 
such as C:\documents and Settings\jdoe\Desktop
Task 2 (Entering a program into jGrasp) 
1. We will be using a program called jGrasp for program development.  jGrasp is one of a class of programs called 
integrated development environments, because it integrates editing, compiling, and debugging into one environment.  It is 
installed in the computer science department labs in Fayard 125 and 126, and you should be able to start it by finding  
“jGrasp”  in the start menu or on the desktop.  You can also use jGrasp from the jgraspstick folder (if you have it), 
which looks something like this: 
 
Double-click the jgrasp batch file in the jgraspstick folder. 
CMPS 161 Lab 1,  Page 1 of 7 
2. Whether you start jGrasp from a CS&IT 
lab computer, or from a computer using 
jgraspstick, jGrasp should begin 
and you should see a window that looks 
something like this: 
 
 
 
 
 
 
 
3. Using the lefhand file pane, double click on files and folders to navigate to the lab1 folder created in Task 1.  The jGrasp 
window should look something like this: 
Click File->New->Java.  A editing 
window should appear in the right-
hand pane that looks something like 
this: 
 
Click inside this window for an 
insertion point at which you can type 
the sample program later in this lab. 
 
 
 
 
 
4. Enter the following program in the editing window: 
 
import acm.program.*; 
import acm.graphics.*; 
import acm.io.*; 
 
class Lab1P1 extends GraphicsProgram 
{ 
  void main() 
  { 
    add(new GOval(0, 0, 100, 100)); 
    add(new GOval(25, 25, 10, 10)); 
    add(new GOval(70, 25, 10, 10)); 
    add(new GArc(25, 25, 50, 50, 225, 90)); 
    add(new GLabel("Hello World!", 16, 116)); 
    add(new GLabel("My name is Jane Doe." , 0, 132));  
  } 
 
 
CMPS 161 Lab 1,  Page 2 of 7 
 
  public static void main(String[] args)  
  { 
    new Lab1P1().start(); 
  } 
  public void run() { main(); } 
} 
 
5. Enter the program exactly as shown above, except use your name instead of Jane Doe.  After entering the program, click 
the “Save file” button  to save your program.  You should see a “Save As” dialog box similar to the following: 
 
The file name should already be entered for you as “Lab1P1.java”.  If not, change it to Lab1P1.java and make sure that the 
folder shown in the “Look In:” box is your lab1 folder.  Then click “Save” to save your program.  When you write 
programs, be sure to save your work frequently. 
6. The name at the top of the editing window should change to Lab1P1.java and it should now be listed in the files pane to 
the left. 
 
Click the “Compile file” button  to attempt your first compilation.   
CMPS 161 Lab 1,  Page 3 of 7 
7. If the compilation works without error, the “Compile Messages” pane at the bottom of the window should show the 
following: 
 
 
8. It is likely, however, that you have made a typing error, in which case you may see something like the following: 
 
 
This particular error was caused by omitting the semicolon at the end of line 12.  Notice that the error message identifies 
the general location of the error.  Lab1P1.java:13 means line 13 of the Lab1P1.java file, and the caret symbol shows it at 
the beginning of the line (actually at the end of the previous line).  Note also that there are more messages available above 
and below what is shown – use the scroll bar to see it all.  For now, if you have errors, carefully compare what you have 
typed to the source code given in step 4 above.  You may want to use the error messages to focus in on particular lines of 
the program.  Remember that every character is important, including punctuation, and that case matters!  If a letter is 
uppercase in step 4, it needs to be typed as uppercase.  If a letter is lowercase in step 4, it needs to be typed as lowercase! 
 
9. Once Lab1P1.java has compiled without error (see step 7), you can run the program by clicking the “Run application” 
  button.  You should see an execution window appear similar to the following: 
 
Your execution window will probably be bigger.  Click File->Print in the execution window and print a copy of this 
execution for a lab deliverable.  Remember that your printout should have your name instead of Jane Doe’s.  Note that the 
execution window will stay on top of any other windows while it is showing.  Once you have printed it, click the close 
button  on the execution window to close it. 
CMPS 161 Lab 1,  Page 4 of 7 
Task 3 (Correcting Syntax Errors) 
1.  Carefully modify your program so that it looks like the one below.   You are deliberately inserting errors so that you can 
see what happens.  The arrows indicate modifications (don't type arrows or notes). 
import acm.program.*; 
import acm.graphics.*; 
import acm.io.*; 
 
class Lab1P1 extends GraphicsProgram 
{ 
  void main() 
  { 
    add(new GOval(0, 0, 100, 100); 
    add(new GOval(25, 25, 10, 10)); 
    add(new GOval(70, 25, 10, 10)) 
    add(new GArc(25, 25, 50, 50, 225, 90)); 
    add(new GLabel("Hello World!", 16 116)); 
    add(new GLabel("My name is Jane Doe. , 0, 132));  
  } 
 
  public static void main(String[] args)  
  { 
    new Lab1P2().start(); 
  } 
  public void run() { main(); } 
} 
Missing closing quotation marks.
Missing ending semicolon.
Unbalanced parentheses. 
Doesn’t match the class name.
2.  Compile the program again, and examine the error messages that result.  Note that if you click on an error message, the 
source code line indicated in the error message will be highlighted in the editing window.  Also note that a caret (^) points 
to the exact spot in each line where the error was discovered (but not necessarily where the offense occurs). Look at each 
error message,  compare it to the change that was made in the source code, and answer the following questions: 
a. What is “expected” in the first error message? 
 
 
b. The second error message seems to indicate a missing semicolon, but the line it references has a terminating 
semicolon.  Where is the missing semicolon, and why does it reference this line? 
 
 
c. Does the caret (^) in the third error message point to an error?                        Why or why not? 
 
 
 
d. The fourth error indicates a missing parenthesis on a line where no modification was made.  What do you think 
caused this error? 
 
 
 
 
3.  Correct the first 3 errors one at a time, starting with the error on line 15, and working back to the error on line 10.  After 
each correction, recompile and observe the difference in the error messages.  Do you now have a better explanation for the 
fourth error, or has your original answer been reinforced?  Explain. 
 
 
 
4.  After correcting the error on line 10, you should notice that all the original error messages have disappeared, but a 
completely new error message is generated for the last error inserted.  This demonstrates that sometimes one set of errors 
will “cover up” other errors.  Correcting one set may simply reveal another, so debugging requires patience as you work 
through all the errors. 
5.  Make sure that the program is working correctly, and save your work before you continue to the next task. 
CMPS 161 Lab 1,  Page 5 of 7 
Task 4 (Documenting Your  Program) 
1.  You should always include some basic documentation in your programs.  Required documentation rules are posted on the 
class website.  Usually, you would include the documentation from the beginning, so that it can assist you as you are 
writing your program, but we will add some now.  So open Lab1P1.java in a jGrasp editing window (if it is not open 
already). 
2.  Add a description of the program.  We will be using doc comments for description and identification information, so start 
by adding the following doc comment just before the class statement.  The first sentence of the description should be a 
descriptive title for the program, followed by additional sentences describing the program in more detail. When finished, 
that portion of the program should look like this: 
 
import acm.io.*; 
 
/** 
 * Sample program #1 for CMPS 161 Lab #1.  This program displays a smiley face, 
 * with the text "Hello World!" and the name of the programmer displayed below. 
 */ 
class Lab1P1 extends GraphicsProgram 
 
Note that doc comments are similar to simple multi-line comments, ending with */ but beginning with /** (instead of 
beginning with /*).  They can be used later to create documentation files using the javadoc system. Every program you 
write must contain a doc comment description for its main class. 
3.  Insert some identification information into the doc comment for Lab1P1.  Add the following doc comment tags as shown, 
replacing the name “Jane Doe” with your full name, “w1234567” with your w-number, “9/6/2005” with the actual due 
date for this lab, and “CSMP 161-01” with your class and section: 
 
import acm.io.*; 
 
/** 
 * Sample program for CMPS 161 Lab #1.  This program displays a smiley face, with 
 * the text "Hello World!" and the name of the programmer displayed below. 
 *  
 * @author:       Jane Doe (w1234567) 
 * @dueDate:      9/6/2005 
 * @classSection: CSMP 161-01 
 */ 
class Lab1P1 extends GraphicsProgram 
 
Every program you write must contain these identification tags in the doc comment description for its main class.  
4.  Add comments to the program body where necessary.  Generally, your program should be self-descriptive, by using clear 
logic and good identifiers.  But sometimes it is useful to add comments to more difficult parts.  So, just as an example, 
modify Lab1P1.java with the following program-body comments: 
    add(new GOval(0, 0, 100, 100)); // Add an oval at position 0,0 which is 100 x 100 
    add(new GOval(25, 25, 10, 10)); // Add an oval at position 25,25 which is 10 x 10 
    add(new GOval(70, 25, 10, 10)); // Add an oval at position 70,25 which is 10 x 10 
    add(new GArc(25, 25, 50, 50, 225, 90));  /* Add an arc at position 25,25 which is 
                                                on a 50 x 50 oval, starting at 225  
                                                degrees and ending at 90 degrees */  
    add(new GLabel("Hello World!", 16, 116)); /* Add the label "Hello World!" at 
                                                position 0,116 */ 
Notice that some of these are single-line comments (beginning with //) and some are multi-line comments (beginning with 
/* and ending with */).  Be careful that you properly begin and end the muti-line comments, or you can accidentally 
comment-out portions of your program! 
5.  In later programs you will need to include comments to describe variables and methods, but this program doesn’t have 
much of that, so we will leave it out for now. 
6.  Compile the program again to make sure that no new errors have been introduced.  If they have, fix them.  Make sure that 
the program will compile without error before you finish, and save Lab1P1.java before continuing. 
 
CMPS 161 Lab 1,  Page 6 of 7 
Task 5 (Getting a Printout) 
1. To print your program, click the “Native print” icon  .  You should see the “jGrasp Print” dialog box: 
 
You can click the “Print Preview” button to see what your printout will look like before printing (which is a good idea).  If 
you look at a preview, close it to return to this dialog box.  Click the “Print” button to continue. 
2. You should now get a standard “Print” dialog box.   
 
Choose a printer in the “Printer Name:” pull-down menu and click “OK” to make your printout.  Save this printout for a 
lab deliverable. 
3. Staple together the printout of this program, the printout of the run (from Task 2 Step 9), and Task 3 (with your answers), 
and turn them in to your instructor. 
Task 6 (Submitting your Program) 
1. When it comes time to turn in assignments, you may be asked to present some output on paper, as described in Task 5.  
However, the primary means of turning in programs is through the Concurrent Versions System (CVS). 
2. Step through the CVS Tutorial to learn how to use CVS.  When you are given an assignment, it will include a specific 
folder name in which to store the required materials for an assignment.  You will notice that the folder structure we used 
for this assignment is the same as that used in the CVS Tutorial.   
Task 7 (Finishing Up) 
1. When you are finished with your session, you should save your work and close jGrasp. 
2. If you are using a public computer, copy any source code files you may have stored on that computer to removable media 
and then delete them from the public computer. 
CMPS 161 Lab 1,  Page 7 of 7