### Welcome to CS1316

Please visit t-square for the updated class website.

## Midterm Exam 1 Review-Spring 2007

Below are questions like those I plan to ask on Midterm #1 (Feb. 9). The exam will consist of 4 or 5 questions like these.

1. If you don't post, neither will I. I will not be posting official solutions to these problems at all! If one of you gets it right, terrific!
2. I will try to always point out if an answer is wrong. I won't always point out when the answer is right.
3. I am glad to work with you toward the right answer. I will give hints, and I'm glad to respond to partial guesses.

### Posterize

Below is an example posterize() method. Create a newPosterize method that sets:

• If red is greater than 128, make it 255
• If red is less than or equal to 128, make it 5
• If blue is less than or equal to 250, make it 200
• If blue is greater than 250, make it 201
• If green is less than 10, make it 200.
• If green is greater than 200, make it 10. Leave other values of green alone

```  /**
* Method to posterize (reduce the number of colors) in the picture
* The number of reds, greens, and blues will be 4
*/
public void posterize()
{
Pixel pixel = null;
int redValue = 0;
int greenValue = 0;
int blueValue = 0;

// loop through the pixels
for (int x = 0; x < this.getWidth(); x++) {
for (int y = 0; y < this.getHeight(); y++) {

// get the current pixel and colors
pixel = this.getPixel(x,y);
redValue = pixel.getRed();
greenValue = pixel.getGreen();
blueValue = pixel.getBlue();

// check for red range and change color
if (redValue < 64)
redValue = 31;
else if (redValue < 128)
redValue = 95;
else if (redValue < 192)
redValue = 159;
else
redValue = 223;

// check for green range
if (greenValue < 64)
greenValue = 31;
else if (greenValue < 128)
greenValue = 95;
else if (greenValue < 192)
greenValue = 159;
else
greenValue = 223;

// check for blue range
if (blueValue < 64)
blueValue = 31;
else if (blueValue < 128)
blueValue = 95;
else if (blueValue < 192)
blueValue = 159;
else
blueValue = 223;

// set the colors
pixel.setRed(redValue);
pixel.setGreen(greenValue);
pixel.setBlue(blueValue);
}
}
}
```

### What do they do?

Below are three methods from the class Picture. Assume that we run each of these with:
```> Picture p = new Picture("D:/cs1316/MediaSources/Swan.jpg");
> p.methodA(); p.show(); // Then methodB and methodC ```

What will each of these do to the picture?

```  public void methodA(){
Pixel p;
for (int x=0; x < this.getWidth(); x++)
{for (int y = 0; y < this.getHeight(); y++)
{
p=this.getPixel(x,y);
p.setRed(255-p.getRed());
p.setGreen(255-p.getGreen());
p.setBlue(255-p.getBlue());
}}}

public void methodB(){
Pixel p;
for (int x=(int) (this.getWidth()/2); x < this.getWidth(); x++)
{for (int y = 0; y < this.getHeight(); y++)
{
p=this.getPixel(x,y);
p.setRed(255-p.getRed());
p.setGreen(255-p.getGreen());
p.setBlue(255-p.getBlue());
}}}

public void methodC(){
Pixel p;
for (int x=0; x < this.getWidth(); x++)
{for (int y = 0; y < this.getHeight()/2; y++)
{
p=this.getPixel(x,y);
p.setRed(255-p.getRed());
p.setGreen(255-p.getGreen());
p.setBlue(255-p.getBlue());
}}}
```

Questions, comments, answers at Midterm Exam 1 Review-Spring 2007: What do they do?

### Turtle Initial

Below is the example turtle drawing from class. Create a TurtleInitial class with a main() method that draws the first initial of your family (last) name.

