RU beehive logo ITEC dept promo banner
ITEC 380
2019spring
ibarland

structs
hw04

Due:
Feb.21 (Thu(!)) 14:00 accepted through Feb.21 24:00, due to power-failure; bring hardcopy to next class, D2L and hardcopy.
Your submitted file should be named “hw04.rkt”, plus any .java files.

Standard instructions for all homeworks:


(This is the completion of hw04a, from D2L.) Consider the following information, which might be part of a sports-simulation:

A team is:

  1. (5pts) Give a data definition (including examples of the data, and the template for any function which processes a team), as per steps 1-3 of the design recipe. Give at least three examples of a team.

  2. (5pts) We'll say one team is “greater” than another if its offense is higher than the other's defense, and its defense is higher than the other's offense. Write the function team>? to implement this. Follow all the steps of the design recipe1. As always, be sure to think about enough test-cases to cover the interesting situations (half of the pts). (In this case, six or seven should suffice, if they each cover a different situation.)


Familiarize yourself with the arcade game Breakout (a.k.a. “brick breaker”), such as this example. We will write a simplified version which does not need to deal with: score, powerups2, having more than one level, or number-of-lives.

There are three types of things (objects) which our program will need to model: bricks, balls, and paddles.

  1. (10pts) Give a data-definition for a ball in Java — that is, a class Ball. Include examples of the data, and a template for any Ball-processing function(method). As always, be sure to mention the units for any fields, as needed.

    hints: General tips for deciding what fields an object needs:
    • Imagine a friend is playing your game, and paused+saved it mid-play. When they later re-open that save-file, what info needs to be in there about a particular ball, so the program can exactly re-construct the ball how it was the moment your friend saved it?

    • Do not include information that is not part of the object itself.

    • 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. (You wouldn't want 1000 objects, each with their own field, which all necessarily hold the exact same value.)

    • Sometimes x,y coordinates suffice to describe something's position. However, sometimes more context might be needed. For example: what is the latitude and longitude of Virginia?

      On a related note: For a problem below, you might be using place-image, which places the center of an image at specified coordinates. If your internal representation is consonant with libraries you use, it can make things a smidgen easier.

    Since this is Java, you will also need to create: a constructor, equals(Object), and hashCode() (and optionally, toString()). These will all be boilerplate methods similar to lecture’s Book.java.

    Note: You do not need to create getters/setters (and moreover, you should not). Moreover, for this homework and course, we will use immutable data, so you should not reassign to any fields3

  2. (5pts) Now write a Java Ball method Ball move() which returns a Ball whose position is changed, to reflect its traveling for one frame (one “tick”). This function should not worry about collisions.

The remaining functions are to be in racket (not Java; the Java version is just to compare and emphasize how similar the immutable-Java and racket programs will be.

  1. (5pts) Give racket data-definitions, examples-of-data, and template for ball, racketpaddle, and brick.
    Note: Although full racket does support struct-inheritance, we will not use it here
  2. (5pts) Write move-ball in racket, which behaves the same as Ball#move()5.
  3. (5pts) Write bounceS-side : ball -> ball which changes the direction the ball is moving (left vs. right). It should not move the ball at all.
    Then, write bounceT-topB-bottom which does the save, but for moving up vs. down.
  4. (5pts) Write paddleH-handleK-key : paddle, key-event? -> paddle, which updates a paddle in response to a keypress.
  5. (5pts) Drawing functions:
    1. Write the function draw-brick : brick, image -> image, which takes a brick and a “background” image, and returns that background image with the brick drawn on top of it.
      hint: place-image is a handy function; it is similar to overlay/xy except that it crops the result to the background.
      hint: For test-cases, include drawing an brick 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, but do submit it on D2L so that I can run your program.
    2. Write draw-ball, similarly.
    3. Write draw-paddle, similarly.

1 Note that the stub-function, step 5e, gets replaced with the real body (step 7), so it won't be visible in what you submit. Also, you don't need to include the names of constructors/selectors; that was part of quiz03 but not part of the design recipe.      
2 If completing itec380 for an honors-college credit: After completing the regular part of this homework, choose one powerup, and implement it.
Afterwards, brainstorm and come and meet w/ me to discuss: how other powerups might be added, but in a more general way so that adding additional powerups would mean not needing to update any existing code?      
3 With the possible exception of caching an object's hashcode, as in Book.java.      
4 The long flag --enableassertions can be abbreviated -ea.      
5 The notation ``Ball#move()'' is standard Java for "a method move inside class Ball which takes no parameters".      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.