CSCI 121 Lab Assignment 3
|
Outcomes: Upon completion of the lab, the student will know
how to |
|
|
|
· Declare
and initialize one-dimensional arrays |
|
|
· Manipulate
the data in arrays use for-each and while loops |
|
|
· Implement
and use methods that modify Picture objects |
|
Activities: |
|
|
1 |
Login to the lab network using the username and
password assigned to you by the Computer Lab staff. |
|
2 |
Navigate to Mr. Clark’s website. From there go to the
Computer Science I page, then to the Lecture notes and assignments page. The
url is: http://www.wvutech.edu/mclark/CSCI121-2006/cs1_lectures.htm Click on the link to MyPicture.java and
save it to your J: drive. Also click on the link to caterpillar.jpg
and save it to your J: drive. |
|
3 |
Start the DrJava application, and then click the
Open button on the toolbar. Navigate
to your J: drive and open the file MyPicture.java. As we discussed in
class, MyPicture is a Picture with slightly more functionality (once we
define methods) |
|
4 |
Open the Tools menu and select Compile
Current Document. When the
compilation is successfully completed, type the command > java MyPicture in the Interactions pane. This will execute the main method of the MyPicture class. When the Open dialog box appears, navigate to your J: drive and select the file caterpillar.jpg. You should see a drawing a caterpillar appear in a window. |
|
5 |
In the MyPicture main method, change the statement
p.show(); to p.explore(); then recompile and execute the main
method, again choosing caterpillar.jpg.
Use the picture explorer to find the red, green and blue values for
each of the following pixels with specified (x,y) coordinates: X = 20, Y
= 50 X = 113, Y = 32
X = 120, Y = 54
X = 137, Y = 116
Place your answers on the lab sheet. |
|
6 |
In the MyPicture class, define a new method with
header public void increaseRGB(int red, int green, int
blue) that causes a MyPicture object to add the amount
specified by increment to the red component of each of its pixels. To
implement this, you will use the getPixels(), getRed(), setRed(),
getGreen(), setGreen(), getBlue() and setBlue() methods and a Java
for-each statement as discussed in class. Change the main method of MyPicture to this: public
static void main(String[] args) { String
fileName = FileChooser.pickAFile();
MyPicture p = new MyPicture(fileName); /* put
code to test lab assignment here */
p.increaseRGB(-100, -100, 100);
p.show(); } The caterpillar picture should look like this:
Show your code to the lab instructor and have
her/him sign your lab sheet in the appropriate place. |
|
7 |
Redo the method increaseRGB from problem 6
so that it is implemented using a Java while loop instead of the
for-each statement. Do not change the
main method. You will need an extra
variable to make this work. Rerun
your program and show your code to the lab instructor; have her/him sign your
lab sheet in the appropriate place. |
|
8 |
In the MyPicture class, define a new method with
header public void averageRedGreen() that causes a MyPicture object to change the red
and green components of each pixel to the average of their current
values. For example, a pixel with red
= 100, green = 200, and blue = 134 will have its values changed so that red =
150, green = 150, and blue = 134. To implement this, you will use the getPixels(),getRed(),
setRed(), getGreen() and setGreen() methods and a Java for-each
statement as discussed in class. Change the main method of MyPicture to this: public
static void main(String[] args) { String
fileName = FileChooser.pickAFile(); MyPicture
p = new MyPicture(fileName); /* put
code to test lab assignment here */
p.averageRedGreen();
p.show(); } The caterpillar picture should look like this:
Show your code to the lab instructor and have
her/him sign your lab sheet in the appropriate place. |
|
9 |
A color is gray whenever its red, green, and blue
components have the same value. To
convert a picture to grayscale, we can set each pixel’s color to the average
of its red, green, and blue components; that is, (red + green + blue) / 3. In the MyPicture class, define a new method with
header public void grayscale() that converts a MyPicture object to grayscale
using the technique defined above. Change the main method of MyPicture to this: public
static void main(String[] args) { String
fileName = FileChooser.pickAFile();
MyPicture p = new MyPicture(fileName); /* put
code to test lab assignment here */
p.grayscale();
p.show(); } The caterpillar picture should look like this:
Show your code to the lab instructor and have
her/him sign your lab sheet in the appropriate place. |
|
10 |
In the MyPicture class, define a new method with
header public void setEvenPixels(Color color) that sets the even-subscripted (0, 2, 4,…)
elements of a picture’s pixel array to the specified color. Hint: use a while loop to set the color of
pixels[count] and increment count by 2 each time through the loop. Change the main method of MyPicture to this: public
static void main(String[] args) { String
fileName = FileChooser.pickAFile();
MyPicture p = new MyPicture(fileName); /* put
code to test lab assignment here */
p.setEvenPixels(Color.YELLOW);
p.show(); } The caterpillar picture should look like this:
Show your code to the lab instructor and have
her/him sign your lab sheet in the appropriate place. |
|
11 |
Print a copy of your code in MyPicture.java. Give the listing of MyPicture.java and
your completed lab worksheet to the lab instructor. |