Homework 11: Working with Java libraries and classes

The homework problems below will help you prepare for the final debugging quiz.

Introduction

Mandarin duck

In this homework you will write your own classes and test cases to simulate some basic image transformations on a series of images of ducks.

To begin, download and unzip the folder here. It contains the following files:

  • five JPEGs of images of ducks
  • four text files that correspond to pixels used for desired results in test cases
  • Tester.java, a file you will run to test your solutions
  • DuckImage.java, a starter file for one of the classes you need to write
  • ImageFunctions.java, a library you can use for some pre-existing image functionality to help you with this assignment

In addition to the files above, you will also be creating a file called ImageFolder.java that will allow you to store a bunch of images.

Please note that this is an individual assignment and students must complete it on their own.

Image files

The images in this homework are color images of different species of ducks. They are all 300x200 pixels long and tall, and, because they are in color, these pixels are stored as grids of red, green, and blue pixels. Each image has three such sets of pixels that, when displayed on your monitor, look like normal color images to the human eye.

To get a sense of how this works, examine how the following color image of a cell was created using its red, green, and blue (RGB) channels:

RGB channels

To learn more about using RGB channels for images, you can read the article that hosts the image above here.

In this homework you will be manipulating the red, green, and blue grids of pixels for images to transform them by flipping them, converting them to grayscale, and taking the average of a set of images.

Completing the assignment

Open the Tester.java file and complete the instructions at the top.

Remember that you can compile all Java files in a folder with javac *.java

As shown in the test cases, you can use some of the library functions to print out the transformed images you are creating. This should help you debug your assignment. Furthermore, don’t forget about using print statements to help you debug!

How to greyscale an image

One of the methods you need to write is to convert an image to greyscale. A greyscale image is just a black-and-white image; you’ve seen these before!

For reference, it’s possible to have a greyscale image of just one channel, where each pixel is between 0 and 255 in a range from pure white to pure black. For this assignment, however, we will continue to use the three channel RGB values and apply a formula to make greyscale images.

It turns out you can use the following weighted formula (read about it more here) to convert an RGB pixel into a greyscale pixel:

int grey_pixel = (int) (redPixel* 0.299) + (int) (greenPixel * 0.587) + (int) (bluePixel * 0.114);

Now, if we only needed one output channel, we’d be done, but in our problem we still have the three RGB channels. To solve this, we can write the grey_pixel value back into each of the three colored pixels; when the image is rendered the human eye will see these colors as black and white images.

Extra credit

In the bottom of Tester.java, write code that loads five new images of whatever you choose into an ImageFolder object called extra_credit. Note that all these images should be the same width and height (you can typically resize images on your computer using various applications). Then, write a new method for the ImageFolder class that performs a different kind of transformation, transforms all the images in the folder, and saves each transformed image as extra_credit_X.png where the X is replaced with numbers 1-5 that match the original images.

Up to five extra points can be earned here, based on the difficulty of the transformation. Note that we expect students to have different extra credit images and transformations since they are working individually.

Grading

You will be graded on:

  • 5 pts: Having five sufficient explanations of what the test cases are doing (one short paragraph each). ChatGPT/LLM-sounding answers will not get credit. Your answers must demonstrate understanding the test case, not just writing out what every single line of it does.
  • 2 pts: Writing the sixth test case correctly
  • 3 pts: Having three sufficient explanations of what the methods in ImageFunctions.java are doing (one short paragraph each). ChatGPT/LLM-sounding answers will not get credit. Your answers must demonstrate understanding the test case, not just writing out what every single line of it does.
  • 6 pts: pass all test cases in Tester.java without harcoding.
  • up to 5 pts extra credit

We will reduce points for any English answers that exhibit any of the following:

  • Overly long, flattering, and/or flowery writing that does not get to the point
  • Generalizations and other high-level statements that do not answer the question and do not demonstrate an understanding of the test cases
  • Unnecessary equivocation (talking about multiple possibilities or discussion of multiple answers) and ambiguous meanings.