Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS 161 Page 1 of 4 Lab 2 
CS 161 – LAB 2 
 
Before doing this lab, if you have not finished Lab 1, please do so before starting this lab.  Also, for 
this lab please read chapter 2, sections 2.1 through 2.9. 
 
In this lab, we will begin the process of writing our own program code.  To begin, create a folder in 
your CS161 folder on your H:\ drive for Lab3.  For the lab, we will use the book-exercise project of 
chapter 2 and we will be implementing Exercises 2.83, 2.84, 2.85, 2.86, and 2.87 on pages 59-60 of 
your textbook. Copy this project to your Lab2 folder. 
 
Before attempting to open this project, check to see that it is not read-only.  You can do this either 
using the windows explorer or by double-clicking on the “My Computer” icon and click until you find 
the folder on your H:\ drive for CS161\Labs\Lab2\book-exercise.  Right-click on the book-
exercise folder and select Properties.  When the properties box comes up, if there is a check in the 
box that says Read-only, click to remove the check and click the Apply button.  If there is an option to 
apply this change to all files and subfolders, choose this option. 
 
Now when you open the book-exercise project, only a class called Book appears.  We are going to 
edit this class, so we need to know how to open the editor.  Right click on the Book class and from the 
pop-up menu select Open Editor.  You should see something similar to:  
 
 
 
 
What you see is the editor.  This is where we create and modify code.  It works just like a text editor, 
but it is also tuned to help with writing Java code.  The sentences in blue are comments. They are part 
of the documentation, but not the actual code.  Look at the top where there are two statements, one 
that says  
CS 161 Page 2 of 4 Lab 2 
 
@author (Insert your name here.) 
 
and one that says 
 
