![]() |
![]() |
|
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
Due Oct.19 (Mon.) in class (hardcopy, and on D2L).
Your name and the assignment-number must be in a comment at the start of the file, and your hardcopy must be stapled. All functions/data must include the appropriate steps1 of the-design-recipe—the design recipe: final version. In particular, test cases alone might be worth half the credit for a function. Unless otherwise indicated, two test cases will suffice for most problems, if they are testing different situations.
I encourage you to actually put all the nonterminals in upper-case (or at least first-letter-upcased), just to make it easier to tell terminals from non-.
<JsFunc> → function <JsIdent>( <JsIdents> ) { <JsBody> } |
(2pts)
As we consider how to define
Working towards a solution, the programmer MC Carthy comes up with the following attempt for a nonterminal that generates comma-seperated lists of “x”s (also ending in a semicolon):
<XS> → ; | x; | x, <XS> |
Prove that this grammar is not correct, by giving a derivation5 of some string which is not a valid comma-separated list (ending in a semicolon).
For the grammar rules, write them as in class: in basic BNF
using “→” (and “
(6pts) Generating a grammar
Give a grammar for Java's boolean expressions.
(That is: a grammar describing exactly what you are allowed to put
inside a the parentheses of a Java
For this problem, assume that you already have grammar rules for
a Java numeric expression
(“
Using your grammar, give a parse tree (not a derivation) for:
In the tree, for children of
The following problems will form the basis of our space-invader program, which we'll complete in the next homework. You may base your answer on hw04b-soln.rkt (but you certainly don't need to); of course give credit and URL if you do so.
Complete problems in racket, except for 5 and 6 which you'll complete in Java and
racket,
as instructed.
Your Java method should compile and run and be correct,
but then you can paste it into a racket block-comment (
(5pts)
In both racket and in Java:
Write the function
The Java and racket versions should both do the same thing:
return a new object, rather than mutating any fields
(Cf.
We won't use Java test-cases (since that entails
overriding
(3 pts) Cannons
As discussed, a cannon might be represented as just a single real-number (see hw04b-soln.rkt).
Write the function
Since the documentation shows that
(2pts) Define a “world” structure which contains a cannon , one bullet, and (for now10) exactly one alien.
As usual for our data-definitions, make examples of the data (at least two), and a template.
For the time being, you can have
For now, you don't need test cases that involve the bullet colliding with anything — you can just have the bullet far away from any aliens or cannons.
hint:place-image is a handy function; it is similar tooverlay/xy except that it crops the result to the background.
hint: For test-cases, include drawing an alien that is: (a) in the middle of a small image; and (b) one that is mostly off the left-edge but has just a few pixels showing.
Note: Here's an image you can (modify to) use in your test-cases, in addition to a solid rectangle or whatever else you might choose: house-with-flowers.rkt. If you place this file in the same directory as other functions, you can just(require "house-with-flowers.rkt") , and then use its exported id (“house-with-flowers ”, coincidentally). You don't need to print this file.
hint: Calldraw-alien with anempty-scene for the background; calldraw-cannon passing it the result ofdraw-alien as the second argument.
(5pts) To do collision detection, write a suitable helper function to detect when two rectangles overlap (e.g. the bullet's bounding-box and the region occupied by a brick). Make at least four test-cases11 for this.
There are several reasonable ways to represent rectangles,
but it helps if your representation aligns with
the arguments needed for
hint: I recommend using half-open intervals, to define a rectangular region: A rectangle centered at 40,50 that is 60x80 would be considered to have x-coordinates in [40-30,40+30) — up to (but not including) the endpoint. It's not that big a deal, but it does mean it's easy to tile the entire plane while neither overlapping at all, nor leaving any 1-pixel-wide (or even infitesimal) gaps.If doing so, a useful helper function to write might be
(<=< a b c) : isa ≤b <c ?
cool hint, from robotics: We can reduce this problem involving two rectangles to a simpler one: checking if an expanded version of the first rectangle merely contains the center of the second.
For example: consider a 20×30 rectangle whose centercorneris at (500,400) and a second rectangle which is 80×60. These two rectangles overlap exactly when the second rectangle's center is inside the (20+80)×(30+60) rectangle centered at (500,400). Sketching this example on paper will help you understand why this works.
All the above should have their tests, as well as signatures and (brief) purpose statements. Only after all tests pass, the following should work (in racket):
(require 2htdp/universe) (big-bang some-initial-world [on-key world-handle-key] [on-tick update-world] [to-draw draw-world]) |
1 Your final program doesn't need to include any "transitional" results from the template: For example, you don't need the stubbed-out version of a function from step 5. However, you should still have passed through this phase. ↩
3 If you really, really want keep everything in one single file, you can insert your image into DrRacket via Edit &go; Insert.... ↩
2 You can photograph or scan your your drawing (jpg, png, gif or pdf only, please), and submit it on D2L alongside your .rkt file.3 ↩
4 I encourage you to notate, to the right of each step, the exact rule-number used for that step (numbering the grammar rules 1,2,3a,3b, etc.). ↩
5 The best answer is the shortest: if you have a simple example that uncovers the grammar's flaw, that is better than a long example doing so. ↩
6 You can turn in your printout + your drawing, or if If you really want just one file to turn in, you can photo and then insert it into DrRacket. ↩
7
On a future hw, we'll write “
9
The problem is that (in a class
public boolean equals( /* Foo this, */ Object that ) { if (that==null) { return false; } else if (this==that) { return true; // short-circuit a common case } else if (that.getClass() != this.getClass()) { return false; // CAUTION: unless we can be .equals to a subclass? } else { Foo thatt = (Foo)that; // NOW you can add your code that actually compares fields, say: return (this.someField==null ? thatt.someField==null : this.someField.equals( thatt.someField )) && (this.someOtherField==null ? thatt.someOtherField==null : this.someOtherField.equals(thatt.someOtherField) // If our constructors never create objects w/ `null`, we can shorten the preceding lines a bit. && … ; } } |
10We'll upgrade the world so that it contains a list-of-aliens in a future homework. ↩
11A comprehensive set of black-box test cases is much much more involved: one rectangle defines 9 regions of potential interest, plus 16 dividing line segments/points; the second rectangle can have its northwest corner in any of those 9+16 regions, and its southwest corner in many of those. From counting-skills learned in discrete math, I count (…lemme think…) 25*24/2 + 25 = 325 test cases, to be reasonably comprehensive. You need to provide about 1% of such tests, whew! ↩
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
©2015, Ian Barland, Radford University Last modified 2015.Oct.17 (Sat) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |