Java辅导、辅导CS编程、Assignment
- 首页 >> Java编程Assignments are to be turned in on paper. On every assignment, write your name,
course and section number at the top. Write your name as it appears in your university
records! Use the section number for which you are registered! We will not do detective
work to figure out who did what. Work must be legible. Homework is due on paper in
discussion section, usually at the start except when the assignment indicates that time
to work on the assignment will be provided in class. Exceptions will be made only by
advance arrangement with your TA (excepting "acts of God" outside your control).
Never push homework under someone's door!
1. Background: The code for class ScanSupport given at the end of the notes for
Lecture 10 on Sept. 26 raises some questions: Consider writing a nextFloat method
to add to ScanSupport. A preliminary version of this would be very similar to nextName
except that the final two lines would be:
sc.skip( number );
return Float.parseFloat( sc.match().group() );
a) ScanSupport has no tools equivalent to the hasNext methods of the scanner. If there
is no next name on the current input line, what does nextName() return? (This has an
impact on the pattern you use and it is the origin of an error in the above code.)
(0.5 points)
b) Give a declaration for the pattern named number above that recognizes 10, 9.12, 87.
and .6345 (for example), as well as handling the possibility that there is no floating
point number before the end of line. (0.5 points)
c) Fix the code given above so that it returns Float.NaN if there is no next floating
point number on the current input line. (That's a special float value that represents
something that is not a number. If you have a float named f, f.isNaN() tests to see if
the value is not a number.) (0.5 points)
2. Background: Look at the code distributed with Lecture 10 last week. Several
changes were made from the code presented previously. One change is that
several error messages that had been created by concatenating numerous
components have been simplified by using this.toString() in place of most of the
concatenations.
A question: Why not use this.toString to replace the concatenations in other error
messages. For example, why not replace the no-such-intersection error messages
in Road? (0.5 points)
3. Background: In the code distributed with Lecture 10, this code appears:
if (sc.hasNextFloat()) {
travelTime = sc.nextFloat();
if (travelTime < 0.0F) {
Errors.warn( "Negative travel time:" + this.toString() );
travelTime = 99999.0F;
}
} else {
Errors.warn( "Floating point travel time expected: Road "
+ sourceName + " " + dstName
);
travelTime = 99999.0F; // no failure needed, use bogus value
}
Assume you have written ScanSupport.nextFloat(sc,message) where the message
parameter is the context part of the error message to output if there is no next
float, and where it returns Float.NaN if there is no next float.
A question: Rewrite the above code to use this version of ScanSupport.nextFloat. (1
point)