i have a computer science 201  assignment to be done by tonight homework + extra credit

profileabutid
cmpt_201_homework_5.htm

CMPT 201 Westminster College Homework 5 Due Thursday, September 19 at 11:00 pm

Before You Begin - Time Estimate

Before you begin this assignment, read through this description and estimate how long it will take you to complete this assignment (designing, programming, commenting, testing, and debugging). Write this estimate down in your README. Then keep track of the time you spend on this assignment. While completing this estimate and measuring your time will be part of your grade, you will not be graded on how long the assignment took or how close your estimate was.

Estimating the time it takes to develop software is very difficult, even for experienced programmers. Comparing your estimates to your real development time should help you improve your estimates.

Questions:

These questions explore using boolean expressions with while loops. If you get confused by the output as you work through these questions, you should review those pages in your textbook, with special attention to the operators &&, ||, and !.

For these eight exercises, you need to type what will be printed by the while loops. You do not need to run the loops - in fact, you should be able to determine what is printed without running the code. Of course, you can double-check your work by running the loop in the Interactions pane!

In Questions.txt, you should copy and paste the output from Dr. Java (or just report what you think will happen for that loop). If a loop does not stop (an infinite loop), click the Reset button in Dr. Java and report an infinite loop.

  1. 		    int x = 0;
    		    while (x < 3)
    		    {
    		         x = x+1;
    		         System.out.println(x);
    		    }
    		
  2. 		    int x = 3;
    		    while (x > 0)
    		    {
    		         x = x+1;
    		         System.out.println(x);
    		    }
    		
  3. 		    int x = 2;
    		    int y = 0;
    		    while (x < 10 && y < 1)
    		    {
    		         x = x+1;
    		         y = y+1;
    		         System.out.println(x + ", " + y);
    		    }
    		
  4. 		    int x = 2;
    		    int y = 0;
    		    while (x < 10 || y < 1)   // only change
    		    {
    		         x = x+1;
    		         y = y+1;
    		         System.out.println(x + ", " + y);
    		    }
    		
  5. 		    int x = 12;    // only change
    		    int y = 0;
    		    while (x < 10 && y < 1)
    		    {
    		         x = x+1;
    		         y = y+1;
    		         System.out.println(x + ", " + y);
    		    }
    		
  6. 		    int x = 12;    // change initialization
    		    int y = 0;
    		    while (x < 10 || y < 1)    // change condition
    		    {
    		         x = x+1;
    		         y = y+1;
    		         System.out.println(x + ", " + y);
    		    }
    		
  7. 		    int x = 12;    // change initialization
    		    int y = 0;
    		    while (!(x < 10 || y < 1))   // change condition
    		    {
    		         x = x+1;
    		         y = y+1;
    		         System.out.println(x + ", " + y);
    		    }
    		
  8. 		    int x = 2;
    		    int y = 0;
    		    while (!(x < 10 && y < 1))   // change condition
    		    {
    		         x = x+1;
    		         y = y+1;
    		         System.out.println(x + ", " + y);
    		    }
    		
    

Program: Picture

