diff options
| -rw-r--r-- | java/01-helloworld/.gitignore | 1 | ||||
| -rw-r--r-- | java/01-helloworld/HelloWorld.java | 5 | ||||
| -rw-r--r-- | java/02-hello/.gitignore | 1 | ||||
| -rw-r--r-- | java/02-hello/Hello.java | 5 | ||||
| -rw-r--r-- | java/03-count/.gitignore | 1 | ||||
| -rw-r--r-- | java/03-count/Count.java | 29 | ||||
| -rw-r--r-- | java/04-factorial/.gitignore | 1 | ||||
| -rw-r--r-- | java/04-factorial/Factorial.java | 21 |
8 files changed, 64 insertions, 0 deletions
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); + } +} |
