Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Part 2d: Carve and Carve Many

Implement the following method to carve seams from a Picture:

  • public Picture carve()
    should return a new Picture of a size one pixel less in width than this. The new Picture should be the same as this except with the seam (found from computeSeam) removed.
  • public Picture carveMany(int numSeams)
    should return a new image similar to carve, but it should have removed numSeams number of seams. You’ll want to call carve in a loop to implement carveMany.

The carve method is similar in spirit to showSeam, except that instead of coloring the seam red, it removed those pixels altogether. Because of this, you won’t be able to use the Picture constructor that copies this, because your newPicture has to have a different size! In this case, use the brand-new Picture constructor, e.g., starting with:

Picture newPicture = new Picture(newWidth, newHeight);

where you’ll need to define newWidth and newHeight based on the height and width of this! From there, you’ll need to copy pixel colors from this to newPicture. Notice that we always copy the colors of Pixels, not the Pixels themselves… . However, in doing this, your code will need to be careful to skip the seam’s pixels and to shift over all of the pixels to the right of the seam by one pixel place to the left. For the carveMany method, you will want to loop numSeams times, carving once each time! Note that you can write a line such as this:

Picture p = this;

and then re-assign p to the result of p.carve() the correct number of times!

Tests

When you complete this part, all of the tests from PictureTest_Carve.java and PictureTest_CarveMany.java should pass.