Start with the Picture class from lab. Make a copy of this file (still called Picture.java) and store it in a new folder. It should already have many methods in it (like clearRed). You should use these methods as a reference as you write the following methods in this class:

    public void blueOnly() public void maxBlue() public void negate() public void darken() public void posterize() public int getPosterizeValue(int original)

    Then save a new copy of Lab5.java and rename it Homework5.java.

    On Westminster Anywhere, to set up Dr. Java, you will need to set the classpath to: F:\CLASSAPS\javaclasses\cmpt201\mediaClasses

    You can find several image files here: F:\CLASSAPS\Helen Hu\201\mediaSources\

    If you have trouble setting up your laptop, look at these directions and the Picture exercise we did together in class.

    blueOnly

    Begin with the Picture class from lab. It's okay if you haven't completed the lab yet; just remember to make a copy of the Picture class and put it in another folder.

    Write a method that will keep only the blue color (and set green and red to zero). Be sure to be thinking about how to do this with a minimal amount of new code (to earn full credit on this method).

    public void blueOnly()

    maxBlue

    Next, write a method that will maximize the blue color (set it to 255), but leave the red and green colors untouched.

    public void maxBlue()

    negate

    Write a method to create a negative of the original image. The values of the each pixel will be the inverse of the original value. For example, the new red value will be 255-originalRed.

    public void negate()
    This method will create images like:

    darken

    The next method is very similar to the lighten method from step 7 of the lab. You will need to use the Color class. You'll need to use a loop to retrieve individual Pixels, and then inside the loop, you'll want to retrieve the Color of each Pixel. Use the Color class' darker method to darken the entire image.

    You may need to reference the API to recall how these three classes differ:

    posterize

    We can posterize an image by reducing the number of color values in the image. Currently, red, green, and blue can be set to any integer from 0 to 255. If we reduce the possibilities to four: 31, 95, 159, and 223, then we can create a posterized effect.

    Write a method to posterize an image:

    public void posterize()

    Your method should check each color value (red, green, and blue) separately. You can either use 12 if-else if statements (most of the credit), or you could write a helper method with four if-else branches (and call the helper method three times from posterize) for full credit. A helper method is a method that helps you simplify the original, more complex method. By writing this helper method, we are able to write fewer if-statements, but still execute the same number of if-statements. Your helper method should be called:

    public int getPosterizeValue(int original)

    This helper method should take an original color value and return the new color value (31, 95, 159 or 223). You have used many String methods that return a value, but this is the first time you'll be writing a method that returns a value. Refer to your textbook (pages 273-275 in Section 5.1) for an explanation of how to use the keyword return.

    Your getPosterizeValue should declare a int variable (newValue). It should then have several if-else branches that calculates the newValue, based on the value of original (the argument to getPosterizeValue). After the if-else branches, as the last line of your getPosterizeValue method, you will return newValue:

            return newValue;
    Notice your getPosterizeValue method only needs to work with ints; it does not need to worry about Pictures, Pixels, or Colors.

    The original posterize() method would then call this getPosterizeValue method three times.

    If the original value was between: Set the new value to:
    0 - 63 31
    64 - 127 95
    128 - 191 159
    192 - 255 223

     

    You should turn in two images: original.jpg and posterize.jpg.

    Extra Credit: 

    In the homework assignment, you wrote a method that posterized an image by limiting the number of red / green / blue values to 4. Write a general method that will posterize an image for any number of color levels:

    public void posterize(int numLevels)

    It's okay that this method has the same name as what you did for the original homework assignment. The compiler will accept both in the same program, because the method headers differ slightly (one takes an argument, and the other does not).

    If numLevels is 4, the output image will be identical to the output from the original homework assignment. If numLevels is 256 (or higher), there will be no change to image, because the original number of color levels was 256 (0 to 255). 

    There is a way to do this WITHOUT a lot of repetitive code by using loops!

    If you're not sure how to start this, you should realize that this extra credit program would need to derive the values (0-63, 31, etc) in the above table from a numValues of 4. What would these values be if numValues were 2?

    The following files should be submitted in a single zip file:

    • Questions.txt
    • Picture.java - with blueOnly, maxBlue, negate, darken, and posterize
    • A Homework5.java with code that calls explore, darken, and then explore again.
    • an example image from negate (negate.jpg)
    • an example image from posterize (posterize.jpg)

    As always, use good programming style for your code, which includes:

    • using meaningful variable names
    • using proper indentation
    • including a comment block before the program that describes what the program does and includes your name and the assignment
    • including a comment block before each method that describes what the method does
    • adding comments to any code that is confusing
    Also submit a README file, describing which java files you handed in, what the goals of the program were, whether you achieved them, and what outside resources you used (if any).