|
| 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 | +} |
0 commit comments