Review studio procedures before
Some guidelines for this week's studio:
- Form groups of no more than three people. If your group
is joined by a fourth person, split into two groups of two.
- Try to pair with somebody who attended this morning's optional lecture.
- Be sure you are signed in with a TA and that you are cleared before
- Use two computers for this studio, with one open to the studio web page and one
for the eclipse workspace.
- Load the eclipse workspace using the same directions as you used for
studio 1, but use studio2 as the prefix to your
- The code from class should appear in the lecture package of
We will first write a tool that reads a file and dumps (prints out)
its contents as it is actually represented in that file. We will use this tool
to study how various data types are represented persistently.
- Open the Dumpster class.
- Notice how the class already imports java.io.*, which
contains the classes we need to do input and output (I/O).
- The String passed to the constructor is the name of
the file that should be dumped.
- In the constructor, do the following:
- Instantiate a new
object based on
the String passed to the constructor.
- Use that File object to instantiate a new
At this point, eclipse should complain about some unhandled exceptions. Click
on the yellow light bulb to see the possible fixes:
Discuss amongst yourselves the tradeoffs in the above approaches, and
settle on what your group wants to do. There is no right answer here,
but you must be prepared to defend your choice and explain its
- You can catch the exception here using try...catch.
- You can propagate the exception back to the caller by adding the
throws .... clause to the constructor's declaration.
- Use that FileInputStream object to instantiate a new
If you decided on the try...catch approach for the previous
statement, then you can extend the scope of the try block
to include this statement as well.
- Look at the objects you instantiated so far and at the documentation
of the java.io package
(scroll down to the Class Summary).
What are the responsibilities of the three objects you instantiated above?
Explain how the Decorator pattern is used to build
increasingly sophisticated input streams.
- Make sure that the DataInputStream object is available via
an instance variable in your Dumpster class.
- Now write a method void dumpNextByte() that:
- reads then next byte of the DataInputStream object using
its readByte() method.
- Receive the result of readByte() as a byte
- You must again figure out what to do with the
exceptions that might get thrown from this action. Remember that the goal
is just to dump the file, and that end of input will eventually happen.
- Prints out the the byte using System.println.
For now, you can just print out the byte, but you may eventually
want to print out the location of the byte in terms of how many bytes have
been printed before this one.
You may also want to see the byte written in a different format as well
as the integer format. For example, writing the byte in hex makes it
easier to see the bit pattern, since each 4-bit group corresponds to one
Here is a method that converts an int into a String
showing its hex representation. A numerical character means what it says.
The character A corresponds to 10, B to 11, and so on.
- Next write the boolean isDone() method that returns
true iff there is no more useful information in the file.
You should arrange for an instance variable to keep track of whether
the file has useful information remaining. The exceptions thrown by
the code in dumpNextByte() could set this instance variable to true.
- Now test your Dumpster program by running it
(as a Java application). If you look at Dumpster's main method,
you see that it will use the test.txt file.
It is important at this point to understand why you see what you see.
The test.txt file contains ASCII characters, encoded as
shown in this table.
- Based on your analysis, answer the following questions in your feedback file:
- What is the numeric value of a lower-case a?
- What is the numeric value of the character(s) that separate lines?
- What is the numeric value of the character(s) in front of the word fun?
What does that value mean?
Modify test.txt using the eclipse editor and rerun the
Dumpster to experiment with various other texts.
- Modify the main of your Dumpster program so that
test.bin instead of
- Run your program again and discuss what you see?
The test.bin file contains a consecutive
stream of bytes with values from 0 to 100.
Don't try to modify the test.bin file: it has binary data in
it that cannot easily be edited in eclipse.
We next experiment by writing out some primitive data types to
persistent storage, and then using your Dumpster program
to examine the results.
- Open the Experiment class, and take a look.
Figure out what the class does, getting help if needed.
- Run the Experiment class as a Java application.
This will write the file exper.bin, which was included
in your repository.
It will also run your Dumpster on the file to show you the
output, which is what the persistent file really contains.
You should see the single byte with value 32.
Now have some fun to answer the following questions.
- In each case,
simply modify the run() method in Experiment to
cause things to be written to the exper.bin file.
- To write out a primitive type, find the appropriate
method (writeByte, writeInt, writeBoolean, etc.)
Don't change your Dumpster code -- let it continue to read
the file as a byte stream, so you can see what is inside.
- Modify run() so it writes the byte 132 instead of 32.
What does your Dumpster show and why?
representation if needed.
- Try writing 132 using writeInt. What do you see and why?
- Try writing the boolean values true and
false. What do you see?
- As time permits, try writing other types and seeing how they look.
If you have time, look at the weasley package and discuss
the implications of the design we did in class. Talk through how you
might implement the model and/or view.
- Complete out the feedback.txt file and be sure to commit your
- Remember the group name you had for today so you can refer to your work later.
Futher investigation on your own
Consider how Java I/O libraries use the
Submitting your work (read carefully)
- Complete the feedback.txt file and be sure to list
your group members' names and ID last-3-digits.
- Answer the other questions in the feedback.txt file.
- You must commit all of your work to your repository. It's best to do this
from the top-most level of your repository, which bears your studio name.
- You must have the TA clear you by signing your studio group sheet.
Be sure your names and IDs are written
clearly on the TA's demo sheet.
Last modified 08:59:25 CDT 03 June 2010
by Ron K. Cytron