                    ## Midterm Exam 1 Review Sp2006

Below are questions like those I plan to ask on Midterm #1 (Feb. 10). 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.

### 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 Sp2006: What do they do?

### Properties and Data Structures

Here are several properties of data structures. List the letters of the properties (e.g., A, C, D) next to the data structure that has those properties:

Array:

REMOVEDked List:

A. Fast to access a specific element
B. More complex to traverse
C. Easy to traverse
D. Hard to insert/delete in the middle
E. Slower to access a specific element
F. Easier to insert/delete in the middle
G. Static size
H. Dynamic size

Questions, comments, answers at Midterm exam 1 review Sp2006: Properties and Data Structures

### Flip, the other way.

Below is the method of class Picture for flipping an image left to right. Write the method that goes top-to-bottom (so that you see a top on the top, and a top on the bottom, but flipped).

```  /**
* Method to flip an image left-to-right
**/
public Picture flip() {

Pixel currPixel;
Picture target = new Picture(this.getWidth(),this.getHeight());

for (int srcx = 0, trgx = getWidth()-1; srcx < getWidth(); srcx++, trgx--)
{
for (int srcy = 0, trgy = 0; srcy < getHeight(); srcy++, trgy++)
{
// get the current pixel
currPixel = this.getPixel(srcx,srcy);

/* copy the color of currPixel into target
*/
target.getPixel(trgx,trgy).setColor(currPixel.getColor());
}
};
return target;
}
```

Questions, comments, answers at Midterm exam 1 review Sp2006: Flip the other way

### 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);
}
}
}
```

### Short Essay

A. Why would someone want to extend another class, in the way that Picture extends SimplePicture?

B. What is the difference between public and private?

C. What is a static method and why would you ever want one?

D. What in the world is a void method?

E. Why would you want to set a variable to null?

### Making Concentric Boxes

Below is the turtle drawing example from class. Write a TurtleBoxes class with a main() method that draws THREE boxes, one 100 pixels per side, one 150 pixels per side, and one 200 pixels per side. The boxes should overlap (like screenshot below). Be sure to create a canvas (Picture) of 500,500 and a Turtle to draw on that canvas.

```public class MyTurtlePicture {

public static void main(String [] args) {

FileChooser.setMediaPath("D:/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();
}

}
``` Questions, comments, answers at Midterm exam 1 review Sp2006: Making Concentric Boxes

### Java Meanings

Match the words below to the meanings at the bottomwrite the letter of the meaning next to the corresponding keyword.

__ Static ___ Public ___ Private

__ Extends ___ Void ___ Null

A. The value to give a variable when its going to refer to an object, but it doesnt have a value yet.

B. The keyword thats used to make one class a subclass of anotherit means that the class being declared inherits from the other class.

C. Used to declare a method that can be used even if no instances of the class exist.

D. Used to declare that a method or instance variable is meant to be only accessed from within this class.

E. Used to declare that a method or instance variable is meant to be accessed from any other class.

F. Used to declare a method that doesnt return anything.

### Tracing a Complex Picture Method

Consider the below method in the class Picture:

```     /**
* Complex manipulation
**/
public void complex1() {
Pixel[] pixels = this.getPixels();
Pixel pixel = null;
int value = 0;

// loop through all the pixels
for (int i = 0; i < pixels.length; i++) {
pixel = pixels[i];
value = pixel.getRed()-pixel.getBlue();
if (value < 0) { value = -value;}
if (value > 150) {value = (int) (value/10);}

for (int j=0; j<value; j++) {
if (pixel.getGreen() < 128) {
pixel.setGreen(pixel.getGreen()+1);}
else {
pixel.setGreen(pixel.getGreen()-1);}}}

}
```

If we ran this method on a picture that contained pixels with the below(red, green, blue) values, what would the new values of the pixels be?

(a) (255,255,255)

(b) (128,36,100)

(c) (200,250,5)

(d) (5,25,200)

Questions, comments, answers at Midterm exam 1 review Sp2006: Tracing Complex Picture Method

### 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. The below code might be of use in remembering how to write a class and main() that creates a turtle drawing. 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.)
```public class MyTurtlePicture {

public static void main(String [] args) {

FileChooser.setMediaPath("D:/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();
}

}
```

Questions, comments, answers at Midterm exam 1 review Sp2006: Make Turtle Boxes

### 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 Sp2006: Understanding Java Types