Compile a two-line text-printing program


Discuss the below:

Q: Compile a two-line text-printing program, similar to the Java welcome programming examples on pp. 37, 43, 45, and 46 of the text, using one of the techniques explained in. The first line should display the following text: "Hi, my name is [insert your name]." The second line should display the following text: "I was born in [insert your birth place].2.2 First Program in Java: Printing a Line of Text pg. 37

1 // Fig. 2.1: Welcome1.java
2 // Text-printing program.
34
public class Welcome1
5 {
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.println( "Welcome to Java Programming!" );
10
11 } // end method main
12
13 } // end class Welcome1
Welcome to Java Programming!

Fig. 2.1 | Text-printing program.

Line 1
// Fig. 2.1: Welcome1.java
begins with //, indicating that the remainder of the line is a comment. Programmers insert
comments to document programs and improve their readability. This helps other
people to read and understand programs. The Java compiler ignores comments, so they
do not cause the computer to performany action when the programis run.We begin every
program with a comment indicating the figure number and file name.
A comment that begins with // is called an end-of-line (or single-line) comment,
because the comment terminates at the end of the line on which it appears. A // comment
also can begin in the middle of a line and continue until the end of that line (as in lines 11
and 13).
Traditional comments (also called multiple-line comments), such as
/* This is a traditional
comment. It can be
split over many lines */
can be spread over several lines. This type of comment begins with the delimiter /* and ends with */. All text between the delimiters is ignored by the compiler. Java incorporated traditional comments and end-of-line comments from the C and C++ programming languages,
respectively. In this book, we use end-of-line comments. Java also provides Javadoc comments that are delimited by /** and */. As with traditional comments, all text between the Javadoc comment delimiters is ignored by the compiler. Javadoc comments enable programmers to embed program documentation directly in their programs. Such comments are the preferred Java commenting format in industry. The javadoc utility program (part of the J2SE Development Kit) reads Javadoc comments and uses them to prepare your program's documentation in HTML format. We demonstrate Javadoc comments and the javadoc utility in Appendix H. For complete information, visit Sun's javadoc Tool Home Page at java.sun.com/j2se/javadoc.

Pg 43
1 // Fig. 2.3: Welcome2.java
2 // Printing a line of text with multiple statements.
3
4 public class Welcome2
5 {
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.print( "Welcome to " );
10 System.out.println( "Java Programming!" );

11
12 } // end method main
13
14 } // end class Welcome2

Pg 45

1 // Fig. 2.4: Welcome3.java
2 // Printing multiple lines of text with a single statement.
3
4 public class Welcome3
5 {
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.println( "Welcome to Java Programming!" );
10
11 } // end method main
12
13 } // end class Welcome3

Pg 46

1 // Fig. 2.6: Welcome4.java
2 // Printing multiple lines in a dialog box.
34
public class Welcome4
5 {
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.printf( "%s\n%s\n",
10 "Welcome to", "Java Programming!" );
11
12 } // end method main
13
14 } // end class Welcome4

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Compile a two-line text-printing program
Reference No:- TGS01935158

Now Priced at $25 (50% Discount)

Recommended (96%)

Rated (4.8/5)