CS1026 Computer Science Fundamentals I (Java)

Lab 7: nested loops


Objectives
Preparation

Exercise 0: single loops

Run the following in the Interactions pane. In each case, can you predict what the value of sum will be after execution? Note: reset the Interactions pane after each question.

  1.  

    int i = 0;
    int sum = 0;
    while (i < 10)
    {
      sum += i;
      i++;
    }
    

  2.  

    int i = 0;
    int sum = 0;
    while (i < 10)
    {
      sum += i;
    }
    

  3.  

    int i = 0;
    int sum = 0;
    while (i < 10)
    {
      sum += i;
      i++;
    }
    i = 0;
    while (i < 10)
    {
      sum += i;
      i++;
    }
    

  4.  

    int i = 0;
    int sum = 0;
    while (i < 10)
    {
      sum += i;
      i++;
    }
    while (i < 10)
    {
      sum += i;
      i++;
    }
    

  5.  

    int sum = 0;
    for (int i = 0; i < 10; i++)
    {
      sum += i;
      i++;
    }
    

Exercise 1: single loops

In each case, write a loop (in the Interactions pane) that prints the requested output (for the ones involving decimal points, it's OK to obtain for instance 0.7000000001 or 0.6999999999 instead of 0.7).

  1.  

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    

  2.  

    0.0
    0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.7
    0.8
    0.9
    

  3.  

    0.9
    0.8
    0.7
    0.6
    0.5
    0.4
    0.3
    0.2
    0.1
    0.0
    

Exercise 2: nested loops

In class, we implemented a method clearBlue() of the Picture class that cleared the blue in all the pixels of an image, by using the one-dimensional array representation of the imageand a single loop. In this exercise, you will implement a method that does the same thing, using nested loops.

  1. Add the following method definition to your Picture class and fill in the blanks.

    public void clearBlue2()
    {
      Pixel pixelObj;
      // loop through the columns (x direction)
      for (int x = 0; x < this.______________; x++)
      {
        // loop through the rows (y direction)
        for (int y = 0; y < _______________; y++) 
        {
          // get pixel at the x and y location
          pixelObj = this.getPixel(x,y);
          // set its blue to 0
          pixelObj.____________________;
        } //end of inner loop
      } // end of outer loop
    }
    
    Compile Picture.java and test your method in the Interactions pane.

  2. Write the equivalent while loop, in a new method called clearBlueWithWhile.

  3. Start again from the initial version and modify the inner loop, by replacing y++ by y+=2 and test again. Do you understand what is happening? Try again with y+=3 and y+=4

  4. (optional and more challenging) Modify your method so that you obtain stripes of height 10 where the blue is alternatively cleared / not cleared, as on the following example. Before:

    and after:
    Hint: use a third nested loop.

Exercise 3: progressive darkening

In this exercise, you will write an object method progressiveDarken in the class Picture that will darken a picture, but in a progressive manner, to obtain the following effect:

To achieve this, when you are dealing with the pixel at position x, y, store its red component in a variable red and replace it by red*x/getWidth(); do the same with the green and blue.



Exercise 4: looping through pixels

In this exercise, you will access pixels of an image by using its one-dimensional array representation, as returned by the Picture method getPixels()

  1. In the Interactions pane, make a variable pictureObj refer to a picture that contains the image in the file butterfly2.jpg (in your mediaSources folder).

  2. Get the one-dimensional array representation of the image by Pixel[] pixelArray = pictureObj.getPixels();

  3. Complete the following statement, filling in the length of the array in order to show how many pixels there are in the image:

    System.out.println("The number of pixels in the image is " + ___________ );
    

  4. Write a loop that accesses the first 100 pixels in pixelArray and prints each of them (using System.out.println).


Exercise 5: a method for looping through pixels

In this exercise, you will write a method randomChange() that modifies all pixels in the picture. This method will be an object method in the Picture class; it will take no argument, and have a void return type (it will thus be similar to the method decreaseRed() seen in class).

Math.random() is a static method of the class Math that returns a random number between 0 and 1. If x is an int variable, (int) (x*(1+(Math.random()-0.5)/5)) is the product of x by a random number between 0.9 and 1.1. In randomChange, each component (red, green, blue) of the color of each pixel will be replaced by the result of the transformation above

Compile the file Picture.java and test your method in the Interactions pane.


Exercise 6: copying an image

In the Interactions pane, make a variable pictureObj refer to the picture turtle.jpg (in the mediaSources folder); let a variable pixelArray be its array of pixels (as in exercise 2).

  1. Create variables width and height, and assign them the dimensions (respectively width and height) of the image; use the methods getWidth() and getHeight()

  2. Create a new picture pictureObj2 by Picture pictureObj2=new Picture(width, height); and create a variable pixelArray2 that contains the array of pixels for that picture

  3. Run the following loop

    int i = 0;
    while (i < pixelArray.length)
    {
       Pixel currentPixel = pixelArray[i];
       Pixel currentPixel2 = pixelArray2[i];
       currentPixel2.setColor(currentPixel.getColor());
       i++;
    }
    

    and show pictureObj2. What happened? Why?

  4. Reset the Interactions pane, and redo the previous steps (you can use the up arrow to recall old statements), but this time create pictureObj2 by pictureObj2=new Picture(width, 2*height);. Run the same loop as before. What happens?

  5. Start over again; this time, change the loop so that the white lines are at the top of the picture. Hint: what is the pixel at pixelArray2[i+width*height]?.