home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
structs
structs
hw04b
Due:
Oct.0204 (Wed) 15:00, D2L and hardcopy.
Your submitted file should be named “hw04.rkt”, plus any .java files.
hw04a is a multiple-choice quiz on D2L
Standard instructions for all homeworks:
-
Your name,
the assignment-number,
and
the URL of this assignment
must be in a comment at the start of the file.
-
Any code which results in syntax errors
will generally not earn more than 50%
of the points-possible.
For grading, I will ignore any commented-out code
(unless the problem is explicitly asking for commented-out code, ’course).
-
If your hardcopy is more than 1page, it must be stapled.
-
Format well
(in particular,
each open-paren should be preceded by a space or start-of-line, for now).
Familiarize yourself with the
frogger arcade game.
We will write a version of this game, but simplified in some ways:
-
The frog itself can face and move in one of four directions.
-
The frog starts at the bottom of the screen.
-
The frog's goal is to kiss a prince(ss).
Princ(ess)es only appear at
a random location at the top of the screen,
stay there for a fixed amount of time (say, 10seconds),
and then disappear, with another prince(ss) appearing somewhere else.
-
Unfortunately for our frog,
there is a highway separating the bottom of the screen from
the top.
S/he must hop to a prince(ss) at the top,
while avoiding speeding trucks.
-
Trucks travel left to right, at different speeds.
You can either have them in separate lanes (as the original
arcade game), or let them overlap.
-
We will not worry about:
rivers/turtles; having multiple frogs trying to several destinations;
trucks that
travel at different speeds or look different.
You are welcome to add additional features, or modify details.
(For instance, having trucks that travel in each direction, etc..)
If you use images or sounds for your game,
you must make sure you have the right to use them.
-
We start by representing trucks, and writing a method involving them.
-
Give a Java class to represent
a truck
(non-static fields and constructor only).
Note that no images are involved
(since the image doesn't vary from one truck to another).
-
Give some Java statements which
create two different Truck objects.
-
The static method move will
which takes in a truck object,
and returns a whole new truck object which has traveled for
one unit of time (a "tick").
-
Write a stub method.
- Write two test cases.
Your tests can either (a) print the actual and expected Trucks
(you'll need a toString),
or (b) use assert and override equals
(or (c) use Junit).
In any case, you may want to refer to the code we wrote in class,
for
Book.java.
You can put those test cases in Truck.main.
- After writing the test cases and getting them to compile,
now complete the method.
You should have a Truck.java file (which compiles and runs);
it should be less than a page of well-spaced, commented code.
-
Repeat all parts of the previous question using
scheme (racket) structs and functions,
rather than Java classes and methods.
(Use check-expect for your test cases, of course.)
-
Now that we understand how to represent and process trucks,
we'll do the similar thing for a prince(ss).
Write a scheme (racket) struct for a prince(ss).
Following the design recipe,
write the function udpate-prince(ss) which
returns a new prince(ss) one tick later (in the same location,
but a little closer to disappearing.)
Note that you don't have toshouldn't worry about what to
do when the prince(ss)'s time has run out, if you don't want;
other later code can check for whether a prince(ss) should
disappear/teleport.
Hints:
-
Imagine a friend has two different snap-shots of the game;
what is the minimal information that your friend needs to tell you so that
you can always properly draw/construct an image of that object on the screen.
-
Do NOT include information which never changes (is “invariant”) for a class.
For example, if all instances of an object are always the same color,
you wouldn't have a field for the color.
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)