Homework Assignment 5

Toward a Better Ball World


CS 2530
Intermediate Computing


Due: Friday, October 5, at 5:00 PM


Introduction

For this assignment, you will make some changes to the Ball World program we studied this week. These changes will give you a chance to practice with inheritance and constructors, the concepts at the center of this program. They will also give you a chance to make decisions about where code belongs in a system of many parts.



A Little More Java

In Session 8, we learned that variables and methods declared as static are features of the classes, not of the instances we create. This can be used to create named constants, as in the BallWorld class:

    private static final int FrameWidth  = 600;
    private static final int FrameHeight = 400;
    private static final int Iterations  = 20;

The static modifier can also be used to simulate class methods, which allow us to send messages to the class itself. This is an advanced feature of OO programming, which we probably won't cover in any detail this semester.

However, static can also be used to create utility methods, which are useful in a wide variety of circumstances but which don't belong to any particular object. The Java libraries provide some handy static utility methods, especially math functions for which there are no operators.

Going Random. For this assignment, you will want to use the utility method Math.random(), which returns a double in the range [0.0, 1.0). That means it could return 0.0, but it will never return 1.0.

As it is a class method, you can invoke it directly, without creating an instance of the Math class. For example,

    double smallNumber = Math.random();

will store a value between 0.0 (inclusive) and 1.0 (exclusive) in smallNumber. To simulate a coin flip, we might try

    int coinFlip = (int) (2 * Math.random());

The expression (2 * Math.random()) returns a value of type double between 0.0 (inclusive) and 2.0 (exclusive). When cast as an integer, the value will be rounded down to either 0 or 1. Voilá! Heads or tails. Well, sort of.

Slowing Down. For this assignment, you will also want to use the utility method Thread.sleep(). This method takes as an argument a number of milliseconds It "puts the program to sleep" for that long before resuming. This can be useful for making the balls in our Ball World move slowly enough that we can see them!

Thread.sleep() is a method that involves some Java we aren't quite ready to talk about yet. For now, simply place this statement immediately after the ball moves

    try { Thread.sleep( delay ); } catch (Exception e) {}
We will learn what that Java means in a little while.



Tasks

Modify.    Make the following changes to the Ball World program, in order:

  1. Make it so that the BallWorldFrame closes when we press the close button on the window.

  2. Make it so that Ball starts in the middle of the window.

  3. Make it so that the color of the Ball is chosen at random from a set of three colors.
    You can choose the color palette, if you like, or use the standards red, blue, and green.

  4. Make it so that Ball moves in a random direction.

  5. Create a MultiBallWorldFrame that creates more than one Ball of random color and direction in the same window.
    The number of balls to create is passed to the MultiBallWorldFrame at the time it is created.

Explain.    Create a file named readme.txt. In it, explain why you implemented each of these tasks in the place you did, rather than somewhere else in the program.

Enjoy.    Add a new feature of your own choosing to the program. Have as much as you are willing! Add a note to readme.txt explaining your implementation.



Deliverables

By the due date and time, submit a zipped file containing:

For your hardcopy, submit only your readme.txt file.

Be sure that your submission follows all homework submission requirements.



Eugene Wallingford ..... wallingf@cs.uni.edu ..... October 2, 2012