From 2f5ed4aec41eafc68ec1b2fd7806f383f7a2fe6d Mon Sep 17 00:00:00 2001 From: Om Paliwal Date: Sun, 8 Dec 2019 10:02:50 +0530 Subject: [PATCH 1/8] Section 11: package and scope exercise/challenge --- README.md | 3 +- Section11/pom.xml | 13 +++++ .../main/java/challenge/packages/Main.java | 29 ++++++++++ .../main/java/challenge/packages/Series.java | 53 +++++++++++++++++ .../src/main/java/challenge/scope/README.md | 17 ++++++ .../src/main/java/challenge/scope/X.java | 17 ++++++ .../src/main/java/challenge/scope/main/X.java | 13 +++++ .../java/challenge/packages/SeriesTest.java | 57 +++++++++++++++++++ pom.xml | 3 + 9 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 Section11/pom.xml create mode 100644 Section11/src/main/java/challenge/packages/Main.java create mode 100644 Section11/src/main/java/challenge/packages/Series.java create mode 100644 Section11/src/main/java/challenge/scope/README.md create mode 100644 Section11/src/main/java/challenge/scope/X.java create mode 100644 Section11/src/main/java/challenge/scope/main/X.java create mode 100644 Section11/src/test/java/challenge/packages/SeriesTest.java diff --git a/README.md b/README.md index 7e4c078..e9b0a9e 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,5 @@ Course related code - Section 7: OOPS Part2: Composition, Encapsulation and Polymorphism - Section 8: Arrays, ArrayList, AutoBoxing and LinkedList - Section 9: Abstract Classes, Inner Classes and Interfaces -- Section 10: Java Generics \ No newline at end of file +- Section 10: Java Generics +- Section 11: Naming Conventions, Packages, Scope, static keyword and final keyword \ No newline at end of file diff --git a/Section11/pom.xml b/Section11/pom.xml new file mode 100644 index 0000000..066e45a --- /dev/null +++ b/Section11/pom.xml @@ -0,0 +1,13 @@ + + + 4.0.0 + + + udemyjavacourse + JavaUdemyCourse + 0.0.1 + + Section11 + \ No newline at end of file diff --git a/Section11/src/main/java/challenge/packages/Main.java b/Section11/src/main/java/challenge/packages/Main.java new file mode 100644 index 0000000..0b3e8c0 --- /dev/null +++ b/Section11/src/main/java/challenge/packages/Main.java @@ -0,0 +1,29 @@ +package challenge.packages; + +public class Main { + public static void main(String[] args) { + int[] series = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + + //Print nth sum + System.out.println("----------------Sum Series-------------------------"); + for (int i : series) { + System.out.println("Sum of first " + i + " numbers is " + Series.nSum(i)); + } + System.out.println("------------------------------------------------"); + + + //Print nth sum + System.out.println("----------------Factorial series-------------------------"); + for (int i : series) { + System.out.println("Factorial(" + i + ") : " + Series.factorial(i)); + } + System.out.println("------------------------------------------------"); + + //Print fibonacci series + System.out.println("----------------Fibonacci Series-------------------------"); + for (int i : series) { + System.out.println("Fibonacci(" + i + ") : " + Series.fibonacci(i)); + } + System.out.println("------------------------------------------------"); + } +} diff --git a/Section11/src/main/java/challenge/packages/Series.java b/Section11/src/main/java/challenge/packages/Series.java new file mode 100644 index 0000000..ba48e35 --- /dev/null +++ b/Section11/src/main/java/challenge/packages/Series.java @@ -0,0 +1,53 @@ +package challenge.packages; + +public class Series { + + /** + * Sum of first n natural numbers + * + * @param n + * @return + */ + public static long nSum(int n) { + if (n <= 0) { + return 0; + } else { + //thanks to https://www.math-only-math.com/sum-of-first-n-natural-numbers.html + return ((n + 1) * n) / 2; + } + } + + /** + * get factorial of n numbers + * + * @param n + * @return + */ + public static long factorial(int n) { + + if (n <= 0) { + return -1; + } else if (n == 1) { + return 1; + } else { + //thanks to https://probabilityformula.org/factorial.html + return n * factorial(n - 1); + } + } + + /** + * Returns nth fibonacci number (starting from 0) + * + * @param n any non negative integer + * @return + */ + public static long fibonacci(int n) { + if (n <= 0) { + return 0; + } else if (n == 1) { + return 1; + } else { + return fibonacci(n - 1) + fibonacci(n - 2); + } + } +} diff --git a/Section11/src/main/java/challenge/scope/README.md b/Section11/src/main/java/challenge/scope/README.md new file mode 100644 index 0000000..f248c23 --- /dev/null +++ b/Section11/src/main/java/challenge/scope/README.md @@ -0,0 +1,17 @@ +## Scope +### Problem statement +- [X] Write a small program to read an integer from the keyboard +(using Scanner) and print out the times table for that number. +The table should run from 1 to 12. + +- [X] You are allowed to use one variable called scanner for your +Scanner instance. +- [X] You can use as many other variables as you need, but they must must all be called x. That includes any class instances and loop control variables that you may decide +to use. + +- [X] If you use a class, the class can be called X (capital), but any instances of it must be called x (lower case). +Any methods you create must also be called x. + +### Optional Challenge: +- [X] Change your program so that ALL variables (including the scanner +instance) are called x. \ No newline at end of file diff --git a/Section11/src/main/java/challenge/scope/X.java b/Section11/src/main/java/challenge/scope/X.java new file mode 100644 index 0000000..0c2696d --- /dev/null +++ b/Section11/src/main/java/challenge/scope/X.java @@ -0,0 +1,17 @@ +package challenge.scope; + +//Multiplier Table class +public class X { + private int x; + + public X(int x) { + this.x = x; + } + + public void printTable() { + System.out.println("Table of " + x); + for (int x = 1; x <= 12; x++) { + System.out.println(this.x + "\tX\t" + x + "\t=\t" + (x * this.x)); + } + } +} diff --git a/Section11/src/main/java/challenge/scope/main/X.java b/Section11/src/main/java/challenge/scope/main/X.java new file mode 100644 index 0000000..e94874b --- /dev/null +++ b/Section11/src/main/java/challenge/scope/main/X.java @@ -0,0 +1,13 @@ +package challenge.scope.main; + +import java.util.Scanner; + +public class X { + static Scanner x = new Scanner(System.in); + + public static void main(String[] args) { + System.out.print("Enter number : "); + challenge.scope.X x = new challenge.scope.X(X.x.nextInt()); + x.printTable(); + } +} diff --git a/Section11/src/test/java/challenge/packages/SeriesTest.java b/Section11/src/test/java/challenge/packages/SeriesTest.java new file mode 100644 index 0000000..24a229b --- /dev/null +++ b/Section11/src/test/java/challenge/packages/SeriesTest.java @@ -0,0 +1,57 @@ +package challenge.packages; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SeriesTest { + + public static Stream sumOfNNumberData() { + return Stream.of( + Arguments.of(0, 0), + Arguments.of(10, 55), + Arguments.of(100000, 705082704), + Arguments.of(-10, 0) + ); + } + + public static Stream factorialData() { + return Stream.of( + Arguments.of(0, -1), + Arguments.of(1, 1), + Arguments.of(5, 120), + Arguments.of(-1, -1) + ); + } + + public static Stream fibonacciData() { + return Stream.of( + Arguments.of(0, 0), + Arguments.of(1, 1), + Arguments.of(9, 34), + Arguments.of(-1, 0) + ); + } + + @ParameterizedTest(name = "Sum of first {0} numbers is {1}") + @MethodSource("sumOfNNumberData") + void nSum(int num, long sumOfN) { + assertEquals(sumOfN, Series.nSum(num)); + } + + @ParameterizedTest(name = "Factorial of first {0} numbers is {1}") + @MethodSource("factorialData") + void factorial(int n, long factorialOfN) { + assertEquals(factorialOfN, Series.factorial(n)); + } + + @ParameterizedTest(name = "{0}th fibonacci number is {1}") + @MethodSource("fibonacciData") + void fibonacci(int n, long nThFibonacci) { + assertEquals(nThFibonacci, Series.fibonacci(n)); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index f01abdf..2c391a2 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ Section8 Section9 Section10 + Section11 @@ -42,5 +43,7 @@ 3.0.0-M3 + src/main/java + src/test/java \ No newline at end of file From 5bde5597af01c4d73b115d6dafec4587dff333af Mon Sep 17 00:00:00 2001 From: Om Paliwal Date: Sun, 8 Dec 2019 11:28:52 +0530 Subject: [PATCH 2/8] Section 11: Examples: final keyword, static keywords and static initializer block --- .../exercise/finalkeyword/ExampleClass.java | 16 +++++++ .../main/java/exercise/finalkeyword/Main.java | 17 +++++++ .../java/exercise/finalkeyword/Password.java | 45 +++++++++++++++++++ .../exercise/finalkeyword/PasswordMain.java | 13 ++++++ .../static_initializer/SIBExample.java | 29 ++++++++++++ .../static_initializer/SIBExampleMain.java | 9 ++++ .../main/java/example/interfaces/Test.java | 6 +++ pom.xml | 1 - 8 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 Section11/src/main/java/exercise/finalkeyword/ExampleClass.java create mode 100644 Section11/src/main/java/exercise/finalkeyword/Main.java create mode 100644 Section11/src/main/java/exercise/finalkeyword/Password.java create mode 100644 Section11/src/main/java/exercise/finalkeyword/PasswordMain.java create mode 100644 Section11/src/main/java/exercise/static_initializer/SIBExample.java create mode 100644 Section11/src/main/java/exercise/static_initializer/SIBExampleMain.java create mode 100644 Section9/src/main/java/example/interfaces/Test.java diff --git a/Section11/src/main/java/exercise/finalkeyword/ExampleClass.java b/Section11/src/main/java/exercise/finalkeyword/ExampleClass.java new file mode 100644 index 0000000..28f30f1 --- /dev/null +++ b/Section11/src/main/java/exercise/finalkeyword/ExampleClass.java @@ -0,0 +1,16 @@ +package exercise.finalkeyword; + +public class ExampleClass { + + public static int instanceCount = 0; + private final int instanceNumber; + + public ExampleClass() { + instanceCount++; + this.instanceNumber = instanceCount; + } + + public int getInstanceNumber() { + return instanceNumber; + } +} diff --git a/Section11/src/main/java/exercise/finalkeyword/Main.java b/Section11/src/main/java/exercise/finalkeyword/Main.java new file mode 100644 index 0000000..2c3ec25 --- /dev/null +++ b/Section11/src/main/java/exercise/finalkeyword/Main.java @@ -0,0 +1,17 @@ +package exercise.finalkeyword; + +public class Main extends ExampleClass { + public static void main(String[] args) { + ExampleClass class1 = new ExampleClass(); + ExampleClass class2 = new ExampleClass(); + ExampleClass class3 = new ExampleClass(); + + System.out.println("Total instances of " + + ExampleClass.class.getSimpleName() + + " : " + ExampleClass.instanceCount); + + System.out.println(class1.getInstanceNumber()); + System.out.println(class2.getInstanceNumber()); + System.out.println(class3.getInstanceNumber()); + } +} diff --git a/Section11/src/main/java/exercise/finalkeyword/Password.java b/Section11/src/main/java/exercise/finalkeyword/Password.java new file mode 100644 index 0000000..13bc71e --- /dev/null +++ b/Section11/src/main/java/exercise/finalkeyword/Password.java @@ -0,0 +1,45 @@ +package exercise.finalkeyword; + +public class Password { + private int key = 9876543; + private int encryptedPassword; + + public Password(int password) { + this.encryptedPassword = encryptDecrypt(password); + } + + private int encryptDecrypt(int password) { + return password ^ key; + } + + /** + * This is made final to prevent overriding by subclasses. + * Reason being that someone can do like below code + * + * public class ExtendedPassword extends Password { + * private int decryptedPassword; + * public ExtendedPassword(int password) { + * super(password); + * this.decryptedPassword = password; + * } + *

+ * \@Override public void storePassword() { + * System.out.println("Saving password as " + this.decryptedPassword); + * } + * } + * + */ + final void storePassword() { + System.out.println("Saving password as " + encryptedPassword); + } + + boolean logMeIn(int password) { + if (encryptDecrypt(password) == encryptedPassword) { + System.out.println("Welcome"); + return true; + } else { + System.out.println("Can't let you in buddy."); + return false; + } + } +} diff --git a/Section11/src/main/java/exercise/finalkeyword/PasswordMain.java b/Section11/src/main/java/exercise/finalkeyword/PasswordMain.java new file mode 100644 index 0000000..2fa2e4f --- /dev/null +++ b/Section11/src/main/java/exercise/finalkeyword/PasswordMain.java @@ -0,0 +1,13 @@ +package exercise.finalkeyword; + +public class PasswordMain { + public static void main(String[] args) { + Password password = new Password(123456); + password.storePassword(); + password.logMeIn(1); + password.logMeIn(-1); + password.logMeIn(0); + password.logMeIn(123465); + password.logMeIn(123456); + } +} diff --git a/Section11/src/main/java/exercise/static_initializer/SIBExample.java b/Section11/src/main/java/exercise/static_initializer/SIBExample.java new file mode 100644 index 0000000..62607de --- /dev/null +++ b/Section11/src/main/java/exercise/static_initializer/SIBExample.java @@ -0,0 +1,29 @@ +package exercise.static_initializer; + +public class SIBExample { + //This variable will be like constant + public static final String howYouDoin; + + static { + howYouDoin = "How you doing dear?"; + System.out.println("First static block called"); + } + + static { + System.out.println("Second static init block called"); + } + + public SIBExample() { + //Below will be compile error +// howYouDoin = "dfdfd"; + System.out.println("Constructor called"); + } + + public static String getString() { + return "SIBExample{" + + howYouDoin + + "}"; + } + + +} diff --git a/Section11/src/main/java/exercise/static_initializer/SIBExampleMain.java b/Section11/src/main/java/exercise/static_initializer/SIBExampleMain.java new file mode 100644 index 0000000..82e5d33 --- /dev/null +++ b/Section11/src/main/java/exercise/static_initializer/SIBExampleMain.java @@ -0,0 +1,9 @@ +package exercise.static_initializer; + +public class SIBExampleMain { + + public static void main(String[] args) { + SIBExample sibExample = new SIBExample(); + System.out.println(SIBExample.getString()); + } +} diff --git a/Section9/src/main/java/example/interfaces/Test.java b/Section9/src/main/java/example/interfaces/Test.java new file mode 100644 index 0000000..d0c0be1 --- /dev/null +++ b/Section9/src/main/java/example/interfaces/Test.java @@ -0,0 +1,6 @@ +package example.interfaces; + +interface Test { + int CONSTANT_VAR = 1000; + +} diff --git a/pom.xml b/pom.xml index 2c391a2..1078034 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,6 @@ Section11 - org.junit.jupiter From 3315895b6f74f9c61c0b241aaf948f32aa2c129f Mon Sep 17 00:00:00 2001 From: krishnom Date: Tue, 10 Dec 2019 23:12:53 +0530 Subject: [PATCH 3/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e9b0a9e..54d14af 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![Build Status](https://travis-ci.org/Krishnom/UdemyJavaCourse.svg?branch=master)](https://travis-ci.org/Krishnom/UdemyJavaCourse) - +[Code Quality](https://www.codacy.com/gh/Krishnom/UdemyJavaCourse?utm_source=github.com&utm_medium=referral&utm_content=Krishnom/UdemyJavaCourse&utm_campaign=Badge_Grade_Dashboard) # UdemyJavaCourse Course related code @@ -15,4 +15,4 @@ Course related code - Section 8: Arrays, ArrayList, AutoBoxing and LinkedList - Section 9: Abstract Classes, Inner Classes and Interfaces - Section 10: Java Generics -- Section 11: Naming Conventions, Packages, Scope, static keyword and final keyword \ No newline at end of file +- Section 11: Naming Conventions, Packages, Scope, static keyword and final keyword From 13d00c9fd522dafbd3a5cceec4e00d0f88d2b5bc Mon Sep 17 00:00:00 2001 From: krishnom Date: Tue, 10 Dec 2019 23:13:38 +0530 Subject: [PATCH 4/8] Code Quality Badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 54d14af..9ae98a7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![Build Status](https://travis-ci.org/Krishnom/UdemyJavaCourse.svg?branch=master)](https://travis-ci.org/Krishnom/UdemyJavaCourse) -[Code Quality](https://www.codacy.com/gh/Krishnom/UdemyJavaCourse?utm_source=github.com&utm_medium=referral&utm_content=Krishnom/UdemyJavaCourse&utm_campaign=Badge_Grade_Dashboard) +[![Codacy Badge](https://api.codacy.com/project/badge/Grade/4cbe076de2f343af805b39540cebebdf)](https://www.codacy.com/gh/Krishnom/UdemyJavaCourse?utm_source=github.com&utm_medium=referral&utm_content=Krishnom/UdemyJavaCourse&utm_campaign=Badge_Grade) # UdemyJavaCourse Course related code From c9bbd2e46f7668a89957b9e2d9a812f509e685a6 Mon Sep 17 00:00:00 2001 From: krishnom Date: Sat, 4 Jan 2020 14:23:38 +0530 Subject: [PATCH 5/8] Integrate Github Action workflow for maven --- .github/workflows/maven.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/workflows/maven.yml diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..89cbf21 --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,17 @@ +name: Java CI + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - name: Set up JDK 1.11 + uses: actions/setup-java@v1 + with: + java-version: 1.11 + - name: Build with Maven + run: mvn -B package --file pom.xml From a0c59a0a7903dcd58ac47775b00c984d8a1ec923 Mon Sep 17 00:00:00 2001 From: krishnom Date: Sat, 4 Jan 2020 14:30:12 +0530 Subject: [PATCH 6/8] Enable code coverage (#11) * Initial Commit: Enabling code coverage * Initial Commit: Code coverage status * enabled code coverage using coveralls. https://github.com/trautonen/coveralls-maven-plugin * enabled code coverage using coveralls. https://github.com/trautonen/coveralls-maven-plugin * enabled code coverage using coveralls. https://github.com/trautonen/coveralls-maven-plugin --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ae98a7..49ef803 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ [![Build Status](https://travis-ci.org/Krishnom/UdemyJavaCourse.svg?branch=master)](https://travis-ci.org/Krishnom/UdemyJavaCourse) -[![Codacy Badge](https://api.codacy.com/project/badge/Grade/4cbe076de2f343af805b39540cebebdf)](https://www.codacy.com/gh/Krishnom/UdemyJavaCourse?utm_source=github.com&utm_medium=referral&utm_content=Krishnom/UdemyJavaCourse&utm_campaign=Badge_Grade) + +[![Coverage Status](https://coveralls.io/repos/github/Krishnom/UdemyJavaCourse/badge.svg?branch=master)](https://coveralls.io/github/Krishnom/UdemyJavaCourse?branch=master) # UdemyJavaCourse Course related code From f32c35e356469161328f4abb71c2208389ebcfcf Mon Sep 17 00:00:00 2001 From: krishnom Date: Sat, 4 Jan 2020 14:31:18 +0530 Subject: [PATCH 7/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 49ef803..994980d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![Build Status](https://travis-ci.org/Krishnom/UdemyJavaCourse.svg?branch=master)](https://travis-ci.org/Krishnom/UdemyJavaCourse) -[![Coverage Status](https://coveralls.io/repos/github/Krishnom/UdemyJavaCourse/badge.svg?branch=master)](https://coveralls.io/github/Krishnom/UdemyJavaCourse?branch=master) +[![Codacy Badge](https://api.codacy.com/project/badge/Grade/658b238e0b8e4ef0905d047ccd5eda7a)](https://www.codacy.com/manual/BalajiTechs/UdemyJavaCourse?utm_source=github.com&utm_medium=referral&utm_content=Krishnom/UdemyJavaCourse&utm_campaign=Badge_Grade) # UdemyJavaCourse Course related code From 1c29040b65e3d106c67834e4065d2953b36c36d8 Mon Sep 17 00:00:00 2001 From: krishnom Date: Sat, 4 Jan 2020 14:36:56 +0530 Subject: [PATCH 8/8] Update README.md --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 994980d..53a3dda 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ -[![Build Status](https://travis-ci.org/Krishnom/UdemyJavaCourse.svg?branch=master)](https://travis-ci.org/Krishnom/UdemyJavaCourse) - -[![Codacy Badge](https://api.codacy.com/project/badge/Grade/658b238e0b8e4ef0905d047ccd5eda7a)](https://www.codacy.com/manual/BalajiTechs/UdemyJavaCourse?utm_source=github.com&utm_medium=referral&utm_content=Krishnom/UdemyJavaCourse&utm_campaign=Badge_Grade) +[![Build Status](https://travis-ci.org/Krishnom/UdemyJavaCourse.svg?branch=master)](https://travis-ci.org/Krishnom/UdemyJavaCourse) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/658b238e0b8e4ef0905d047ccd5eda7a)](https://www.codacy.com/manual/BalajiTechs/UdemyJavaCourse?utm_source=github.com&utm_medium=referral&utm_content=Krishnom/UdemyJavaCourse&utm_campaign=Badge_Grade) # UdemyJavaCourse Course related code