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

Midterm Exam 1 Review-Fall 2007

Below are questions like those I plan to ask on Midterm #1 (Sept 21). The exam will consist of 5 or 6 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.



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-Fall 2007: Posterize

What do they do?


Below are three methods from the class Picture. Assume that we run each of these with:
> Picture p = new Picture(FileChooser.getMediaPath("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-Fall 2007: What do they do?

Manipulating Sounds

Write a void method called putInZeros(int start, int skip, int value) that sets every skip sample to value starting at start.

The delete method from class Sound may be helpful for reference.
  public void delete(int start, int end){
    
    int value = 0;
    
    // Basically, we simply copy from "end" to getLength back to start
    for (int source=end, target=start;
         source < this.getLength();
         source++, target++){
      value = this.getSampleValueAt(source);
      this.setSampleValueAt(target,value);
     }
    
    // Then clear out the rest. Gap is end-start length
    int gap = end-start;
    for (int i=1; i <= gap ; i++){
      this.setSampleValueAt(this.getLength()-i,0);
    }
  }


Questions, comments, answers at Midterm Exam 1 Review-Fall 2007: Manipulating Sounds

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



Questions, comments, answers at Midterm Exam 1 Review-Fall 2007: Turtle Initial

Turtle Boxes

Use the following code to help you with this question
public class TurtleSquares {
  	public static void main(String [] args){
    		World world = new World(); 
    		Turtle t = new Turtle(world);
       		 for (int i = 0; i  4; i++){
      			t.forward(100);
      			t.turn(90);
    		} 
 	 }
}


turtledrawing.GIF
Within a class called TurtleBoxes write a main method to generate the picture shown above. You only need one Turtle to do that drawing. The Turtle turns 15 degrees each time before drawing a 100 by 100 pixel box. Think about how many times the Turtle must turn in order to cover all 360 degrees of a circle. You must use a loop (for or while)in your method.

public class TurtleBoxes{
//write code in here
}

Ask question at Midterm Exam 1 Review-Fall 2007: Turtle Boxes

What in the world?


1.) Give definitions for:


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

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? Disadvantages?

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

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-Fall 2007: Understanding Java Types


Links to this Page