Problem Solving Assignment 5
Due: Tuesday, February 3rd, 11:59pm
There are two problems in this PSA.
Note 1: Besides writing the code there is an interview process required
for this PSA. Your interview is worth 10% of the PSA grade and is done individually.
Note 2: You will be required to submit your result from Part B in the class
wiki. This will count for 5% of your PSA grade.
Be sure to leave your submission as a comment in the PSA5 section.
Collage
Description:
For this part of the assignment, you will be making a collage. The first file you will create is
PSA5A.java. This file will choose a picture, and copy it twice. This will allow you to have 3
copies of the picture. Then you will call a filter method on each picture. Then you will send
the 3 pictures to a collage method, that will put them all on one picture.
The three methods are going to be your favorite 2, plus the mirror filter (which can be found in
the book or here .
The collage method will take in all three pictures and copy them onto a canvas (blank picture)
that is the exact size of all three put together (more on that later).
Overall this part of the assignment will put three filtered pictures next to each other on one picture.
Methods:
- Add three methods to Picture.java.
- Each method should change its calling object.
- Choose your favorite two filters, they can be ones that you create yourselves, or ones
such as these here .
- Your third filter should be some kind of mirror image. (found above or on page135-136 of your text)
- These methods are not specified other than you must change the calling object, we
are encouraging you to be creative. But note that part B of this assignment might take some
more time. You will have a chance in the next assignment to take this collage and make it better.
Collage method
- This method should take in 3 parameters. They are the pictures that will be put onto
the calling object.
- You should have 3 nested for loops.
- The first loop:
Outer Loop:
for(sourceX = 0, targetX = 0; sourceX < pic1.getWidth(); sourceX++, targetX++)
Inner Loop:
for(sourceY = 0, targetY = 0; sourceY < pic1.getHeight(); sourceY++, targetY++)
Inside the Loop:
sourcePixel is pic1's pixel at (sourceX, sourceY). targetPixel is calling object's pixel (targetX, targetY). Change targetPixel's color to sourcePixel's color.
- The other two loops are exactly the same except:
Outer Loop 2: for(sourceX = 0, targetX = pic1.getWidth(); sourceX < pic1.getWidth(); sourceX++, targetX++)
Outer Loop 3: for(sourceX = 0, targetX = 2*pic1.getWidth(); sourceX < pic1.getWidth(); sourceX++, targetX++)
There is an example of this on page 143-144 of your text.
Using methods in an application:
- Create a file called PSA5A.java
- In this file you will choose one picture, but copy it twice, so that you have three instances of the same photo.
- You should show the original picture (just one instance is fine).
- You should then create a picture that is the same height as the one you chose, but three times as wide.
- Picture constructors to keep in mind can be found here here
.
- Then call each method you created on one of the pictures.
- On the picture you created, you should call the collage method, sending in the three pictures you altered.
- Then you should show the final image and write it to your current directory:
picture.write("collage_name1_name2.jpg");
- Please see the following
image for an example.
This is the barbara.jpg collage, the first is grayScale, the second is createPattern and the third is mirror.
Picture Flip
Refer to this picture for understanding the flipping.
Method 1:flipVertical
- Add a method called flipVertical to Picture.java
- This method will take in 3 parameters, the x and y coordinates of the
middle of the box to flip, and the size of the box to flip.
- First, this method should figure out the top left most x and y coordinates and save
those to variables. Then is should save the bottom right most x and y coordinates and
save those to other variables.
- Then you should have two nested for loops. These will loop through the proper x
and y coordinates and allow you to flip the box specified.
- The outer for loop should loop through all y coordinates, so from the
top left most y coordinate to the bottom right most y coordinate.
- The inner for loop should loop through half of the x coordinates, so from the
top let most x coordinate to the x coordinate that was passed in as a parameter (aka the middle x).
- Inside of the nested for loops you should get the target pixel, which is the pixel that the
for loops are on, and the source pixel, which is the x-opposite of this pixel. Then you should swap
their colors. There is an example drawing to help explain this here.
- NOTE: Don't forget to use a "friend" variable that will hold one of the colors when
swapping.
Method2:flipHorizontal
- Add another method to Picture.java called flipHorizontal
- This method is almost exactly like flipVertical except everything is reversed. The outer loop will loop through
all of the x pixels, from the top left most x to the bottom right most x. The inner loop will loop through
half of the y pixels, from the top left most y to the y that was passed in as the parameter (ala the middle y).
- The inside of the nested loops are pretty much the same, except backwards, we expect you to
mess around with it and think about it to figure it out.
Using methods in an application
- Create a new file PSA5B.java (you can start with this template
here).
- Create a new picture.
- Show the original picture.
- Using either a for loop or a while loop, loop three times. Each iteration of the loop should include
what was given to you, plus the call to the flipVertical method.
- Have a second loop that does the same as the first, except you should call flipHorizontal.
- Be sure to repaint the picture for each iteration of both loops to see the changes in progress.
- here is the picture we want to alter
- Please see the following
image for an example.
This is the barbara.jpg with a vertical box flipped of size 50 at (100,100)
- Please see the following
image for an example.
This is the barbara.jpg with a horizontal box flipped of size 50 at (100,100)
Details
You must put a comment at the top of your code that specifies, for both you
and your partner, your: real names, login names, and PIDs, And a two to four
sentence description of the program (one description for each program).
Use a multi-line comment (/* */).
You must observe an 80 column limit. No line of your program may be longer
than the 80th column on the screen.
How to turnin your homework electronically
When you are ready to turn in your homework, first open a terminal window by right clicking on the empty area of your computer's desktop (have to use
lab computers or remotely connect from your home computer using SSH or Putty)
and select "Open a New Terminal". Then, run bundlePSA5 from your PSA5/bookClasses directory.
3 files are required to be submitted for PSA5:
- Picture.java
- PSA5A.java
- PSA5B.java
If you don't have one of these files present or have misspelled the name of one of these files,
the bundle script will complain! Here you can see how you can run these commands:
-bash-2.05b$ cd ~/PSA5/bookClasses
-bash-2.05b$ bundlePSA5
Good; all required files are present:
Picture.java
PSA5A.java
PSA5B.java
Do you want to go ahead and turnin these files? [y/n] y
OK. Proceeding.
Performing turnin of approx. XXXX bytes
Copying to /home/linux/ieng6/cs8f/turnin.dest/cs8wxx.PSA5
.
Done.
Total bytes written: XXXX
Please check to be sure that's reasonable.
Turnin successful.
Make sure the Turnin is successful, otherwise we won't receive your submission! . If you want to make changes after you've turned in your homework, make the changes and run bundlePSA5 again. The program will warn you that a "previously turned-in file exists" and ask if "you wish to over-write this existing file". Respond in the affirmative by typing y and pressing the Enter key.
Getting your interview done INDIVIDUALLY
10% of your grade on this assignment will come from an interview you will have with one of the tutors during Open Lab Hours.
You have 48 weekday hours after the electronic turnin deadline to get your interview done. The deadline for the interview for PSA5 is Thurs Feb 5th (theoretically at midnight, but actually by the end of the last tutor hour scheduled). Remember, the tutors may be busy after 4pm helping CSE8B students
You get an interview by going into B260 during a scheduled tutor hour. Find the tutor, ask them if you can interview for the 8A PSA. If they have others who are waiting for interviews, your name will go on the end of the list of people waiting. Interviews take about 5 minutes each. It is your responsibility to go in early enough that you can get your interview completed. If everyone waits until the last hour on Thurs, this will not work.
For the interview you will need to have a copy of your team's assignment in your account. If you are using your partner's account to submit the files, make sure you email them to yourself and save them under your account so that you have them available for the interview.
The tutor will ask you to log in, change to the directory where your work is and then to follow their directions.
They will ask you to run your program to show that it works, open files in Dr. Java (or your favorite editor), and ask you questions about HOW your code works. Additionally, they may question you as to how you
would (theoretically) make small changes to the program to have it do something slightly different. You will not actually be required to change your code.
Learning to be able to explain your problem solving process and discuss code with another human being is a very important professional skill. We hope that this process will help you develop this skill.