From 932ded0c6e3e830a6083c17ecd073615e94ff59b Mon Sep 17 00:00:00 2001 From: Percival Tapera Date: Fri, 9 Oct 2020 15:33:47 +0200 Subject: [PATCH 1/4] aaded an example to do with recursion added an example to do with recursion --- Examples/Recursion.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Examples/Recursion.java diff --git a/Examples/Recursion.java b/Examples/Recursion.java new file mode 100644 index 00000000..fd3b8415 --- /dev/null +++ b/Examples/Recursion.java @@ -0,0 +1,20 @@ +class HelloWorld{ + + public static void main(String[] args){ + + static int fact(int n) + { + if (n == 1) + + // base condition + return 1; + else + return n*fact(n-1); + } + public static void main(String[] args) { + int result = fact(10); + + System.out.println("10! = " + result); + } + } +} From 605a6e736a628c42bbdabcd4fbb26eac98d0b59c Mon Sep 17 00:00:00 2001 From: Percival Tapera Date: Fri, 9 Oct 2020 15:43:42 +0200 Subject: [PATCH 2/4] contributed to the documentation and added issue to deal with exceptions --- Documentation/OOPS.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Documentation/OOPS.md b/Documentation/OOPS.md index 9ca20d23..5869c904 100644 --- a/Documentation/OOPS.md +++ b/Documentation/OOPS.md @@ -152,6 +152,25 @@ accessible within same class and package within which its class is defined. * `Method body:` it is enclosed between braces. The code you need to be executed to perform your intended operations. +## An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. + +** An exception can occur for many different reasons. Following are some scenarios where an exception occurs.** + +* A user has entered an invalid data. + +* A file that needs to be opened cannot be found. + +* A network connection has been lost in the middle of communications or the JVM has run out of memory. + +* Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner. + +* Based on these, we have three categories of Exceptions. You need to understand them to know how exception handling works in Java. + +* Checked exceptions − A checked exception is an exception that is checked (notified) by the compiler at compilation-time, these are also called as compile time exceptions. These exceptions cannot simply be ignored, the programmer should take care of (handle) these exceptions. + +* Unchecked exceptions − An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation. + +* Errors − These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation. From a84dfbac0fa70206bd4496088f5f2b6fab0a5e3f Mon Sep 17 00:00:00 2001 From: Percival Tapera Date: Fri, 9 Oct 2020 15:57:52 +0200 Subject: [PATCH 3/4] added a singleton design pattern example --- Design Patterns/SingleObject.java | 18 ++++++++++++++++++ Design Patterns/SingletonPatternDemo.java | 14 ++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 Design Patterns/SingleObject.java create mode 100644 Design Patterns/SingletonPatternDemo.java diff --git a/Design Patterns/SingleObject.java b/Design Patterns/SingleObject.java new file mode 100644 index 00000000..320d2360 --- /dev/null +++ b/Design Patterns/SingleObject.java @@ -0,0 +1,18 @@ +public class SingleObject { + + //create an object of SingleObject + private static SingleObject instance = new SingleObject(); + + //make the constructor private so that this class cannot be + //instantiated + private SingleObject(){} + + //Get the only object available + public static SingleObject getInstance(){ + return instance; + } + + public void showMessage(){ + System.out.println("Hello World!"); + } +} \ No newline at end of file diff --git a/Design Patterns/SingletonPatternDemo.java b/Design Patterns/SingletonPatternDemo.java new file mode 100644 index 00000000..4dfa92fc --- /dev/null +++ b/Design Patterns/SingletonPatternDemo.java @@ -0,0 +1,14 @@ +public class SingletonPatternDemo { + public static void main(String[] args) { + + //illegal construct + //Compile Time Error: The constructor SingleObject() is not visible + //SingleObject object = new SingleObject(); + + //Get the only object available + SingleObject object = SingleObject.getInstance(); + + //show the message + object.showMessage(); + } +} \ No newline at end of file From bd11b04c16fbff3869fc3cd234a925c538fcebd3 Mon Sep 17 00:00:00 2001 From: Percival Tapera Date: Tue, 20 Oct 2020 17:37:53 +0200 Subject: [PATCH 4/4] Quick Sort algorithm in Java --- Algorithms/QuickSort.java | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Algorithms/QuickSort.java diff --git a/Algorithms/QuickSort.java b/Algorithms/QuickSort.java new file mode 100644 index 00000000..2a873e53 --- /dev/null +++ b/Algorithms/QuickSort.java @@ -0,0 +1,54 @@ +import java.util.*; + +class QuickSort { + //selects last element as pivot, pi using which array is partitioned. + int partition(int intArray[], int low, int high) { + int pi = intArray[high]; + int i = (low-1); // smaller element index + for (int j=low; jpartitioning index and return pi + int pi = partition(intArray, low, high); + + // sort each partition recursively + quick_sort(intArray, low, pi-1); + quick_sort(intArray, pi+1, high); + } + } +} + +class Main{ + public static void main(String args[]) { + //initialize a numeric array, intArray + int intArray[] = {4,-1,6,8,0,5,-3}; + int n = intArray.length; + //print the original array + System.out.println("Original Array: " + Arrays.toString(intArray)); + //call quick_sort routine using QuickSort object + QuickSort obj = new QuickSort(); + obj.quick_sort(intArray, 0, n-1); + //print the sorted array + System.out.println("\nSorted Array: " + Arrays.toString(intArray)); + } +}