```public class MyTurtlePicture {

public static void main(String [] args) {

//FileChooser.setMediaPath("C:/cs1316/MediaSources/");
Picture canvas = new Picture(600,600);
Turtle jenny = new Turtle(canvas);
Picture lilTurtle = new Picture(FileChooser.getMediaPath("Turtle.jpg"));

for (int i=0; i <=40; i++)
{
if (i < 20)
{jenny.turn(20);}
else
{jenny.turn(-20);}
jenny.forward(40);
jenny.drop(lilTurtle.scale(0.5));
}

canvas.show();
}

}
```

### Make Turtle Boxes

Make a class with a main method that draws a square in a square, like this:

Make the smaller one 100 pixels (turtle steps) per side, and the larger 200 per side. You might also want to know that turtles understand penUp() (where future moves and turns do not leave a pen trace) and penDown() (go back to normally leaving a pen trail when the turtle moves.)

Questions, comments, answers at Midterm Exam 1 Review-Spring 2007: Make Turtle Boxes

### Rearrange and Flip

Consider this method in the Picture class
```public Picture rearrangeAndFlip(int interval){

Picture canvas = new Picture(this.getWidth(), this.getHeight());
Color value = null;

int start=0;
int index = 0;

Pixel [] pixels = this.getPixels();
Pixel [] targetPixels = canvas.getPixels();

while ((index+interval) < pixels.length){
start = index;
int src=start;
int trg=start+interval;

while(src < start+interval){

value = pixels[src].getColor();
targetPixels[trg].setColor(value);

index++;
src++;
trg--;

}
}

canvas = canvas.flip();

return canvas;
}
```

A. What does this method do?

B. If you were to execute the below, what would you expect to the result to be?

> Picture p = new Picture(FileChooser.getMediaPath("swan.jpg"))
> Picture p2 = p.rearrange(5)
> p2.show()

C. Imagine that you have a Picture p with only 20 pixels in it, with these values:

 (188,235,176) (19,113,159) (61,18,217) (26,52,245) (169,130,150) (181,114,123) (106,255,236) (103,37,23) (164,38,119) (245,78,29) (31,171,199) (143,170,84) (200,20,103) (192,95,48) (95,98,3) (93,160,122) (225,238,83) (38,68,78) (238,5,119) (4,165,224)

What values would result from executing p.rearrangeAndFlip(8)? Write down the values in these cells:

Questions, comments, answers at Midterm Exam 1 Review-Spring 2007: Rearrange and Flip

### What in the world?

1.) What is the difference between public and private?

2.) What is the difference between null and void?

3.) What would you use these objects for?
Sound
Picture
String
Turtle

4.) What are the differences between the primitive variables?
int, long, double, boolean, char?

5.) What do these lines mean?
public class TurtleDance{..}
public static void main(String[]args)
public Picture scale(double factor)
Turtle [] myTurtles = new Turtle[50];
Picture p = new Picture(FileChooser.getMediaPath("swan.jpg"));

6.) What are some advantages to Arrays? REMOVEDked Lists?

Questions, comments, answers at Midterm Exam 1 Review-Spring 2007: What in the World?

### Understanding Java Types

(a) If I have a line in my method int fred; and later say fred=”george”; What will happen? If I get an error, what kind of error would you expect? (You don’t have to be exact—just give a sense for what happens.)

(b) If I have a small Picture smallpic, will Picture newpic=smallpic.increaseRed(); work (compile and execute) where increaseRed() is declared void? Will Picture newpic2=smallpic.scale(0.25); work where scale() is declared Picture? If one of them will not work, which one and what kind of error message would you expect?

(c) You want to add a new method to your Picture class that does a newPosterize() to the picture this that the method has been called upon, e.g., mypict.newPosterize(). Should I declare this method void or Picture? Should I declare this method static? Please explain your answers.

(d) If you have a PictureCollage class with a main method that creates and draws your collage, can this class extend Picture and still compile? Would the collage still work? Is this a good idea?

Questions, comments, answers at Midterm Exam 1 Review-Spring 2007: Understanding Java Types