//----------------------------------------------------------------------------- // balls 2/22/2000 T.Yan, DIS // This example is deliberately over-commented to help explain // some of the programming concepts. // // P-Code version //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // class Ball is the class used to instantiate a ball object // in the main class //----------------------------------------------------------------------------- class Ball { char name; // you may use String instead double pos; double vel; } // class Ball //----------------------------------------------------------------------------- // balls is the target for main class // Don't mix up the names balls and Ball! //----------------------------------------------------------------------------- public class balls { public static void main(String[] args) { //--------------------------------------------------------------------- // Allocate and assign 3 ball objects //--------------------------------------------------------------------- // code to fill in //--------------------------------------------------------------------- // Initial Values //--------------------------------------------------------------------- // code to fill in //--------------------------------------------------------------------- // Repeated move and draw balls limit=100 times // Think of each loop as "1 second" of time passing //--------------------------------------------------------------------- int limit = 100; // target number of moves int moves = 0; // number of moves performed while (moves < limit) { //----------------------------------------------------------------- // Sort Balls // The program assumes that a < b < c for position. So, if the // balls swap order, the references must be swapped. //----------------------------------------------------------------- Ball tmp; // code to fill in: // if a's position comes before b's, swap a and b // if b's position comes before c's, swap b and c // now check a and b again: // if a's position comes before b's, swap a and b //----------------------------------------------------------------- // Print balls //----------------------------------------------------------------- // code to fill in: // print blanks // print a.name at a's current pos // print blanks // print b.name at b's current pos if there's no conflict with a // print blanks // print c.name at c's current pos if there's no conflict with b // introduce a "pause" to slow output // print next line to advance the output //----------------------------------------------------------------- // Move balls // Slight problem -- ball cannot go "below ground"! // When the ball hits, assume a completely elastic collision. // So, the ball will bounce in the opposite direction with the // same velocity. In pseudocode, if ball's position <= 0, negate // the velocity. Note that this code would improve using methods! //----------------------------------------------------------------- // increment positions for balls a, b, and c // increment velocities for balls a, b, and c // if position of ball is <=0, // the velocity of that ball reverses //----------------------------------------------------------------- // Recall that the ball stops bouncing after reaching the // target number of moves. //----------------------------------------------------------------- moves++; } } // method main } // class balls