//******************************************************************** // // Class Name: Countdown10.java // Program goal: An Object-Oriented Rocket Countdown // // Author Name: Prof. Lew Hitchner // Email: hitchner@csc.calpoly.edu // URL: http://www.csc.calpoly.edu/~hitchner/ // UNIX home: /home/b1a/lhitchne/ // // Course: CSC/CPE 101 // Section: 03 // Term Spring 2000 // Department: Computer Science // School: California Polytechnic State University // Address: San Luis Obispo, CA 93407 USA // // History: // 01/04/2000 Lewis and Loftus: original program // 03/24/2000 Hitchner: added some documentation // 01/07/2001 Scheftic: modified documentation to current CSC101 template // //******************************************************************** // // Program Description and Design Overview: // Prints lines of output representing a rocket countdown. // // Input Requirements: // None. // // Output Requirements: // First line: 10... 9... 8... 7... 6... 5... 4... 3... 2... 1... 0... Liftoff! // Second line: Houston, we have liftoff. // // Variables Used: // private data fields with class scope // startCount // count // // Included Methods: // BlastOff( ) prints blast-off message // Countdown10( ) default constructor, starts at 10 // Countdown10(int) alternate constructor, sets starting value // Down(int) mutator, counts down (by int decrement) // IsCounting( ) test if countdown still in progress // PrintCount( ) prints count at each step // Reset ( ) mutator, to restart countdown // main // // Imported Methods: // java.lang: // System.out.print // System.out.println // // Program Assumptions: // Unless code is deliberately modified, // the countdown always starts at 10 // // Pending Problems: // could modify to accept user input for countdown starting value // //******************************************************************** public class Countdown10 { //----------------------------------------------------------------- // Fields (private data) //----------------------------------------------------------------- private int startCount; // starting countdown value private int count; // current countdown value //----------------------------------------------------------------- // The default constructor //----------------------------------------------------------------- public Countdown10() { startCount = 10; count = startCount; } //----------------------------------------------------------------- // Constructor for starting at an alternate count value //----------------------------------------------------------------- public Countdown10( int start ) { if ( start >= 0 ) startCount = start; else startCount = 0; count = startCount; } //----------------------------------------------------------------- // Accessor method for printing the current value of count //----------------------------------------------------------------- public void PrintCount() { if ( (count % 11) == 0 && count != 0 ) System.out.println ( count + "... " ); else System.out.print ( count + "... " ); } //----------------------------------------------------------------- // Accessor method for testing if countdown is still in progress //----------------------------------------------------------------- public boolean IsCounting() { if ( count >= 0 ) return true; else return false; } //----------------------------------------------------------------- // Accessor method for printing blastoff message //----------------------------------------------------------------- public void BlastOff() { // Print final message strings System.out.println ( "Liftoff!" ); System.out.println ( "Houston, we have liftoff." ); } //----------------------------------------------------------------- // Mutator method for counting down //----------------------------------------------------------------- public void Down( int decrement ) { count = count - decrement; } //----------------------------------------------------------------- // Mutator method for restarting the countdown //----------------------------------------------------------------- public void Reset() { count = startCount; } //----------------------------------------------------------------- // The main method. //----------------------------------------------------------------- public static void main (String[] args) { // Construct a new Countdown rocket object // (default object counts down from 10) Countdown10 rocket = new Countdown10(); // Loop until countdown is over while ( rocket.IsCounting() ) { rocket.PrintCount(); // print the current count rocket.Down( 1 ); // count down by 1 } // Print final message strings rocket.BlastOff(); } // end main } // end class Countdown10