//----------------------------------------------------------------------------- // ball0 2/16/2000 T.Yan, DIS // This example is deliberately over-commented to help explain // some of the programming concepts. //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // class Ball is the class used to instantiate a ball object // in the main class //----------------------------------------------------------------------------- class Ball { char name; double pos; double vel; } // class Ball //----------------------------------------------------------------------------- // ball0 is the target for main class // Don't mix up the names ball0 and ball! //----------------------------------------------------------------------------- public class ball0 { public static void main(String[] args) { //--------------------------------------------------------------------- // Allocate and assign a ball object //--------------------------------------------------------------------- Ball a = new Ball(); //--------------------------------------------------------------------- // Customize each run //--------------------------------------------------------------------- TokenReader in = new TokenReader(System.in); do { System.out.print("Enter initial pos: "); a.pos = in.readDouble(); if(a.pos < 0) System.out.println("Wrong!"); } while(a.pos < 0); // should account for other kinds of limitations.... System.out.print("Enter initial vel: "); a.vel = in.readDouble(); System.out.print("Enter acceleration: "); double accel = in.readDouble(); a.name = '*'; // Icon to draw //--------------------------------------------------------------------- // 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) { //----------------------------------------------------------------- // Print ball //----------------------------------------------------------------- for (int i = 0; i < a.pos; i++) System.out.print(" "); // Print blanks for(int s=0; s < 1e6; s++); // Introduce a "pause" to slow output System.out.print(a.name); // Print ball at current pos System.out.println(); // Advance output for new pos //----------------------------------------------------------------- // Move ball // 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. //----------------------------------------------------------------- a.pos = a.pos + a.vel + accel/2.0; a.vel = a.vel + accel; if (a.pos <= 0) { // hit or went "below ground" a.vel = -a.vel; // change dir of vel } //----------------------------------------------------------------- // Recall that the ball stops bouncing after reaching the // target number of moves. //----------------------------------------------------------------- moves++; } } // method main } // class ball0