Final Review QuestionsΒΆ
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
Suppose
age
is a variable of typeint
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.
- if
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(); }
Suppose you have a classed called
Frog
that 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 aFrog
object, draws it on the screen. - It has a function called
update()
that, when called on aFrog
object, 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
Frog
class 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
ArrayList
to store theFrog
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.- It has one constructor called
Write a class called
Point
that 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
origin
initialized to (0, 0), and the other calledmiddle
initialized to (100, 100).Suppose
p
andq
arePoint
variables, and you don’t know the values of theirx
andy
variables.Write a fragment of code that prints “same” if
p
andq
are the same points, and “different” if they are not.Write a function called
same(a, b)
that takes two points,a
andb
, as input and returnstrue
if they are the same, andfalse
if they are different.Write a
void
function calleddisplay()
that goes inside thePoint
class and prints the point in the form(x, y)
(wherex
andy
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)
Write a class called
Ball
that stores the center and radius (not diameter!) of a circular ball. Use aPoint
object to store the location of the center of the ball, and include arender()
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
Suppose
p
,q
, andr
are objects of typePoint
that have already been created. Create an initially emptyArrayList
calledpoints
and then do the following:- add
p
,q
, andr
topoints
- next, use the
get(i)
function ofpoints
to print out just thex
values of the points it holds - next, use a for-in loop to display every point in
points
in the console window
- add
Suppose
a
,b
, andc
are objects of typeBall
that have already been created. Create an initially emptyArrayList
calledcircles
and then do the following:- add
a
,b
, andc
tocircles
- next, use the
get(i)
function ofcircles
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
- 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.