Page 360 - Introduction to Programming with Java: A Problem Solving Approach
P. 360

                326 Chapter 8 Software Engineering
3. Maintain a “standard” bank of test input data (and the corresponding output data), and use it to verify that any changes you have made affect only the problem you are trying to solve and do not have other unwanted effects that ripple through the program.
8.10 Merging Driver Method into Driven Class
It’s legal to include a main method in any class. Figure 8.10 contains a simple Time program that in- cludes its own main method.
  /******************************************************************
*
Time.java
Dean & Dean
This class stores time in the form of hours, minutes, and
seconds. It prints the time using military format.
******************************************************************/
*
*
*
*
public class Time
{
private int hours, minutes, seconds;
//***************************************************************
         Apago PDF Enhancer
public Time(int h, int m, int s)
{
}
this.hours = h;
this.minutes = m;
this.seconds = s;
//***************************************************************
public void printIt()
{
System.out.printf("%02d:%02d:%02d\n",
hours, minutes, seconds);
} // end printIt
//***************************************************************
public static void main(String[] args)⎫ {⎪
  Time time = new Time(3, 59, 0);
⎪ ⎬
This is a driver for the rest of the code in this class.
  }
time.printIt(); ⎪ } // end main ⎪ // end class Time ⎭
Figure 8.10
Time class with built-in main driver method


























































   358   359   360   361   362