[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
[CS152 2005S]
[CS152 2005F]
Assigned: Friday, February 17, 2006
Due: 8:00 a.m., Monday, February 20, 2006
Summary: In this assignment, you will rewrite your
readInt method.
Purposes: To help you further explore exceptions; To give you the opportunity to improve a method you will regularly call.
Contents:
At this point in your Java career, you've regularly written programs
that read integers. You've gone from explicitly writing a sequence
of lines to using a readInt method that you've written.
The basic readInt method is defined as follows:
public static int readInt(PrintWriter pw, BufferedReader br, String prompt)
throws Exception
{
pen.print(prompt);
pen.flush();
String response = br.readLine();
return Integer.parseInt(response);
} // readInt(PrintWriter, BufferedReader, String)
Unfortunately, the standard readInt has a number of failings.
For example, it does not reprompt the user if the user has entered
something that does not seem to be an integer. It also refuses to
accept perfectly reasonable responses, like one.
Rewrite readInt so that it is friendlier. In particular,
readInt is interactive (that is, it is reading from
the keyboard rather than from a file), reprompt for a valid value as
long as the user does not enter a valid value.
When you are satisfied with your work, send me the code of
readInt. You need not send your enclosing class
or test methods.
There are, as always, a variety of ways with which you can earn extra
credit. One of the most natural ways is to support a wider variety
of written descriptions of integers (such as one thousand two hundred
and twelve
or one hundred thousand
).
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
[CS152 2005S]
[CS152 2005F]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue May 9 08:31:10 2006.
The source to the document was last modified on Wed Feb 22 08:44:11 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Homework/hw.09.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby