Midterm Exam 1 Review Sp2005
Below are questions like those I plan to ask on Midterm #1 (Feb. 11). The exam will consist of 4 or 5 questions like these.
Please do try these questions! Post your answers, questions, comments, concerns, and criticisms on the pages for each question. Those comments, questions, etc. can also be about each others' answers! If someone posts an answer that you don't understand, ask about it! If you see a question here that you know the answer to, don't keep it to yourself – help your fellow students!
I will be reading your answers. Here are the ground rules for the interaction.
- 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!
- I will try to always point out if an answer is wrong. I won't always point out when the answer is right.
- 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 Sp2005: 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 Sp2005: Properties and Data Structures
Flip, the other way.
Below is the method of class Picture for flipping an image left to right. Write the other method, the one that goes right to left.
/**
* 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 Sp2005: 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);
}
}
}
Questions, comments, answers at Midterm exam 1 review Sp2005: Posterize
Turtle Initial
Below is the example turtle drawing from class. Create a TurtleInitial method 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("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 Sp2005: Turtle Initial
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?
Questions, comments, answers at Midterm exam 1 review Sp2005: Short Essay
Link to this Page
- Exam Reviews last edited on 4 December 2007 at 9:14 pm by c-76-17-114-206.hsd1.ga.comcast.net