CSE 131 Module 1: Methods

Goals

By the end of this lab, you should...

Practice Problems

Important note: Most CSE131 modules will have practice problems section with a quiz. However, do not wait until after the quiz to begin working on the project. You should go a head and start working on the project as soon as you've finished and understood the practice problems.

The purpose of the practice problems is to help you check your understanding of material covered in class. You do not need to turn in your solutions, but should do the practice problems in preparation for the quiz. Solutions to selected exercises are available, but you will benefit most by doing the exercises before you look at the solutions.

There will be an optional help session on Wednesday in Wilson 214 where these problems will be discussed. You will learn more if you do the problems before attending the help session.

  1. Arithmetic Expressions: Draw an expression tree for each of the following Java expressions. Provide the value of each expression. Remember that Java computes left to right, and that negation takes precedence over multiplication and division, which take precedence over addition and subtraction.
  2. a.  (50 - 5 * 4) / 10 - 7
    
    b.  12 + -5 * 2 + 6 / 3
    
    c.  1 + 2 + 3 + 4 + 5 + 1 * 2 * 3 * 4 * 5
  3. Expressions Containing Variables: Draw an expression tree for each of the following Java expressions. Note that each variable is represented here by a single character.
  4. a.  (-b - Math.sqrt(b * b - 4*a*c)) / (2*a)
    
    b.  a + (b * c) - ((d * f) / e) - g
    
    c.  a + b * c - d * f / e - g
  5. String Expressions: Provide the string value resulting from evaluation of each expression, or if the expression would result in an error, explain the error.
  6. a.  "this " + "string " + "has " + "spaces "
    
    b.  "this" + "string" + "has" + "no" + "spaces"
    
    c.  "the sum of " + 6 + " + " + 10 + " is " + (6 + 10)
    
    d.  "the sum of " + "6 + 10" + " is " + (6 + 10)
    
    e.  6 + 10 + " equals " + (6 + 10)
    
    f.  "" + 6 + "+" + 10 + " equals " + 6 + 10
  7. Boolean Expressions: Provide the boolean value (true or false) resulting from evaluation of each expression. (Remember that "&&" represents AND, "||" represents OR, and "!" represents NOT.)
  8. a.  3 > 7
    
    b.  (3 > 7) || (4 < 9)
    
    c.  135 == 100 + 35
    
    d.  (true && false) || (true || false)
    
    e.  (3 > 7) || ((10 < 9) == (3 == 8))
    
    f.  false || !(3 > 7)
  9. Data Type Identification: Identify the type (int, double, boolean, or String) of the value that would result from evaluating each Java expression, or if the expression would result in an error, explain the error. If there is no error, provide the value of the expression as well as the type.
  10. a. 7 / 2
    
    b. 7.0 / 2
    
    c. ((7 / 2) == (7.0 / 2))
    
    d. "x = " + 6
    
    e. 6 + " = x"
    
    f. ((6 * 3) / 80) / (35 - 7 * 5)
    
    g. 1 / 2
  11. Writing Java Expressions for Arithmetic Formulae: Write Java expressions for each of the following arithmetic formulae. In these expressions, use variable names r for radius, w for width, and h for height.
  12. a. the area of a circle with radius r
    
    b. the circumference of a circle with radius r
    
    c. the perimeter of a rectangle with height h and width w
    
    d. the volume of a cylinder with radius r and height h
    
    e. the length of the diagonal of a rectangle with height h
    
       and width w
    
    f. the area of a right triangle with height h and width w
  13. A truth table is a table that shows the value of a boolean expression for all possible assignments to the variables of that expression. Each row of the table corresponds to a possible assignment of the variables and the resulting value of the expression.  Complete the following truth table for the logical operators AND (&&) and OR (||). (Fill in the missing values on each line of the table.)
  14. x y x AND y x OR y
    true true
    true false true
    false true
    false false
     


