Java Reference
In-Depth Information
entered but does not yet include the detailed code. While the program stub can
be compiled and run, it needs additional code to provide the functionality speci-
fied in the requirements document.
Compiling and Testing the Program Stub
Recall that the compiler automatically saves the program and then translates
the source code into Java bytecode. The following steps show how to test the
Commission program stub by compiling the source code and then running the
bytecode.
To Compile and Test the Program Stub
OTHER WAYS
1. With the Data Disk in drive A, compile the program by clicking Compile
Java on the Tools menu. If TextPad notifies you of errors, click
Commission.java in the Selector window, fix the errors, and then
compile again.
2. When the program compiles with no errors, if necessary, click
Commission.java in the Selector window to display the code in the
TextPad window. Click Run Java Application on the Tools menu.
3. When the command prompt window displays, click the Close button in
the title bar.
TextPad compiles and runs the program. No output displays unless the code
has errors. If TextPad notifies you of errors, fix the errors and then compile
the program again.
1. To compile, press
CTRL + 1
2. To compile at
command prompt,
type javac
Commission.java
3. To run, press CTRL + 2
4. To run at command
prompt, type java
Commission
Coding, compiling, and then testing a program stub before moving on to the
next section of code can provide several benefits. You can check for compilation
errors with fewer lines of code, see the results of just one condition or one set of
inputs, or debug and look for problems within a narrower framework.
Writing Methods
The object-oriented nature of Java lends itself to modularity in both the
program design and implementation processes. Modularity is a characteristic
of a program in which a larger program's source code is broken down into
smaller sections, or modules, of source code. In Java, these modules include
methods and classes.
Breaking a large program's source code down into smaller modules applies
to both the design process and implementation. As shown in the previous steps,
a Java program needs only one public class and one main() method in order to
compile the source code. After that, it does not matter how many other methods
you add. In a program with many different methods, good program design
involves keeping the code for each method in its own separate and reusable sec-
tion. Thus, instead of writing all code in the main() method, you can write code
to transfer execution from the main() method to a user-defined method. The
user-defined method (sometimes called a programmer-defined method) is
coded in its own separate and reusable section of code, or module.
Search WWH ::




Custom Search