summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamal Wickramanayake <kamal@inbox.lk>2026-02-09 20:02:34 +0530
committerKamal Wickramanayake <kamal@inbox.lk>2026-02-09 20:02:34 +0530
commit933cdd1780fef88ae4b12d313a462ab48af68d7c (patch)
tree7c080b08d093d77bdb38035f92d7f8ec7eb91aa6
parent7790862c32df0a61b6ee55a42d85e1e03c6bf44e (diff)
Added comments to Java classes showing how to run the programs.
-rw-r--r--java/02-hello/Hello.java4
-rw-r--r--java/03-count/Count.java56
-rw-r--r--java/04-factorial/Factorial.java7
3 files changed, 44 insertions, 23 deletions
diff --git a/java/02-hello/Hello.java b/java/02-hello/Hello.java
index bacee99..764f59f 100644
--- a/java/02-hello/Hello.java
+++ b/java/02-hello/Hello.java
@@ -1,3 +1,7 @@
+/*
+* Run this as below:
+* java Hello Kamal
+*/
public class Hello {
public static void main(String[] args) {
System.out.println("Hello " + args[0]);
diff --git a/java/03-count/Count.java b/java/03-count/Count.java
index cbc651e..4d1abd5 100644
--- a/java/03-count/Count.java
+++ b/java/03-count/Count.java
@@ -1,29 +1,39 @@
+/*
+ * Run this as below:
+ * java Count
+ * java Count 1
+ * java Count 3 8
+ * java Count 5 1
+ * java Count 2 Kamal
+ * java Count 5 12.3
+ * java Count 3 10000000000000000000000000000000000000000000000000
+ */
public class Count {
- public static void main(String[] args) {
- if (args.length < 2) {
- System.err.println("[ERROR] Type two integers as arguments.");
- System.exit(1);
- }
+ public static void main(String[] args) {
+ if (args.length < 2) {
+ System.err.println("[ERROR] Type two integers as arguments.");
+ System.exit(1);
+ }
+
+ int start = 0;
+ int end = 0;
- int start = 0;
- int end = 0;
-
- try {
- start = Integer.parseInt(args[0]);
- end = Integer.parseInt(args[1]);
- } catch(NumberFormatException nfe) {
- System.err.println("[ERROR] You didn't provide integer arguments.");
- //System.err.println("Details: " + nfe.getMessage());
- System.exit(2);
- }
+ try {
+ start = Integer.parseInt(args[0]);
+ end = Integer.parseInt(args[1]);
+ } catch (NumberFormatException nfe) {
+ System.err.println("[ERROR] You didn't provide integer arguments.");
+ // System.err.println("Details: " + nfe.getMessage());
+ System.exit(2);
+ }
- if(start > end) {
- System.err.println("[ERROR] Start is greater than end.");
- System.exit(3);
- }
+ if (start > end) {
+ System.err.println("[ERROR] Start is greater than end.");
+ System.exit(3);
+ }
- for(int i = start; i <= end; i++) {
- System.out.println(i);
- }
+ for (int i = start; i <= end; i++) {
+ System.out.println(i);
}
+ }
}
diff --git a/java/04-factorial/Factorial.java b/java/04-factorial/Factorial.java
index 0b2e650..f81c54a 100644
--- a/java/04-factorial/Factorial.java
+++ b/java/04-factorial/Factorial.java
@@ -1,3 +1,7 @@
+/*
+* Run this as:
+* java Factorial
+*/
public class Factorial {
public static void main(String[] args) {
int x = factorial(5);
@@ -11,6 +15,9 @@ public class Factorial {
System.out.println(factorial(3));
}
+ /*
+ * Here is a subroutine defined. In Java, we call it a method.
+ */
public static int factorial(int x) {
if (x == 0) {
return 1;