Problem Solving Assignment #4
Turnin for Extra Credit due Sunday, February 3rd, 11:59 pm
Turnin Due Monday, February 4th, 11:59 pm
Interview due Thursday, February 7th, 12:00 pm
Required Reading: Chapter 2, Chapter 3, Chapter 4, Chapter 5
Please read this whole page and be able to answer the understanding questions before starting on this assignment. Ask a tutor, TA or instructor if you have any questions before you begin.
You will work with a partner on this PSA. Before you begin you MUST have a partner. Please review the guidelines for pair programming. There is an individual interview required for this PSA which is worth 20% of your PSA grade.
Note: This PSA has a total of 16 points. However, all PSA assignments are weighted equally. The interview is always 20% of the total PSA grade (i.e. if you managed to obtain all 16 points but failed to complete an interview your total PSA grade would be 80%).
Please read this whole page and be able to answer the understanding questions before starting on this assignment. Ask a tutor, TA or instructor if you have any questions before you begin.
You will work with a partner on this PSA. Before you begin you MUST have a partner. Please review the guidelines for pair programming. There is an individual interview required for this PSA which is worth 20% of your PSA grade.
Note: This PSA has a total of 16 points. However, all PSA assignments are weighted equally. The interview is always 20% of the total PSA grade (i.e. if you managed to obtain all 16 points but failed to complete an interview your total PSA grade would be 80%).
Setup
1) Right-click in an open space on the Desktop and select 'Open in Terminal' from the drop down menu.
(it will bring up the following window)
(it will bring up the following window)
2) Highlight the following 10 lines with the mouse, right-click to copy:
cd ../public/PSA4
make directories
make install
cd
cd PSA4
mv PSA4A.java.empty PSA4A.java
mv PSA4B.java.empty PSA4B.java
mv Picture.java.empty Picture.java
ls
3) Select the terminal window with the mouse and right-click to paste.
4) Verify that the 'PSA4A.java', 'PSA4B.java', 'Picture.java' files are shown as the result of the last command.
Following these procedures, your files will be named correctly and be located in the correct places. For every assignment you need to follow correct
naming of assignments in the correct locations to get credit for your work.
5) Proceed to start your programming assignment. You must write your code in the files in your PSA4 folder.
cd ../public/PSA4
make directories
make install
cd
cd PSA4
mv PSA4A.java.empty PSA4A.java
mv PSA4B.java.empty PSA4B.java
mv Picture.java.empty Picture.java
ls
3) Select the terminal window with the mouse and right-click to paste.
4) Verify that the 'PSA4A.java', 'PSA4B.java', 'Picture.java' files are shown as the result of the last command.
Following these procedures, your files will be named correctly and be located in the correct places. For every assignment you need to follow correct
naming of assignments in the correct locations to get credit for your work.
5) Proceed to start your programming assignment. You must write your code in the files in your PSA4 folder.
Problem
Collage - 8 points Total
Description: For this part of the assignment, you will be making a collage. To do this, you will write three methods in Picture.java and a main method in PSA4A.java to test your methods by showing a collage of three pictures. The main method in PSA4A.java will choose a picture and make two copies of it; you will have 3 copies total of the one picture. Then you will call a filter method on each picture. You will send the 3 filtered pictures to a collage method, that will put them all on one picture. The three filter methods are going to be two of your choice, plus the mirror filter (which can be found in the book on pages 135-136). 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. Overall this part of the assignment will put three filtered pictures next to each other on one picture.
Creating the methods:
Testing the methods:
Description: For this part of the assignment, you will be making a collage. To do this, you will write three methods in Picture.java and a main method in PSA4A.java to test your methods by showing a collage of three pictures. The main method in PSA4A.java will choose a picture and make two copies of it; you will have 3 copies total of the one picture. Then you will call a filter method on each picture. You will send the 3 filtered pictures to a collage method, that will put them all on one picture. The three filter methods are going to be two of your choice, plus the mirror filter (which can be found in the book on pages 135-136). 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. Overall this part of the assignment will put three filtered pictures next to each other on one picture.
Creating the methods:
- Add three methods to Picture.java. The three methods should apply filters to an image (6 points; 2 for each method)
- Each method should change its calling object (that means, the one reference by the keyword "this")
- Choose two filters; they can be ones that you create yourselves
- Your third filter should be either a horizontal or vertical mirror image
- These methods are not specified other than you must change the calling object
- The method header should be: public void collage (Picture p1, Picture p2, Picture p3) where p1, p2, and p3 are the pictures that will be put onto the calling object.
- You should have 3 nested for loops (There is an example of this on page 150-151 in your book)
Testing the methods:
- In this PSA4A.java you will define a main method in which you choose one picture and copy it twice, so that you have three instances of the same photo.
- You should show the original picture
- You should then create a new picture that is the same height as the one you chose, but three times as wide. You can do this by using Picture pictObj = new Picture (int width, int height);
- Call the filter methods you wrote, one on each of the 3 instances of the original photo.
- On the new three-times-wider picture you created, you should call the collage method, sending in the three pictures you altered. (2 points)
- Show the final image and write it to your current directory: picture.write("collage_name1_name2.jpg") where name1 and name2 are you and your partner's names.
- See the picture below for an example, the first filter is grayscale, the second is createPattern, and the third is mirror
- As always remember to comment your code and include your and your partners Name, Email, and Partner History
Picture Flip - 8 points Total
Description: For this part of the assignment you will be making two methods, one to flip a square section of a picture vertically and one to flip a square section of a picture horizontally. Your methods will take in three parameters the x, y coordinates of the middle of the square and the side length of the square. See the image below for a diagram
Description: For this part of the assignment you will be making two methods, one to flip a square section of a picture vertically and one to flip a square section of a picture horizontally. Your methods will take in three parameters the x, y coordinates of the middle of the square and the side length of the square. See the image below for a diagram
Creating the methods:
Method: flipAcrossVertical (3 points)
Method: flipAcrossHorizontal (3 points)
Testing your methods:
Method: flipAcrossVertical (3 points)
- Add a method called flipAcrossVertical 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 (length/width) of the square 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 must 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 most y coordinate to the bottom most y coordinate.
- The inner for loop should loop through half of the x coordinates, so from the left 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.
- NOTE: Don't forget to use a temporary "friend" variable that will hold one of the colors when swapping (don't try to use a Pixel object as a friend; it may not work for reasons we haven't yet covered in class. A Color object will be fine, though.). For example, to create a friend variable when swapping the colors of two Pixel objects p and q, do: Color friend = p.getColor();
Method: flipAcrossHorizontal (3 points)
- Add another method to Picture.java called flipAcrossHorizontal
- This method is almost exactly like flipAcrossVertical except the axis that you flip around is vertical. The outer loop will loop through all of the x coordinates, from the left most x to the right most x. The inner loop will loop through half of the y coordinates pixels, from the top 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 different! (We expect you to think about it and experiment with it to figure it out.)
Testing your methods:
- Create a new Picture using FileChooser.pickAFile().
- Show the original Picture.
- For the following steps - you will be modifying the original Picture; there is no need to create any copies of the picture.
- Modify the code in the PSA4B template to add a loop which will iterate 3 times (you may use a for loop or a while loop). Each iteration of the loop should include what was given to you, plus the call to your flipHorizontal method. (2 points)
- Have a second loop that does the same as the first, except you should call flipVertical.
- Be sure to repaint the picture for each iteration of both loops to see the changes in progress.
- Please see the following images below for some examples. The first is the image we want to alter, the second is the barbara.jpg with a box of size 50 at (100,100) flipped over a vertical line, and the third is the barbara.jpg with a box of size 50 at (100,100) filled over a horizontal line.
Original
barbara.jpg with a box of size 50 at (100,100) flipped over a vertical line
barbara.jpg with a box of size 50 at (100,100) filled over a horizontal line.
Sample Code
PSA4A.java
/* Filename: PSA4A.java
* Created by: Vinay Venkatesh, cs8w3 and Alberto Garcia, cs8w4
* Due Date: February 5, 2013
*
* Partner Histories:
* Vinay Venkatesh:
PSA1 Partner: Garret Rodriguez, cs8w1
PSA2 Partner: Shari Haynes, cw8w2
PSA3 Partner: Alberto Garcia, cs8w4
PSA4 Partner: Alberto Garcia, cs8w4
PSA5 Partner: To be determined...
PSA6 Partner: To be determined...
PSA7 Partner: To be determined...
PSA8 Partner: To be determined...
PSA9 Partner: To be determined...
* Alberto Garcia:
PSA1 Partner: Shari Haynes, cs8w2
PSA2 Partner: Garret Rodriguez, cs8w1
PSA3 Partner: Vinay Venkatesh, cs8w3
PSA4 Partner: Vinay Venkatesh, cs8w3
PSA5 Partner: To be determined...
PSA6 Partner: To be determined...
PSA7 Partner: To be determined...
PSA8 Partner: To be determined...
PSA9 Partner: To be determined...
* Date:
* Description: This class is designed to... */
public class PSA4A
{
//The line below is magic, you don't have to understand it (yet)
public static void main (String[] args)
{
...
}
}
PSA4B.java
/* Filename: PSA4B.java
* Created by: Vinay Venkatesh, cs8w3 and Alberto Garcia, cs8w4
* Due Date: February 5, 2013
*
* Partner Histories:
* Vinay Venkatesh:
PSA1 Partner: Garret Rodriguez, cs8w1
PSA2 Partner: Shari Haynes, cw8w2
PSA3 Partner: Alberto Garcia, cs8w4
PSA4 Partner: Alberto Garcia, cs8w4
PSA5 Partner: To be determined...
PSA6 Partner: To be determined...
PSA7 Partner: To be determined...
PSA8 Partner: To be determined...
PSA9 Partner: To be determined...
* Alberto Garcia:
PSA1 Partner: Shari Haynes, cs8w2
PSA2 Partner: Garret Rodriguez, cs8w1
PSA3 Partner: Vinay Venkatesh, cs8w3
PSA4 Partner: Vinay Venkatesh, cs8w3
PSA5 Partner: To be determined...
PSA6 Partner: To be determined...
PSA7 Partner: To be determined...
PSA8 Partner: To be determined...
PSA9 Partner: To be determined...
* Date:
* Description: This class is designed to... */
import java.io.*;
public class PSA4B
{
public static void main(String[] args)throws IOException
{
/* Choosing a picture and initializing variables. */
Picture pic = new Picture(FileChooser.pickAFile());
int x, y, size;
pic.show();
int width = pic.getWidth();
int height = pic.getHeight();
System.out.println("Picture loaded - width: " + pic.getWidth()+" height: "+ pic.getHeight());
//NEED TO FILL IN THE FOR LOOP HERE
{
/* Prompting the user for coordinates. */
String prompt = "Please enter a position (first x then press enter, then y and press enter) in the ";
String prompt2 = "picture to flip horizontally.";
System.out.println(prompt + prompt2);
/* Converting coordinates to ints. */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
x = (new Integer(br.readLine())).intValue();
y = (new Integer(br.readLine())).intValue();
/* Prompting the user for size. */
System.out.println("Please enter the size of the box to flip.");
size = (new Integer(br.readLine())).intValue();
//NEED TO MAKE A CALL TO FLIPACROSSHORIZONTAL HERE
/* Repainting the picture with the horizontal flips completed. */
pic.repaint();
}
//NEED TO FILL IN A SECOND FOR LOOP FOR FLIPACROSSVERTICAL
}
}
/* Filename: PSA4A.java
* Created by: Vinay Venkatesh, cs8w3 and Alberto Garcia, cs8w4
* Due Date: February 5, 2013
*
* Partner Histories:
* Vinay Venkatesh:
PSA1 Partner: Garret Rodriguez, cs8w1
PSA2 Partner: Shari Haynes, cw8w2
PSA3 Partner: Alberto Garcia, cs8w4
PSA4 Partner: Alberto Garcia, cs8w4
PSA5 Partner: To be determined...
PSA6 Partner: To be determined...
PSA7 Partner: To be determined...
PSA8 Partner: To be determined...
PSA9 Partner: To be determined...
* Alberto Garcia:
PSA1 Partner: Shari Haynes, cs8w2
PSA2 Partner: Garret Rodriguez, cs8w1
PSA3 Partner: Vinay Venkatesh, cs8w3
PSA4 Partner: Vinay Venkatesh, cs8w3
PSA5 Partner: To be determined...
PSA6 Partner: To be determined...
PSA7 Partner: To be determined...
PSA8 Partner: To be determined...
PSA9 Partner: To be determined...
* Date:
* Description: This class is designed to... */
public class PSA4A
{
//The line below is magic, you don't have to understand it (yet)
public static void main (String[] args)
{
...
}
}
PSA4B.java
/* Filename: PSA4B.java
* Created by: Vinay Venkatesh, cs8w3 and Alberto Garcia, cs8w4
* Due Date: February 5, 2013
*
* Partner Histories:
* Vinay Venkatesh:
PSA1 Partner: Garret Rodriguez, cs8w1
PSA2 Partner: Shari Haynes, cw8w2
PSA3 Partner: Alberto Garcia, cs8w4
PSA4 Partner: Alberto Garcia, cs8w4
PSA5 Partner: To be determined...
PSA6 Partner: To be determined...
PSA7 Partner: To be determined...
PSA8 Partner: To be determined...
PSA9 Partner: To be determined...
* Alberto Garcia:
PSA1 Partner: Shari Haynes, cs8w2
PSA2 Partner: Garret Rodriguez, cs8w1
PSA3 Partner: Vinay Venkatesh, cs8w3
PSA4 Partner: Vinay Venkatesh, cs8w3
PSA5 Partner: To be determined...
PSA6 Partner: To be determined...
PSA7 Partner: To be determined...
PSA8 Partner: To be determined...
PSA9 Partner: To be determined...
* Date:
* Description: This class is designed to... */
import java.io.*;
public class PSA4B
{
public static void main(String[] args)throws IOException
{
/* Choosing a picture and initializing variables. */
Picture pic = new Picture(FileChooser.pickAFile());
int x, y, size;
pic.show();
int width = pic.getWidth();
int height = pic.getHeight();
System.out.println("Picture loaded - width: " + pic.getWidth()+" height: "+ pic.getHeight());
//NEED TO FILL IN THE FOR LOOP HERE
{
/* Prompting the user for coordinates. */
String prompt = "Please enter a position (first x then press enter, then y and press enter) in the ";
String prompt2 = "picture to flip horizontally.";
System.out.println(prompt + prompt2);
/* Converting coordinates to ints. */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
x = (new Integer(br.readLine())).intValue();
y = (new Integer(br.readLine())).intValue();
/* Prompting the user for size. */
System.out.println("Please enter the size of the box to flip.");
size = (new Integer(br.readLine())).intValue();
//NEED TO MAKE A CALL TO FLIPACROSSHORIZONTAL HERE
/* Repainting the picture with the horizontal flips completed. */
pic.repaint();
}
//NEED TO FILL IN A SECOND FOR LOOP FOR FLIPACROSSVERTICAL
}
}
How to turnin your homework
1) Open the terminal
2) cd PSA4
3) autograder login -e yourucsdemailhere -p (wait for password prompt)
4) type in your password
5) turnin PSA4
2) cd PSA4
3) autograder login -e yourucsdemailhere -p (wait for password prompt)
4) type in your password
5) turnin PSA4
PSA4 Understanding Quiz
The answers can be found here
1. If you call flipAcrossVertical(int x, int y, int size) on a Picture p as follows:
p.flipAcrossVertical(100, 100, 25)
What pixel coordinate represents the top left corner of the box which will be flipped?
Choose one answer:
a. (88, 88)
b. (112,112)
c. (113,87)
d. (87, 113)
2. When you need to swap the pixels in flipAcrossVerical or flipAcrossHorizontal, which of the following do we recommend?
a. Use a temporary (friend) variable of type Pixel and swap using traditional assignment (=)
b. Use a temporary (friend) variable of type Picture and swap using traditional assignment (=)
c. Use a temporary (friend) variable of type Color and use .setColor to make the changes.
3. When submitting your PSA, should only one of the pair programming partners submit or should both submit on their separate accounts?
a. Both should submit using their separate lab accounts
b. Only one of the pair should submit.
4. The outlined sections of the following images of Picture p were flipped using either flipAcrossVertical or flipAcrossHorizontal given some information determine what method call was used:
1. If you call flipAcrossVertical(int x, int y, int size) on a Picture p as follows:
p.flipAcrossVertical(100, 100, 25)
What pixel coordinate represents the top left corner of the box which will be flipped?
Choose one answer:
a. (88, 88)
b. (112,112)
c. (113,87)
d. (87, 113)
2. When you need to swap the pixels in flipAcrossVerical or flipAcrossHorizontal, which of the following do we recommend?
a. Use a temporary (friend) variable of type Pixel and swap using traditional assignment (=)
b. Use a temporary (friend) variable of type Picture and swap using traditional assignment (=)
c. Use a temporary (friend) variable of type Color and use .setColor to make the changes.
3. When submitting your PSA, should only one of the pair programming partners submit or should both submit on their separate accounts?
a. Both should submit using their separate lab accounts
b. Only one of the pair should submit.
4. The outlined sections of the following images of Picture p were flipped using either flipAcrossVertical or flipAcrossHorizontal given some information determine what method call was used:
Picture 1
The top right corner of the box is at (450,250) and the bottom left corner of the box is at (500,300). What method call on Picture p will produce the image
a. p.flipAcrossVertical(475,275,50)
b. p.flipAcrossHorizontal(475,275,50)
c. p.flipAcrossVertical(450,250,50)
d. p.flipAcrossHorizontal(450,250,50)
a. p.flipAcrossVertical(475,275,50)
b. p.flipAcrossHorizontal(475,275,50)
c. p.flipAcrossVertical(450,250,50)
d. p.flipAcrossHorizontal(450,250,50)
Picture 2
The top right corner of the box is at (200,200) and the bottom left corner of the box is at (400,400). What method call on Picture p will produce the image
a. p.flipAcrossVertical(200,200,100)
b. p.flipAcrossHorizontal(300,300,100)
c. p.flipAcrossVertical(300,300,200)
d. p.flipAcrossHorizontal(300,300,200)
a. p.flipAcrossVertical(200,200,100)
b. p.flipAcrossHorizontal(300,300,100)
c. p.flipAcrossVertical(300,300,200)
d. p.flipAcrossHorizontal(300,300,200)
Picture 3
The top right corner of the box is at (400,100) and the bottom left corner of the box is at (500,200). What method call on Picture p will produce the image
a. p.flipAcrossVertical(500,200,100)
b. p.flipAcrossHorizontal(450,100,100)
c. p.flipAcrossVertical(400,100,100)
d. p.flipAcrossHorizontal(450,150,100)
a. p.flipAcrossVertical(500,200,100)
b. p.flipAcrossHorizontal(450,100,100)
c. p.flipAcrossVertical(400,100,100)
d. p.flipAcrossHorizontal(450,150,100)