Java Reference
In-Depth Information
In this and previous chapters you learned how to write methods to divide a problem into
subproblems, solve each subproblem, and then combine the methods to form the
complete program to get a solution of the problem. A program may contain a number
of methods. In a complex program, usually, when a method is written, it is tested and
debugged alone. You can write a separate program to test the method. The program that
tests a method is called a driver program. For example, the program in Example 7-12,
contains methods to convert the length from inches to centimeters and vice versa. Before
writing the complete program, you could write separate driver programs to make sure
that each method is working properly.
Sometimes the results calculated by one method are needed in another method. In that
case, the method that depends on another method cannot be tested alone. For example,
consider the following program that determines the time to fill a swimming pool:
import
java.util.*;
DEBUGGING
public class
Pool
{
static
Scanner console =
new
Scanner(System.in);
static final double
GALLONS_IN_A_CUBIC_FEET = 7.48;
public static void
main(String[] args)
{
double
length, width, depth;
double
fillRate;
int
fillTime;
System.out.print("Enter the length, width, and the "
+ "depth of the pool, (in feet): ");
length = console.nextDouble();
width = console.nextDouble();
depth = console.nextDouble();
System.out.println();
System.out.print("Enter the rate of the water, "
+ "(in gallons per minute): ");
fillRate = console.nextInt();
System.out.println();
fillTime = poolFillTime(length, width, depth, fillRate);
print(fillTime);
}
public static double
poolCapacity(
double
len,
double
wid,
double
dep)
Search WWH ::
Custom Search