Good Programming Structure of a Simple JAVA Program
Documentation Section
\* a line of single space must be given */
Import Files Section
\* a line of single space must be given */
Definition Section
\* a line of single space must be given */
Global Declaration Section
\* a line of single space must be given */
Definition of the class
\* a line of single space must be given */
main method
{
Declaration Section
\* a line of single space must be given */
Input Section
\* a line of single space must be given */
Calculation Section
\* a line of single space must be given */
Output Section
}
Write the following good steps to write a simple JAVA program
Step 1: Documentation Section
Step 2: import Files Section
Step 3: Definition Section
Step 4: Global Declaration Section
Step 5: Definition of the Class
Step 6: Specification of main method
public static void main(String args[])
Step 7: Body of the main method
public static void main(String args[])
{
\* a line of single space must be given */
}
Step 8: Within main method,
(a) Declaration Section
(b) Input Section
(c) Calculation Section
(d) Output Section
About Documentation Section (that is, Step 1:)
The documentation section comprises a set of comment lines giving the name of the program, the author name and other
details etc.,
In dictionary, comment: (n) an observation, remark
1. The comment statement is used to identify the purpose of the program
2. Usually specified at the start of a program. (That is, to give about program details)
3. styles of comments in Java
Style-1: (/* */)
Single line Comment:
(a) The single line comments begin with /* and end at the end of the line with */
(b) Single-line comments are especially useful when short, line-by-line descriptions are needed.
General Form: /* a comment details */
Example: /* This is a single-line comment */
Multiple line comment:
(a) The multi-line comments start with /* and end with a */,
(b) Multi-line comments may not be nested. That is, one comment may not contain another comment.
(c) Programmers use multi-line comments for longer remarks
General Form: /* Comment line-1
Comment line-2
Comment line-3 */
Example: /* This is a
multi-line
comment */
Note: There must be no spaces between the asterisk and the slash.
Style-2:
Single line Comment:
(a) The single line comments begin with /* and end at the end of the line.
(b) Single-line comments are especially useful when short, line-by-line descriptions are needed.
General Form: // a comment details
Example: // This is a single-line comment */
Style-3:
Documentation comment
(a) Java also uses a third style of comment /** */ known as documentation comment.
(b) This form of comment is used for generating documentation automatically.
4. The comment statement is not an executable statement.
5. This statement may or may not be present in the program as per the programmers wish.
6. The compiler skips the comment line when it encounters a comment statement
7. Comment statements may be included in between the program to understand each line of the program.
8. Comments are added at each end of the statement in order to understand the overall program organisation easily.
9. Comments would greatly help in maintaining the program.
10. It is a good programming practice to include comment statements in a program. /* important point */
11. Comments should not appear in the middle of a keyword or identifier.
12. In real applications, comments generally explain how some part of the program works or what a specific feature does.
Documentation Section
\* a line of single space must be given */
Import Files Section
\* a line of single space must be given */
Definition Section
\* a line of single space must be given */
Global Declaration Section
\* a line of single space must be given */
Definition of the class
\* a line of single space must be given */
main method
{
Declaration Section
\* a line of single space must be given */
Input Section
\* a line of single space must be given */
Calculation Section
\* a line of single space must be given */
Output Section
}
Write the following good steps to write a simple JAVA program
Step 1: Documentation Section
Step 2: import Files Section
Step 3: Definition Section
Step 4: Global Declaration Section
Step 5: Definition of the Class
Step 6: Specification of main method
public static void main(String args[])
Step 7: Body of the main method
public static void main(String args[])
{
\* a line of single space must be given */
}
Step 8: Within main method,
(a) Declaration Section
(b) Input Section
(c) Calculation Section
(d) Output Section
About Documentation Section (that is, Step 1:)
The documentation section comprises a set of comment lines giving the name of the program, the author name and other
details etc.,
In dictionary, comment: (n) an observation, remark
1. The comment statement is used to identify the purpose of the program
2. Usually specified at the start of a program. (That is, to give about program details)
3. styles of comments in Java
Style-1: (/* */)
Single line Comment:
(a) The single line comments begin with /* and end at the end of the line with */
(b) Single-line comments are especially useful when short, line-by-line descriptions are needed.
General Form: /* a comment details */
Example: /* This is a single-line comment */
Multiple line comment:
(a) The multi-line comments start with /* and end with a */,
(b) Multi-line comments may not be nested. That is, one comment may not contain another comment.
(c) Programmers use multi-line comments for longer remarks
General Form: /* Comment line-1
Comment line-2
Comment line-3 */
Example: /* This is a
multi-line
comment */
Note: There must be no spaces between the asterisk and the slash.
Style-2:
Single line Comment:
(a) The single line comments begin with /* and end at the end of the line.
(b) Single-line comments are especially useful when short, line-by-line descriptions are needed.
General Form: // a comment details
Example: // This is a single-line comment */
Style-3:
Documentation comment
(a) Java also uses a third style of comment /** */ known as documentation comment.
(b) This form of comment is used for generating documentation automatically.
4. The comment statement is not an executable statement.
5. This statement may or may not be present in the program as per the programmers wish.
6. The compiler skips the comment line when it encounters a comment statement
7. Comment statements may be included in between the program to understand each line of the program.
8. Comments are added at each end of the statement in order to understand the overall program organisation easily.
9. Comments would greatly help in maintaining the program.
10. It is a good programming practice to include comment statements in a program. /* important point */
11. Comments should not appear in the middle of a keyword or identifier.
12. In real applications, comments generally explain how some part of the program works or what a specific feature does.
puplic static void main(String args[])
(a) This line begins the main() method
(b) This is the line at which the program will begin executing.
(c) All Java applications begin execution by calling main().
public
(1). It is a keyword.
(2). It is an access specifier.
(3). It declares the main method as unprotected..
(4). Making main method accessible to all other classes..
(5). Allows the programmer to control the visibility of the class members.
(6). When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.
(7). This is similar to the C++ public modifier.
Important Note: - main() method must be declared as public, since it must be called by code outside of its class when the
program is started.
Static
(1). It is a keyword.
(2). It is a storage class.
(3). Allows main() method to be called without having to instantiate a particular instance of the class.
(4). This is necessary since main() method is called by the Java interpreter before any objects are made.
(5). Declares the main() method as belongs to the entire class and not a part of any objects of the class.
Important Note: - main() method must be declared as static, since the interpreter uses this method before any objects are
created.
Void
(1). It is a keyword.
(2). It is a basic data type.
(3). It is a type modifier
(4). It simply tells the compiler that main() method does not return a value.
main
(1). It is a method.
(2). This method is called when a Java application begins.
(3). Java application program must include main() method.
(4). This is the starting point for the interpreter to begin the execution of the program.
(5). A Java application can have any number of classes but only one of them must include a main() method to initiate the
execution.
(6). This is similar to the main() function in C/C++.
Important Note: - When you begin creating applets - Java programs that are embedded in Web browsers - you won't use
main() method at all, since the Web browser uses a different means of starting the execution of applets.
Parameters
Any information that you need to pass to a method is received by variables specified within the set of parentheses that
follow the name of the method. These variables are called parameters. In main() method, only one parameter is used.
Important Note: - If there no parameters required for a given method, you still need to include the empty parentheses.
String
In Java a string is a sequence of characters. But, unlike many other languages that implement strings as character arrays.
Java implements strings as objects of type String.
String args[]
String args[] declares a parameter named args
The parameter args, which is an array of instances of the class String
Important Note: - Arrays are collections of similar objects.
Objects of type String store character strings. In this case, args[] receives any command-line arguments present when the
program is started.
(b) This is the line at which the program will begin executing.
(c) All Java applications begin execution by calling main().
public
(1). It is a keyword.
(2). It is an access specifier.
(3). It declares the main method as unprotected..
(4). Making main method accessible to all other classes..
(5). Allows the programmer to control the visibility of the class members.
(6). When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.
(7). This is similar to the C++ public modifier.
Important Note: - main() method must be declared as public, since it must be called by code outside of its class when the
program is started.
Static
(1). It is a keyword.
(2). It is a storage class.
(3). Allows main() method to be called without having to instantiate a particular instance of the class.
(4). This is necessary since main() method is called by the Java interpreter before any objects are made.
(5). Declares the main() method as belongs to the entire class and not a part of any objects of the class.
Important Note: - main() method must be declared as static, since the interpreter uses this method before any objects are
created.
Void
(1). It is a keyword.
(2). It is a basic data type.
(3). It is a type modifier
(4). It simply tells the compiler that main() method does not return a value.
main
(1). It is a method.
(2). This method is called when a Java application begins.
(3). Java application program must include main() method.
(4). This is the starting point for the interpreter to begin the execution of the program.
(5). A Java application can have any number of classes but only one of them must include a main() method to initiate the
execution.
(6). This is similar to the main() function in C/C++.
Important Note: - When you begin creating applets - Java programs that are embedded in Web browsers - you won't use
main() method at all, since the Web browser uses a different means of starting the execution of applets.
Parameters
Any information that you need to pass to a method is received by variables specified within the set of parentheses that
follow the name of the method. These variables are called parameters. In main() method, only one parameter is used.
Important Note: - If there no parameters required for a given method, you still need to include the empty parentheses.
String
In Java a string is a sequence of characters. But, unlike many other languages that implement strings as character arrays.
Java implements strings as objects of type String.
String args[]
String args[] declares a parameter named args
The parameter args, which is an array of instances of the class String
Important Note: - Arrays are collections of similar objects.
Objects of type String store character strings. In this case, args[] receives any command-line arguments present when the
program is started.