CS 40 Fall 2009 Homework 6

1. Using Greenfoot and PictureWorld, make a subclass Homework61 of Picture with a method imprison that takes an arbitrary photo, and puts it behind bars, like this:

In this case, the original picture of the cat is located in the PictureWorld images folder. (It is Dr. Cay Horstmann's cat. I am pretty sure that he is a criminal, so I have put him behind bars.)

This is much simpler than it looks. Check out the MyPicture1 class that comes with PictureWorld. One of the methods is almost what you need. You can probably guess what the getX and getY methods do. The % has nothing to do with percentages. In Java, it computes the remainder of a division. For example, 15 % 2 is 1 since there is a remainder of 1 when dividing 15 by 2. In general, a number is even if number % 2 == 0. Here, I have made the bars start every 50 pixels, and they are 5 pixels wide. A fairly simple modification of the method given in MyPicture1 will do the trick. (The new method, other than its new name, can be exactly the same as the old one, except for a change to just one line of code.)

Grading criteria:

Is the image striped vertically?

Are the "bars" a bit further apart, and with a width of more than one pixel?

Is the class called Homework61 and the method called imprison?

2. Using Greenfoot, make a subclass Homework62 of Picture with a method

   public void swapRedAndGreen()
   {
      // your work here
   }

that swaps the red and green components of every pixel.

For example, consider a pixel with lots of red and a little green:

double red = pix.getRed(); // assume it gets 255
double green = pix.getGreen(); // assume it gets 10

Afterwards, that pixel should have redness 10 and greenness 255.

If you try this out on the road photo, the sky should still look blue, but the trees should be red. (Sorry, it doesn't actually work so well on that photo. Try some other photos. Or, try to swap two other pairs of colors.

Grading criteria:

Does it really swap two colors? (You can pick which two, but the code should be named accordingly.)

Does it leave the 3rd color alone?

Is the class called Homework62 and the method called swapRedAndGreen? (Or, two other colors if they are swapped instead.)

Attach just the two files Homework61.java and Homework62.java to your submission. They will be in your PictureWorld folder.