Recitation R10
- 2D Arrays, Start P7
Summer 2016
CS160: Foundations in Programming
The purpose of this lab is to:
- Practice coding multidimensional arrays
- Do some simple image manipulations
- Test your understanding of recent topics
Phase 1
The purpose of the assignment is to write a Java class that can be called by a user interface
to manipulate images in the Portable GreyMap (PGM) format. To do this you need to write an R10
object that implements an interface called RecitationInterface, and instantiates and calls methods
in PictureLibrary.java, and is in turn called from a graphical user interface (GUI) class
called RecitationProgram.java. All of these are supplied below.
Write the following program from scratch in the following phases:
Phase 2
- Create a new project and class called R10, without a main method.
- Download and import PictureLibrary.java
- Download and import RecitationProgram.java
- Download and import RecitationInterface.java
- Make the class implement the RecitationInterface interface, your TA will explain this.
- Do NOT modify any of the given files, just import them into your R10 src folder
Phase 3
- At the top of the class, declare the following instance (non-static) variables:
- An object of type PictureLibrary, set to null
- An integer to store the image width, set to 0
- An integer to store the image height, set to 0
- A 2-dimensional array of integers to store the image data, not allocated
- Name your variables whatever you want
- Create a constructor for the R10 class as shown below that instantiates an object
of type PictureLibrary into the associated class instance variable.
public R10(){
//Instantiate PictureLibrary object
}
Phase 4
Implement the methods declared in the interface, but put the code in R10.java!
readImage and writeImage
The readImage method should call the readPGM method on the PictureLibrary object, passing
the input file name, then it should call the getHeight, getWidth, and getData methods
to fill in the class instance data defined above. The writeImage method should call the
setData method in the PictureLibrary object with the image data, then call the writePGM method
passing the output file name. The parameters and return types of the methods in PictureLibrary.java are not
documented here, so you must look at the file to find them. The calls to readImage and writeImage
should be wrapped in a try catch block as follows:
try {
// Calls to readPGM or writePGM and associated code here
} catch (Exception e) {
System.out.println(e.getMessage());
}
imageData
Implement imageData by simply returning a reference to the image array.
negateImage
Calling negateImage inverts each pixel by subtracting the pixel value from MAXVAL which is
set in the PictureLibrary class. We suggest writing only these four methods first, then testing them before
implementing the remaining transformations.
increaseContrast
Calling increaseContrast subtracts 16 from pixels with 0 <= value <= 127 and adds
16 to pixels with 128 <= value <= MAXVAL. Do not allow pixel values to overflow MAXVAL or become
negative, i.e. clamp the pixels to 0 when subtracting and MAXVAL when adding. Your TA will explain
what is meant by clamping.
decreaseContrast
Calling decreaseContrast adds 16 to pixels with 0 <= value <= 127 and subtracts 16 from pixels with
128 <= value <= MAXVAL. No clamping is necessary.
Phase 5 - Testing
We have provided a test file called Cam.pgm that you can download to
the R10 project directory (not the src or bin). Test all of your methods by using the associated menu
items in the user interface.
HackerRank
- Once your are finished go here and complete a few more problems on 2D array manipulation on Hackerrank.
Submit your R10.java program to Checkin and complete the associated
Hackerrank problems to get full credit for this lab. This program
should make a good starting point for the P7 programming assignment.
© 2016 CS160 Colorado State University. All Rights Reserved.