CS 140 - Homework 4 - Due Feb 4

Name:______________________________________


Read sections 4.3 and 4.4 in your book.

This section is full of fun methods to add to the Picture class (in the Picture.java file in your P:\cs140\intro-prog-java\bookClasses\ directory). To really get a handle on what's going on in this next section you should implement and test every program that's in section 4.3 in the book.

  1. Explain in one sentence what the following Picture method does (this would be a method which might exist inside the Picture.java file). Try and figure out what it does and how it works just by reading the code.
            public void mysteryMethod() {
               Pixel[] pixelArray = this.getPixels();
               Color c = new Color(255,255,255);
    
               for (Pixel pixelObj : pixelArray) {
                 pixelObj.setColor(c);
               }
            }
    				
  2. Below is the definition of a method decreaseRed, that reduces the red in a picture by 50%. Implement it in your own Picture.java file and make sure you understand how it works.
          public void decreaseRed() {
            Pixel[] pixelArray = this.getPixels();
    
            for (Pixel pixelObj : pixelArray) {
              int value=pixelObj.getRed();
              value = (int) (value * 0.5);
              pixelObj.setRed(value);
           }
         }
    		
  3. Rename this method extractRedChannel and modify it so that instead of reducing the red component of each pixel by 50%, it replaces the green and blue component of each pixel with the pixel's red value. For example, if the original value of one pixel was (r=123,g=38,b=210), the modified value should be (r=123,g=123,b=123). Write (or attach a printout) of your method below.
  4. Implement the clearBlue method (page 108) but instead of using the while loop that's given to you, replace it with a for loop so that it still does the same thing. while loops are introduced on page 94, and for loops are on page 117. Additionally, there are many examples in Section 4.3 that use both for and while loops if you need extra help. Write your method below or attach a printout.