Final Review QuestionsΒΆ

  1. Write a while-loop that uses the println function to print the numbers from 15 to 188 on the console:

    15
    16
    17
    18
    ...
    187
    188
    
  2. Suppose age is a variable of type int that has already been defined and given a value (although you don’t know what the value is). Using if-statements, write code that does the following:

    • if age is 0 or less, then print “that’s impossible!”
    • if age is 1 or 2, then print “you’re very young”
    • if age is 3 to 12, then print “Pokemon rocks!”
    • if age is 13 to 19, then print “you’re a teenager”
    • if age is 20 or more, then print “you are incredibly old”

    Your code should print exactly one message.

  3. Write a class called Wheel that can draw a wheel. The object should store the center of the wheel, the diameter of the wheel, and its fill color.

    Wheel should have the following functions:

    • A constructor that takes, as input, the position of the wheel’s center, it’s diameter, and its fill color.
    • A render() function that, when called, draws the wheel on the screen according to the data in the object.

    When it’s done, your Wheel class should work with code like this:

    Wheel w;
    
    void setup() {
      size(500, 500);
      smooth();
    
      w = new Wheel(250, 250, 40,
                    color(255, 0, 0));
    }
    
    void draw() {
      w.render();
    }
    
  4. Suppose you have a classed called Frog that is used to animate a frog. You know the following facts about Frog:

    • It has one constructor called Frog().
    • It has a function called draw() that, when called on a Frog object, draws it on the screen.
    • It has a function called update() that, when called on a Frog object, updates its internal variables. You should always call update() before render().
    • The frogs are animated and so will move around the screen. You don’t have to worry about this movement — the code in the Frog class takes care of all of that for you as long as you call update() and render().

    Write a complete Processing program that creates a particle system of 1000 frogs moving aroung the screen without leaving trails. Also, we want to see you use:

    • An ArrayList to store the Frog objects.
    • At least one while-loop, and at least on for-in loop used in sensible ways.

    Except for the Frog class, write the entire program.

  5. Write a class called Point that represents an (x, y) point. It should have a constructor that takes x and y as input.

  6. Using the Point class from the previous question and create two points: one called origin initialized to (0, 0), and the other called middle initialized to (100, 100).

  7. Suppose p and q are Point variables, and you don’t know the values of their x and y variables.

    Write a fragment of code that prints “same” if p and q are the same points, and “different” if they are not.

  8. Write a function called same(a, b) that takes two points, a and b, as input and returns true if they are the same, and false if they are different.

  9. Write a void function called display() that goes inside the Point class and prints the point in the form (x, y) (where x and y are replaced by the values for the point).

    When it’s done, you can use it like this:

    Point p = new Point(3, 4);
    p.display();  // prints (3.0, 4.0)
    
  10. Write a class called Ball that stores the center and radius (not diameter!) of a circular ball. Use a Point object to store the location of the center of the ball, and include a render() function that draws the ball. Ball should have constructor that takes in 3 numbers like this:

    Ball b = new Ball(50, 35, 20);  // (50, 35) is the ball's center
                                    // 20 is the ball's radius
    
  11. Suppose p, q, and r are objects of type Point that have already been created. Create an initially empty ArrayList called points and then do the following:

    • add p, q, and r to points
    • next, use the get(i) function of points to print out just the x values of the points it holds
    • next, use a for-in loop to display every point in points in the console window
  12. Suppose a, b, and c are objects of type Ball that have already been created. Create an initially empty ArrayList called circles and then do the following:

    • add a, b, and c to circles
    • next, use the get(i) function of circles to display just the centers of the circles it holds
    • next, use a for-in loop to print the radius of every circle in circles to the console window
  13. Using a particle system and sensible classes create an animation with the following features:

    • Snow (e.g. little white rectangles) falls gently from the top of the screen to the bottom of the screen. You should be able to vary the number of snow flakes by changing a single number in your program.
    • Snow builds up on the ground, from the bottom of the screen to the top, eventually filling, say, half the screen. This can be drawn as a large white rectangle that grows taller as more snow falls.
    • Kids play in the snow. Make the kids little red rectangles, and they should move across the snow, either left or right, and change direction when they hit an edge. The kids should always be on top of the snow, so as it builds up on the ground the kids will move up the screen with it.