Because the timing of the labs has been messed up with the assignments, I'm adding to the labs to make them more relevant.

  1. Lab #12
  2. Lab #7
  3. Lab #6
  4. Lab #5
  5. Lab #4
  6. Lab #3

Lab #12

Exercise 2

The StringTokenizer class is used to break a line of text up into words.

  1. Run the following code in the Interactions pane. What does it do?

    import java.util.*;
    String line = SimpleInput.getString("Enter a sentence of words");
    StringTokenizer tokenizer = new StringTokenizer(line);
    System.out.println(tokenizer.nextToken());
  2. Modify the example above so that it only prints out the second word of a sentence.

Lab #7

You should do the original lab 7 first.

Exercise 4

Like morphStage, which you have to do for assignment 3 (boo), another simple digital animation technique is movement. The illusion of movement is given by having something appear in a slightly different position for each frame of a movie.

In your Picture class you have a copyPictureTo method which copies a picture onto another picture at a specified location (or at least you should, since you were supposed to use it for assignment 2). We can use this method to copy a picture of a pig to a different spot on a blank canvas to get the illusion of a pig walking (note: well okay, his legs won't be moving so it won't really look like "walking", more like "gliding").

You can do this either in the interactions pane or in a main method. Probably the interactions pane will be easier (unless you screw something up and then you'll have to type everything over again).

  1. Download the pig picture.
  2. Create a Picture object for "pig.png".
  3. Create an array of 20 Pictures (or more if your pig is very restless).
  4. Write a loop, which will iterate through each Picture in your array:
    1. Create the ith Picture element in your array, of dimensions 400x235. It will be a blank picture.
    2. Using the copyPictureTo method, copy the pig onto the blank picture you just created. The starting y-coordinate will be 0. The starting x-coordinate will be 100 – 5 * i.
  5. As in assignment 3 part B, create a FrameSequencer object and add every picture in your array.
  6. As in assignment 3 part B, create a MoviePlayer object so that you can watch your pig in action!

Lab #6

You should do the original lab 6 first.

Exercise 5

When calling a method (sending an object a message), you can sometimes get an answer back. All methods we've written so far are void, meaning they do not return anything back. Using a type name other than void will allow a method to return a value of that type, though. The getWidth and getHeight methods are methods we've seen which use return values (specifically they return int). Here's an example method you could put in the Picture class which would return a value:

public int totalPixels() {
    return this.getWidth() * this.getHeight();
}

For exercise 5, take one of your transformation methods from Assignment 2 and change it so that, rather than modifying the current picture, it creates a copy of the current picture, modifies it, and returns that copy.

Lab #5

You should do the original lab 5 first.

Exercise 5

There are two types of methods we make you deal with: static methods and non-static methods (i.e., methods that don't have the static keyword). The differences are in how you call them and in how they operate.

Exercise #5: take any of the methods you wrote for assignment 1 or assignment 2 (if you did the assignments correctly they will be non-static methods) and write them again as static methods. You'll have to show me both versions, the static and the non-static, and show me how you call them!

Lab #4

You should do the original lab #4 first.

Exercise 5

Here are two ways to invert a Picture p. First, with for-loops:

for (int y = 0; y < p.getHeight(); y = y + 1) {
    for (int x = 0; x < p.getWidth(); x = x + 1) {
        Pixel pix = p.getPixel(x, y);
        int newR = 255 - pix.getRed();
        int newG = 255 - pix.getGreen();
        int newB = 255 - pix.getBlue();
        pix.setRed(newR);
        pix.setGreen(newG);
        pix.setBlue(newB);
    }
}

Secondly, with while-loops:

Pixel[] pixels = p.getPixels();
int i = 0;
while (i < pixels.length) {
    Pixel pix = pixels[i];
    int newR = 255 - pix.getRed();
    int newG = 255 - pix.getGreen();
    int newB = 255 - pix.getBlue();
    pix.setRed(newR);
    pix.setGreen(newG);
    pix.setBlue(newB);
    i = i + 1;
}
  1. Write the code to load in your picture, p, and then try the above code to ensure it works.
  2. Take one of the above two approaches so that it only inverts pixels if they're on an even-numbered row (e.g., row #0, row #2, ...). Is it easier to do this with the for-loop approach or the while-loop approach?

Lab #3

Here is the original lab #3. You are not required to do any of this, but it may be good practice. It forces you to try making another Turtle method, specifically drawVee!

I'm going to number my exercises continuing on from the original lab #3's, but all you have to show me is exercise 7.

Exercise 6

The following program prints out the numbers 1 to 10. Type it in and convince yourself it works:
import java.io.*;

public class Lab3Part6 {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i = i + 1) {
            System.out.println("the number " + i);
        }
    }
}
  1. Change it to count from 1 to 5 instead of 1 to 10.
  2. What happens if you change the <= to a <?
  3. What happens if you change i = i + 1 to i = i + 2? Why?
  4. Change the program to count down from 10 to 1 instead of counting up! Warning: this is going to take some creative thought and a lot of playing around!

Exercise 7

For-loops can be used with any Java construct that you want to do a certain number of times. Why not use them with turtles?

  1. To draw concentric squares, we could write code like so:

    t.drawSquare(10);
    t.drawSquare(20);
    t.drawSquare(30);
    t.drawSquare(40);
    t.drawSquare(50);

    This would produce the following picture:

    Doing it this way has two problems: (a) it's tedious writing that out 5 times! (b) you can't change it to draw 6 or 7 or 100 squares without changing the code!

    Rewrite the above in the interactions pane so that you only have one call to drawSquare, but it's inside a for-loop that will call it 5 times of varying sizes.

  2. Get your turtle to draw a circle. Warning: THIS IS HARD! If you don't get it, don't worry about it. Your hint is that you can a draw a square by repeatedly moving forward by, say, 80 and turning by, say, 30 degrees, and doing that 12 times (so that you do the full 360 degrees). It would be crazy to try and do this without a loop!

    Note: modifying the turtle inside the loop may cause a ConcurrentModificationException. This is because the book classes are poorly written; it has nothing to do with your code and may be ignored.