Downloads

  1. Save this lab1.zip file on your desktop or in the cse131 folder you created in Lab 0.
  2. Start Eclipse.
  3. In the "Package Explorer" at the left edge of your Eclipse window, right click on your CSE131 project, and select "Import..." (If the Package Explorer isn't visible, go to the Window menu and select "Show view -> Package Explorer.")
  4. Choose Archive file and click next.
  5. Using the "browse..." button at the upper right, open the lab1.zip file that you downloaded in step 1.
  6. Make sure that there is a checkmark next to the folder icon. Then click finish.


Project: Image Processing Methods

  1. In Eclipse, open the lab1 package in the Package Explorer. You should see a file called "ImageProcessor.java". Double click on that file to open it in the Java editor. This is the file in which you will write the image processing methods for this lab. Fill in your name and the date at the top of the file.
  2. Notice that there are a few sample methods in the file.
  3. To run the program the first time, right click on "ImageProcessor.java" in the Package Explorer, and select Run -> Java application from the popup menu. You should see a window with three panels. The first two panels should contain photographs of Brookings Hall and Chancellor Wrighton, respectively.
  4. If you open the "methods" menu in YOPS, you will see the names of the sample methods that were provided for you in the ImageProcessor.java file. Try selecting these methods from the menu to see what they do.
  5. To run the program again laber, just click the "run" icon near the upper left corner of the eclipse window. (It's a white triangle in a green circle.)
Directions:  In this lab, you will write methods that perform image processing functions. For each method, you will be given a specification of what the method should do. You will implement the method so that it satisfies the specification. We have provided a skeleton application program called Ye Olde Photo Shoppe (YOPS). After you define write each method, you will run YOPS to test it on actual images.
  1. Read the introductory portions of the YOPS documentation. Stop before reading the section entitled "Operating on each image as a whole," since the rest of the documentation is not necessary for this lab.

  2. Writing methods that operate on primitive values: Recall that every method in Java has a return type and may accept parameters of various types. For example, the following method takes a two double parameters and returns their sum.
    double sum(double a, double b) {
       return a + b;
    }
    
    Recall from the YOPS documentation that each pixel of an image has three color components (red, green, and blue), and that each of these components is an int value in the range 0 to 255, where 0 is no intensity and 255 is full intensity. In this part of the lab, you will write methods whose parameters and return values represent the color components of a pixel. When you execute these method in YOPS, they will be applied to every component of every pixel of the image to produce a new image. Within YOPS, use the browse button to load in image files.

    In the file ImageProcessor.java, write methods as described below. In the method bodies, use mathematical expression. Do not use a conditional (if) statement.

    1. Write a method called copyFirstToSecond that copies the first image to the second panel. (Hint: This is a very simple method.)

    2. Write a method called copySecondToThird that will copy the second image to the third panel. Your method will take two parameters (corresponding to pixels from the first and second images), but you will ignore the first. (Hint: This is also a very simple method.)

    3. Write a method that will composite the images in the first two panels by averaging their components. You will need to take two parameters, which will be the color components from the corresponding pixels in the two images being composited.

    4. Write a method called negative that will produce the negative image by inverting the intensity of each component. (For example, if the parameter value is 0, you should return 255. If the parameter value is 1, you should return 254, and so on.)

    5. Write a method that will posterize an image by reducing the number of possible colors. For each color component, you will choose between two intensities, 0 or 255, which corresponding to that color component being turned off or on completely. So, since each color has three components (red, green, and blue), you will end up with an image that has only 8 different colors. Remember that you are not allowed to use conditionals (if) statements for this part of the lab. (Hints: Recall that color components are in the range 0-255. Also, recall that if you divide an int by another int, the result number will be truncated. For example, 130 / 128 = 1, but 125 / 128 = 0.)

  3. Writing methods that use objects: Reread the part of the YOPS documentation that concerns "operating on each pixel as a whole." Note that each pixel of an image is represented as a Color object. Create and test methods with the following specifications.

    1. Write a method that makes an image brighter by returning, for each pixel, a Color that is brighter than the original. (The Color class makes this easy because it already provides a method brighter() that returns a brighter color.)

    2. Write a method that creates a grayscale image from a color image. To do this, you will take in one Color parameter (the Color object for a pixel from the original image) and will produce a new Color in which all the components (red, green, and blue) have the same value. Hint: To choose which value, average the three components of the original color.

  4. Writing methods that use conditional statements: In this last group of methods, you will use conditional statements to make decisions within the method bodies.

    1. Write a method that produces a black and white image by returning, for each pixel, a Color that is either black or white, depending upon the overall intensity of the original color. For your return value, use the constant Color.BLACK or Color.WHITE. It's up to you how to decide when a color's components, taken as a whole, should be considered black or white.

    2. Write a method that combines two images by choosing for each pixel the color from the image that has the brighter pixel in that location. To determine which pixel is brighter, compare the sums of the red, green, and blue components of the two pixels. Since YOPS will run your method for every pair of pixels, the resulting image will have some pixels from the first image and some from the second image.


Optional Extension

  1. We have provided a pair of images that were taken by the same camera on a tripod, one after the other. The main difference between the two images is that one contains two bears and the other contains only one. You will need to use these images for the rest of this lab. To have YOPS load them automatically, uncomment the last line of code from the "main" method at the end of the file ImageProcessor.java.

  2. Write a method that will take in the pixels from two different images and perform background subtraction. The first image should be considered the background, which you want to remove from the second image. So, if the color of a pixel in the second image is close to the color of the corresponding pixel from the first, then that pixel a background pixel, and you should color it Color.BLUE. Otherwise, use the color of the pixel from the second image. For the provided images, the result should be a bear by itself on a blue background. (Note: We do not live in a perfect world. Since the two images were taken at two different times, lighting conditions are not exactly the same. As a result, corresponding pixels in the background may be slightly different in the two images. This is why you need to determine if the pixels are "close enough," by some measure. It will not be sufficient to see if the corresponding pixels match exactly.) You may have a few stray pixels in the result, but you should devise a closeness measure that avoids having a lot of them.

  3. Write a method to perform background replacement. If the pixel from the first image is Color.BLUE, use the color from the second image. When you compare two colors for equality, use the equals method of the first color. For example, to find out if Color c is blue, you could write c.equals(Color.BLUE) but you should not write c == Color.BLUE because the == operator checks to see if both sides refer to the same object. The == operator does not look inside the objects to see if they contain the same values, so it would evaluate to false for different objects that hold the same values.

    Test your method by putting a different background behind the "bear on blue" image that you created above.

    Note: You can use the browse button above any YOPS panel to load an image file into that panel. Also, since your "bear on blue" image will be in the third panel, you will need to select "reverse" from the YOPS "options" menu to combine the image from the 3rd panel with the image in the 2nd panel to yield a new image in the 1st panel.


What To Turn In:

Follow these submission instructions to turn in the following files and demonstrate your running program for a TA.
  1. cover-page.txt -- Be sure to fill out the provided file completely.
  2. ImageProcessor.java -- Be sure to complete the header information at the top of the file (name, etc.).
Note: Lab 0 and Lab 1 are due on the same day, so you will be doing both demos at the same time. If you complete the learning styles survey for extra credit on Lab 0, remember to turn in your printout of the learning styles survey results when you do your demo.


Kenneth J. Goldman (kjg@cs.wustl.edu)