Scanner
(LineScanner)Scanner
, you will see a constructor that accepts a String
as it's argument. The semantics
of this constructor is that the passed string is treated like an input source upon which you can then call the various Scanner
methods:
next
, nextInt
, etc. Thus, for example, if one had the file:
1 2 3 4 5 6 7one could read in a line at a time, and then using each line as an input source, you could then process the integers on each line.
The basic code structure of this technique is:
… Scanner fileScanner = new Scanner(new File(filename); while (fileScanner.hasNextLine() { // while there are lines left to read String line = fileScanner.nextLine(); // read next line of file Scanner lineScanner = new Scanner(line); // set up a Scanner to read the 'contents of the line' … // use lineScanner just like any other Scanner object // i.e., you can call all theThis technique can be useful when one want to limit their processing of data to that on a single line of a file. UsinghasNext
andnext
methods … }
next
or
nextInt
doesn't work because those methods treat newlines like blanks (i.e., all whitespace is treated in the same fashion),
so one doesn't know when the end of the line has been reached. While there are several ways of handling this, the above-described technique of
reading in a line at a time — using nextLine
and then creating a new Scanner
object from the resulting string
returned is a fairly straightforward way of accomplishing this.
Write an application class, named LineScanner
, that opens the file numbers.text
and prints out the number of integers
on each line of the file. Again, to accomplish this, read each line into a String using nextLine
and then create a second Scanner object
using that string as the constructor's argument.
Sample Test Run
For example if the file numbers.text
contains (notice the blank third line):
1 2 3 5 10 15 20 25 10 9 8 7 6 5 4 3 2 1the program should produce the following output:
There are 3 numbers on line 1 There are 5 numbers on line 2 There are 0 numbers on line 3 There are 10 numbers on line 4
LineScanner
), as specified, requires nothing more than a main
method;
i.e., we will not be instantiating LineScanner
objects, and as such there is no state and behavior.
main
method that
is doing all the work.
Scanner
The file numbers.text
contains data in the following format:
header1 int1 int2 … header2 int1 int2 … …i.e., each line begins with a header value, followed by that number of integers. Your program's task is to print out the average of each of these sequences.
However, the file contents is often corrupted by the time it gets to your program, so the data must be validated. Here are the possible problems:
Sample Test Run
For example if the file numbers.text
contains:
3 1 2 3 0 1 12 -12 14 1 2 3 5 1 2 3 4 5 6 4 10 20 30 40the program should produce the following output:
The average of the values on line 1 is 2.0 *** Error (line 2): Line is empty - average can't be taken *** Error (line 3): Header value of 0 - average can't be taken The average of the values on line 4 is 12.0 *** Error (line 5): Corrupt line - negative header value *** Error (line 6): Corrupt line - fewer values than header *** Error (line 7): Corrupt line - extra values on line The average of the values on line 8 is 25.0 There were 3 valid lines of data There were 5 corrupt lines of data
main
method, than a repository of state and behavior; i.e.,
again, we will not be instantiating DataChecker
objects, rather we will merely be running its main
method.
main
method that opens the numbers.text
file and read in the lines
(using hasNextLine
and nextLine
), and passes each line to the average
method
described in the next bullet.
main
should also catch any exceptions thrown by average
static
method (I called it average
) that accepts the line (as String
), creates a
Scanner using that string as its argument, and then reads in the header and then the other data items on the line
using the newly created Scanner, throwing an exception when a problem is detected.
static
; i.e.,
the structure of this program is similar to that of your 1115 programs.
User
class:
getUsername
and getHint
methods (you should getPassword
method).
verifyPassword
method that accepts a string and returns true/false depending on whether the
parameter matches the receiver's (i.e. the User
's) password
toString
method that returns User username
.
read
method (i.e., accepts a Scanner
, reads in the username, password, and hint from
a file and uses them to create and return a new User
object).
In CodeLab, I will be supplying a UserApp
class to test your class by reading in User
s from a file using
your read
method, print out the User
using your toString
, and makes sure the password is not the same as the
username by calling verifyPassword
passing it the username.
For example if the file users.data
contains:
weiss puppy2 woof-woof arnow java cuppa tenenbaum tenenbaum da-same sokol brooklyn collegethe program (i.e.,
UserApp
) should produce the following output:
User weiss hint: woof-woof OK -- the password is different than the user name User arnow hint: cuppa OK -- the password is different than the user name User tenenbaum hint: da-same *** Error the password should not be the same as the user name User sokol hint: college OK -- the password is different than the user name
User
object, the password is no longer accessible to the outside world (i.e.,
there is no getPassword
method). What we're doing here i not realy all that realistic —
in a real system, passwords would be encrypted, but this just gives you a sense of what designers of such classes
think about.
main
within the
class itself, or create a second app class). Feel free to use the UserApp
class I describe above as a role model.
main
in your class).
Authenticator
:
User
(use a capacity of 100 — I would recommend using a class constant for the capacity
the way I do in my classes in the lecture notes).
size
Scanner
on the file and reads in User
objects
authenticate
that accepts a username and password and attempts to authenticate them against
the User
array (by doing a search).
verifyPassword
) causes an exception with a
different message to be thrown (this one with the password hint included).
void
, i.e., the method returns nothing if the username and password are matched;
otherwise an exception is thrown, as described above. (This is a common pattern for authentication methods — if everything
is fine, the method simply returns, otherwise it throws an exception.)
Authenticator
class by supplying it with a user data file,
and then a sample login session. The login session consists of:
Authenticator
object, passing it the name of a file of user data (in the example below, the name of the file is users.data
)
authenticate
method of the Authenticator
object created above
Sample Test Run #1
For example if the file users.data
contains:
weiss puppy2 woof-woof arnow java cuppa sokol brooklyn collegeexecution of the program should look like:
Sample Test Run #2
Given the same users.data
file as above, execution of the program should look like:
Sample Test Run #3
Given the same users.data
file as above, execution of the program should look like:
AuthenticaorApp
prior to submitting this
to CodeLab.
main
Authenticator
class of Lab 4.2.2, code an AuthenticatorApp
application that reproduces the behavior of the sample runs of Lab 4.2.2, i.e., your app should instantiate an
Authenticator
object passing it the filename users.data
, and then proceed to prompt the user at the
keyboard for a user name and password; handling any exceptions in the manner described in Lab 4.2.2.
Authenticator
class.
AuthenticatorApp
class (and not the User
and/or Authenticator
classes).
FileNotFoundException
, prompt the keyboard for another file name. This process repeats until
a valid (i.e., existing) file name is provided.
Here is a small writeup on how to use command=line arguments in a Java app.
Here are some sample test runs trying to open the file FileOpener.java. We'll highlight the command-line argument in green
Sample Test Run #1
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
Sample Test Run #2
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
Sample Test Run #3
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
Name
Class (Name)Name
with the following behavior:
String
)
toString
(last name, first name)
equals
methods (first and last name are equal).
read
that is passed a Scanner
object and returns a Name
object created from data (last and first names)
read in from the scanner.
Name
and other classes. Feel free to use them when developing in your IDE (or write your own).
PhoneNumber
class (similar but not necessarily identical to the one on the exam):
number
String
or three int
s
String
: a constructor that accepts a string representing the phone number (you can assume that
this argument is in the proper format of (nnn)nnn-nnnn
.
getAreaCode
, getExchange
, and getLineNumber
that return the first three digits,
the middle three digits, and the last four digits respec. of the number
isTollFree
that returns true if the number is a toll-free phone number (i.e., the area code begins with an '8').
toString
(prints the full number in its proper format)
equals
(eqaul when the full numbers are the same)
read
I will be using an app class similar to the one provided for Name
.
Name
and
PhoneNumber
classes of the previous two labs to create a third, more complex class named PhonebookEntry
.
The basic structure is the same — only now you will be employing (leveraging) methods of the previous two classes when coding this new class.
The state for this class is:
Name
named name
PhoneNumber
named phoneNumber
Name
object and a PhoneNumber
object and assigns them
to the corresponding instance variables
getName
, and getPhoneNumber
methods to return the corresponding instance variables.
toString
, equals
, and read
methods with the usual semantics
Name
and PhoneNumber
classes … feel free to use
your own (as long as they conform to the specs, of course).
And again, I will be using an app class similar to the one provided for Name
.
toString
and read
methods of this class are coded using the corresponding methods
of the classes of the instance variables (Name
and PhoneNumber
)
PhonebookEntry
object as a string, one would get the string representation
of its instance variables (i.e., call the toString
methods of the Name
and PhoneNumber
classes with the instance variables as the receivers), concatenate those values, and return the result.
read
method: to read a PhonebookEntry
one 'reads' a Name
object
by invoking the read
method of the Name
class (passing it the Scanner
) parameter);
similarly for then reading in a PhoneNumber
object. These two objects are then used to create a PhonebookEntry
object (in the read
method of the PhonebookEntry
class) and the result returned.
PhonebookEntry
method as passing the buck
to the inner (Name
and PhoneNumber
) methods to perform their reads
and then using the results to create the larger (PhonebookEntry
object).
Name
, PhoneNumber
and PhonebookEntry
.
The basic output is identical to that of the previous version, but now:
read
method of your PhonebookEntry
class
(which in turn uses the read
methods of the Name
and PhoneNumber
classes).
equals
methods of the Name
and PhoneNumber
classes in your lookup
and reverseLookup
methods.
toString
methods to print out information.
100
the capacity of your Phonebook
array
Exception
) if the capacity of the Phonebook
array is exceeded.try
/catch
around your entire main and catch both FileNotFoundException
s and
Exception
s (remember, the order of appearance of the exception types in the catch blocks can make a difference).
Sample Test Run #1
For example if the file phonebook.text
contains:
Arnow David (123)456-7890 Harrow Keith (234)567-8901 Jones Jackie (345)678-9012 Augenstein Moshe (456)789-0123 Sokol Dina (567)890-1234 Tenenbaum Aaron (678)901-2345 Weiss Gerald (789)012-3456 Cox Jim (890)123-4567 Langsam Yedidyah (901)234-5678 Thurm Joseph (012)345-6789here is a sample execution of the program:
Sample Test Run #2
If the file phonebook.text
contains:
… … … More than 100 entries … … …here is a sample execution of the program:
Sample Test Run #3
If the file phonebook.text
is missing,
here is a sample execution of the program:
equals
methods of the classes.
Phonebook
class.