Professional Documents
Culture Documents
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
11. Suppose you are given a text file that contains the
names of people. Every name in the file consists of a first
name and last name. Unfortunately, the programmer that
created the file of names had a strange sense of humor and
did not guarantee that each name was on a single line of the
file. Read this file of names and write them to a new text file,
one name per line. For example, if the input file contains
Bob Jones Fred
Charles Ed
Marston
Jef
Williams
the output file should be
Bob Jones
Fred Charles
Ed Marston
Jef Williams
Solution:
Solution:
Solution:
Solution:
15. Even though a binary file is not a text file, it can contain
embedded text. To find out if this is the case, write a
program that will open a binary file and read it one byte at a
time. Display the integer value of each byte as well as the
character, if any, that it represents in ASCII.
Technical details: To convert a byte to a character, use the
following code:
char[] charArray = Character.toChars(byteValue);
The argument byteValue of the method toChars is an int
whose value equals
that of the byte read from the file. The character represented
by the byte is
charArray[0]. Since an integer is four bytes, byteValue can
represent four
ASCII characters. The method toChars tries to convert each
of the four bytes to a character and places them into a char
array. We are interested in just the character at index 0. If a
byte in the file does not correspond to a character, the
method will throw an IllegalArgumentException. If the
exception is thrown, display only the byte value and continue
on to the next byte.
Solution:
Notes:
This project is deceptive: it requires more programs than the ones listed in the
problem definition to process binary and text files containing floating point numbers;
additional programs are required to create and view the data files. The solution shown
here has separate programs to create and view data files, one for text and another for
binary files. The program to process text files requires a method to translate the
numbers from a String to type double. The easiest way is to use the wrapper
class Double’s parseDouble method.
1) Develop the class to create and view binary data files. Good models to follow
are Doubler, Listing 10.8, for the overall program organization, and
BinaryOutputDemo, Listing 10.5, for showing the file contents.
2) Develop the class to process the data in a binary file (display the high, low and
average), again using Doubler, Listing 10.8, as the model.
Use the programs from these two steps to develop similar programs for text files, but
use TextFileOutputDemo, Listing 10.1, and TextFileInputDemo, Listing
10.2, as models for writing to and reading from text files.
References:
Solution:
Notes:
This project is similar to Project 1: data files need to be created before any processing
can be done and it is easier to work with binary files than text. So a good approach is
start with the binary file classes from Project 1 and modify them so data files can be
created and displayed, then write the program to process the binary data files. Note
that a separate program to display data files is necessary to view the files created by
the program that removes the duplicates. If students do not have the programs from
Project 1 to work from, then they could start with files from the text,
BinaryOutputDemo.java (Listing 10.5) and Doubler.java (Listing 10.8).
After all the binary file programs are written and tested, it is easier to develop the
programs to create, read and process text files. Just as with Project 1, the code for
setting up the input and output streams needs to be changed, the while-loop condition
needs to be changed to end when a null string is read, and text Strings in the data
file must be changed to ints by using the parseInt method in the Integer class
References:
Solution:
Notes:
This project is deceptively simple. The problem statement is clear enough, with only
a couple ambiguities to clarify. The solution shown here assumes that there is at least
one line in the file to process and keeps all tabs and newlines unless they precede the
first word on the first line. A helper method processes the first part of the first line to
remove all leading white space and capitalize the first letter. After that, main simply
reads one line of text at a time until it gets a null string, and uses another helper
method to process the remaining text. This helper method is where most of the work
is done: It processes each line, character by character, and uses flags to control the
processing; to print only one space when there more than one in sequence, and to
capitalize the first word in each sentence. Notice how the helper method to convert a
character to upper case is written. The code first checks to see if the character is a
lower case letter, and, if it is, it does integer arithmetic to convert the ASCII lower
case code to the ASCII upper case code. A check of an ASCII chart will show that the
upper case codes are 32 less than those for lower case. After doing the subtraction it
is necessary to cast the integer result back to char to match the method’s return type,
char.
References:
Solution:
Notes:
This project requires careful placement of try/catch blocks to gracefully respond to file
names and Species that do not exist, and, at the same time, allow the user to continue
looking for Species in a file or choose another file to search.
References:
Solution:
Notes:
This project requires only one new file since the classes to build and view Species
files are provided in the text. Populations, of course, are not unique, so more than one
species may have the same smallest or largest population. If more than Species has
the smallest or largest population the solution simply displays the record for the first
one it encounters it its sequential search through the records in the file. Test data files
should include the following situations:
multiple records with the smallest and largest populations,
populations that are out of order numerically,
records with largest and smallest values in various positions in the file (the
first record, the last record, and an intermediate position),
the record with the smallest population placed before the one with the
largest, and
the record with the largest population placed before the one with the
smallest.
References:
Solution:
Notes:
This project requires only one file to be written. This solution is organized as a
sequence of just four method calls in main, one to open an input file for reading
Species records, one to open an output file for writing new records, one to do the
processing (read a record from the input file and write a record to the output file with
the population changed to the projected population after 100 years), and one to close
the input and output files.
References:
Solution:
Notes:
The solution for this project makes use of a couple methods that break out the
processing of a line. The major method processes and marks a line for a single
abbreviation. It finds the index of the abbreviation in the line and then breaks the line
up into 3 parts. It then gets the character immediately before and after the
abbreviation and checks to see if either is a letter or digit. If so, then we assume that
the abbreviation is part of a legal word and don’t mark it. Otherwise, we splice in the
<> marker. This is done in a while loop that processes the remaining part of the line
until the abbreviation is not found.
Solution:
Notes:
One difficulty in the solution to this project is that we have not yet seen the collection
classes. We will read the telephone numbers into an array. We either need to
determine the number of objects in the file or expand the array as we read the objects.
This solution reads the file twice, once to determine the number of objects and then to
read the objects into an appropriately sized array.
Once this is done, we make a single change and then write the file back out.
References:
Exercise 10.6
Solution:
Notes:
References:
Solution:
Notes:
This project requires only one file and has a structure similar to Project 6 (in fact it has
the same code for getInputFile() and closeFile() methods.
References:
Solution:
Notes:
Fairly short text-file program. The dictionary of words can be used for many other
puzzle-based programs involving words.
Solution:
Write a program that reads the CSV file and calculates the
average number of positive axillary nodes detected for
patients that survived 5 years or longer, and the average
number of positive axillary nodes detected for patients that
died within 5 years. A significant diference between the two
averages suggests whether or not the number of positive
axillary nodes detected can be used to predict survival time.
Your program should ignore the age and year fields for each
record.
Notes:
The case study in Listing 10.4 shows how to process a CSV file and makes a good
starting point for this project.
References:
Listing 10.4
Solution:
Notes:
This project creates a simple little applet that will let students explore the formatting
of various kinds of files. It is very helpful to have a couple methods that process the
file. This solution has two such methods. The first method gets 20 bytes and converts
them into characters. It uses the method toChars from the Character class to create an
array of 8 characters of which the only one we want is the first. The second method
opens the file and calls the first method to get the initial 20 bytes for the display.
Solution:
Notes:
Surprisingly, using just the built-in text area component of Java a simple editor can be
created. The text editing is done via the operating system on the text in the
component. What is left for us to do is to read and write the file, which is
accomplished with a pair of methods.
Solution: