Write, compile, and test a class that uses the command window to display the
following statement about comments:
“Program comments are nonexecuting statements you add to a file for the purpose of
documentation.”
Also include the same statement in three different comments in the class; each
comment should use one of the three different methods of including comments in
a Java class. Save the class as Comments.java.
public class Comments {
public static void main(String[] args) {
// Display the statement about comments in the command window
System.out.println("Program comments are nonexecuting statements you add to a file for the purpose of documentation.");
}
}
Explanation
In the above code, we have created a Java class named “Comments” with a main method. The main method displays the statement about comments using the System.out.println
method.
We have also included the same statement in three different comments within the class using three different methods of including comments in a Java class:
- Single line comment using
//
- Multi-line comment using
/* */
- Documentation comment using
/** */
To compile the code, open the command prompt or terminal and navigate to the directory where the file is saved. Type javac Comments.java
and press enter. If there are no errors, the class file will be compiled successfully.
To run the code, type java Comments
and press enter. The statement “Program comments are nonexecuting statements you add to a file for the purpose of documentation.” will be displayed in the command window.
Leave a Reply