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
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.
- 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(); }Suppose you have a classed 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
draw()that, when called on aFrogobject, draws it on the screen. - It has a function called
update()that, when called on aFrogobject, updates its internal variables. You should always callupdate()beforerender(). - The frogs are animated and so will move around the screen. You don’t have
to worry about this movement — the code in the
Frogclass takes care of all of that for you as long as you callupdate()andrender().
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
ArrayListto store theFrogobjects. - At least one while-loop, and at least on for-in loop used in sensible ways.
Except for the
Frogclass, write the entire program.- 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.Using the Point class from the previous question and create two points: one called
origininitialized to (0, 0), and the other calledmiddleinitialized to (100, 100).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.Write a function called
same(a, b)that takes two points,aandb, as input and returnstrueif they are the same, andfalseif they are different.Write a
voidfunction calleddisplay()that goes inside thePointclass and 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)
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 radiusSuppose
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
- 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
- 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.