|
|
Homework J7
Due: Tuesday, 3 May 2005 by 11:59 p.m.
|
Testing
- An explanation is available of what actions
the tester() method of the GUI class performs.
Objectives
- Gain experience with the manipulation of multi-dimension lists
- Continue to practice class implementation
- Demonstrate effectiveness of simple list manipulations for problem solving
Files
Two files need to be downloaded: Pixelator.java (a homework template) and
GUI.class. The two files should be kept in the same folder.
Only one file needs to be
submitted: Pixelator.java. Class Pixelator provides a fading mechanism for observing how one graphical
image can fade into another image. The initial, final, and intermediate
graphical images are stored as a two-dimensional array with int-valued elements. Class GUI provides a way for you to test your Pixelator implementation. The
default constructor for class GUI creates a graphical user interface that
enables uses of the GUI to select both initial and final images. The interface
also provides a button whose selection initiates a testing of Pixelator.
We provide the initial and
final images.
Pixelator Requirements
Constructor
- public Pixelator(int[][] i1, int[][] i2)
- Configures the new pixelator so that
- Its initial and final image representations are the ones indicated by
i1 and
i2.
- Fading is inactive.
- The current out image is null.
Instance methods
- public int[][] getImage()
- Returns a reference to the out image array of the pixelator.
- public int getImageWidth()
- If the out image is null, then the method returns 0. Otherwise, the method
returns the width of the out image array of the pixelator.
- public int getImageHeight()
- If the out image is null, then the method returns 0. Otherwise, the method
returns the height of the out image array of the pixelator.
- public boolean canIncreaseFade()
- If fading is inactive, the method returns false. Otherwise, the method returns
whether there are any untransformed pixels in the out image.
- public void unitFadeIncrease()
- If a fade increase is possible, then a randomly selected untransformed out
image pixel is modified to be a copy of the corresponding pixel in the final
image. Otherwise, no action is taken.
- public void percentageFadeIncrease(int n)
- If a fade increase is possible, then m single fade increases are attempted,
where m is n% of the pixels in the out image (if m is not a whole number then
m
is rounded up). Otherwise, no action is taken.
- public void stopFade()
- The out image is made null and fading is made inactive.
- public void beginFade()
- If either the initial or final images are null or if the initial and final
images have different dimensions then a stop fade is performed. Otherwise, the pixelator is configured so that
- Fading is active
- The pixels in the out image are copies of the pixels in the initial image.
Therefore, none of the out image pixels are considered to be transformed.
Class methods (static methods)
- public static int[][] copy2DArray(int[][] grid)
- If grid is null, then null is returned. Otherwise, the method returns a new
two-dimensional array whose values are copies of the values in array grid.
- public static void randomize(Object[] list)
- If list is null, no action is taken. Otherwise, the method rearranges the
values in list in a uniformly random manner (i.e., where an initial element
value ends up is equally likely to be at any of the element position in list.
How we did it – some observations on instance variables and implementation
Instance variables
We maintained six instance variables to represent the current pixelator
situation. Methods made sure that these variables always reflect the current
goings-on of the pixelator
- private int[][] initialImage
- Represents the initial image of the fading process.
- private int[][] finalImage
- Represents the final image of the fading process
- private int[][] outImage
- Represents the current image of the fading process
- int numberTransformed
- Indicates the number of pixels in the out image that have been copied from the
final image
- private boolean fading
- Indicates whether a fading process is underway
- Point[] copyOrder
- Represents a random order of the pixel locations of the out image
Constructor observations regarding the state of our instance variables
- The initial image and final image should be deep copies of the array
parameters. The copying can be accomplished by configuring initialImage and
finalImage to be the targets of copy2DArray() invocations.
- Fading is inactive and the out image is null. These two activities can be
accomplished by a stopFade() invocation.
- The values of the other two instance variables are inconsequential until the
fading process begins.
Method beginFade() observations regarding our implementation
- If the initial image and final image do not make sense for a fade, then a stop
fade is performed and the method returns.
- Otherwise a new fading process begins. The implications of a new fading are
- The out image should be a copy of the initial image.
- No image pixels have been copied as of yet from the final image.
- Array copyOrder must be turned into a random ordering of the pixel locations
of the out image. To produce a random ordering
- Set copyOrder so that it initially represents the pixel locations in flow
order (0, 0), (1, 0), … (width – 1, 0), (0, 1), … (width – 1, 1), …, (0,
height
– 1), … (width – 1, height – 1).
- With only a little imagination the flow order can be accomplished with nested
loops.
- Rearrange copyOrder through an invocation of method randomize()
Method randomize() observations regarding our implementation
- Our algorithm
- Let n be the length of the list.
- Iterate i over the values 0 through n-1.
- On the i-th iteration determine a random value j in the inclusive interval 0
through n-1.
- Swap the values in the list elements indexed by i and
j.
Testing
In an effort to test your code, we have provided a seires of
testing routines for you to use. In order to run them, change your
main() method to contain only the line:
GUI.tester();
NOTE: If you downloaded the GUI.class file
prior to 8:45 p.m. on Monday, 25 April 2005, you will have to download
it again (and overwrite your old GUI.class with the new one)
This line replaces the two lines of your main() method
that are in the skeleton code (you probably want to comment the
existing lines out so you can easily restore them later). When run,
the output should be the following.
randomizeWorks: yes
copy2DArrayWorks: yes
constructorCompletes: yes
constructorWorks: yes
beginFadeWorks: yes
stopFadeWorks: yes
canIncreaseFadeWorks: yes
getImageWidthWorks: yes
getImageHeightWorks: yes
getImageWorks: yes
unitFadeIncreaseWorks: yes
percentageFadeIncreaseWorks: yes
For each method, it will simply print a 'yes' or 'no' if the method
works (or does not work) correctly. Using this tester()
method, you can test the methods that you write one at a time. Once
they are all working properly, you can restore the original
main() method, and test out the entire program.
Submission
When you are finished,
submit
Pixelator.java.