@version (Insert today's date here.) 
 
Go ahead and replace the phrase (Insert your name here.) with your name and do the same for 
today’s date.  You may remove the parentheses as you do this. 
 
Proper documentation is important in programming.  Suppose you had a question about one of the 
classes in Java’s built-in library.  From the main view in BlueJ—the one containing the rectangle 
representing the Book class, select Java Class Libraries in the Help menu.  This should start up a web 
browser that takes you to the official Java documentation site.  If you look at the panel on the left 
labeled All Classes and scroll down until you find the class String, you will discover that String is 
more than just a data type.  It is a class with a great many interesting and useful methods.  Most of 
these will not make sense to you yet, but, in time, they will. If you wanted to know more about the 
String class, this is where you could go to find out. 
 
Now, back in the editor for the Book class, there is a box in the upper right-hand corner that says 
Source Code.  Click that box and you should see the option 
 
 
 
Select the Documentation option and look at the result.  You should see that BlueJ automatically 
generated documentation that looks just like the official Java documentation.  You should also note 
that the documentation generated by BlueJ came from the comments in the code.  That is, if there 
was a comment such as: 
 
 /** 
  * This is the method to my madness! 
  */ 
 
This comment will be part of the documentation.  In fact, BlueJ helps us create this style of comment.  
We will see this when we start writing our own methods.  You may also close the web browser now. 
 
Now, we will begin with exercise 2.83 on page 59.  “Add two accessor methods to the class—
getAuthor and getTitle—that return the author and title fields as their respective results.  
Test your class by creating some instances and calling the methods.”  First you might study the 
getName method that is on page 52 of your text book.  These two methods will be quite similar to that 
method. 
 
The two accessor methods should be declared as public, as they will be used to ask an object for 
information.  Since both author and title fields are String, these methods need a return type of 
String.  Neither method requires any extra information to perform its task, so neither method will 
require parameters.  Thus, the first method will look similar to the following. 
 
 
 
 
/** 
 *  Returns the value in the author field 
CS 161 Page 3 of 4 Lab 2 
 */ 
public String getAuthor() 
{ 
 return author; 
}  
 
Now, type this first method in the editor below the comment in gray that says 
 
 // Add the methods here ... 
 
Remember, however, to stay inside the outer class wrapping.  That is, you must add this method above 
the closing curly brace of the class. 
 
Once you have added this method, click the Compile button, and—if all goes well—you will see the 
message at the bottom of the editor:  
 
 
 
Also check the Documentation view. Does your newly added method appear in the documentation 
along with the comment that you added?  If all this happens: Congratulations!  You have just written 
your first method! (And if not, you will now experience the meaning of the term debugging.) 
 
Now you may add the second accessor method on your own.  It will be similar to the first.  Do not 
forget to add appropriate documentation, compile the program, check the Documentation view, and 
as the textbook exercise states, “Test your class by creating some instances and calling the methods.” 
 
At this point you are ready to try Exercise 2.84, page 60.  “Add two methods, printAuthor and 
printTitle, to the outline Book class.  These methods should print the author and title fields 
respectively to the terminal window.”  These two methods will not return a value, hence they will be 
void methods, but they will use Java’s built-in printing method System.out.println.  You might 
study the printTicket method on page 41 of your text book before continuing.  We not only want to 
print the value of one of the fields, but we want to identify which field we are printing.  This means 
we will be using string concatenation.  The first method, then, will look like the following. 
 
/** 
 * Prints the author field to the terminal window 
*/ 
public void printAuthor() 
{ 
 System.out.println( “The author is ” + author ); 
}  
 
In the method above, notice the space after the word is.  Also notice that the author field does not 
have quotes around it.  Type this method in the editor, compile it and fix any syntax error you made, 
check the Documentation view, and test your method by creating some instances of this class and 
calling your method.  Is the output correct?  Does the output make sense?  If this method is correct, 
you can implement the printTitle method on your own. 
 
If you have gotten this far, you are ready to try Exercise 2.85.  “Add a further field, pages, to the 
Book class to store the number of pages.  This should be of type int, and its initial value should be 
passed to the single constructor, along with the author and title strings.  Include an appropriate 
getPages accessor method for this field.”  As you do this, test for syntax errors by compiling the 
modified class. Remember to add appropriate documentation.  Check the Documentation view to be 
sure your documentation makes sense.  And, as always, “Test your class by creating some instances 
← This is what you want to see. 
 
CS 161 Page 4 of 4 Lab 2 
and calling the methods.”  Note, there is an old programmer’s maxim that is bad grammar but good 
advice: “Write a little; test a lot.” 
 
Exercise 2.86: “Add a method, printDetails, to the Book class.  This should print the details of the 
author, title, and pages to the terminal window.  It is your choice how the details are formatted.  
For instance, all three items could be printed on a line, or each could be printed on a separate line.  
You might also choose to include some explanatory text to help a user work out which is the author 
and which is the title, for example 
 
 Title: Robinson Crusoe, Author: Daniel Defoe, Pages: 232” 
 
You should be able to do this exercise on your own.  If you are having trouble with the formatting, try 
printing just the title first.  When this works, try adding the author to the output.  When both 
title and author appear the way you want them to, add the pages.  The idea is to create the 
output a little at a time rather than all at once. 
 
Now you are ready for exercise 2.87.  “Add a further field, refNumber, to the Book class.  This field 
can store a reference number for a library, for example.  It should be of type String and initialized 
to the zero length string ( “” ) in the constructor as its initial value is not passed in a parameter to 
the constructor.  Instead, define a mutator for it with the following signature: 
 
 public void setRefNumber( String ref )  
 
The body of this method should assign the value of the parameter to the refNumber field.  Add a 
corresponding getRefNumber accessor to help you check that the mutator works correctly.” 
 
When you have completed all these exercises correctly, print out a copy of the code in the editor.  The 
print option is in the Class menu of the editor.  Your name and the date should already be on the 
printout but double check that this is so.  When you have the printout, demonstrate the following for 
your lab instructor.  (Please run these tests yourself to be sure all work correctly before showing 
them to your lab instructor.) 
 
1. Create an instance of the Book class using the following: The Hobbit (title) by J. R. R. Tolkien 
(author), 288 pages. 
 
2. Demonstrate the getAuthor and getTitle methods.  (Ex. 2.83) 
 
3. Demonstrate the printAuthor and printTitle methods.  (Ex. 2.84) 
 
4. Demonstrate the getPages method.  (Ex. 2.85) 
 
5. Demonstrate the printDetails method.  (Ex. 2.86) 
 
6. Demonstrate the setRefNumber method using the string PR6039.032.  Then demonstrate the 
getRefNumber method.  (Ex. 2.87) 
 
7. Open the editor and show your lab instructor the Documentation view. 
 
8. When all these pass successfully, give the printout to your lab instructor and breathe a large 
sigh of relief. 
 
 
Note: This lab was originally written by Dr. Broeg.