CS100, Spring 2000, Exercise 3: Objects in Motion and Separation of Concerns
Due in lecture Thursday, February 24

Concepts
+ Class definitions, objects, references, fields.
+ Declaration versus initialization versus instantiation/allocation.
+ Problem solving: separation of concerns and incremental development.
+ Simple sorting.
+ Simple kinematics (physics of motion): position, velocity, acceleration.

Warning/Tip:

    This exercise is broken up into Parts.  Complete them one at a time; doing
    so avoids adding complication atop unfinished or buggy earlier Parts.
    Although it is tempting to do a lot at once, this is not a win because it
    makes it harder to track down errors since you must deal with more untested
    code.  Careful separation of concerns and incremental development will save
    you time.
    
Overview

    Describing the Motion of One Body (summarized from 2/17 lecture discussion)
    ############################################
    
    Assume you a body, like a ball, dropping near the surface of the Earth. The
    force of gravity causes the object to accelerate towards the ground.
    
    y
    ^          
    |           |
    |  ball     | gravity (g)
    |   o       V
    |   
    +----------------> x
    
    Assume that the object is a perfectly elastic ball that bounces perfectly,
    with no losses, such as friction and heat loss.  How do you model the
    motion of the ball?
    
    Given time t (sec) and position y (units of distance, like meters), the
    ball has an average velocity between two states (think "snapshots" in
    time):
    
	Vave = dy/dt where dy=y2-y1 and dt=t2-t1

    Since the ball's real velocity will change as it bounces up and down, you
    can measure this change with acceleration a:
    
        a = dV/dt where dV=V2-V1 and dt=t2-t1

    (Yes, a = -g = -10 m/s^2 near the surface of the Earth.)
    
    Solving for V2 gives V2=V1+a dt.  Since Vave = (V1+V2)/2, you may combine
    these equations to produce Vave = V1 + (a dt)/2.  Since Vave=dy/dt, you may
    substitute results for Vave to produce the following equation:

	      y2 = y1 + v1 dt + (a/2)(dt)(dt)
 
    This equation describes the approximate position of a bouncing ball given a
    previous position y1, velocity v1, constant acceleration a, and time step
    dt=t2-t1.

    Motion of Multiple Bodies
    ############################################

    Consider a simple physical system of 3 non-interacting, massless balls
    moving in one dimension (all along the same line) under the influence of
    constant gravity.  The behavior of each ball in a time step of $dt$ seconds
    can be approximated by these simple equations of motion.

        Position p:     p <-- p + v dt + (a/2)(dt)(dt)
        Velocity v:     v <-- v + a dt
        Acceleration a: a <-- - gravity

    But since the balls do not interact (e.g. collide) with each other, this
    system a a little boring.  We can spice it up a bit by having the balls
    bounce perfectly (elastically) off the ground at p=0.  This requires adding
    in a "bounce" step between updating the position and velocity:

        if p < 0 then v <-- -v.

    (To get a bounce correct, we actually need to change the velocity exactly
    at p=0, which requires solving for p=0.  For simplicity, we omit that
    step.)

    It is your job to use these equations to model the three balls bouncing
    around and to display the results over time.  (Out of necessity, the
    display must be turned on its side.)

Part 0: Getting Started

    Define a class $Ball$ with floating point fields $position$, $velocity$,
    and $acceleration$ for representing the corresponding quantities of a Ball,
    and a $String$ field $name$ for use in displaying the ball.

    In the $main$ method of a separate target class $Exercise3$, declare a
    $Ball$ variable $a$ and instantiate a Ball.  Initialize the fields to
    $name="+"$, $position=20$, $velocity=5$, $accleration=-.2$.

[Note: The original draft had name="a".]

    Complete Part 0 before doing Parts 1, 2, or 3.  It is advised that you do
    parts 1, 2, and 3 separately, and then put everything together in Part 4.

Part 1: Bouncing one Ball

    Use a time step of $dt=1$ and run for a total of 100 seconds.
    (Write your program so that it is easy to change $dt$.)

    Move Ball $a$ according to the rules specified earlier.

    After each time step, in the output go to the next row and draw Ball $a$ as
    its $name$ in column $position$.  For example, if $a$ has $name$ "+" and
    $position$ 5, print 5 spaces and then "+".

[Note: The original draft had name="a".]

    Test your code: verify that the Ball bounces "up and down" off the ground.

Part 2: Drawing sorted Balls

    Declare and instantiate two other balls $b$ and $c$; name them "*" and "#",
    respectively.  Make the balls $a$, $b$, and $c$ have positions in
    increasing order.

[Note: The original draft had names "b" and "c".]

[Note: Key idea: $a$ is the leftmost ball, $b$ the middle, $c$ the rightmost.]

    Draw all of them on the same line of output.  If more than one ball is in
    the same column, then draw only one of them.

    HINT: draw spaces to the left of $a$, draw $a$, draw the spaces between $a$
    and $b$, draw $b$, draw the spaces between $b$ and $c$, draw $c$.

Part 3: Sorting Balls
    
    Declare and instantiate two other balls $b$ and $c$; name them "*" and "#",
    respectively.  Try placing the balls $a$, $b$, and $c$ in various orders.

[Note: The original draft had names "b" and "c".]

    Sort the balls in increasing order.

    HINTS: Put the rightmost in c (sort a&b, sort b&c), then sort a&b.  
	   To sort two items, swap them if they are out of order.

Part 4: Putting it all together

    Combine Parts 1, 2, and 3 after they are working so that you have
    3 balls bouncing for 100 simulation seconds.

    Show your program running from the following initial (starting) conditions:

        "+": position 15, velocity  4
        "*": position 28, velocity   .5
        "#": position  3, velocity -2

What to Hand In:

    Hand in a printout of your program and at least 40 lines of its output.

    See Resizing the Console Window on the Advice page of the course website
    for how to resize the Console (input/output) window.