# Picture Lab Part 1

1. Compare forEach to for, answer A4 questions on 2d arrays.
2. Explore the class sturcture of Picture Lab. Answer a5 questions.
3. Work on the Picture Lab together. Find darkest, and lightest Colors of each quadrant.

Here is a method in the IntArrayWorker class that totals all the values in a 2D array of integers in a private instance variable (field in the class) named matrix. Notice the nested for loop and how it uses matrix.length to get the number of rows and matrix.length to get the number of columns. Since matrix returns the inner array in a 2D array, you can use matrix.length to get the number of columns.

```public int getTotal()  {
int total = 0;
for (int row = 0; row < matrix.length; row++)  {
for (int col = 0; col < matrix.length; col++)  {
total = total + matrix[row][col];
}  }

Because Java two-dimensional arrays are actually arrays of arrays, you can also get the total using nested for-each loops as shown in getTotalNested below. The outer loop will loop through the outer array (each of the rows) and the inner loop will loop through the inner array (columns in that row). You can use a nested for-each loop whenever you want to loop through all items in a 2D array and you don't need to know the row index or column index.

```public int getTotalNested()  {
int total = 0;  for (int[] rowArray : matrix)  {
for (int item : rowArray)  {
total = total + item;
}  }

Exercises

1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester.

2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetLargest() and the call to it in the main method of IntArrayWorkerTester.

3. Write a getColTotal method in the IntArrayWorker class that returns the total of all integers in a specified column. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetColTotal() and the call to it in the main method of IntArrayWorkerTester. Questions 1. Open Picture.java and look for the method getPixels2D. Is it there?

2. Open SimplePicture.java and look for the method getPixels2D. Is it there?

3. Does the following code compile? DigitalPicture p = new DigitalPicture();

4. Assuming that a no-argument constructor exists for SimplePicture, would the following code compile? DigitalPicture p = new SimplePicture();

5. Assuming that a no-argument constructor exists for Picture, does the following code compile? DigitalPicture p = new Picture();

6. Assuming that a no-argument constructor exists for Picture, does the following code compile? SimplePicture p = new Picture();

7. Assuming that a no-argument constructor exists for SimplePicture, does the following code compile? Picture p = new SimplePicture();

The following code is the zeroBlue method in the Picture class.

```public void zeroBlue()  {
Pixel[][] pixels = this.getPixels2D();
for (Pixel[] rowArray : pixels)  {
for (Pixel pixelObj : rowArray)  {
pixelObj.setBlue(0);  }
}
} ```

Exercises

3. Using the zeroBlue method as a starting point, write the method keepOnlyBlue that will keep only the blue values, that is, it will set the red and green values to zero. Create a class (static) method to test this new method in the class PictureTester. Be sure to call the new test method in the main method in PictureTester.

4. Write the negate method to negate all the pixels in a picture. To negate a picture, set the red value to 255 minus the current red value, the green value to 255 minus the current green value and the blue value to 255 minus the current blue value. Create a class (static) method to test this new method in the class PictureTester. Be sure to call the new test method in the main method in PictureTester.

5. Write the grayscale method to turn the picture into shades of gray. Set the red, green, and blue values to the average of the current red, green, and blue values (add all three values and divide by 3). Create a class (static) method to test this new method in the class PictureTester. Be sure to call the new test method in the main method in PictureTester.

6. Challenge — Explore the "water.jpg" picture in the images folder. Write a method fixUnderwater() to modify the pixel colors to make the fish easier to see. Create a class (static) method to test this new method in the class PictureTester. Be sure to call the new test method in the main method in PictureTester.