ptg7068951
150 HOUR 11:Describing What Your Object Is Like
- Enter VirusLabin the Main Class field, and enter the number of
Virusobjects you’d like the program to create in the Arguments
field. - Click OK to close the dialog.
To run a program you’ve configured in this manner, choose Run, Run
Main Project in NetBeans.
Arguments are read into an application using a string array that’s sent to
the main()method. In the VirusLabclass, this occurs in Line 2.
To work with an argument as an integer, it must be converted from a
Stringobject to an integer. This requires the use of the parseInt()class
method of the Integerclass. In Line 3, an intvariable named numViruses
is created from the first argument sent to the program on the command
line.
If the numVirusesvariable is greater than 0, the following things take place
in the VirusLabapplication:
. Line 5: An array of Virusobjects is created with the numVirusesvari-
able determining the number of objects in the array.
. Lines 6–8: Aforloop is used to call the constructor method for each
Virusobject in the array.
. Lines 9–10: After all the Virusobjects have been constructed, the
getVirusCount()class method of the Virusclass is used to count the
number of its objects that have been created. This should match the
argument that was set when you ran the VirusLabapplication.
If the numVirusesvariable is not greater than 0, nothing happens in the
VirusLabapplication.
After the VirusLab.javafile has been compiled, test it with any command-
line argument you’d like to try. The number of Virusobjects that can be
created depends on the memory that’s available on your system when you
run the VirusLabapplication. On the author’s system, anything greater
than 5.5 million viruses causes the program to crash after displaying an
OutOfMemoryErrormessage.
If you don’t specify more Virusobjects than your system can handle, the
output should be something like Figure 11.1.