Solutions to Final Review QuestionsΒΆ
Write a while-loop that uses the
printlnfunction to print the numbers from 15 to 188 on the console:15 16 17 18 ... 187 188
Sample solution:
int i = 15; while (i <= 188) { println(i); ++i; }
Suppose
ageis a variable of typeintthat 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
ageis 0 or less, then print “that’s impossible!” - if
ageis 1 or 2, then print “you’re very young” - if
ageis 3 to 12, then print “Pokemon rocks!” - if
ageis 13 to 19, then print “you’re a teenager” - if
ageis 20 or more, then print “you are incredibly old”
Your code should print exactly one message.
Sample solution:
if (age <= 0) { println("that's impossible!"); } else if (age == 1 || age == 2) { println("you're very young"); } else if (age >= 3 && age <= 12) { println("Pokemon rocks!"); } else if (age >= 13 && age <= 19) { println("you're a teenager"); } else if (age >= 20) { println("you are incredibly old"); }
- if
Write a class called
Wheelthat can draw a wheel. The object should store the center of the wheel, the diameter of the wheel, and its fill color.Wheelshould 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
Wheelclass 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(); }
Sample solution:
class Wheel { float x, y; // center of wheel float diam; // diameter of wheel color c; // color of wheel Wheel(float x_init, float y_init, float diam_init, color c_init) { x = x_init; y = y_init; diam = diam_init; c = c_init; } void render() { noStroke(); fill(c); ellipse(x, y, diam, diam); } }
Suppose you are given an already-written class called
Frogthat is used to animate a frog. You know the following facts aboutFrog:- It has one constructor called
Frog(). - It has a function called
render()that, when called, draws a frog on the screen. - It has a function called
update()that, when called, updates a frog’s internal variables. - The frogs are animated and so will move around the screen. You don’t have
to do anything except repeatedly call
render()andupdate().
Write a complete Processing program that makes 1000 frogs move around the screen without leaving trails. In your code, we want to see you use:
- An
ArrayListto store theFrogobjects. - At least one while-loop, and at least one for-in loop. Use them sensibly!
Don’t write the
Frogclass — it’s been given to your for free. But write the entire rest of the program.Sample solution:
ArrayList<Frog> frogs; void setup() { size(500, 500); frogs = new ArrayList<Frog>(); int i = 0; while (i < 1000) { Frog f = new Frog(); frogs.add(f); i++; } } void draw() { background(255); for(Frog f : frogs) { f.update(); f.render(); } } // you don't need to write this Frog class // to answer this question; it's provided // here to help test your code; plus, // the frogs look a lot like rectangles class Frog { float x, y; float dx, dy; color c; float w, h; Frog() { x = 250; //random(width); y = 250; //random(height); dx = random(-2.0, 2.0); dy = random(-2.0, 2.0); c = color(random(255), random(255), random(255)); w = random(5, 20); h = random(5, 20); } void update() { x += dx; y += dy; if (x < 0 || x >= 500 || y < 0 || y >= 500) { dx = -dx; dy = -dy; } } void render() { fill(c); rect(x, y, w, h); } }
- It has one constructor called
Write a class called
Pointthat represents an (x, y) point. It should have a constructor that takes x and y as input. You should be able to use it like this:Point middle = new Point(100, 200); println(middle.x); // 100 println(middle.y); // 200
Sample solution:
class Point { float x; float y; Point(float init_x, float init_y) { x = init_x; y = init_y; } }
Suppose
pandqarePointvariables, and you don’t know the values of theirxandyvariables.Write a fragment of code that prints “same” if
pandqare the same points, and “different” if they are not.Sample solution:
if (p.x == q.x && p.y == q.y) { println("same"); } else { println("different"); }
Write a function called
same(a, b)that takes two points,aandb, as input and returnstrueif they are the same, andfalseif they are different.Sample solution:
boolean same(Point a, Point b) { if (a.x == b.x && a.y == b.y) { return true; } else { return false; } }
Write a
voidfunction calleddisplay()inside thePointclass that prints the point in the form(x, y)(wherexandyare 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)
Sample solution:
class Point { float x; float y; Point(float init_x, float init_y) { x = init_x; y = init_y; } void display() { print("(" + x + ", " + y + ")"); } }
Write a class called
Ballthat stores the center and radius (not diameter!) of a circular ball. Use aPointobject to store the location of the center of the ball, and include arender()function that draws the ball.Ballshould 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
Sample solution:
class Ball { Point center; float radius; Ball(float x, float y, float r) { center = new Point(x, y); radius = r; } void render() { ellipse(center.x, center.y, 2*radius, 2*radius); } }
Suppose
p,q, andrare objects of typePointthat have already been created. Create an initially emptyArrayListcalledpointsand then do the following:- add
p,q, andrtopoints - next, use the
get(i)function ofpointsto print out just thexvalues of the points it holds - next, use a for-in loop to display every point in
pointsin the console window
Sample solution:
ArrayList<Point> points; void setup() { Point p = new Point(1, 2); Point q = new Point(3, 4); Point r = new Point(5, 6); points = new ArrayList<Point>(); points.add(p); points.add(q); points.add(r); println(points.get(0).x); println(points.get(1).x); println(points.get(2).x); for(Point a : points) { a.display(); } }
- add
Suppose
a,b, andcare objects of typeBallthat have already been created. Create an initially emptyArrayListcalledcirclesand then do the following:- add
a,b, andctocircles - next, use the
get(i)function ofcirclesto display just the centers of the circles it holds - next, use a for-in loop to print the radius of every circle in
circlesto the console window
Sample solution:
ArrayList<Ball> circles; void setup() { Ball a = new Ball(100, 100, 10); Ball b = new Ball(200, 200, 20); Ball c = new Ball(300, 300, 30); circles = new ArrayList<Ball>(); circles.add(a); circles.add(b); circles.add(c); circles.get(0).center.display(); circles.get(1).center.display(); circles.get(2).center.display(); for(Ball d : circles) { println(d.radius); } }
The statements for displaying the centers are a bit complex, and so, for instance, you could rewrite
circles.get(0).center.display()like this:Ball x = circles.get(0); Center c = x.center; c.display();
- add
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.
Sample solution:
class Sprite { float x, y; float dx, dy; void update() { x += dx; y += dy; } } class SnowFlake extends Sprite { float sz; // size of the snow flake SnowFlake() { reset(); } void reset() { x = random(10, width - 10); y = random(-200, 0); dx = random(-0.5, 0.5); dy = random(1.0, 3.0); sz = random(1.0, 6.0); } void render() { pushMatrix(); fill(255); noStroke(); rect(x, y, sz, sz); popMatrix(); } void update() { super.update(); if (y >= height) { reset(); ground.addFlake(); } } } // class SnowFlake class Snowbank extends Sprite { float sz; void render() { noStroke(); fill(255); rect(0, height - sz, width, sz); } void addFlake() { sz += 0.01; sz = constrain(sz, 0.0, height * 0.35); } } // class Snowbank class Kid extends Sprite { float w; // width float h; // height Kid() { w = 5; h = 10; x = random(10, width - 10); y = height - ground.sz - h; dx = 1.0; if (random(2) < 1) { dx = -dx; } } void render() { pushMatrix(); fill(255, 0, 0); noStroke(); rect(x, y, w, h); popMatrix(); } void update() { super.update(); y = height - ground.sz - h; if (x <= 0 || x >= width) { dx = -dx; } } } // class Kid ArrayList<SnowFlake> snow; Snowbank ground; ArrayList<Kid> kids; void setup() { size(500, 500); snow = new ArrayList<SnowFlake>(); int i = 0; while (i < 100) { snow.add(new SnowFlake()); i += 1; } ground = new Snowbank(); kids = new ArrayList<Kid>(); i = 0; while (i < 1) { kids.add(new Kid()); i += 1; } } void draw() { background(200); ground.render(); ground.update(); for (SnowFlake flake : snow) { flake.render(); flake.update(); } for (Kid k : kids) { k.render(); k.update(); } }