Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 1 Review Fall2006

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



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. =>mirror across the horizontal axis)

  /**
   * 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 Fall2006: 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 Fall2006: Posterize


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) (200,250,5)


(b) (128,36,100)


(c) (15,35,200)


(d) (125,100,150)

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

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


External Image

Questions, comments, answers at Midterm exam 1 review Fall2006: Making Concentric Boxes

Make Turtle Boxes


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

Uploaded Image: boxes-midtermreview1.jpg

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 Fall2006: Make Turtle 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.

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

Understanding Java Types

Short answers:

(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 Fall2006: Understanding Java Types

Exploring Java Objects


Consider the Java class below that uses JMusic:
import jm.music.data.*;
import jm.JMC;
import jm.util.*;
import jm.JMC;

public class NotePlay {
  
  public static void main(String[] args){
    Score score = new Score();
    Note noteC = new Note(JMC.C5,JMC.EIGHTH_NOTE);
    Note noteE = new Note(JMC.E5,JMC.SIXTEENTH_NOTE);
    Note noteG = new Note(JMC.G5,JMC.EIGHTH_NOTE);
    Phrase phr1 = new Phrase();
    for (int i = 0; i < 2; i++){
      phr1.addNote(noteC.copy());
      phr1.addNote(noteE.copy());
      phr1.addNote(noteG.copy());
    }
    Phrase phr2 = new Phrase();
    for (int i = 0; i < 3; i++){
      phr2.addNote(noteC.copy());
      phr2.addNote(noteG.copy());
    }
    Phrase phr3 = new Phrase();
    for (int i = 0; i < 3; i++){
      phr3.addNote(noteE.copy());
      phr3.addNote(noteC.copy());
    }
    
    Part part1 = new Part("Flute Part",JMC.FLUTE,1);
    part1.addPhrase(phr1.copy());
    part1.addPhrase(phr2.copy());
    part1.addPhrase(phr1.copy());
    
    Part part2 = new Part("Piano Part",JMC.PIANO,2);
    part2.addPhrase(phr3.copy());
    part2.addPhrase(phr1.copy());
    part2.addPhrase(phr3.copy());
    
    score.addPart(part1);
    score.addPart(part2);
    
    View.notate(score);

  }
}



When run, this score is generated:
Uploaded Image: clip_image001.jpg
(and it goes on further to the right…)

Answer the below questions about the objects created in this main() method:

A. What are the notes in phr1? (Ex…CGCEGG)



B. What are the notes in phr3? (Ex…CGCEGG)



C. Counting every Note and Phrase as a distinct object, how many objects are in part1?
How many instances are there of each class (Note and Phrase) in part1?



D. Counting every Note, Phrase, and Part as a distinct object, how many objects are in score?



Questions, comments, answers at Midterm exam 1 review Fall2006: Exploring Java Objects



Links to this Page