Skip to content

Commit d687e5a

Browse files
committed
Add java files about comments in Java
1 parent 094fb1b commit d687e5a

3 files changed

Lines changed: 107 additions & 8 deletions

File tree

CodeStart/chap_01/Comments/CommentDemo.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
// =============================================================
2-
// copyright: Dr. Saad Laouadi 2024
3-
// =============================================================
4-
// This is one line comment
1+
/*****************************************************************************
2+
* PROJECT: JAVA-101 COMPREHENSIVE PROGRAMMING COURSE
3+
* MODULE: INTRODUCTION TO JAVA FUNDAMENTALS
4+
* LESSON: 1.2 - COMMENTS AND STRING FORMATTING
5+
*
6+
* AUTHOR: DR. SAAD LAOUADI, PH.D.
7+
* CREATED: MARCH 2025
8+
*
9+
* COPYRIGHT © 2025 DR. SAAD LAOUADI. ALL RIGHTS RESERVED.
10+
* SEE LICENSE FILE FOR COMPLETE TERMS.
11+
*****************************************************************************/
512

13+
// This is one line comment
614

715
public class CommentDemo {
816
// This is a simple method to create a banner
917

1018
/*
11-
This is multi-line comment
12-
add here
19+
This is multi-line comment
20+
add here
1321
add other thing here
1422
*/
1523

@@ -30,7 +38,7 @@ public static void main(String[] args) {
3038
System.out.println(banner);
3139
System.out.println(" Comments in Java ");
3240
System.out.println(banner);
33-
41+
3442
System.out.println("program run successfully and ended here!");
3543
// passAfterEnd(3);
3644
addEmptyLineAtEnd(3);
@@ -56,4 +64,4 @@ public static void addEmptyLineAtEnd(int nlines) {
5664
}
5765
System.out.print(sb.toString());
5866
}
59-
}
67+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*****************************************************************************
2+
* PROJECT: JAVA-101 COMPREHENSIVE PROGRAMMING COURSE
3+
* MODULE: INTRODUCTION TO JAVA FUNDAMENTALS
4+
* LESSON: 1.2 - COMMENTS AND STRING FORMATTING
5+
*
6+
* AUTHOR: DR. SAAD LAOUADI, PH.D.
7+
* CREATED: MARCH 2025
8+
*
9+
* COPYRIGHT © 2025 DR. SAAD LAOUADI. ALL RIGHTS RESERVED.
10+
* SEE LICENSE FILE FOR COMPLETE TERMS.
11+
*****************************************************************************/
12+
13+
/**
14+
* This class demonstrates the different types of comments in Java
15+
* and how to create simple text banners.
16+
*/
17+
public class CommentDemoWithDocstring {
18+
/**
19+
* Creates a string by repeating a character a specified number of times.
20+
*
21+
* @param ch The character to repeat
22+
* @param n The number of times to repeat the character
23+
* @return A string containing the character repeated n times
24+
*/
25+
public static String repeatChar(char ch, int n) {
26+
StringBuilder sb = new StringBuilder(); // Create a string builder
27+
for (int i = 0; i < n; i++) {
28+
sb.append(ch);
29+
}
30+
return sb.toString(); // return a string object from this method
31+
}
32+
33+
/**
34+
* The main method demonstrates comment usage and displays a formatted banner.
35+
*
36+
* @param args Command line arguments (not used in this example)
37+
*/
38+
public static void main(String[] args) {
39+
// Create a banner with repeated characters
40+
String banner = repeatChar('=', 52);
41+
42+
// Display the banner and title
43+
System.out.println(banner);
44+
System.out.println(" Comments in Java ");
45+
System.out.println(banner);
46+
47+
// This shows how to use single-line comments for brief explanations
48+
System.out.println("program run successfully and ended here!");
49+
50+
/*
51+
* This is an example of a multi-line comment block.
52+
* Notice how each line starts with an asterisk for readability.
53+
* This style is commonly used in professional Java code.
54+
*/
55+
56+
// Using the method to add empty lines at the end
57+
addEmptyLineAtEnd(3);
58+
}
59+
60+
/**
61+
* Adds empty lines to the console output.
62+
* Note: This method is not used in the current implementation.
63+
*
64+
* @param nlines The number of empty lines to add
65+
*/
66+
public static void passAfterEnd(int nlines) {
67+
for (int i = 0; i < nlines; i++) {
68+
System.out.println("\n");
69+
}
70+
}
71+
72+
/**
73+
* Adds a specified number of empty lines to the console output.
74+
* This method uses a more efficient approach than passAfterEnd.
75+
*
76+
* @param nlines The number of empty lines to add
77+
* @throws IllegalArgumentException if nlines is negative
78+
*/
79+
public static void addEmptyLineAtEnd(int nlines) {
80+
if (nlines < 0) {
81+
throw new IllegalArgumentException("Number of lines must be non-negative.");
82+
}
83+
StringBuilder sb = new StringBuilder();
84+
for (int i = 0; i < nlines; i++) {
85+
sb.append(System.lineSeparator());
86+
}
87+
System.out.print(sb.toString());
88+
}
89+
}

CodeStart/chap_01/Comments/CommentJavaCode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
12
/**
23
* This is a Java program to demonstrate different types of comments.
34
* Comments are important for explaining code, making it easier to read
45
* and maintain.
56
*/
7+
68
public class CommentJavaCode {
79

810
public static void main(String[] args) {

0 commit comments

Comments
 (0)