Do these steps:
- In your workspace, make sure you have the latest homework files from
the base repository. This will create a hw09 directory in which you
will do your work.
- You may be familiar with the concept of ASCII art. The basic idea is
that you use letters and symbols available on a typical keyboard to create
pictures. Take a moment to look at the four ASCII art images in hw09
directory.
- In your repository (in the hw09 directory) create a document
called answers.txt and in it record your answers to the following
questions.
- Each of the images begins with two numbers. What do you think is the meaning
of those numbers?
- Now take a look at Driver.java. Notice it refers to a
non-existent class called MyCanvas. The purpose of MyCanvas is
to allow an ASCII image to be loaded and have parts of the image filled
with a “color” (conceptually, each character/symbol on the keyboard
represents a different color). Of course if you are filling an area of
an image with a color there needs to be some sort of “stop character”
that marks a boundary which the filling should not cross.
- Based on the description just given and based on how the MyCanvas
class is being used, create an ASCII-ish UML diagram for this class.
Include the private attributes you guess will probably be helpful in
implementing the class. HINT: You will probably need a 2-dimensional
array, among other things. Example of ASCII-ish UML diagram:
=========================
MyCanvas
=========================
- type attr1
- type attr2
=========================
+ type method1(params)
+ type method2(params)
=========================
- Add and commit your answers.txt document which contains your
answer to the questions and the UML diagram.
Begin by showing your prelab work to the instructor. Then do these steps:
- Create a new document MyCanvas.java and begin implementing
the MyCanvas class you considered in the pre-lab assignment.
Create private attributes and describe their purpose. Also, provide
JavaDoc comments for the class. Attributes should include a 2d array of
char variables along with the number of rows and columns currently
in use. At some point you will need to store the identified “border
character” as well. (You can always change/add something later if
needed.)
- Go ahead and create a load() method, but for the moment don't
worry about loading an image from a file. Instead, reserve memory for
a array of characters. Then manually fill in the 12 slots
with whatever characters you want. Also, set the attributes that store
image size appropriately. Make sure your work compiles.
- Now write the show() method which should display the image
appropriately. Your show method should not be hard-coded to work
only with the array. Its limits should be based on
the attributes of the class.
- Test your show() method and then document it. Commit your
progress.
- Modify your load() method so it will accept the name of
an ASCII art document as a parameter and will reserve memory for
the characters:
- Add a parameter to accept the name of a file.
- In your driver pass image1.txt as a parameter when
calling load().
- Using Scanner open the file and close it. Use try-catch
or throws to handle the exception. Verify that your
code will compile and run.
- Begin by reading the number of rows and columns on the first
line of the file. Store the values in the appropriate attributes.
- NOTE: Scanner does not have a .nextChar() method
so you will have to read one line at a time. Once you have read
the line you can either copy the individual characters into
your 2d array or you can use String's .toCharArray()
method to convert the letters in a single command. Depending on
which approach you take will affect how you reserve memory for
the array. Start by reserving memory and read/store just one
line of the file.
- Once the first row is correctly being read and stored, go ahead
and read the entire file.
- Test, document, commit, push.