Change Contents of the Bubble
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 1 Review Fall2005

Below are questions like those I plan to ask on Midterm #1 (Sept. 23). 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.
  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 Fall2005: 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 Fall2005: 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 Fall2005: Flip the other way


Posterize


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


  /**
   * 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 Fall2005: 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 Fall2005: 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 Fall2005: Short Essay



Make Boxes with Turtles


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();
  }
  
}


Uploaded Image: turtle-midterm1-review.jpg

Questions, comments, answers at Midterm exam 1 review Fall2005: Making Boxes with Turtles



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.

Questions, comments, answers at Midterm exam 1 review Fall2005: Java Meanings


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 Fall2005: Tracing Complex Picture Method


Links to this Page