CS 101, Spring 2012
Computer Science 1 in Java
Homework 5 (due Mar. 16)

This assignment deals with program design using procedural abstraction, and with recursion. See sections 2.1 and 2.3 of the textbook, as well as the code examples posted on the CS1 homepage. You may also need to consult the Java API documentation at the Sun Java website (see the references section on CS1 homepage for the link).

Deliverables

Submit your HW as a zipped folder on the CS101 site on BB Vista. Include the source files (.java files) for your programs as individual files. Screenshots should also be included. Use names for your screenshots that explain what they correspond to (for example, task2.inputScreen.jpg).

Notes

Each source file submitted should include the following items as comments:


  1. (5 points) Write a program for single-player ping-pong. Your program should display a window that shows a thin vertical rectangular "racket". The racket should track the mouse (cursor). A small circular ball should enter the window from the right edge, traveling in a non-horizontal, non-vertical direction at constant speed. This ball should bounce elastically off the edges of the window, as well as off the right side of the racket (but not the left). The object of the game is for the player to "hit" the ball as many times as possible. The program should keep score, continuously displaying the current score near the top right corner of the window, in the format "n out of m", where n is the number of hits and m is the number of tries (a try is either a hit, or a bounce off the left edge of the window). The illustration below shows the screen at the instant at which a hit is occurring.

    Notes

    • Design the overall structure of the program carefully. Define and use static methods liberally, in order to encapsulate key procedural ingredients and provide code that can be more easily understood by others. This will count toward your grade.

    • The StdDraw class provides useful methods mouseX() and mouseY(). See the documentation.

    Grading:

    • Adequate comments provided in source code, including your name, e-mail address, and attributions (or references): 0.5 points
    • Program compiles and runs without errors when StdDraw.class and StdAudio.class are provided in the same folder: 0.5 points
    • Program code makes effective use of static methods for encapsulation of procedural ingredients: 1 point
    • Program draws racket and ball that moves at constant speed and bounces elastically off of window edges and right side of racket: 1 point
    • Program tracks mouse (cursor) with racket: 1 point
    • Scores (hits and tries) are reported correctly: 1 point


  2. (5 points) The following main method illustrates how the recursive drawing methods that you are asked to implement in this task should be invoked.
    	public static void main(String[] args) {
    		// parameters are x, y, size, recursion depth
    		draw(0.5, 0.5, .25, 6);
    	}
    
    Each of the examples below illustrates the output produced by one of the recursive drawing methods that you are asked to implement in this task. Submit a complete, documented source file named RecursiveDrawing.java that includes implementations of each of these recursive drawing methods. The method header and comments are provided in each case below. Include screenshots of the drawings produced with each of your methods when invoked as shown in the main method above. Note that the figures below were generated by calling the respective methods with precisely the parameter values shown above. In particular, the recursion depth is 6 in all cases. The usual StdDraw scale conventions are in effect.

    a)

    // draw1 draws recursive pattern of n nested squares, with center at (x,y)
    static void draw1(double x, double y, double size, int n)
    

    b)

    // draw2 draws recursive pattern of circles (only circles should be drawn - no other figures);
    // three smaller circles along circumference should be centered on circumference itself, one 
    // centered at the topmost point, the other two symmetrically left and right, each equidistant 
    // from the others, so that the centers form the vertices of an equilateral triangle;
    // center of largest circle is at (x,y), size is the radius, n is the recursion depth
    static void draw2(double x, double y, double size, int n)
    

    c)

    // draws recursive pattern of n nested squares, with center of main square at (x,y);
    // size is half-width of main square, n is recursion depth of overall pattern;
    // a picture is worth a thousand words; look carefully
    static void draw3(double x, double y, double size, int n)
    

    Grading:

    • Adequate comments provided in source code, including your name, e-mail address, and attributions (or references): total of 1 point for entire task
    • Program compiles and runs without errors when StdDraw.class is provided in the same folder: total of 1 point for entire task
    • draw1 works correctly: 1 point
    • draw2 works correctly: 1 point
    • draw3 works correctly: 1 point