From 7790862c32df0a61b6ee55a42d85e1e03c6bf44e Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Mon, 9 Feb 2026 19:11:09 +0530 Subject: Added initial java samples. --- java/01-helloworld/.gitignore | 1 + java/01-helloworld/HelloWorld.java | 5 +++++ java/02-hello/.gitignore | 1 + java/02-hello/Hello.java | 5 +++++ java/03-count/.gitignore | 1 + java/03-count/Count.java | 29 +++++++++++++++++++++++++++++ java/04-factorial/.gitignore | 1 + java/04-factorial/Factorial.java | 21 +++++++++++++++++++++ 8 files changed, 64 insertions(+) create mode 100644 java/01-helloworld/.gitignore create mode 100644 java/01-helloworld/HelloWorld.java create mode 100644 java/02-hello/.gitignore create mode 100644 java/02-hello/Hello.java create mode 100644 java/03-count/.gitignore create mode 100644 java/03-count/Count.java create mode 100644 java/04-factorial/.gitignore create mode 100644 java/04-factorial/Factorial.java (limited to 'java') diff --git a/java/01-helloworld/.gitignore b/java/01-helloworld/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/java/01-helloworld/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/java/01-helloworld/HelloWorld.java b/java/01-helloworld/HelloWorld.java new file mode 100644 index 0000000..299d161 --- /dev/null +++ b/java/01-helloworld/HelloWorld.java @@ -0,0 +1,5 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/java/02-hello/.gitignore b/java/02-hello/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/java/02-hello/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/java/02-hello/Hello.java b/java/02-hello/Hello.java new file mode 100644 index 0000000..bacee99 --- /dev/null +++ b/java/02-hello/Hello.java @@ -0,0 +1,5 @@ +public class Hello { + public static void main(String[] args) { + System.out.println("Hello " + args[0]); + } +} diff --git a/java/03-count/.gitignore b/java/03-count/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/java/03-count/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/java/03-count/Count.java b/java/03-count/Count.java new file mode 100644 index 0000000..cbc651e --- /dev/null +++ b/java/03-count/Count.java @@ -0,0 +1,29 @@ +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); + } + + 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); + } + + 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); + } + } +} diff --git a/java/04-factorial/.gitignore b/java/04-factorial/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/java/04-factorial/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/java/04-factorial/Factorial.java b/java/04-factorial/Factorial.java new file mode 100644 index 0000000..0b2e650 --- /dev/null +++ b/java/04-factorial/Factorial.java @@ -0,0 +1,21 @@ +public class Factorial { + public static void main(String[] args) { + int x = factorial(5); + + System.out.println(x); + + int y = factorial(10); + + System.out.println(y); + + System.out.println(factorial(3)); + } + + public static int factorial(int x) { + if (x == 0) { + return 1; + } + + return x * factorial(x - 1); + } +} -- cgit v1.2.3