├── Basic-Programs ├── AdamNum.java ├── Armstrong.java ├── BinaryToDec.java ├── BitwiseDecToBin.java ├── DecToBin.java ├── DecToHex.java ├── DecToHexm2.java ├── LeapYear.java ├── Perfect.java ├── SwapTrick.java ├── basic.txt ├── checkPrime.java ├── conTrick.java ├── factorial.java ├── fast_prime.java ├── fibonacci.java ├── genRandomNum.java ├── helloWithoutSemi.java ├── nonRecFib.java ├── numConTrick.java ├── palindrome.java ├── pascal.java ├── perfectNum.java ├── primeRange.java ├── segmentSieve.java └── sum_of_digit.java ├── Bit_Manipulation ├── BitOperations.java ├── BitShift.java ├── PowerOfNumber.java ├── checPositiveNum.java ├── checkBitSet.java ├── countNumOfOne.java ├── lowerToUpper.java ├── oddEven.java ├── powerOfTwo.java ├── subsetOfSet.java └── upperToLower.java ├── ClassAndObjects ├── Actor.java ├── Bank.java ├── BicycleDemo.java ├── CricketScorer.java ├── EveryClassExtendsObject.java ├── Monster.java ├── ObjectPass.java ├── Radio.java ├── Solution.java └── TrickyQue.java ├── Constructor ├── Car.java ├── ConstEx1.java └── CopyConst.java ├── Inheritance ├── InheritEx.java ├── NblockInherit.java ├── SblockInherit.java ├── SingleLevel.java └── TrickyEx.java ├── MathPrograms ├── BigFactorial.java ├── Factorial.java ├── GCD.java ├── GCDEuclid.java ├── LCM.java ├── MyBigFactorial.java ├── NegPowerOfN.java ├── PascalTriangle.java ├── PowerOfX.java ├── SeriesSum.java ├── factorOfN.java ├── greatestPrimeCheck.java └── nCr.java ├── MethodOverloading ├── NullTest1.java ├── NullTest2.java ├── TestMethod1.java ├── TestMethod2.java ├── TestMethod3.java ├── TestMethod4.java └── TestMethod5.java ├── ObjectOrdering ├── ComparableMovie.java └── ComparatorMovie.java ├── Patterns ├── Mantry.java ├── Pascal.java ├── Pattern01sc.java ├── Pattern27.java ├── Pattern28.java ├── Pattern29.java ├── flagPattern.java ├── halfDiamond.java ├── hollowDiamond.java ├── pattern1.java ├── pattern10.java ├── pattern11.java ├── pattern12.java ├── pattern13.java ├── pattern14.java ├── pattern15.java ├── pattern16.java ├── pattern17.java ├── pattern18.java ├── pattern2.java ├── pattern20.java ├── pattern21.java ├── pattern22.java ├── pattern23.java ├── pattern24.java ├── pattern25.java ├── pattern3.java ├── pattern4.java ├── pattern5.java ├── pattern6.java ├── pattern7.java ├── pattern8.java ├── pattern9.java └── templePattern.java ├── README.md ├── Reflection └── ReflectionEx.java ├── String ├── ASCIIstr2char.java ├── Anagram.java ├── AnagramSt.java ├── Colindrome.java ├── CountChar.java ├── CountVowel.java ├── CountVowels.java ├── DuplicateCharsInString.java ├── ExchangeAlter.java ├── LargeWordCount.java ├── LowToUpEx1.java ├── OcurrenceOfLetter.java ├── OnlyNumInString.java ├── PasswordValidation.java ├── ReverseSentence.java ├── ReverseStringEx1.java ├── StringBufferBuilderExamples.java ├── StringEx.java ├── StringExamples.java ├── StringR.java ├── StringRecursive.java ├── StringReverse.java ├── StringSort.java ├── StringSub.java ├── String_Tokenizer.java ├── String_Tokenizer_2 ├── SubString.java ├── charCount.java ├── charCountFull.java ├── checkFirstCharOfStr.java ├── checkStrChar.java ├── hollowString.java ├── sentencePalindrome.java ├── string2char.java ├── stringPalindrome.java └── stringPalindrome2.java ├── StringPatterns ├── AlphabetPattern.java └── pattern26.java ├── abstractKey └── Example1.java ├── boxing ├── BoxingUnBoxing.java └── Example1.java ├── collection ├── List │ ├── ArrayAsList.java │ ├── ArrayListExample.java │ ├── LinkedListExample.java │ ├── ListToArray.java │ └── VectorExample.java ├── Map │ ├── HashMapExample.java │ ├── LinekedHAshMapExample.java │ └── TreeMapExample.java ├── Queue │ ├── PriorityQueueExample.java │ └── QueueExample.java └── Set │ ├── Book.java │ ├── BookNameComparator.java │ ├── BookPriceComparator.java │ ├── HashSetExample.java │ ├── LinkedHashSetEx.java │ └── TreeSetExample.java ├── designPattern ├── Decorator │ ├── DecorateIcecream.java │ ├── Icecream.java │ └── TestDecorator.java ├── Factory │ ├── BigUFOEnemyShip.java │ ├── EnemyShip.java │ ├── EnemyShipFactory.java │ ├── EnemyShipTesting.java │ ├── RocketEnemyShip.java │ └── UFOEnemyShip.java ├── Observer │ ├── Observer.java │ ├── Student.java │ ├── StudentData.java │ └── TestObserver.java └── Singleton │ ├── Settings.java │ └── TestSetting.java ├── encapsulation └── EncapsulationExample.java ├── exceptionHandling ├── CheckedUncecked.java ├── CustomerAccount.java ├── DivideByZero.java └── Example1.java ├── generics ├── GenericClass.java └── GenericMethod.java ├── hello.txt ├── interfaceExamples ├── Aeroplane.java ├── Bird.java ├── Flyable.java ├── IntefaceRules.java ├── InterfaceExamples.java ├── InterfaceWithMain.java ├── SameType.java ├── practice1.java ├── practice2.java ├── practice3.java └── practice4.java ├── loopAndFlow ├── BreakExamples.java ├── ContinueExamples.java ├── DoWhileLoopExamples.java ├── ForLoopExample.java ├── IfElseExamples.java ├── SwitchExamples.java └── WhileLoopExamples.java └── polymorphism ├── Animal.java ├── Cat.java ├── Dog.java └── TestPolymorphism.java /Basic-Programs/AdamNum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * lets num=12; 3 | * now, square of 12=144 4 | * reverse of 12 = 21 5 | * square of 21= 441 6 | * 144=rev(441) 7 | * 8 | * So, the number 12 is a adam number 9 | * 10 | * */ 11 | 12 | import java.util.Scanner; 13 | 14 | public class AdamNum { 15 | 16 | static int revDigit(int num) { 17 | int rev = 0; 18 | while (num > 0) { 19 | rev = rev * 10 + num % 10; 20 | num /= 10; 21 | } 22 | return rev; 23 | } 24 | 25 | static int square(int n) { 26 | return n * n; 27 | } 28 | 29 | static boolean checkAdam(int num) { 30 | int a = square(num); 31 | System.out.println("Number square: " + a); 32 | int b = square(revDigit(num)); 33 | System.out.println("Reverse Number Square: " + b); 34 | 35 | return (a == revDigit(b)); 36 | } 37 | 38 | public static void main(String[] args) { 39 | 40 | Scanner sc = new Scanner(System.in); 41 | System.out.println("Enter the Number: "); 42 | int n = sc.nextInt(); 43 | if (checkAdam(n)) { 44 | System.out.println("Adam Number! "); 45 | } else { 46 | System.out.println("Not a Adam Number! "); 47 | } 48 | sc.close(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Basic-Programs/Armstrong.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | /** 4 | * 5 | * @author Vidhikara 6 | */ 7 | public class Armstrong { 8 | 9 | public static void main(String[] args) { 10 | 11 | Scanner in = new Scanner(System.in); 12 | System.out.println("Enter the no. - "); 13 | long n=in.nextInt(); 14 | long sum=0,res,temp=n; 15 | while(n>0){ 16 | res=n%10; 17 | sum=sum+(res*res*res); 18 | n=n/10; 19 | } 20 | if(temp==sum){ 21 | System.out.println("Armstrong"); 22 | }else{ 23 | System.out.println("Not Armstrong"); 24 | } 25 | in.close(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Basic-Programs/BinaryToDec.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | /** 5 | * 6 | * @author Vidhikara 7 | */ 8 | public class BinaryToDec { 9 | 10 | public static void main(String[] args) { 11 | Scanner sc=new Scanner(System.in); 12 | System.out.println("Enter the binary no. - "); 13 | String bin=sc.nextLine(); 14 | int flag=1; 15 | long dec=0,num; 16 | for(int i=0;i> 1); 34 | System.out.print(remainder); 35 | } 36 | } -------------------------------------------------------------------------------- /Basic-Programs/DecToBin.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class DecToBin { 5 | 6 | public static String toBinary(int n) { 7 | if (n == 0) { 8 | return "0"; 9 | } 10 | String binary = ""; 11 | while (n > 0) { 12 | int rem = n % 2; 13 | binary = rem + binary; 14 | n = n / 2; 15 | } 16 | return binary; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Scanner sc = new Scanner(System.in); 21 | System.out.print("Enter a number: "); 22 | int decimal = sc.nextInt(); 23 | String binary = DecToBin.toBinary(decimal); 24 | System.out.println("The binary representation is " + binary); 25 | sc.close(); 26 | } 27 | } -------------------------------------------------------------------------------- /Basic-Programs/DecToHex.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class DecToHex 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner input = new Scanner( System.in ); 7 | System.out.print("Enter a decimal number : "); 8 | int num =input.nextInt(); 9 | int rem; 10 | String str2=""; 11 | char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; 12 | while(num>0){ 13 | rem=num%16; 14 | str2=hex[rem]+str2; 15 | num=num/16; 16 | } 17 | System.out.println("Decimal to hexadecimal: "+str2); 18 | input.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Basic-Programs/DecToHexm2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class DecToHexm2 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner input = new Scanner( System.in ); 7 | System.out.print("Enter a decimal number : "); 8 | int num =input.nextInt(); 9 | int rem,base=16; 10 | String digits="0123456789ABCDEF"; 11 | String hex=""; 12 | 13 | while(num>0){ 14 | rem=num%base; 15 | hex=digits.charAt(rem)+hex; 16 | num=num/base; 17 | } 18 | System.out.println("Decimal to hexadecimal: "+hex); 19 | System.out.println("---------------------------------"); 20 | System.out.print("Enter a HEX number : "); 21 | String hexa=input.next(); 22 | System.out.println("Decimal Number of '"+hexa+"' is : "+hex2Dec(hexa)); 23 | System.out.println("---------------------------------"); 24 | input.close(); 25 | 26 | } 27 | public static int hex2Dec(String s){ 28 | String digits="0123456789ABCDEF"; 29 | int dec=0; 30 | s=s.toUpperCase(); 31 | for(int i=0;i 0) 5 | // x = Integer.parseInt(argv[0]); // number from command line 6 | 7 | System.out.printf("%d dec == %s bin\n", x, dec2m(x, 2)); 8 | System.out.printf("%d dec == %s oct\n", x, dec2m(x, 8)); 9 | System.out.printf("%d dec == %s hex\n", x, dec2m(x, 16)); 10 | } 11 | 12 | static String dec2m(int N, int m) { 13 | String str = ""; 14 | for (int n = N; n > 0; n /= m) { 15 | int rem = n % m; 16 | str = rem < 10 ? rem + str : (char) ('A' - 10 + rem) + str; 17 | } 18 | return str; 19 | } 20 | } -------------------------------------------------------------------------------- /Basic-Programs/palindrome.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | /** 5 | * 6 | * @author Vidhikara 7 | */ 8 | public class palindrome { 9 | 10 | public static void main(String[] args) { 11 | 12 | Scanner input=new Scanner(System.in); 13 | System.out.println("Enter the string: "); 14 | String s = input.nextLine(); 15 | 16 | String rev=""; 17 | for(int i=s.length()-1;i>=0;--i){ 18 | rev=rev+s.charAt(i); 19 | } 20 | System.out.println(rev); 21 | if(rev.equalsIgnoreCase(s)){ 22 | System.out.println("Palindrome"); 23 | }else{ 24 | System.out.println("Not palindrome"); 25 | } 26 | input.close(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Basic-Programs/pascal.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | class pascal{ 5 | public static void main(String[] arg){ 6 | 7 | Scanner in = new Scanner(System.in); 8 | System.out.println("Enter the number: "); 9 | int n = in.nextInt(); 10 | int[][] arr = pascal1(n); 11 | System.out.println(arr); 12 | 13 | in.close(); 14 | } 15 | 16 | private static int[][] pascal1(int n) { 17 | int[][] pt = new int[n][]; 18 | for(int i=0; i0){ 16 | res=n%10; 17 | sum=sum+res; 18 | n=n/10; 19 | } 20 | System.out.println("Sum of digits: "+sum); 21 | in.close(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Bit_Manipulation/BitOperations.java: -------------------------------------------------------------------------------- 1 | package Bit_Manipulation; 2 | class BitOperations{ 3 | 4 | public static void main(String[] args){ 5 | 6 | int a=15; //0000 1111 7 | int b=21; //0001 0101 8 | 9 | //AND Operation 10 | /* 11 | 0000 1111 12 | & 0001 0101 13 | -------------- 14 | 0000 0101 15 | */ 16 | int f=(a&(a-1)); 17 | System.out.println("Power of 2 Operation : "+f+" Binary value: "+Integer.toBinaryString(f)); 18 | 19 | int and=a&b; 20 | System.out.println("AND Operation : "+and+" Binary value: "+Integer.toBinaryString(and)); 21 | //OR Operation 22 | /* 23 | 0000 1111 24 | | 0001 0101 25 | -------------- 26 | 0001 1111 27 | */ 28 | int or=a|b; 29 | System.out.println("OR Operation : "+or+" Binary value: "+Integer.toBinaryString(or)); 30 | //XOR Operation 31 | /* 32 | 0000 1111 33 | & 0001 0101 34 | -------------- 35 | 0001 1010 36 | */ 37 | int xor=a^b; 38 | System.out.println("XOR Operation : "+xor+" Binary value: "+Integer.toBinaryString(xor)); 39 | int x=7; 40 | //NOT Operation 41 | int not=~x; 42 | System.out.println("NOT Operation : "+not+" Binary value: "+Integer.toBinaryString(not)); 43 | 44 | } 45 | } -------------------------------------------------------------------------------- /Bit_Manipulation/BitShift.java: -------------------------------------------------------------------------------- 1 | package Bit_Manipulation; 2 | 3 | import java.util.Scanner; 4 | 5 | class BitShift{ 6 | 7 | public static void main(String[] args){ 8 | 9 | Scanner sc=new Scanner(System.in); 10 | System.out.print("Enter a decimal number: "); 11 | int x=sc.nextInt(); 12 | int a=15; //0000 1111 13 | int b=22; //0001 0101 14 | int c=24; 15 | System.out.println("Your numbers "+x+" binary value: "+Integer.toBinaryString(x)); 16 | System.out.println(a+" Binary value: "+Integer.toBinaryString(a)); 17 | System.out.println(b+" Binary value: "+Integer.toBinaryString(b)); 18 | System.out.println(c+" Binary value: "+Integer.toBinaryString(c)); 19 | System.out.println("------------------------------------------"); 20 | int lshift=a<<1; 21 | System.out.println("left shift Operation : "+lshift+"\t\t Binary value: "+Integer.toBinaryString(lshift)); 22 | 23 | int rshift=b>>1; 24 | System.out.println("Right shift Operation : "+rshift+"\t\t Binary value: "+Integer.toBinaryString(rshift)); 25 | 26 | int zshift=c>>>1; 27 | System.out.println("Right shift zero fill Operation : "+zshift+"\t Binary value: "+Integer.toBinaryString(zshift)); 28 | 29 | System.out.println("------------------------------------------"); 30 | int l=x<<1; 31 | System.out.println("left shift Operation on your number: "+l+"\t\t Binary value: "+Integer.toBinaryString(l)); 32 | int r=x>>1; 33 | System.out.println("Right shift Operation on your number: "+r+"\t\t Binary value: "+Integer.toBinaryString(r)); 34 | int rz=x>>>1; 35 | System.out.println("Right shift zero fill Operation on your number: "+rz+"\t\t Binary value: "+Integer.toBinaryString(rz)); 36 | System.out.println("-------------------------------------------"); 37 | 38 | sc.close(); 39 | } 40 | } -------------------------------------------------------------------------------- /Bit_Manipulation/PowerOfNumber.java: -------------------------------------------------------------------------------- 1 | package Bit_Manipulation; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PowerOfNumber { 6 | 7 | public static void main(String[] args){ 8 | Scanner sc=new Scanner(System.in); 9 | // System.out.print("Enter the Number: "); 10 | // int n=sc.nextInt(); 11 | System.out.print("Upto: "); 12 | int x=sc.nextInt(); 13 | for(int i=0;i<=x;i++){ 14 | int t=2<> 31 == 0) 13 | System.out.println("Positive Number!"); 14 | else 15 | System.out.println("Negative Number!"); 16 | sc.close(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Bit_Manipulation/checkBitSet.java: -------------------------------------------------------------------------------- 1 | package Bit_Manipulation; 2 | 3 | import java.util.Scanner; 4 | 5 | public class checkBitSet { 6 | 7 | public static void main(String... args){ 8 | Scanner sc=new Scanner(System.in); 9 | System.out.print("Enter the Number: "); 10 | int num=sc.nextInt(); 11 | System.out.print("Position to check: "); 12 | int pos=sc.nextInt(); 13 | System.out.println("Your number is: "+Integer.toBinaryString(num)); 14 | if(!checkBit(num,pos)){ 15 | System.out.println("The "+pos+" bit is set(or 1). "); 16 | }else{ 17 | System.out.println("The "+pos+" bit is not set(or 0)."); 18 | } 19 | sc.close(); 20 | } 21 | static boolean checkBit(int n,int p){ 22 | boolean res=((n&(1<0){ 18 | n=n&(n-1); 19 | count++; 20 | } 21 | return count; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Bit_Manipulation/lowerToUpper.java: -------------------------------------------------------------------------------- 1 | package Bit_Manipulation; 2 | 3 | import java.util.Scanner; 4 | 5 | public class lowerToUpper { 6 | 7 | public static void main(String[] args){ 8 | Scanner sc=new Scanner(System.in); 9 | System.out.print("Enter the lowercase Character: "); 10 | char ch=sc.next(".").charAt(0); //it will take only one character(Strict) 11 | // char c=sc.findInLine(".").charAt(0); //it will take first line from the characters 12 | int n=(int)ch; 13 | n=(n&'_'); 14 | System.out.println("Your char '"+ch+"' in UpperCase : "+(char)n); 15 | 16 | sc.close(); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Bit_Manipulation/oddEven.java: -------------------------------------------------------------------------------- 1 | package Bit_Manipulation; 2 | 3 | import java.util.Scanner; 4 | 5 | public class oddEven { 6 | public static void main(String[] args){ 7 | Scanner sc=new Scanner(System.in); 8 | System.out.print("Enter a number to check: "); 9 | int x=sc.nextInt(); 10 | if(checkOddEven(x)){ 11 | System.out.println(x+" is a Even Number. "); 12 | }else{ 13 | System.out.println(x+" is a Odd Number. "); 14 | } 15 | sc.close(); 16 | } 17 | static boolean checkOddEven(int num){ 18 | return ((num & 1)==0); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Bit_Manipulation/powerOfTwo.java: -------------------------------------------------------------------------------- 1 | package Bit_Manipulation; 2 | 3 | import java.util.Scanner; 4 | 5 | public class powerOfTwo { 6 | public static void main(String[] args){ 7 | Scanner sc=new Scanner(System.in); 8 | System.out.print("Enter a number to check: "); 9 | int x=sc.nextInt(); 10 | 11 | if(isPowerOfTwo(x)){ 12 | System.out.println("Power of two!"); 13 | 14 | }else{ 15 | System.out.println("Not a power of two!!"); 16 | } 17 | sc.close(); 18 | } 19 | static boolean isPowerOfTwo(int n){ 20 | return (n>0)&&((n&(n-1))==0); 21 | } 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Bit_Manipulation/subsetOfSet.java: -------------------------------------------------------------------------------- 1 | package Bit_Manipulation; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Generate possible Subsets for given set. 7 | * 8 | * */ 9 | 10 | public class subsetOfSet { 11 | public static void main(String... args) { 12 | Scanner sc = new Scanner(System.in); 13 | // char ch[]={'a','b','c','d'}; 14 | System.out.print("How many element in set? "); 15 | int n = sc.nextInt(); 16 | char[] ch = new char[n]; 17 | System.out.print("Enter the char elements: "); 18 | for (int k = 0; k < n; k++) { 19 | char c = sc.next().charAt(0); 20 | ch[k] = c; 21 | } 22 | subsetOfSet.possibleSubset(ch, n); 23 | sc.close(); 24 | } 25 | 26 | static void possibleSubset(char[] c, int n) { 27 | 28 | for (int i = 0; i < (1 << n); ++i) {// here i loop until it reaches 29 | // 2powN (1< 6) 26 | scorer.four(); 27 | // State of scorer is (score => 10) 28 | System.out.println(scorer.getScore()); 29 | } 30 | } -------------------------------------------------------------------------------- /ClassAndObjects/EveryClassExtendsObject.java: -------------------------------------------------------------------------------- 1 | package ClassAndObjects; 2 | 3 | //Every Java class extends Object class 4 | public class EveryClassExtendsObject { 5 | public void testMethod() throws CloneNotSupportedException { 6 | // toString,hashCode,clone methods are 7 | // inherited from Object class 8 | System.out.println(this.toString()); 9 | System.out.println(this.hashCode()); 10 | System.out.println(this.clone()); 11 | } 12 | 13 | public static void main(String[] args) throws CloneNotSupportedException { 14 | EveryClassExtendsObject example1 = new EveryClassExtendsObject(); 15 | 16 | EveryClassExtendsObject example2 = new EveryClassExtendsObject(); 17 | 18 | if (example1 instanceof Object) { 19 | System.out.println("I extend Object");// Will be printed 20 | } 21 | 22 | // equals method is inherited from Object class 23 | System.out.println(example1.equals(example2));// false 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ClassAndObjects/Monster.java: -------------------------------------------------------------------------------- 1 | package ClassAndObjects; 2 | 3 | 4 | class MonsterWorks { 5 | 6 | public final String TOMBSTONE = "Here Lies a Dead monster"; 7 | private int health = 500; 8 | private int attack = 20; 9 | private int movement = 2; 10 | private boolean alive = true; 11 | public String name = "Big Monster"; 12 | 13 | public int getAttack() { 14 | return attack; 15 | } 16 | 17 | public int getMovement() { 18 | return movement; 19 | } 20 | 21 | public int getHealth() { 22 | return health; 23 | } 24 | 25 | public void setHealth(int decreaseHealth) { 26 | health = health - decreaseHealth; 27 | if (health < 0 && alive == true) { 28 | alive = false; 29 | } 30 | } 31 | 32 | public void setHealth(double decreaseHealth) { 33 | int intDecreaseHealth = (int) decreaseHealth; 34 | health = health - intDecreaseHealth; 35 | if (health < 0) { 36 | alive = false; 37 | } 38 | } 39 | } 40 | //Moster Class 41 | public class Monster{ 42 | 43 | public static void main(String[] args){ 44 | MonsterWorks gorilla = new MonsterWorks(); 45 | gorilla.name = "Jumbo"; 46 | System.out.println(gorilla.name+" got health power: "+gorilla.getHealth()); 47 | System.out.println(gorilla.name + " has an attack value of " + gorilla.getAttack()); 48 | gorilla.setHealth(30); 49 | System.out.println(gorilla.name+" got attacked, Now Health "+gorilla.getHealth()); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /ClassAndObjects/ObjectPass.java: -------------------------------------------------------------------------------- 1 | package ClassAndObjects; 2 | 3 | class PassObjRef { 4 | 5 | int a, b; 6 | 7 | public PassObjRef(int i, int j) { 8 | a = i; 9 | b = j; 10 | } 11 | 12 | // pass an object 13 | // Java always pass object (call-by-reference) 14 | // reference of an object stored in stack area. 15 | void meth(PassObjRef o) { 16 | o.a *= 2; 17 | o.b /= 2; 18 | } 19 | } 20 | 21 | class ObjectPass{ 22 | public static void main(String args[]) { 23 | PassObjRef ob = new PassObjRef(15, 20); 24 | System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b); 25 | ob.meth(ob); 26 | System.out.println("ob.a and ob.b after call: " + ob.a + " " + ob.b); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /ClassAndObjects/Radio.java: -------------------------------------------------------------------------------- 1 | package ClassAndObjects; 2 | 3 | public class Radio { 4 | int volume; 5 | String station; 6 | boolean powerStatus; 7 | 8 | public void increaseVolume() { 9 | volume+=2; 10 | } 11 | 12 | public void decreaseVolume(){ 13 | volume--; 14 | } 15 | 16 | public void changeStation(String newStation) { 17 | station = newStation; 18 | } 19 | 20 | public void turnOn() { 21 | volume = 3; 22 | powerStatus = true; 23 | } 24 | 25 | public void turnOff() { 26 | volume = 0; 27 | station="Nil"; 28 | powerStatus = false; 29 | } 30 | void status(){ 31 | if(powerStatus) 32 | System.out.println("Radio is On! "); 33 | else 34 | System.out.println("Radio is off! "); 35 | System.out.println("Station Name: "+station); 36 | System.out.println("Volume: "+volume); 37 | 38 | } 39 | public static void main(String[] args) { 40 | Radio r1=new Radio(); 41 | r1.changeStation("Radio Mirchi! "); 42 | r1.turnOn(); 43 | r1.status(); 44 | System.out.println("----------------------------------"); 45 | r1.increaseVolume(); 46 | r1.status(); 47 | System.out.println("----------------------------------"); 48 | r1.decreaseVolume(); 49 | r1.turnOff(); 50 | r1.status(); 51 | System.out.println("----------------------------------"); 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /ClassAndObjects/Solution.java: -------------------------------------------------------------------------------- 1 | package ClassAndObjects; 2 | //public class Cricketer { 3 | // String name; 4 | // int odiRuns; 5 | // int testRuns; 6 | // int t20Runs; 7 | // 8 | // public int totalRuns() { 9 | // int totalRuns = odiRuns + testRuns + t20Runs; 10 | // return totalRuns; 11 | // } 12 | //} 13 | import java.util.Scanner; 14 | 15 | public class Solution { 16 | 17 | public static void main(String[] args) { 18 | Scanner scan = new Scanner(System.in); 19 | int i = scan.nextInt(); 20 | double d = scan.nextDouble(); 21 | scan.nextLine();// to consume extra line generated by inputBuffer in nextDouble 22 | String s = scan.nextLine(); 23 | 24 | System.out.println("String: " + s); 25 | System.out.println("Double: " + d); 26 | System.out.println("Int: " + i); 27 | scan.close(); 28 | } 29 | } -------------------------------------------------------------------------------- /ClassAndObjects/TrickyQue.java: -------------------------------------------------------------------------------- 1 | package ClassAndObjects; 2 | 3 | public class TrickyQue { 4 | 5 | public static void main(String[] args) { 6 | Integer i1 = 127; 7 | 8 | Integer i2 = 127; 9 | 10 | System.out.println(i1 == i2); 11 | 12 | Integer i3 = 128; 13 | 14 | Integer i4 = 128; 15 | 16 | System.out.println(i3 == i4); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Constructor/Car.java: -------------------------------------------------------------------------------- 1 | package Constructor; 2 | 3 | class CarCreat { 4 | 5 | int regNum; 6 | private static int num=1000; 7 | 8 | CarCreat(){regNum=num++;} 9 | 10 | void cardetails(){ 11 | System.out.println("Your car: "+regNum); 12 | } 13 | 14 | void start(){ 15 | System.out.println("Car Starts. "); 16 | } 17 | void accelerate(){ 18 | System.out.println("Car Accelerate. "); 19 | } 20 | void stop(){ 21 | System.out.println("Car Stops. "); 22 | } 23 | } 24 | 25 | class Driver{ 26 | String name; 27 | CarCreat c=new CarCreat(); 28 | Driver(String n){ 29 | name=n; 30 | } 31 | void driverName(){ 32 | System.out.println(name); 33 | } 34 | } 35 | 36 | public class Car{ 37 | public static void main(String... args){ 38 | 39 | Driver dr=new Driver("xyz"); 40 | dr.driverName(); 41 | dr.c.cardetails(); 42 | dr.c.start(); 43 | 44 | Driver dr1=new Driver("abc"); 45 | dr.driverName(); 46 | dr1.c.cardetails(); 47 | dr1.c.start(); 48 | } 49 | } -------------------------------------------------------------------------------- /Constructor/ConstEx1.java: -------------------------------------------------------------------------------- 1 | package Constructor; 2 | 3 | public class ConstEx1 { 4 | 5 | // if we won't provide any constructor compiler adds default constructor 6 | // int this case, ConstEx1(){} , but if we provide any other constructor 7 | // compiler won't add. 8 | 9 | int x, y; 10 | 11 | // no-argument constructor 12 | ConstEx1() { 13 | getX(); // we can call both static and non-static method from 14 | // constructor 15 | x = 0; 16 | y = 0; 17 | } 18 | 19 | // Parameterized Constructor 20 | ConstEx1(int l) { 21 | 22 | x = y = l; 23 | getX(); 24 | } 25 | 26 | ConstEx1(int l, int m) { 27 | x = l; 28 | y = m; 29 | } 30 | 31 | static void getX() { 32 | System.out.println(" You're inside method getX() "); 33 | // return x; 34 | } 35 | 36 | public static void main(String[] args) { 37 | ConstEx1 c1 = new ConstEx1(); 38 | System.out.println("[C1]X= " + c1.x + " Y= " + c1.y); 39 | 40 | ConstEx1 c2 = new ConstEx1(20); 41 | ConstEx1 c3 = new ConstEx1(12, 13); 42 | 43 | System.out.println("[C2]X= " + c2.x + " Y= " + c2.y); 44 | System.out.println("[C3]X= " + c3.x + " Y= " + c3.y); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Constructor/CopyConst.java: -------------------------------------------------------------------------------- 1 | package Constructor; 2 | 3 | public class CopyConst { 4 | 5 | double real, imaginary; 6 | 7 | public CopyConst(double re, double img) { 8 | real = re; 9 | imaginary = img; 10 | } 11 | 12 | // Copy Constructor 13 | public CopyConst(CopyConst c) { 14 | 15 | real = c.real; 16 | imaginary = c.imaginary; 17 | } 18 | 19 | @Override // toString() is the override method of Object Class 20 | public String toString() { 21 | return "(" + real + " + " + imaginary + "i)"; 22 | } 23 | 24 | public static void main(String[] args) { 25 | 26 | CopyConst c1 = new CopyConst(10, 15); 27 | CopyConst c2 = new CopyConst(c1); 28 | CopyConst c3 = c1; 29 | 30 | System.out.println("[C2]" + c2); // if we directly print object Output 31 | // Constructor.CopyConst@6d06d69c 32 | 33 | System.out.println("[C3]" + c3); // on directly printing object it will 34 | // call toString() of Object class 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Inheritance/InheritEx.java: -------------------------------------------------------------------------------- 1 | package Inheritance; 2 | 3 | class SuperClass { 4 | int i, j; 5 | 6 | void showij() { 7 | System.out.println("i and j: " + i + " " + j); 8 | } 9 | } 10 | 11 | class SubClass extends SuperClass { 12 | int k; 13 | 14 | void showk() { 15 | System.out.println("k:" + k); 16 | } 17 | 18 | void sum() { 19 | System.out.println(i + j + k); 20 | } 21 | 22 | } 23 | 24 | class InheritEx { 25 | public static void main(String args[]) { 26 | SuperClass sup = new SuperClass(); 27 | SubClass sub = new SubClass(); 28 | 29 | sup.i = 10; 30 | sup.j = 20; 31 | System.out.println("Contents of super class "); 32 | sup.showij(); 33 | sub.i = 7; 34 | sub.j = 8; 35 | sub.k = 9; 36 | System.out.println("Contents of sub class"); 37 | sub.showij(); 38 | sub.showk(); 39 | System.out.println("Sum of i, j and k in sub class"); 40 | sub.sum(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Inheritance/NblockInherit.java: -------------------------------------------------------------------------------- 1 | package Inheritance; 2 | 3 | class Box1 { 4 | { 5 | System.out.println(1); 6 | } 7 | } 8 | 9 | class Box2 extends Box1 { 10 | { 11 | System.out.println(2); 12 | } 13 | } 14 | 15 | class Box3 extends Box2 { 16 | { 17 | System.out.println(3); 18 | } 19 | } 20 | 21 | public class NblockInherit { 22 | public static void main(String[] args) { 23 | Box3 c = new Box3(); 24 | System.out.println(c); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Inheritance/SblockInherit.java: -------------------------------------------------------------------------------- 1 | package Inheritance; 2 | 3 | class Box11 { 4 | static { 5 | System.out.println(1); 6 | } 7 | } 8 | 9 | class Box12 extends Box11 { 10 | static { 11 | System.out.println(2); 12 | } 13 | } 14 | 15 | class Box13 extends Box12 { 16 | static { 17 | System.out.println(3); 18 | } 19 | } 20 | 21 | public class SblockInherit { 22 | public static void main(String[] args) { 23 | Box13 c = new Box13(); 24 | System.out.println(c); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Inheritance/SingleLevel.java: -------------------------------------------------------------------------------- 1 | package Inheritance; 2 | 3 | class A { 4 | private int i = 12; 5 | protected int j = 13; 6 | public int k = 14; 7 | int m = 3; 8 | 9 | A() { 10 | System.out.println("I'm Constructor from class A."); 11 | } 12 | 13 | private void m1() { 14 | System.out.println("Private method of class A " + i); 15 | } 16 | 17 | protected void m2() { 18 | System.out.println("Protected method of class A " + j); 19 | m1(); 20 | } 21 | 22 | public void m3() { 23 | System.out.println("Public method of class A " + k); 24 | } 25 | 26 | void m4() { 27 | System.out.println("Package method of class A" + i + " " + m); 28 | } 29 | } 30 | 31 | class B extends A { 32 | 33 | B() { 34 | System.out.println("I'm Constructor from class B."); 35 | } 36 | 37 | public void m5() { 38 | // System.out.println(i); // private variables not accessible in 39 | // sub-class 40 | System.out.println(" Method from class B. "); 41 | System.out.println("Protected j= " + j); 42 | System.out.println("public k= " + k); 43 | System.out.println("Package m= " + m); 44 | // m1(); // Private method is not accessible in sub-class 45 | m2(); 46 | m3(); 47 | m4(); 48 | } 49 | 50 | } 51 | 52 | public class SingleLevel { 53 | public static void main(String[] args) { 54 | B b = new B(); 55 | System.out.println("----------------------------------"); 56 | System.out.println(" From Main method. "); 57 | b.m5(); 58 | b.m4(); 59 | System.out.println("----------------------------------"); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Inheritance/TrickyEx.java: -------------------------------------------------------------------------------- 1 | package Inheritance; 2 | 3 | class Test { 4 | static int i = 56; 5 | 6 | static { 7 | i = i-- - --i; 8 | System.out.println("i value Static Block of Test Class: "+i); 9 | } 10 | 11 | { 12 | i = i++ + ++i; 13 | System.out.println("i value Non-Static Block of Test Class: "+i); 14 | } 15 | } 16 | 17 | class Test1 extends Test { 18 | static { 19 | i = --i - i--; 20 | System.out.println("i value Static Block of Test1 Class: "+i); 21 | } 22 | 23 | { 24 | i = ++i + i++; 25 | System.out.println("i value Non-Static Block of Test1 Class: "+i); 26 | } 27 | } 28 | 29 | public class TrickyEx { 30 | public static void main(String[] args) { 31 | // Test1 b = new Test1(); 32 | // System.out.println(b.i); 33 | System.out.println(Test.i); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /MathPrograms/BigFactorial.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import java.math.BigInteger; 4 | import java.util.Scanner; 5 | 6 | public class BigFactorial { 7 | 8 | public static void main(String[] args) { 9 | Scanner sc =new Scanner(System.in); 10 | System.out.println("Enter the number: "); 11 | int n=sc.nextInt(); 12 | BigInteger b=new BigInteger("1"); 13 | for(int i=1;i<=n;i++) { 14 | b=b.multiply(BigInteger.valueOf(i)); 15 | } 16 | System.out.println("Factorial of a Number: "+b); 17 | sc.close(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /MathPrograms/Factorial.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Factorial { 6 | 7 | //Recursive Factorial 8 | public static int fact(int n) { 9 | if(n<1) return 1; 10 | return n*fact(n-1); 11 | } 12 | //Non-recursive Factorial 13 | public static int nonRfact(int n) { 14 | int f=1; 15 | while(n!=0) 16 | f=f*n--; 17 | return f; 18 | } 19 | public static void main(String[] args) { 20 | Scanner sc = new Scanner(System.in); 21 | System.out.println("Enter the number: "); 22 | int t = sc.nextInt(); 23 | int result=fact(t); 24 | System.out.println("Factorial of number: "+result); 25 | sc.close(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /MathPrograms/GCD.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import java.util.Scanner; 4 | 5 | // Greatest Common Divisor 6 | public class GCD { 7 | // minimum of two numbers 8 | public static int min(int a, int b) { 9 | return a < b ? a : b; 10 | } 11 | 12 | // GCD calculation 13 | public static int gcdCalculate(int x, int y) { 14 | int m = min(x, y), gcd = 1; // because 1 is common divisor for any number 15 | for (int i = m; i > 0; --i) { 16 | if (x % i == 0 && y % i == 0) { 17 | gcd = i; 18 | break; 19 | } 20 | } 21 | return gcd; 22 | } 23 | 24 | public static void main(String[] args) { 25 | Scanner sc = new Scanner(System.in); 26 | System.out.print("Enter the first number : "); 27 | int first = sc.nextInt(); 28 | System.out.print("Enter the Second number : "); 29 | int second = sc.nextInt(); 30 | long result = gcdCalculate(first, second); 31 | System.out.println("GCD of Number " + first + " And " + second + " = " + result); 32 | sc.close(); 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /MathPrograms/GCDEuclid.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import java.util.Scanner; 4 | 5 | //Euclid's Algorithm to calculate GCD of two numbers 6 | 7 | public class GCDEuclid { 8 | 9 | public static int gcd(int x, int y) { 10 | int max,min; 11 | if(x 0 && result[i] == 0) 27 | --i; 28 | while (i >= 0) 29 | System.out.print(result[i--]); 30 | System.out.println(); 31 | sc.close(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /MathPrograms/NegPowerOfN.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import java.util.Scanner; 4 | 5 | public class NegPowerOfN { 6 | 7 | public static double PowerOfX(int num, int pow) { 8 | double res = 1; 9 | if (pow > 0) { 10 | for (int i = 0; i < pow; i++) 11 | res = res * num; 12 | } else { 13 | for (int i = 0; i < (-pow); i++) 14 | res = res / num; 15 | } 16 | return res; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Scanner sc = new Scanner(System.in); 21 | System.out.print("Enter the value of x: "); 22 | int x = sc.nextInt(); 23 | System.out.print("Enter the power of x: "); 24 | int n = sc.nextInt(); 25 | double result = PowerOfX(x, n); 26 | System.out.println(x + " Power " + n + " = " + result); 27 | sc.close(); 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /MathPrograms/PascalTriangle.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PascalTriangle { 6 | 7 | public static void pascal(int n) { 8 | 9 | } 10 | 11 | public static void main(String[] args){ 12 | 13 | Scanner sc =new Scanner(System.in); 14 | System.out.print("Enter the length of pascal triangle: "); 15 | int n=sc.nextInt(); 16 | // pascal(len); 17 | for(int i=1;i<=n;i++) { 18 | int c=0; 19 | for(int spaces=1;spaces<=n-i+1;spaces++) 20 | System.out.println(" "); 21 | int k=i; 22 | for(int j=1;j>=1;) { 23 | System.out.println(j+" "); 24 | if(j==k) { 25 | j--; k=j; 26 | c=1; 27 | } 28 | if(c==0) 29 | j++; 30 | } 31 | System.out.print("\n"); 32 | } 33 | sc.close(); 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /MathPrograms/PowerOfX.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * if x pow(n) then x(2)(n/2) 7 | * if x pow(n) then x(2)(n-1/2) 8 | * 9 | * */ 10 | public class PowerOfX { 11 | 12 | public static long xPowN(int x, int n) { 13 | int result = 1; 14 | while (n > 0) { 15 | if (n % 2 == 1) 16 | result = result * x; 17 | x = x * x; 18 | n = n / 2; 19 | } 20 | return result; 21 | } 22 | 23 | /* To compute power of multiple numbers...! 2 pow(10 pow(9)) 24 | * 25 | * int modularExponentiation(int x,int n,int M) 26 | { 27 | int result=1; 28 | while(n>0) 29 | { 30 | if(n % 2 ==1) 31 | result=(result * x)%M; 32 | x=(x*x)%M; 33 | n=n/2; 34 | } 35 | return result; 36 | }*/ 37 | 38 | public static void main(String[] args) { 39 | Scanner sc = new Scanner(System.in); 40 | System.out.print("Enter the value of x: "); 41 | int x = sc.nextInt(); 42 | System.out.print("Enter the power of x: "); 43 | int n = sc.nextInt(); 44 | long result = xPowN(x, n); 45 | System.out.println(x + " Power " + n + " = " + result); 46 | sc.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MathPrograms/SeriesSum.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SeriesSum { 6 | 7 | public static int sum(int n) { 8 | return ((n*(n+1))/2); 9 | } 10 | public static int sumSquare(int n) { 11 | return ((n*(n+1)*(2*n+1))/6); 12 | } 13 | public static int sumCubes(int n) { 14 | return ((n*n*(n+1)*(n+1))/4); 15 | } 16 | public static void main(String[] args) { 17 | Scanner sc = new Scanner(System.in); 18 | System.out.println("Enter two numbers: "); 19 | int a=sc.nextInt(); 20 | System.out.println("Sum of Series: "+sum(a)); 21 | System.out.println("Sum of Square of the Series: "+sumSquare(a)); 22 | System.out.println("Sum of Cubes of the Series: "+sumCubes(a)); 23 | sc.close(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MathPrograms/factorOfN.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import java.util.Scanner; 4 | 5 | public class factorOfN { 6 | public static void factor(int n) { 7 | for(int i=1;i<=n/2;i++) { 8 | if(n%i==0) 9 | System.out.println(i+", "); 10 | } 11 | } 12 | 13 | public static void main(String[] args) { 14 | Scanner sc = new Scanner(System.in); 15 | System.out.println("Enter the number: "); 16 | int t = sc.nextInt(); 17 | factor(t); 18 | sc.close(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /MathPrograms/greatestPrimeCheck.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | import trick.FastReader; 3 | 4 | public class greatestPrimeCheck { 5 | 6 | public static int greatestPrime(int num) { 7 | int calculatedPrimeNo = 0; 8 | if (num == 0 || num == 1) { 9 | return 2; 10 | } 11 | do { 12 | boolean isPrime = true; 13 | for (int i = 2; i < Math.sqrt(num); i++) { 14 | if (num % i == 0) { 15 | isPrime = false; 16 | break; 17 | } 18 | } 19 | if (isPrime) { 20 | calculatedPrimeNo = num; 21 | } else { 22 | num++; 23 | } 24 | } while (calculatedPrimeNo != num); 25 | return calculatedPrimeNo; 26 | } 27 | 28 | public static void main(String[] args) { 29 | FastReader fc=new FastReader(); 30 | System.out.println("Enter the number: "); 31 | int num = fc.nextInt(); 32 | System.out.println(greatestPrime(num) - num); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /MathPrograms/nCr.java: -------------------------------------------------------------------------------- 1 | package MathPrograms; 2 | 3 | import MathPrograms.Factorial; 4 | import java.util.Scanner; 5 | 6 | public class nCr { 7 | 8 | public static double permutation(int n, int r) { 9 | int a, b, c; 10 | a = Factorial.fact(n); 11 | b = Factorial.fact(r); 12 | c = Factorial.fact(n - r); 13 | return (double) a / ((double) (b * c)); 14 | } 15 | 16 | public static void main(String[] args) { 17 | Scanner sc = new Scanner(System.in); 18 | System.out.println("Enter the value of 'n': "); 19 | int n = sc.nextInt(); 20 | System.out.println("Enter the value of 'r': "); 21 | int r = sc.nextInt(); 22 | double result; 23 | if (n >= r) { 24 | result = permutation(n, r); 25 | System.out.println("nCr of given inputs: " + result); 26 | } else { 27 | System.out.println("N should not be greater than R. "); 28 | } 29 | sc.close(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MethodOverloading/NullTest1.java: -------------------------------------------------------------------------------- 1 | package MethodOverloading; 2 | 3 | public class NullTest1 { 4 | 5 | /* 6 | * If more than one member method is both accessible and applicable to a method invocation, 7 | * it is necessary to choose one to provide the descriptor for the run-time method dispatch. 8 | * 9 | * The Java programming language uses the rule that the most specific method is chosen. 10 | * */ 11 | 12 | public static void method(Object obj){ 13 | System.out.println("method with param type - Object"); 14 | } 15 | 16 | public static void method(String obj){ 17 | System.out.println("method with param type - String"); 18 | } 19 | 20 | // public static void method(StringBuffer strBuf){ 21 | // System.out.println("method with param type - StringBuffer"); 22 | // } 23 | 24 | /* 25 | * Why is the compiler not able to pick 'the most specific' here - because both "String" and "StringBuffer" are are sub-classes. 26 | of the Object class, but without being in the same inheritance hierarchy. For finding 27 | 'the most specific' method, the compiler needs to find a method having the parameter type, 28 | which is a sub-class of the parameter types of all other overloaded methods. 29 | * */ 30 | 31 | public static void main(String [] args){ 32 | method(null); 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /MethodOverloading/NullTest2.java: -------------------------------------------------------------------------------- 1 | package MethodOverloading; 2 | 3 | public class NullTest2 { 4 | 5 | public static void method(Object obj, Object obj1){ 6 | System.out.println("method with param types - Object, Object"); 7 | } 8 | 9 | public static void method(String str, Object obj){ 10 | System.out.println("method with param types - String, Object"); 11 | } 12 | /* 13 | * In this case the compiler can easily pick 'the most specific' as the method having parameter types (String, Object) as 14 | the other overloaded method is having its parameter types as (Object, Object) - clearly 'String' is a subclass of 'Object' 15 | and the other parameter is of same type, so the method with parameter types (String, Object) can be picked with ease. 16 | * 17 | * */ 18 | public static void main(String [] args){ 19 | method(null, null); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /MethodOverloading/TestMethod1.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/divScorp/Java-Programs/f56d6112ac921bd644beecb92e5a9e6ac9ab50e7/MethodOverloading/TestMethod1.java -------------------------------------------------------------------------------- /MethodOverloading/TestMethod2.java: -------------------------------------------------------------------------------- 1 | package MethodOverloading; 2 | 3 | public class TestMethod2 { 4 | 5 | static void add(int a, double d){ 6 | System.out.println("Argument type (int , double)"); 7 | } 8 | 9 | static void add(double d, int a){ 10 | System.out.println("Argument type (double, int)"); 11 | } 12 | 13 | static void add(double d, double a){ 14 | System.out.println("Argument type (double, double)"); 15 | } 16 | static void add(int a, float f){ 17 | System.out.println("Argument type (int , float)"); 18 | } 19 | static void add(float f, int a){ 20 | System.out.println("Argument type (float, int)"); 21 | } 22 | 23 | public static void main(String... args){ 24 | 25 | //add(10,20); is a ambiguous CTE; 26 | // because he can't able to decide which argument he will take as double or int 27 | // as there exists two methods with args (int, double) and (double, int) 28 | //add(12,13); 29 | add(10,20.0); 30 | add(10.0f,10); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MethodOverloading/TestMethod3.java: -------------------------------------------------------------------------------- 1 | package MethodOverloading; 2 | 3 | public class TestMethod3 { 4 | 5 | static void add(double d, double a){ 6 | System.out.println("Addition: "+(a+d)); 7 | } 8 | static void add(int d, double a){ 9 | System.out.println("Addition: "+(a+d)); 10 | } 11 | // static void add(double d, int a){ 12 | // System.out.println("Addition: "+(a+d)); 13 | // } 14 | // 15 | public static void main(String... str){ 16 | add(3,5); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /MethodOverloading/TestMethod4.java: -------------------------------------------------------------------------------- 1 | package MethodOverloading; 2 | 3 | public class TestMethod4 { 4 | static void add(int a, double d){ 5 | System.out.println("Argument type (int , double)"); 6 | } 7 | 8 | public static void add(double d, int a){ 9 | System.out.println("Argument type (double, int)"); 10 | } 11 | 12 | private static void add(double d, double a){ 13 | System.out.println("Argument type (double, double)"); 14 | } 15 | 16 | protected static void add(int a, float f){ 17 | System.out.println("Argument type (int , float)"); 18 | } 19 | 20 | public static void main(String... str){ 21 | add(3.0,5.0); 22 | add(7,5.0); 23 | add(7,2.12f); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MethodOverloading/TestMethod5.java: -------------------------------------------------------------------------------- 1 | package MethodOverloading; 2 | 3 | public class TestMethod5 { 4 | 5 | void method(String s1){ 6 | method(s1, s1+s1); 7 | } 8 | 9 | void method(String s1, String s2){ 10 | method(s1, s2, s1+s2); 11 | } 12 | 13 | void method(String s1, String s2, String s3) { 14 | System.out.println(s1+s2+s3); 15 | } 16 | 17 | public static void main(String[] args) 18 | { 19 | TestMethod5 one = new TestMethod5(); 20 | 21 | one.method("Java"); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /ObjectOrdering/ComparableMovie.java: -------------------------------------------------------------------------------- 1 | package ObjectOrdering; 2 | import java.util.Arrays; 3 | 4 | class Movie implements Comparable { 5 | private double rating; 6 | private String name; 7 | private int year; 8 | 9 | public Movie(String n, double rt, int y) { 10 | this.name = n; 11 | this.rating = rt; 12 | this.year = y; 13 | } 14 | // compare on the basis of rating 15 | /* 16 | * public int compareTo(Movie arg) { return this.year - arg.year; } 17 | */ 18 | 19 | // compare on the basis of name 20 | public int compareTo(Movie m) { 21 | return this.name.compareTo(m.name); // this will call compareTo method of String Class 22 | } 23 | 24 | public double getRating() { 25 | return rating; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public int getYear() { 33 | return year; 34 | } 35 | } 36 | 37 | public class ComparableMovie { 38 | 39 | public static void main(String[] args) { 40 | Movie[] mov=new Movie[4]; 41 | mov[0]=new Movie("Inception",8.9,2010); 42 | mov[1]=new Movie("The Dark Knight",9.0,2008); 43 | mov[2]=new Movie("Intersteller",8.7,2014); 44 | mov[3]=new Movie("The Prestige",8.2,2005); 45 | Arrays.sort(mov); 46 | System.out.println("Movie list in sorted order: "); 47 | for (Movie mv : mov) { 48 | System.out.println( 49 | "Name: " + mv.getName() + "\t\t[Rating: " + mv.getRating() + "\tYear: " + mv.getYear() + "]"); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /ObjectOrdering/ComparatorMovie.java: -------------------------------------------------------------------------------- 1 | package ObjectOrdering; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Comparator; 6 | 7 | 8 | class MovieCom { 9 | private double rating; 10 | private String name; 11 | private int year; 12 | 13 | public MovieCom(String n, double rt, int y) { 14 | this.name = n; 15 | this.rating = rt; 16 | this.year = y; 17 | } 18 | 19 | public double getRating() { 20 | return rating; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public int getYear() { 28 | return year; 29 | } 30 | public String toString() { 31 | return "Name: "+name+"\tRating: "+rating+"\tYear: "+year; 32 | } 33 | } 34 | 35 | class RatingCompare implements Comparator { 36 | public int compare(MovieCom m1, MovieCom m2) { 37 | return (m1.getRating() < m2.getRating()) ? -1 : ((m1.getRating() > m2.getRating()) ? 1 : 0); 38 | } 39 | } 40 | 41 | class NameCompare implements Comparator { 42 | public int compare(MovieCom m1, MovieCom m2) { 43 | return m1.getName().compareTo(m2.getName()); 44 | } 45 | } 46 | 47 | public class ComparatorMovie { 48 | 49 | public static void main(String[] args) { 50 | ArrayList mov = new ArrayList<>(); 51 | mov.add(new MovieCom("Inception ", 8.9, 2010)); 52 | mov.add(new MovieCom("The Dark Knight", 9.0, 2008)); 53 | mov.add(new MovieCom("Intersteller", 8.7, 2014)); 54 | mov.add(new MovieCom("The Prestige", 8.2, 2005)); 55 | System.out.println("Sorted Movie list on the basis of Name."); 56 | Collections.sort(mov,new NameCompare()); 57 | for(MovieCom m:mov) 58 | System.out.println(m); 59 | System.out.println("\nSorted Movie list on the basis of Rating."); 60 | Collections.sort(mov,new RatingCompare()); 61 | for(MovieCom m:mov) 62 | System.out.println(m); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Patterns/Mantry.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | public class Mantry { 4 | 5 | static void patternPrint(int n) { 6 | int c=1; 7 | for(int i=0;i 0; --p) 16 | System.out.print(" "); 17 | for (x = 0; x <= q; ++x) { 18 | if ((x == 0) || (q == 0)) 19 | bin = 1; 20 | else 21 | bin = (bin * (q - x + 1)) / x; 22 | System.out.print(" "); 23 | System.out.print(bin); 24 | } 25 | 26 | System.out.println(""); 27 | ++q; 28 | } 29 | s.close(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Patterns/Pattern01sc.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | import java.util.Scanner; 4 | /* 5 | * 1 6 | 0 1 7 | 1 0 1 8 | 0 1 0 1 9 | 1 0 1 0 1 10 | 0 1 0 1 0 1 11 | 1 0 1 0 1 0 1 12 | * 13 | */ 14 | public class Pattern01sc { 15 | 16 | public static void main(String[] args) { 17 | Scanner sc = new Scanner(System.in); 18 | System.out.println("How many rows you want in this pattern?"); 19 | int rows = sc.nextInt(); 20 | String str = ""; 21 | for (int i = 1; i <= rows; i++) { 22 | str = (i % 2) + " " + str; 23 | System.out.println(str); 24 | } 25 | sc.close(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Patterns/Pattern27.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern27 { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | System.out.println("How many rows you want in this pattern?"); 9 | int rows = sc.nextInt(); 10 | System.out.println("Here is your pattern....!!!"); 11 | 12 | // Printing upper half of the pattern 13 | for (int i = 1; i <= rows; i++) { 14 | for (int j = 1; j < i; j++) { 15 | System.out.print(" "); 16 | } 17 | for (int j = i; j <= rows; j++) { 18 | System.out.print(j + " "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | // Printing lower half of the pattern 24 | for (int i = rows - 1; i >= 1; i--) { 25 | for (int j = 1; j < i; j++) { 26 | System.out.print(" "); 27 | } 28 | for (int j = i; j <= rows; j++) { 29 | System.out.print(j + " "); 30 | } 31 | System.out.println(); 32 | } 33 | sc.close(); 34 | } 35 | } -------------------------------------------------------------------------------- /Patterns/Pattern28.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | 4 | /* 5 | ********** 6 | **** **** 7 | *** *** 8 | ** ** 9 | * * 10 | * * 11 | ** ** 12 | *** *** 13 | **** **** 14 | ********** 15 | 16 | * 17 | * */ 18 | public class Pattern28 { 19 | public static void main(String args[]) { 20 | int space = 0; 21 | for (int i = 5; i >= 1; i--) { 22 | for (int j = i; j >= 1; j--) 23 | System.out.print("*"); 24 | for (int j = 1; j <= space; j++) 25 | System.out.print(" "); 26 | for (int j = i; j >= 1; j--) 27 | System.out.print("*"); 28 | System.out.println(); 29 | space = space + 2; 30 | } 31 | space = 8; 32 | for (int i = 1; i <= 5; i++) { 33 | for (int j = 1; j <= i; j++) 34 | System.out.print("*"); 35 | for (int j = 1; j <= space; j++) 36 | System.out.print(" "); 37 | for (int j = 1; j <= i; j++) 38 | System.out.print("*"); 39 | System.out.println(); 40 | space = space - 2; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Patterns/Pattern29.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | public class Pattern29 { 4 | 5 | public static void main(String[] args) { 6 | int nos = 4; 7 | for (int i = 1; i <= 5; i++) { 8 | for (int s = nos; s >= 1; s--) { 9 | System.out.println(" "); 10 | } 11 | for (int j = 1; j <= i; j++) { 12 | System.out.println("* "); 13 | } 14 | for (int k = 1; k <= (i - 1); k++) { 15 | if (i == 1) { 16 | continue; 17 | } 18 | System.out.println("* "); 19 | } 20 | System.out.println(); 21 | nos--; 22 | } 23 | nos = 1; 24 | for (int i = 4; i >= 1; i--) { 25 | for (int s = nos; s >= 1; s--) { 26 | System.out.println(" "); 27 | } 28 | for (int j = 1; j <= i; j++) { 29 | System.out.println("* "); 30 | } 31 | for (int k = 1; k <= (i - 1); k++) { 32 | System.out.println("* "); 33 | } 34 | nos++; 35 | System.out.println(); 36 | } 37 | nos = 3; 38 | for (int i = 2; i <= 5; i++) { 39 | if ((i % 2) != 0) { 40 | for (int s = nos; s >= 1; s--) { 41 | System.out.println(" "); 42 | } 43 | for (int j = 1; j <= i; j++) { 44 | System.out.println("* "); 45 | } 46 | } 47 | if ((i % 2) != 0) { 48 | System.out.println(); 49 | nos--; 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Patterns/flagPattern.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | public class flagPattern { 6 | 7 | public static void main(String[] args) { 8 | 9 | Scanner input = new Scanner(System.in); 10 | System.out.println("Enter the no.of stars: "); 11 | int n = input.nextInt(); 12 | int point=n/2; 13 | for(int i=1;i<=n;i++){ 14 | for(int j=1;j<=n;j++){ 15 | if(i==1||i==point||j==1||j==n && i<=point) { 16 | System.out.print("* "); 17 | } 18 | else{ 19 | System.out.print(" "); 20 | } 21 | } 22 | System.out.println(""); 23 | } 24 | input.close(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Patterns/halfDiamond.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | import java.util.Scanner; 4 | /* 5 | * 6 | * * 7 | * * * 8 | * * * * 9 | * * * * * 10 | * * * * * * 11 | * * * * * * * 12 | * * * * * * 13 | * * * * * 14 | * * * * 15 | * * * 16 | * * 17 | * 18 | 19 | * */ 20 | 21 | public class halfDiamond { 22 | public static void main(String[] args) { 23 | Scanner input = new Scanner(System.in); 24 | System.out.println("Enter the no.of stars: "); 25 | int n = input.nextInt(); 26 | int spaces=n-1; 27 | for (int i = 1; i <= n; i++) { 28 | for (int j = i; j <=spaces; j++) { 29 | System.out.print(" "); 30 | } 31 | for (int k = 1; k <= i; k++) { 32 | System.out.print("* "); 33 | } 34 | System.out.println(" "); 35 | } 36 | for (int i = n - 1; i >= 1; i--) { 37 | for (int j = i; j >=spaces; j--) { 38 | System.out.print(" "); 39 | 40 | } 41 | for (int k = 1; k <= i; k++) { 42 | System.out.print("* "); 43 | } 44 | spaces=spaces-2; 45 | System.out.println(" "); 46 | } 47 | input.close(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Patterns/hollowDiamond.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | public class hollowDiamond { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | System.out.print(" How Many Levels? "); 10 | int n = sc.nextInt(); 11 | 12 | if((n&1)==0)n++; 13 | 14 | //Upper Half Triangle 15 | for(int i=1;i<(n/2)+1;i++) { 16 | for(int j=1;j<=n-i;j++) 17 | System.out.print(" "); 18 | 19 | for(int j=1;j<=(2*i)-1;j++) { 20 | if((j==1)||(j==(2*i)-1)) 21 | System.out.print("* "); 22 | else 23 | System.out.print(" "); 24 | } 25 | for(int k=1;k<=n-i;k++) 26 | System.out.print(" "); 27 | System.out.println(); 28 | 29 | } 30 | 31 | //Lower Half Triangle 32 | for(int i=n/2+1;i>=1;i--) { 33 | for(int j=1;j<=n-i;j++) 34 | System.out.print(" "); 35 | 36 | for(int j=1;j<=(2*i)-1;j++) { 37 | if((j==1)||(j==(2*i)-1)) 38 | System.out.print("* "); 39 | else 40 | System.out.print(" "); 41 | } 42 | 43 | for(int k=1;k<=n-i;k++) 44 | System.out.print(" "); 45 | System.out.println(); 46 | 47 | } 48 | 49 | sc.close(); 50 | 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Patterns/pattern1.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | /* 3 | * 4 | * * 5 | * * * 6 | * * * * 7 | * * * * * 8 | * * * * * * 9 | * * * * * * * 10 | */ 11 | import java.util.Scanner; 12 | public class pattern1 { 13 | public static void main(String[] args) { 14 | 15 | Scanner input = new Scanner(System.in); 16 | System.out.println("Enter the no.of stars: "); 17 | int n = input.nextInt(); 18 | for(int i=0;i=1;i--){ 25 | for(int j=n;j>i;j--){ 26 | System.out.print(" "); 27 | } 28 | for(int k=1;k<=i;k++){ 29 | System.out.print("* "); 30 | } 31 | System.out.println(" "); 32 | } 33 | input.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Patterns/pattern13.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | /* 3 | * * * * * * * 4 | * * * * * * 5 | * * * * * 6 | * * * * 7 | * * * 8 | * * 9 | * 10 | */ 11 | 12 | import java.util.Scanner; 13 | /** 14 | * 15 | * @author Vidhikara 16 | */ 17 | public class pattern13 { 18 | 19 | public static void main(String[] args) { 20 | Scanner input = new Scanner(System.in); 21 | System.out.println("Enter the no.of stars: "); 22 | int n = input.nextInt(); 23 | for(int i=n;i>=1;i--){ 24 | for(int j=n;j>i;j--){ 25 | System.out.print(" "); 26 | } 27 | for(int k=1;k<=i;k++){ 28 | System.out.print("* "); 29 | } 30 | System.out.println(" "); 31 | } 32 | input.close(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Patterns/pattern14.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | /* 3 | * 4 | * * * 5 | * * * * * 6 | * * * * * * * 7 | * * * * * * * * * 8 | * * * * * * * * * * * 9 | * * * * * * * * * * * * * 10 | */ 11 | 12 | import java.util.Scanner; 13 | 14 | /** 15 | * 16 | * @author Vidhikara 17 | */ 18 | public class pattern14 { 19 | public static void main(String[] args) { 20 | Scanner input = new Scanner(System.in); 21 | System.out.println("Enter the no.of stars: "); 22 | int n = input.nextInt(); 23 | for(int i=1;i<=n;i++){ 24 | for(int j=i;j=1;i--){ 39 | for(int j=n;j>i;j--){ 40 | System.out.print(" "); 41 | } 42 | for(int k=1;k<(i*2);k++){ 43 | System.out.print("* "); 44 | } 45 | System.out.println(" "); 46 | } 47 | input.close(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Patterns/pattern16.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | /* 3 | * 4 | ** 5 | *** 6 | **** 7 | ***** 8 | ****** 9 | ******* 10 | ****** 11 | ***** 12 | **** 13 | *** 14 | ** 15 | * 16 | */ 17 | 18 | import java.util.Scanner; 19 | 20 | /** 21 | * 22 | * @author Vidhikara 23 | */ 24 | public class pattern16 { 25 | 26 | public static void main(String[] args) { 27 | Scanner input = new Scanner(System.in); 28 | System.out.println("Enter the no.of stars: "); 29 | int n = input.nextInt(); 30 | for(int i=1;i<=n;i++){ 31 | for(int j=i;j=1;i--){ 40 | for(int j=n;j>i;j--){ 41 | System.out.print(" "); 42 | } 43 | for(int k=1;k<=i;k++){ 44 | System.out.print("*"); 45 | } 46 | System.out.println(" "); 47 | } 48 | input.close(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Patterns/pattern17.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | import java.util.Scanner; 3 | /* 4 | * 5 | * * 6 | * * * 7 | * * * * 8 | * * * * * 9 | * * * * * * 10 | * * * * * * * 11 | * * * * * * 12 | * * * * * 13 | * * * * 14 | * * * 15 | * * 16 | * 17 | */ 18 | 19 | /** 20 | * 21 | * @author Vidhikara 22 | */ 23 | public class pattern17 { 24 | 25 | public static void main(String[] args) { 26 | Scanner input = new Scanner(System.in); 27 | System.out.println("Enter the no.of stars: "); 28 | int n = input.nextInt(); 29 | for(int i=1;i<=n;i++){ 30 | for(int j=i;j=1;i--){ 39 | for(int j=n;j>i;j--){ 40 | System.out.print(" "); 41 | } 42 | for(int k=1;k<=i;k++){ 43 | System.out.print("* "); 44 | } 45 | System.out.println(" "); 46 | } 47 | input.close(); 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /Patterns/pattern18.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | /* 4 | * 5 | * * 6 | * * * 7 | * * * * 8 | * * * * * 9 | * * * * 10 | * * * 11 | * * 12 | * 13 | 14 | */ 15 | 16 | import java.util.Scanner; 17 | 18 | /** 19 | * 20 | * @author Vidhikara 21 | */ 22 | public class pattern18 { 23 | 24 | public static void main(String[] args) { 25 | Scanner input = new Scanner(System.in); 26 | System.out.println("Enter the no.of stars: "); 27 | int n = input.nextInt(); 28 | //int x=n*2; 29 | if(n%2==0){ 30 | n++; 31 | } 32 | for(int i=0;i0;j--){ 28 | System.out.print(j+" "); 29 | } 30 | System.out.println(""); 31 | } 32 | input.close(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Patterns/pattern21.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | import java.util.*; 4 | /* 5 | 1234567 6 | 234567 7 | 34567 8 | 4567 9 | 567 10 | 67 11 | 7 12 | 67 13 | 567 14 | 4567 15 | 34567 16 | 234567 17 | 1234567 18 | */ 19 | public class pattern21 20 | { 21 | public static void main(String[] args) 22 | { 23 | Scanner sc = new Scanner(System.in); 24 | System.out.println("How many rows you want in this pattern?"); 25 | int rows = sc.nextInt(); 26 | //Printing upper half of the pattern 27 | 28 | for (int i = 1; i <= rows; i++){ 29 | for (int j = 1; j < i; j++) { 30 | System.out.print(" "); 31 | } 32 | for (int j = i; j <= rows; j++){ 33 | System.out.print(j); 34 | } 35 | System.out.println(); 36 | } 37 | for (int i = rows-1; i >= 1; i--){ 38 | for (int j = 1; j < i; j++){ 39 | System.out.print(" "); 40 | } 41 | for (int j = i; j <= rows; j++){ 42 | System.out.print(j); 43 | } 44 | System.out.println(); 45 | } 46 | sc.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Patterns/pattern22.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | import java.util.Scanner; 4 | /* 5 | 1 6 | 2 8 7 | 3 9 14 8 | 4 10 15 19 9 | 5 11 16 20 23 10 | 6 12 17 21 24 26 11 | 7 13 18 22 25 27 28 12 | */ 13 | 14 | /* 15 | * @author Vidhikara 16 | */ 17 | public class pattern22 { 18 | public static void main(String args[]){ 19 | 20 | Scanner sc =new Scanner(System.in); 21 | System.out.println("Enter the no. :"); 22 | int i,j,k; 23 | int rows=sc.nextInt(); 24 | for( i=1;i<=rows;i++){ 25 | k=i; 26 | for(j=1;j<=i;j++){ 27 | System.out.print(k+" "); 28 | k=k+rows-j; 29 | } 30 | System.out.println(); 31 | } 32 | sc.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Patterns/pattern23.java: -------------------------------------------------------------------------------- 1 | 2 | package Patterns; 3 | 4 | import java.util.Scanner; 5 | /* 6 | 1 7 | 2 1 2 8 | 3 2 1 2 3 9 | 4 3 2 1 2 3 4 10 | 5 4 3 2 1 2 3 4 5 11 | 6 5 4 3 2 1 2 3 4 5 6 12 | 7 6 5 4 3 2 1 2 3 4 5 6 7 13 | */ 14 | 15 | /** 16 | * 17 | * @author Vidhikara 18 | */ 19 | public class pattern23 { 20 | 21 | public static void main(String[] args) { 22 | Scanner sc =new Scanner(System.in); 23 | System.out.println("Enter the no. :"); 24 | int num=sc.nextInt(); 25 | int s=0; 26 | for(int i=1;i<=num;i++){ 27 | s=i; 28 | for(int j=1;j<=2*i-1;j++){ 29 | System.out.print(s+" "); 30 | if(j= 1; s--) { 17 | if (c == n && s == 1) 18 | break; 19 | 20 | System.out.println(" "); 21 | } 22 | for (int k = 1; k <= i; k++) { 23 | if (c == n && k == n + 1) 24 | break; 25 | 26 | System.out.println("*"); 27 | } 28 | System.out.println(""); 29 | space -= n; 30 | ++c; 31 | } 32 | } 33 | scn.close(); 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Patterns/pattern25.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * * 7 | * * * * 8 | * * * * * * 9 | * * * * * * * 10 | * * * * * * 11 | * * * * 12 | * * 13 | * 14 | * */ 15 | 16 | public class pattern25 { 17 | public static void main(String[] args) { 18 | Scanner scn = new Scanner(System.in); 19 | System.out.println("Enter the no: "); 20 | int n = scn.nextInt(); 21 | for (int i = 1; i <= n; i++) { 22 | for (int j = 1; j <= n; j++) { 23 | if (((i + j<=n+1 ) && (i >= j)) || ((i + j >= n+1) && (i <= j))) { 24 | System.out.print("* "); 25 | } else { 26 | System.out.print(" "); 27 | } 28 | } 29 | 30 | System.out.println(); 31 | } 32 | scn.close(); 33 | } 34 | } -------------------------------------------------------------------------------- /Patterns/pattern3.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | /* 3 | * * * * * * * 4 | * * 5 | * * 6 | * * 7 | * * 8 | * * 9 | * * * * * * * 10 | */ 11 | import java.util.Scanner; 12 | public class pattern3 { 13 | public static void main(String[] args) { 14 | 15 | Scanner input = new Scanner(System.in); 16 | System.out.println("Enter the no.of stars: "); 17 | int n = input.nextInt(); 18 | for(int i=1;i<=n;i++){ 19 | for(int j=1;j<=n;j++){ 20 | if(i==1||i==n||j==1||j==n) { 21 | System.out.print("* "); 22 | } 23 | else{ 24 | System.out.print(" "); 25 | } 26 | } 27 | System.out.println(""); 28 | } 29 | input.close(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Patterns/pattern4.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | /* 3 | * * 4 | * * 5 | * * 6 | * 7 | * * 8 | * * 9 | * * 10 | */ 11 | import java.util.Scanner; 12 | public class pattern4 { 13 | public static void main(String[] args) { 14 | 15 | Scanner input = new Scanner(System.in); 16 | System.out.println("Enter the no.of stars: "); 17 | int n = input.nextInt(); 18 | for(int i=0;i=0;j--){ 20 | System.out.print("* "); 21 | if(i==j) break; 22 | } 23 | System.out.println(" "); 24 | } 25 | input.close(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Patterns/pattern6.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | /* 3 | 1 4 | 2 2 5 | 3 3 3 6 | 4 4 4 4 7 | 5 5 5 5 5 8 | 6 6 6 6 6 6 9 | 7 7 7 7 7 7 7 10 | */ 11 | import java.util.Scanner; 12 | public class pattern6 { 13 | public static void main(String[] args) { 14 | 15 | Scanner input = new Scanner(System.in); 16 | System.out.println("Enter the no.of stars: "); 17 | int n = input.nextInt(); 18 | for(int i=1;i<=n;i++){ 19 | for(int j=1;j<=n;j++){ 20 | System.out.print(i+" ");//i prints 21 | if(i==j) break; 22 | } 23 | System.out.println(" "); 24 | } 25 | input.close(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Patterns/pattern7.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | /* 3 | 1 4 | 1 2 5 | 1 2 3 6 | 1 2 3 4 7 | 1 2 3 4 5 8 | 1 2 3 4 5 6 9 | 1 2 3 4 5 6 7 10 | */ 11 | import java.util.Scanner; 12 | public class pattern7 { 13 | public static void main(String[] args) { 14 | 15 | Scanner input = new Scanner(System.in); 16 | System.out.println("Enter the no.of stars: "); 17 | int n = input.nextInt(); 18 | for(int i=1;i<=n;i++){ 19 | for(int j=1;j<=n;j++){ 20 | System.out.print(j+" ");//j prints 21 | if(i==j) break; 22 | } 23 | System.out.println(" "); 24 | } 25 | input.close(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Patterns/pattern8.java: -------------------------------------------------------------------------------- 1 | package Patterns; 2 | /* 3 | 1 4 | 2 3 5 | 4 5 6 6 | 7 8 9 10 7 | 8 | */ 9 | import java.util.Scanner; 10 | public class pattern8 { 11 | public static void main(String[] args) { 12 | 13 | Scanner input = new Scanner(System.in); 14 | System.out.println("Enter the no.of stars: "); 15 | int n = input.nextInt(); 16 | int s=1; 17 | for(int i=0;i= 'a' && c[i] <= 'z') { 16 | c[i] -= 32; 17 | } 18 | } 19 | return new String(c); 20 | } 21 | 22 | public static String removeSpace(String s) { 23 | String t = ""; 24 | for (int i = 0; i < s.length(); i++) { 25 | if (s.charAt(i) != ' ') 26 | t += s.charAt(i); 27 | } 28 | return t; 29 | } 30 | 31 | public static String sort(String s) { 32 | char[] c = s.toCharArray(); 33 | for (int i = 0; i < c.length - 1; i++) 34 | for (int j = i + 1; j < c.length; j++) { 35 | if (c[i] > c[j]) { 36 | char t = c[i]; 37 | c[i] = c[j]; 38 | c[j] = t; 39 | } 40 | } 41 | return new String(c); 42 | } 43 | 44 | public static boolean checkAnagram(String f, String s) { 45 | String s1 = removeSpace(f); 46 | String s2 = removeSpace(s); 47 | if (s1.length() != s2.length()) { 48 | return false; 49 | } 50 | s1 = sort(s1); 51 | s2 = sort(s2); 52 | for (int i = 0; i < s1.length(); i++) { 53 | if (s1.charAt(i) != s2.charAt(i)) 54 | return false; 55 | } 56 | return true; 57 | } 58 | 59 | public static void main(String[] args) { 60 | Scanner sc = new Scanner(System.in); 61 | System.out.print("Enter two String: "); 62 | String first = sc.nextLine(); 63 | String second = sc.nextLine(); 64 | if(checkAnagram(lowerCase(first), lowerCase(second))){ 65 | System.out.println(" Strings are anagram!! "); 66 | }else { 67 | System.out.println(" Strings is not anagram!! "); 68 | } 69 | 70 | sc.close(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /String/AnagramSt.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | import java.util.Scanner; 4 | 5 | public class AnagramSt { 6 | public static void permutation(String str) { 7 | permutation("", str); 8 | } 9 | 10 | private static void permutation(String prefix, String str) { 11 | int n = str.length(); 12 | if (n == 0) 13 | System.out.println(prefix); 14 | else { 15 | for (int i = 0; i < n; i++) 16 | permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n)); 17 | } 18 | } 19 | public static void main(String[] args) { 20 | Scanner sc=new Scanner(System.in); 21 | System.out.println("Enter the String: "); 22 | String str=sc.next(); 23 | permutation(str); 24 | sc.close(); 25 | } 26 | } -------------------------------------------------------------------------------- /String/Colindrome.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Colindrome { 6 | public static String[] split(String s) { 7 | String t = ""; 8 | String r[] = new String[(s.length() / 6)]; 9 | for (int i = 0; i < s.length() / 6;) { 10 | for (int j = 0; j < s.length(); j++) { 11 | if ((j + 1) % 6 == 0) { 12 | t += s.charAt(j); 13 | r[i++] = t; 14 | t = ""; 15 | } else 16 | t += s.charAt(j); 17 | } 18 | } 19 | for (String x : r) { 20 | System.out.println(x); 21 | } 22 | return r; 23 | } 24 | 25 | public static void main(String[] args) { 26 | Scanner sc = new Scanner(System.in); 27 | System.out.println("Enter the String: "); 28 | String s = sc.nextLine(); 29 | sc.close(); 30 | int flag = 0; 31 | if (s.length() < 6 && (s.length() % 6 != 0)) 32 | System.out.println("Not a colindrome!"); 33 | String[] res = split(s); 34 | for (int i = 0; i < res.length; i++) { 35 | if (!isColindrome(res[i])) 36 | flag = 1; 37 | } 38 | if (flag == 0) { 39 | System.out.println("String is colindrome"); 40 | } else { 41 | System.out.println("Not a colindrome!"); 42 | } 43 | 44 | } 45 | 46 | public static boolean isColindrome(String s) { 47 | int k = 2, j = 3; 48 | while (k > 0) { 49 | if (s.charAt(k) != s.charAt(j)) 50 | return false; 51 | k--; 52 | j++; 53 | } 54 | return true; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /String/CountChar.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | /* 3 | Q= Occurence of each letter without using inbuilt function. 4 | i/p - java is easy 5 | o/p - j - 1 6 | a - 3 7 | v - 1 8 | - 2 9 | i - 1 10 | s - 2 11 | e - 1 12 | y - 1 13 | */ 14 | public class CountChar{ 15 | public static void main(String... arg){ 16 | Scanner sc = new Scanner(System.in); 17 | System.out.println("Enter the String: "); 18 | String s=sc.nextLine(); 19 | sc.close(); 20 | int count=0; 21 | char res=' '; 22 | String x=""; 23 | char[] c=s.toCharArray(); 24 | for(int j=0;j dupMap = new HashMap(); 18 | char[] chrs = str.toCharArray(); 19 | for(Character ch:chrs){ 20 | if(dupMap.containsKey(ch)){ 21 | dupMap.put(ch, dupMap.get(ch)+1); 22 | } else { 23 | dupMap.put(ch, 1); 24 | } 25 | } 26 | Set keys = dupMap.keySet(); 27 | for(Character ch:keys){ 28 | if(dupMap.get(ch) > 1){ 29 | System.out.println(ch+"--->"+dupMap.get(ch)); 30 | } 31 | } 32 | } 33 | 34 | public static void main(String a[]){ 35 | DuplicateCharsInString dcs = new DuplicateCharsInString(); 36 | dcs.findDuplicateChars("Java2Novice"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /String/ExchangeAlter.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ExchangeAlter { 6 | public static String[] split(String s) { 7 | int c = 0; 8 | for (int i = 0; i < s.length(); i++) { 9 | if (s.charAt(i) == ' ') { 10 | c += 1; 11 | } 12 | } 13 | String[] st = new String[c + 1]; 14 | String t = ""; 15 | int j = 0; 16 | for (int i = 0; i < s.length(); i++) { 17 | if (s.charAt(i) != ' ') { 18 | t += s.charAt(i); 19 | } else { 20 | st[j++] = t; 21 | t = ""; 22 | } 23 | } 24 | st[j] = t; 25 | return st; 26 | } 27 | 28 | public static void main(String[] args) { 29 | Scanner sc = new Scanner(System.in); 30 | System.out.println("Enter the String: "); 31 | String s = sc.nextLine(); 32 | sc.close(); 33 | String[] str = split(s); 34 | String temp = ""; 35 | temp = str[0]; 36 | str[0] = str[str.length - 1]; 37 | str[str.length - 1] = temp; 38 | for (String s1 : str) { 39 | System.out.print(s1 + " "); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /String/LargeWordCount.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | public class LargeWordCount { 4 | 5 | public static String[] split(String s) { 6 | int c = 0; 7 | for (int i = 0; i < s.length(); i++) { 8 | if (s.charAt(i) == ' ') 9 | c++; 10 | } 11 | String[] temp = new String[c + 1]; 12 | String t = ""; 13 | int i=0; 14 | for (int j = 0; j < s.length(); j++) { 15 | if (s.charAt(j) != ' ') { 16 | t += s.charAt(j); 17 | } else { 18 | temp[i++] = t; 19 | t = ""; 20 | } 21 | } 22 | temp[i] = t; 23 | return temp; 24 | } 25 | public static void sort(String[] s) { 26 | for(int j=0;js[i+1].length()) { 29 | String t=s[i]; 30 | s[i]=s[i+1]; 31 | s[i+1]=t; 32 | } 33 | } 34 | } 35 | } 36 | public static String sortAndCount(String[] sp) { 37 | sort(sp); 38 | String sx=""; 39 | int n; 40 | for(int k=0;k='a'&&c[i]<='z') { 11 | c[i]-=32; 12 | }else { 13 | c[i]+=32; 14 | } 15 | } 16 | return new String(c); 17 | } 18 | public static void main(String[] args) { 19 | String s="Java"; 20 | System.out.print("After Conversion: "+convert(s)); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /String/OcurrenceOfLetter.java: -------------------------------------------------------------------------------- 1 | package String; 2 | import java.util.Scanner; 3 | 4 | public class OcurrenceOfLetter { 5 | public static void letterOccurence(String s) { 6 | int n; char c; 7 | while((s.length()-1)>=0) { 8 | c=s.charAt(0); 9 | n=s.length()-s.replace(c+"","").length(); 10 | System.out.println("Occurence of ["+c+"] = "+n); 11 | s=s.replace(c+"", ""); 12 | } 13 | } 14 | public static void main(String[] args) { 15 | Scanner sc = new Scanner(System.in); 16 | System.out.println("Enter Your String: "); 17 | String str = sc.nextLine(); 18 | letterOccurence(str); 19 | sc.close(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /String/OnlyNumInString.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | public class OnlyNumInString { 4 | 5 | public static void checkNum(String s) { 6 | char[] c=s.toCharArray(); 7 | System.out.print("Number in given String: "); 8 | for(int i=0;i='0'&&c[i]<='9') 10 | System.out.print(c[i]+", "); 11 | } 12 | } 13 | public static void main(String[] args) { 14 | String s = "he9llo w2o5r7ld"; 15 | checkNum(s); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /String/PasswordValidation.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PasswordValidation { 6 | static final String SPECIAL_CHARACTERS = "!,#,$,%,^,&,*,|"; 7 | static String password; 8 | 9 | public static void main(String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | System.out.print("Enter the Password: "); 12 | password = sc.nextLine(); 13 | sc.close(); 14 | 15 | if (isValidPassword(password)) { 16 | System.out.println("Password is in correct format!"); 17 | } else { 18 | System.out.println("Not a valid password!"); 19 | } 20 | } 21 | 22 | public static boolean isValidPassword(String pass) { 23 | boolean loCase = false; 24 | int isDigit = 0; 25 | boolean spChar= false; 26 | 27 | if (pass.length() < 8) { 28 | return false; 29 | } else { 30 | for (int i = 0; i < pass.length(); i++) { 31 | if (pass.matches(".+[a-z].+")) { 32 | loCase = true; 33 | } 34 | if (pass.matches(".+[1-9].+")) { 35 | isDigit++; 36 | } 37 | if (SPECIAL_CHARACTERS.contains(pass.substring(i, i+1))) { 38 | spChar=true; 39 | } 40 | } 41 | } 42 | return loCase && (isDigit >= 2) && !spChar; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /String/ReverseSentence.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReverseSentence { 6 | 7 | public static String reverseSentence(String str) { 8 | String[] words=str.split(" "); 9 | StringBuilder revb = new StringBuilder(""); 10 | for(int j = words.length - 1; j >= 0 ; j--) 11 | { 12 | revb.append(words[j]); 13 | revb.append(" "); 14 | } 15 | return revb.toString(); 16 | } 17 | 18 | public static void main(String[] args) { 19 | Scanner sc=new Scanner(System.in); 20 | String s=sc.nextLine(); 21 | System.out.println("Result: " + reverseSentence(s)); 22 | sc.close(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /String/ReverseStringEx1.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | public class ReverseStringEx1 { 4 | 5 | public static String revLastAndFirst(String s) { 6 | String[] words=s.split(" "); 7 | for(String n:words) 8 | System.out.println(n); 9 | StringBuilder b = new StringBuilder(""); 10 | for(int i=0;i=0;i--) { 34 | rev.append(str.charAt(i)); 35 | } 36 | return rev.toString(); 37 | } 38 | 39 | public static void main(String[] args) { 40 | Scanner sc = new Scanner(System.in); 41 | System.out.println("Enter the String: "); 42 | String s = sc.nextLine(); 43 | sc.close(); 44 | String temp=""; 45 | String[] words=split(s); 46 | for(int i=0;i=0;i--) { 10 | rev.append(str.charAt(i)); 11 | } 12 | return rev.toString(); 13 | } 14 | 15 | public static void main(String[] args) { 16 | Scanner sc=new Scanner(System.in); 17 | System.out.println("Enter the String : "); 18 | String s=sc.nextLine(); 19 | System.out.println("Result: " + reverse(s)); 20 | sc.close(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /String/StringSort.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | 6 | public class StringSort { 7 | 8 | public static void sortByLength(String[] s) { 9 | Arrays.sort(s, new Comparator() { 10 | @Override 11 | public int compare(String s1, String s2) { 12 | return s1.length()-s2.length(); 13 | } 14 | }); 15 | } 16 | 17 | public static void sortByAlphabet(String[] s) { 18 | Arrays.sort(s, new Comparator() { 19 | @Override 20 | public int compare(String s1, String s2) { 21 | if(s1.charAt(0)=0;i--) 18 | { 19 | System.out.print(a[i]+" "); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /String/String_Tokenizer_2: -------------------------------------------------------------------------------- 1 | //swap_first_and_last_letter_of_each_word 2 | 3 | import java.util.*; 4 | public class swap_first_and_last_letter_of_each_word 5 | { 6 | public static void main() 7 | { 8 | Scanner ob=new Scanner(System.in); 9 | System.out.println("enter string"); 10 | String s=ob.nextLine(); 11 | StringTokenizer str=new StringTokenizer(s," ,.,?,!"); 12 | int n=str.countTokens(); 13 | String a[]=new String[n]; 14 | int i,b,k=0;String c[]=new String [n];String s1=""; 15 | for(i=0;i1) 24 | { 25 | char y=a[i].charAt(b-1); 26 | 27 | s1=y+a[i].substring(1,(b-1))+x; 28 | } 29 | else 30 | { 31 | s1=s1+x; 32 | } 33 | c[k]=s1; 34 | k++; 35 | s1=""; 36 | } 37 | for(i=0;i= 1; i--) { 9 | for (int j = 1; j <= len; j++) { 10 | if (j == i || (len+1 - i) == j) { 11 | System.out.print(st1.charAt(len/2+1 - i)); 12 | } else { 13 | System.out.print(' '); 14 | } 15 | } 16 | System.out.println(); 17 | } 18 | for (int i = 2; i <= len/2+1; i++) { 19 | for (int j = 1; j <= len; j++) { 20 | if (j == i || (len+1 - i) == j) { 21 | System.out.print(st1.charAt(len/2-1 + i)); 22 | } else { 23 | System.out.print(' '); 24 | } 25 | } 26 | System.out.println(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /String/sentencePalindrome.java: -------------------------------------------------------------------------------- 1 | /* 2 | Print the palindrome words present in the sentence and its frequency. 3 | */ 4 | 5 | 6 | import java.util.*; 7 | class sentencePalindrome 8 | { 9 | String str; 10 | void accept() 11 | { 12 | Scanner sc=new Scanner(System.in); 13 | System.out.println("Enter a sentence"); 14 | str=sc.nextLine(); 15 | str=str.toUpperCase(); 16 | } 17 | 18 | boolean isPalin(String s) 19 | {//checks if the word is Palindrome or not 20 | int l=s.length(); 21 | String rev=""; 22 | for(int i=l-1; i>=0; i--) 23 | { 24 | rev=rev+s.charAt(i); 25 | } 26 | if(rev.equals(s)) 27 | return true; 28 | else 29 | return false; 30 | } 31 | 32 | void main() 33 | { 34 | accept(); 35 | char ch=str.charAt(str.length()-1); 36 | if(ch=='.' || ch=='!' || ch=='?') 37 | { 38 | int freq=0; 39 | StringTokenizer st=new StringTokenizer(str," .!?"); 40 | int c=st.countTokens(); 41 | for(int i=1; i<=c; i++) 42 | { 43 | String w=st.nextToken(); 44 | boolean r=isPalin(w); 45 | if (r==true) 46 | { 47 | System.out.print(w+" "); 48 | freq++; 49 | } 50 | } 51 | System.out.println(); 52 | if(freq!=0) 53 | System.out.println("NUMBER OF PALINDROMIC WORDS =”+ freq); 54 | else 55 | System.out.println("NO PALINDROMIC WORDS"); 56 | } 57 | } 58 | else 59 | System.out.println("INVALID INPUT"); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /String/string2char.java: -------------------------------------------------------------------------------- 1 | package String; 2 | 3 | 4 | class string2char{ 5 | public static void main(String[] arg){ 6 | char[] c; 7 | String s="Hello world!"; 8 | //String st = "123456"; 9 | int n = 1234567; 10 | //converting store multiple digit integer into single digit array 11 | String temp = Integer.toString(n); 12 | int[] newarray = new int[temp.length()]; 13 | for(int i=0;i= 0; i--) { 12 | rev.append(str.charAt(i)); 13 | } 14 | String result = (str.equals(rev.toString())) ? "YES" : "NO"; 15 | System.out.println(result); 16 | s.close(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /StringPatterns/AlphabetPattern.java: -------------------------------------------------------------------------------- 1 | package StringPatterns; 2 | 3 | public class AlphabetPattern { 4 | public static void main(String abc[]) { 5 | for (int i = 1; i < 10; i++) // increase count 10 to 27 to print till "z" 6 | { 7 | String strChars = ""; 8 | int alphabet = 97; // ASCII value of A = 65 and a=97 9 | for (int j = 1; j <= i; j++) { 10 | strChars = strChars + (char) alphabet + " + "; 11 | alphabet++; 12 | } 13 | System.out.println("(" + strChars.substring(0, strChars.length() - 3) + ")"); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /StringPatterns/pattern26.java: -------------------------------------------------------------------------------- 1 | package StringPatterns; 2 | /* 3 | * P M 4 | R A 5 | O R 6 | G 7 | O R 8 | R A 9 | P M 10 | **/ 11 | public class pattern26 { 12 | public static void main(String[] args) { 13 | 14 | String str = "PROGRAM"; 15 | int x = str.length(); 16 | char[] chars = new char[x]; 17 | for (int i = 0; i < x; i++) { 18 | chars[i] = str.charAt(i); 19 | chars[x - 1 - i] = str.charAt(x - i - 1); 20 | for (int j = 0; j < x; j++) { 21 | if (j == i || j == (x - 1 - i)) { 22 | continue; 23 | } 24 | chars[j] = ' '; 25 | } 26 | System.out.println(new String(chars)); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /abstractKey/Example1.java: -------------------------------------------------------------------------------- 1 | package abstractKey; 2 | 3 | /* If a class is not having any abstract method 4 | * then it is possible to have no abstract method 5 | * 6 | * But if a class have one or more abstract method 7 | * then class should be abstract. 8 | * */ 9 | abstract class Car{ 10 | abstract void start();// Abstract method must be implemented 11 | // in subclasses 12 | } 13 | class Audi extends Car{ 14 | @Override 15 | void start(){ 16 | System.out.println("Audi Car "); 17 | } 18 | } 19 | class BMW extends Car{ 20 | @Override 21 | void start(){ 22 | System.out.println("BMW Car"); 23 | } 24 | } 25 | class Drive{ 26 | void drive(Car c) { 27 | c.start(); 28 | } 29 | } 30 | 31 | public class Example1 { 32 | 33 | public static void main(String[] args) { 34 | Drive d=new Drive(); 35 | d.drive(new Audi()); 36 | d.drive(new BMW()); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /boxing/BoxingUnBoxing.java: -------------------------------------------------------------------------------- 1 | package boxing; 2 | 3 | public class BoxingUnBoxing { 4 | public static void main(String args[]) { 5 | // Boxing 6 | byte a = 1; 7 | Byte byteobj = new Byte(a); 8 | int b = 10; 9 | Integer intobj = new Integer(b); 10 | float c = 18.6f; 11 | Float floatobj = new Float(c); 12 | double d = 250.5; 13 | Double doubleobj = new Double(d); 14 | char e = 'a'; 15 | Character charobj = e; 16 | System.out.println("Values of Boxed objects (printing as objects)"); 17 | System.out.println("Byte object byteobj: " + byteobj); 18 | System.out.println("Integer object intobj: " + intobj); 19 | System.out.println("Float object floatobj: " + floatobj); 20 | System.out.println("Double object doubleobj: " + doubleobj); 21 | System.out.println("Character object charobj: " + charobj); 22 | 23 | // UnBoxing 24 | byte bv = byteobj; 25 | int iv = intobj; 26 | float fv = floatobj; 27 | double dv = doubleobj; 28 | char cv = charobj; 29 | System.out.println("\nUnBoxed values (printing as data types)"); 30 | System.out.println("byte value, bv: " + bv); 31 | System.out.println("int value, iv: " + iv); 32 | System.out.println("float value, fv: " + fv); 33 | System.out.println("double value, dv: " + dv); 34 | System.out.println("char value, cv: " + cv); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /boxing/Example1.java: -------------------------------------------------------------------------------- 1 | package boxing; 2 | 3 | //In Java, values from -128 to 127 are cached, 4 | //so the same objects are returned. 5 | //The implementation of valueOf() uses cached 6 | //objects if the value is between -128 to 127. 7 | 8 | public class Example1 { 9 | 10 | public static String compareWc(Integer x,Integer y){ 11 | return (x==y)?"Same":"Not same"; 12 | } 13 | 14 | public static void main(String[] args) { 15 | Integer x = 40, y = 40; 16 | Integer a=400, b=400; 17 | Integer m=new Integer(40); 18 | Integer n=new Integer(40); 19 | System.out.println("40 , 40 Interger are: "+compareWc(x, y)); 20 | System.out.println("400 , 400 Interger are: "+compareWc(a, b)); 21 | System.out.println("40 , 40 Interger on explicit object creation : "+compareWc(m, n)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /collection/List/ArrayAsList.java: -------------------------------------------------------------------------------- 1 | package collection.List; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class ArrayAsList { 7 | 8 | public static void main(String[] args) { 9 | String[] strs = {"JAVA", "Python", "GoLang","PHP"}; 10 | System.out.println(Arrays.toString(strs)); 11 | 12 | List list = Arrays.asList(strs); 13 | System.out.println(list); 14 | 15 | List IntList = Arrays.asList(22, 44, 11, 33); 16 | System.out.println(IntList); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /collection/List/ArrayListExample.java: -------------------------------------------------------------------------------- 1 | package collection.List; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | public class ArrayListExample { 7 | 8 | public static void main(String[] args) { 9 | ArrayList list= new ArrayList<>(); 10 | list.add(10); 11 | list.add(8); 12 | list.add(17); 13 | list.add(14); 14 | System.out.println("Element at index 2: "+list.get(2)); 15 | Iterator itr=list.iterator(); 16 | while(itr.hasNext()) 17 | System.out.println(itr.next()); 18 | System.out.println("Removed Element: "+list.remove(2)); 19 | System.out.println("New Element at index 2: "+list.get(2)); 20 | System.out.println(list.parallelStream()); 21 | System.out.println(list.iterator()); 22 | list.set(2,15); 23 | System.out.println("Postion 2 Element Changed to "+list.get(2)); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /collection/List/LinkedListExample.java: -------------------------------------------------------------------------------- 1 | package collection.List; 2 | 3 | import java.util.LinkedList; 4 | import java.util.ListIterator; 5 | 6 | public class LinkedListExample { 7 | 8 | public static void main(String[] args) { 9 | LinkedList list= new LinkedList<>(); 10 | list.add(10); 11 | list.add(8); 12 | list.add(17); 13 | list.add(14); 14 | System.out.println("Element at index 2: "+list.get(2)); 15 | ListIterator itr=list.listIterator(); 16 | while(itr.hasNext()) 17 | System.out.println(itr.next()); 18 | System.out.println("Removed Element: "+list.remove(2)); 19 | System.out.println("New Element at index 2: "+list.get(2)); 20 | System.out.println(list.parallelStream()); 21 | System.out.println(list.iterator()); 22 | list.set(2,15); 23 | System.out.println("Postion 2 Element Changed to "+list.get(2)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /collection/List/ListToArray.java: -------------------------------------------------------------------------------- 1 | package collection.List; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ListToArray { 6 | 7 | public static void main(String[] args) { 8 | ArrayList list= new ArrayList<>(); 9 | list.add(10); 10 | list.add(8); 11 | list.add(17); 12 | list.add(14); 13 | Integer[] arr=list.toArray(new Integer[0]); 14 | for(Integer a:arr) { 15 | System.out.println(a.toString()); 16 | } 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /collection/List/VectorExample.java: -------------------------------------------------------------------------------- 1 | package collection.List; 2 | 3 | import java.util.Iterator; 4 | import java.util.Vector; 5 | 6 | public class VectorExample { 7 | 8 | public static void main(String[] args) { 9 | Vector list= new Vector<>(); 10 | list.add("Java"); 11 | list.add("C"); 12 | list.add("C++"); 13 | list.add("HTML"); 14 | System.out.println("Element at index 2: "+list.get(2)); 15 | Iterator itr=list.iterator(); 16 | while(itr.hasNext()) 17 | System.out.println(itr.next()); 18 | System.out.println("Removed Element: "+list.remove(2)); 19 | System.out.println("New Element at index 2: "+list.get(2)); 20 | System.out.println(list.parallelStream()); 21 | System.out.println(list.iterator()); 22 | list.set(2,"JavaScript"); 23 | System.out.println("Postion 2 Element Changed to "+list.get(2)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /collection/Map/HashMapExample.java: -------------------------------------------------------------------------------- 1 | package collection.Map; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | /* 6 | * Output- 7 | Value Associated with key 'two'= 2 8 | Keysets: [six, one, seven, two] 9 | Entry of map: [six=6, one=1, seven=7, two=2] 10 | Size of map: 4 11 | one : 1 12 | {six=6, one=1, seven=7, two=2} 13 | * 14 | */ 15 | public class HashMapExample { 16 | 17 | public static void main(String[] args) { 18 | Map m=new HashMap<>(); 19 | m.put("six", 6); 20 | m.put("one", 1); 21 | m.put("two", 2); 22 | m.put("seven", 7); 23 | System.out.print("Value Associated with key 'two'= "+m.get("two")+"\n"); 24 | System.out.print("Keysets: "+m.keySet()+"\n"); 25 | System.out.print("Entry of map: "+m.entrySet()+"\n"); 26 | System.out.println("Size of map: "+m.size()); 27 | if(m.containsKey("one")) { 28 | Integer x=m.get("one"); 29 | System.out.println("one : "+x); 30 | } 31 | if(!m.isEmpty()) 32 | System.out.println(m); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /collection/Map/LinekedHAshMapExample.java: -------------------------------------------------------------------------------- 1 | package collection.Map; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map; 5 | 6 | public class LinekedHAshMapExample { 7 | 8 | public static void main(String[] args) { 9 | Map m=new LinkedHashMap<>(); 10 | m.put("six", 6); 11 | m.put("one", 1); 12 | m.put("two", 2); 13 | m.put("seven", 7); 14 | System.out.print("Value Associated with key 'two'= "+m.get("two")+"\n"); 15 | System.out.print("Keysets: "+m.keySet()+"\n"); 16 | System.out.print("Entry of map: "+m.entrySet()+"\n"); 17 | System.out.println("Size of map: "+m.size()); 18 | if(m.containsKey("one")) { 19 | Integer x=m.get("one"); 20 | System.out.println("one : "+x); 21 | } 22 | if(!m.isEmpty()) 23 | System.out.println(m); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /collection/Map/TreeMapExample.java: -------------------------------------------------------------------------------- 1 | package collection.Map; 2 | 3 | import java.util.Map; 4 | import java.util.TreeMap; 5 | /* 6 | Value Associated with key 'two'= 2 7 | Keysets: [one, seven, six, two] 8 | Entry of map: [one=1, seven=7, six=6, two=2] 9 | Size of map: 4 10 | one : 1 11 | {one=1, seven=7, six=6, two=2} 12 | */ 13 | public class TreeMapExample { 14 | public static void main(String[] args) { 15 | Map m=new TreeMap<>(); 16 | //Treemap sorts according to key 17 | m.put("six", 6); 18 | m.put("one", 1); 19 | m.put("two", 2); 20 | m.put("seven", 7); 21 | System.out.print("Value Associated with key 'two'= "+m.get("two")+"\n"); 22 | System.out.print("Keysets: "+m.keySet()+"\n"); 23 | System.out.print("Entry of map: "+m.entrySet()+"\n"); 24 | System.out.println("Size of map: "+m.size()); 25 | if(m.containsKey("one")) { 26 | Integer x=m.get("one"); 27 | System.out.println("one : "+x); 28 | } 29 | if(!m.isEmpty()) 30 | System.out.println(m); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /collection/Queue/PriorityQueueExample.java: -------------------------------------------------------------------------------- 1 | package collection.Queue; 2 | 3 | import java.util.Iterator; 4 | import java.util.PriorityQueue; 5 | import java.util.Queue; 6 | 7 | /* 8 | C 9 | JavaScript 10 | Java 11 | Python 12 | */ 13 | 14 | public class PriorityQueueExample { 15 | public static void main(String[] args) { 16 | Queue list= new PriorityQueue<>(); 17 | list.add("Java"); 18 | list.add("Python"); 19 | list.add("C"); 20 | list.add("JavaScript"); 21 | Iterator itr=list.iterator(); 22 | while(itr.hasNext()) 23 | System.out.println(itr.next()); 24 | System.out.println("Removed Element: "+list.remove("C")); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /collection/Queue/QueueExample.java: -------------------------------------------------------------------------------- 1 | package collection.Queue; 2 | 3 | import java.util.ArrayDeque; 4 | import java.util.Iterator; 5 | import java.util.Queue; 6 | /* Java 7 | Python 8 | C 9 | JavaScript 10 | */ 11 | public class QueueExample { 12 | 13 | public static void main(String[] args) { 14 | Queue list= new ArrayDeque<>(); 15 | list.add("Java"); 16 | list.add("Python"); 17 | list.add("C"); 18 | list.add("JavaScript"); 19 | Iterator itr=list.iterator(); 20 | while(itr.hasNext()) 21 | System.out.println(itr.next()); 22 | System.out.println("Removed Element: "+list.remove("C")); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /collection/Set/Book.java: -------------------------------------------------------------------------------- 1 | package collection.Set; 2 | 3 | public class Book { 4 | private int price; 5 | private String title; 6 | public Book(int p,String t) { 7 | price=p; 8 | title=t; 9 | } 10 | public String getTitle() { 11 | return title; 12 | } 13 | public int getPrice() { 14 | return price; 15 | } 16 | @Override 17 | public int hashCode() { 18 | return price/100; 19 | } 20 | 21 | @Override 22 | public boolean equals(Object arg) { 23 | if(!(arg instanceof Book)) 24 | return false; 25 | Book b=(Book)arg; 26 | return title.equals(b.title) && price==b.price; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "[Title: "+title+", Price: "+price+"]"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /collection/Set/BookNameComparator.java: -------------------------------------------------------------------------------- 1 | package collection.Set; 2 | 3 | import java.util.Comparator; 4 | 5 | public class BookNameComparator implements Comparator { 6 | 7 | @Override 8 | public int compare(Book o1, Book o2) { 9 | return o1.getTitle().compareTo(o2.getTitle()); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /collection/Set/BookPriceComparator.java: -------------------------------------------------------------------------------- 1 | package collection.Set; 2 | 3 | import java.util.Comparator; 4 | 5 | public class BookPriceComparator implements Comparator { 6 | @Override 7 | public int compare(Book o1, Book o2) { 8 | return (o1.getPrice()o2.getPrice())?1:0; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /collection/Set/HashSetExample.java: -------------------------------------------------------------------------------- 1 | package collection.Set; 2 | 3 | 4 | import java.util.HashSet; 5 | import java.util.Iterator; 6 | import java.util.Set; 7 | /* Output- 8 | * 9 | * Java 10 | C 11 | JavaScript 12 | Python 13 | * 14 | * */ 15 | public class HashSetExample { 16 | 17 | public static void main(String[] args) { 18 | Set list= new HashSet<>(); 19 | list.add("Java"); 20 | list.add("Python"); 21 | list.add("C"); 22 | list.add("JavaScript"); 23 | Iterator itr=list.iterator(); 24 | while(itr.hasNext()) 25 | System.out.println(itr.next()); 26 | System.out.println("Removed Element: "+list.remove("C")); 27 | System.out.println(list.parallelStream()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /collection/Set/LinkedHashSetEx.java: -------------------------------------------------------------------------------- 1 | package collection.Set; 2 | 3 | /* 4 | * Output- 5 | * 6 | * Java 7 | Python 8 | C 9 | JavaScript 10 | Removed Element: true 11 | 12 | * 13 | * */ 14 | import java.util.Iterator; 15 | import java.util.LinkedHashSet; 16 | import java.util.Set; 17 | 18 | public class LinkedHashSetEx { 19 | 20 | public static void main(String[] args) { 21 | Set list= new LinkedHashSet<>(); 22 | list.add("Java"); 23 | list.add("Python"); 24 | list.add("C"); 25 | list.add("JavaScript"); 26 | Iterator itr=list.iterator(); 27 | while(itr.hasNext()) 28 | System.out.println(itr.next()); 29 | System.out.println("Removed Element: "+list.remove("C")); 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /collection/Set/TreeSetExample.java: -------------------------------------------------------------------------------- 1 | package collection.Set; 2 | 3 | import java.util.Iterator; 4 | import java.util.Set; 5 | import java.util.TreeSet; 6 | 7 | /* Output- 8 | C 9 | Java 10 | JavaScript 11 | Python 12 | */ 13 | 14 | public class TreeSetExample { 15 | 16 | public static void main(String[] args) { 17 | Set list= new TreeSet<>(); 18 | list.add("Java"); 19 | list.add("Python"); 20 | list.add("C"); 21 | list.add("JavaScript"); 22 | Iterator itr=list.iterator(); 23 | while(itr.hasNext()) 24 | System.out.println(itr.next()); 25 | System.out.println("Removed Element: "+list.remove("C")); 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /designPattern/Decorator/DecorateIcecream.java: -------------------------------------------------------------------------------- 1 | package designPattern.Decorator; 2 | 3 | public abstract class DecorateIcecream implements Icecream { 4 | Icecream icecream; 5 | } 6 | 7 | class Chocolate extends DecorateIcecream{ 8 | public Chocolate(Icecream icecream) { 9 | this.icecream = icecream; 10 | } 11 | 12 | @Override 13 | public String description() { 14 | return "Chocolate"; 15 | } 16 | 17 | @Override 18 | public Double cost() { 19 | return this.icecream.cost() + 5.0; 20 | } 21 | } 22 | 23 | class Peanuts extends DecorateIcecream{ 24 | public Peanuts(Icecream icecream) { 25 | this.icecream = icecream; 26 | } 27 | 28 | @Override 29 | public String description() { 30 | return "Peanuts"; 31 | } 32 | 33 | @Override 34 | public Double cost() { 35 | return this.icecream.cost() + 3.0; 36 | } 37 | } -------------------------------------------------------------------------------- /designPattern/Decorator/Icecream.java: -------------------------------------------------------------------------------- 1 | package designPattern.Decorator; 2 | 3 | public interface Icecream { 4 | public String description(); 5 | 6 | public Double cost(); 7 | } 8 | 9 | class Vanilla implements Icecream { 10 | 11 | @Override 12 | public String description() { 13 | return "Vanila Icecream!"; 14 | } 15 | 16 | @Override 17 | public Double cost() { 18 | return 15.0; 19 | } 20 | } 21 | 22 | class Strawberry implements Icecream { 23 | 24 | @Override 25 | public String description() { 26 | return "Strawberry Icecream!"; 27 | } 28 | 29 | @Override 30 | public Double cost() { 31 | return 20.0; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /designPattern/Decorator/TestDecorator.java: -------------------------------------------------------------------------------- 1 | package designPattern.Decorator; 2 | 3 | public class TestDecorator { 4 | 5 | public static void main(String[] args) { 6 | Icecream vanilla = new Vanilla(); 7 | System.out.println("Simple vanilla cost: " + vanilla.cost()); 8 | 9 | Icecream vanillaWithPeanuts = new Peanuts(vanilla); 10 | System.out.println("Vanilla with peanuts cost: " + vanillaWithPeanuts.cost()); 11 | 12 | Icecream vanillaWithChocolate = new Chocolate(vanilla); 13 | System.out.println("Vanilla with chocolate cost: " + vanillaWithChocolate.cost()); 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /designPattern/Factory/BigUFOEnemyShip.java: -------------------------------------------------------------------------------- 1 | package designPattern.Factory; 2 | 3 | public class BigUFOEnemyShip extends UFOEnemyShip { 4 | 5 | public BigUFOEnemyShip() { 6 | setName("Big UFO Enemy Ship"); 7 | setDamage(40.0); 8 | } 9 | } -------------------------------------------------------------------------------- /designPattern/Factory/EnemyShip.java: -------------------------------------------------------------------------------- 1 | package designPattern.Factory; 2 | 3 | public abstract class EnemyShip { 4 | 5 | private String name; 6 | // private double speed; 7 | // private double directionX; 8 | // private double directionY; 9 | private double amtDamage; 10 | 11 | public String getName() { 12 | return name; 13 | } 14 | 15 | public void setName(String newName) { 16 | name = newName; 17 | } 18 | 19 | public double getDamage() { 20 | return amtDamage; 21 | } 22 | 23 | public void setDamage(double newDamage) { 24 | amtDamage = newDamage; 25 | } 26 | 27 | public void followHeroShip() { 28 | System.out.println(getName() + " is following the hero"); 29 | } 30 | 31 | public void displayEnemyShip() { 32 | System.out.println(getName() + " is on the screen"); 33 | } 34 | 35 | public void enemyShipShoots() { 36 | System.out.println(getName() + " attacks and does " + getDamage() + " damage to hero"); 37 | } 38 | } -------------------------------------------------------------------------------- /designPattern/Factory/EnemyShipFactory.java: -------------------------------------------------------------------------------- 1 | package designPattern.Factory; 2 | 3 | //This is a factory thats only job is creating ships 4 | //By encapsulating ship creation, we only have one 5 | //place to make modifications 6 | 7 | public class EnemyShipFactory { 8 | // This could be used as a static method if we 9 | // are willing to give up subclassing it 10 | public EnemyShip makeEnemyShip(String newShipType) { 11 | if (newShipType.equals("U")) { 12 | return new UFOEnemyShip(); 13 | } else if (newShipType.equals("R")) { 14 | return new RocketEnemyShip(); 15 | } else if (newShipType.equals("B")) { 16 | return new BigUFOEnemyShip(); 17 | } else 18 | return null; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /designPattern/Factory/EnemyShipTesting.java: -------------------------------------------------------------------------------- 1 | package designPattern.Factory; 2 | 3 | import java.util.Scanner; 4 | 5 | public class EnemyShipTesting { 6 | 7 | public static void main(String[] args) { 8 | // Create the factory object 9 | EnemyShipFactory shipFactory = new EnemyShipFactory(); 10 | // Enemy ship object 11 | EnemyShip theEnemy = null; 12 | Scanner userInput = new Scanner(System.in); 13 | System.out.print("What type of ship? (U / R / B)"); 14 | if (userInput.hasNextLine()) { 15 | String typeOfShip = userInput.nextLine(); 16 | theEnemy = shipFactory.makeEnemyShip(typeOfShip); 17 | if (theEnemy != null) { 18 | doStuffEnemy(theEnemy); 19 | } else 20 | System.out.print("Please enter U, R, or B next time"); 21 | } 22 | userInput.close(); 23 | } 24 | 25 | public static void doStuffEnemy(EnemyShip anEnemyShip) { 26 | anEnemyShip.displayEnemyShip(); 27 | anEnemyShip.followHeroShip(); 28 | anEnemyShip.enemyShipShoots(); 29 | 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /designPattern/Factory/RocketEnemyShip.java: -------------------------------------------------------------------------------- 1 | package designPattern.Factory; 2 | 3 | public class RocketEnemyShip extends EnemyShip { 4 | 5 | public RocketEnemyShip() { 6 | setName("Rocket Enemy Ship"); 7 | setDamage(10.0); 8 | } 9 | } -------------------------------------------------------------------------------- /designPattern/Factory/UFOEnemyShip.java: -------------------------------------------------------------------------------- 1 | package designPattern.Factory; 2 | 3 | public class UFOEnemyShip extends EnemyShip { 4 | public UFOEnemyShip() { 5 | setName("UFO Enemy Ship"); 6 | setDamage(20.0); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /designPattern/Observer/Observer.java: -------------------------------------------------------------------------------- 1 | package designPattern.Observer; 2 | 3 | public interface Observer { 4 | public void update(String message); 5 | public void display(); 6 | } 7 | -------------------------------------------------------------------------------- /designPattern/Observer/Student.java: -------------------------------------------------------------------------------- 1 | package designPattern.Observer; 2 | 3 | public class Student implements Observer{ 4 | 5 | private String message; 6 | public Student(StudentData data) { 7 | data.registerObserver(this); 8 | } 9 | 10 | public void update(String message) { 11 | this.message = message; 12 | display(); 13 | } 14 | public void display() { 15 | System.out.println(this.message); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /designPattern/Observer/StudentData.java: -------------------------------------------------------------------------------- 1 | package designPattern.Observer; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class StudentData { 6 | private String message; 7 | private ArrayList observers; 8 | 9 | public StudentData() { 10 | observers = new ArrayList<>(); 11 | } 12 | public void registerObserver(Observer o) { 13 | observers.add(o); 14 | } 15 | public void removeObserver(Observer o) { 16 | observers.remove(o); 17 | } 18 | 19 | public void notifyObservers() { 20 | for(int i = 0; i < observers.size(); i++) { 21 | Observer obs = (Observer) observers.get(i); 22 | obs.update(message); 23 | } 24 | } 25 | 26 | public void setMessage(String msg) { 27 | this.message = msg; 28 | } 29 | 30 | public String getMessage() { 31 | return this.message; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /designPattern/Observer/TestObserver.java: -------------------------------------------------------------------------------- 1 | package designPattern.Observer; 2 | 3 | public class TestObserver { 4 | 5 | public static void main(String[] args) { 6 | StudentData data = new StudentData(); 7 | Observer std = new Student(data); 8 | String msg = "Hello World"; 9 | data.setMessage(msg); 10 | 11 | if(!data.getMessage().equals("")) { 12 | data.notifyObservers(); 13 | } 14 | 15 | data.removeObserver(std); 16 | if(!data.getMessage().equals("")) { 17 | data.notifyObservers(); 18 | } 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /designPattern/Singleton/Settings.java: -------------------------------------------------------------------------------- 1 | package designPattern.Singleton; 2 | 3 | public class Settings { 4 | private static Settings firstObject = null; 5 | private String font = "calibri"; 6 | private String color = "black"; 7 | 8 | private Settings() { 9 | } 10 | 11 | public static Settings getObject() { 12 | if (firstObject == null) { 13 | firstObject = new Settings(); 14 | } 15 | return firstObject; 16 | } 17 | 18 | public void setFont(String fontName) { 19 | this.font = fontName; 20 | } 21 | 22 | public void setColor(String colorName) { 23 | this.color = colorName; 24 | } 25 | 26 | public String getFont() { 27 | return this.font; 28 | } 29 | 30 | public String getColor() { 31 | return this.color; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /designPattern/Singleton/TestSetting.java: -------------------------------------------------------------------------------- 1 | package designPattern.Singleton; 2 | 3 | public class TestSetting { 4 | 5 | public static void main(String[] args) { 6 | Settings setting = Settings.getObject(); 7 | System.out.println("Setting Object Color: " + setting.getColor()); 8 | System.out.println("Font: " + setting.getFont()); 9 | Settings set1=Settings.getObject(); 10 | set1.setColor("Red"); 11 | System.out.println("Set Object Color: " + set1.getColor()); 12 | System.out.println("Font: " + set1.getFont()); 13 | System.out.println("Setting Object Color: " + setting.getColor()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /encapsulation/EncapsulationExample.java: -------------------------------------------------------------------------------- 1 | package encapsulation; 2 | 3 | class CricketScorer { 4 | private int score = 0; 5 | 6 | public int getScore() { 7 | return score; 8 | } 9 | 10 | public void addRuns(int score) { 11 | this.score = this.score + score; 12 | } 13 | 14 | public void six() { 15 | addRuns(6); 16 | } 17 | 18 | public void four() { 19 | addRuns(4); 20 | } 21 | 22 | public void single() { 23 | addRuns(1); 24 | } 25 | 26 | public void printScore() { 27 | System.out.println("Score : " + score); 28 | } 29 | 30 | } 31 | 32 | public class EncapsulationExample { 33 | public static void main(String[] args) { 34 | CricketScorer scorer = new CricketScorer(); 35 | scorer.four(); 36 | scorer.four(); 37 | scorer.single(); 38 | scorer.six(); 39 | scorer.six(); 40 | scorer.six(); 41 | scorer.printScore(); 42 | } 43 | } -------------------------------------------------------------------------------- /exceptionHandling/CheckedUncecked.java: -------------------------------------------------------------------------------- 1 | package exceptionHandling; 2 | 3 | public class CheckedUncecked { 4 | 5 | //Unchecked Exception 6 | //here we don't have to declare that method 7 | // will throw exception 8 | static void m1() { 9 | throw new ArithmeticException(); 10 | } 11 | //Checked Exception 12 | //Here We've to method , that this method will 13 | // throw exception 14 | static void m2() throws ClassNotFoundException { 15 | throw new ClassNotFoundException(); 16 | } 17 | public static void main(String[] args) { 18 | m1(); //for Unchecked exception 19 | // compiler doesn't check for exception 20 | try { 21 | m2(); 22 | }catch(ClassNotFoundException e) { 23 | System.out.println("Checked Exception handled."); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /exceptionHandling/CustomerAccount.java: -------------------------------------------------------------------------------- 1 | package exceptionHandling; 2 | 3 | class HighBalanceException extends Exception{ 4 | private static final long serialVersionUID = 1L; 5 | 6 | public HighBalanceException() { 7 | super("Customer Balance is more than limit."); 8 | } 9 | } 10 | class CustomerAccount { 11 | private int acctNum; 12 | private double balance; 13 | public static final double HIGH_CREDIT_LIMIT=2000.00; 14 | 15 | public CustomerAccount(int num,double bal) throws HighBalanceException { 16 | acctNum=num; 17 | balance=bal; 18 | if(balance>HIGH_CREDIT_LIMIT) { 19 | throw new HighBalanceException(); 20 | } 21 | System.out.println("Account Number: "+acctNum+"\nBalance: "+balance); 22 | } 23 | 24 | public static void main(String[] args) { 25 | try { 26 | new CustomerAccount(1000211, 3000.00); 27 | }catch(HighBalanceException h){ 28 | System.out.println(h.getMessage()); 29 | h.getStackTrace(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /exceptionHandling/DivideByZero.java: -------------------------------------------------------------------------------- 1 | package exceptionHandling; 2 | 3 | public class DivideByZero { 4 | 5 | public static int divide(int a,int b) { 6 | return a/b; 7 | } 8 | public static int computeDivison(int a, int b) { 9 | return divide(a,b); 10 | } 11 | public static void main(String[] args) { 12 | int a=1, b=0; 13 | try { 14 | int i=computeDivison(a, b); 15 | System.out.println("Divide Result: "+i); 16 | } 17 | catch(ArithmeticException e) { 18 | System.out.println(e.getMessage()); 19 | e.printStackTrace(); 20 | } 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /exceptionHandling/Example1.java: -------------------------------------------------------------------------------- 1 | package exceptionHandling; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Example1 { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc=new Scanner(System.in); 9 | System.out.print("Enter a number: "); 10 | int a; 11 | try { 12 | a=sc.nextInt(); 13 | }catch(Exception e) { 14 | System.out.println("Invalid Number!"); 15 | a=0; 16 | } 17 | System.out.println("Value of a: "+a); 18 | sc.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /generics/GenericClass.java: -------------------------------------------------------------------------------- 1 | package generics; 2 | 3 | public class GenericClass { 4 | 5 | private T t; 6 | public void add(T t) { 7 | this.t=t; 8 | } 9 | public T get() { 10 | return t; 11 | } 12 | public static void main(String[] args) { 13 | GenericClass iObj=new GenericClass<>(); 14 | GenericClass sObj=new GenericClass<>(); 15 | iObj.add(10); 16 | sObj.add(" Hello "); 17 | System.out.println("Integer value: "+iObj.get()); 18 | System.out.println("String value: "+sObj.get()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /generics/GenericMethod.java: -------------------------------------------------------------------------------- 1 | package generics; 2 | 3 | public class GenericMethod { 4 | 5 | public static void printArray(E[] array) { 6 | for (E ele : array) { 7 | System.out.print(ele + " "); 8 | } 9 | System.out.println(); 10 | } 11 | 12 | public static void main(String[] args) { 13 | Integer[] i = { 3, 5, 11, 7, 2 }; 14 | Double[] d = { 1.1, 2.3, 4.7, 2.5 }; 15 | Character[] ch = { 'H', 'E', 'L', 'L', 'O' }; 16 | // int[] in= {1,2,3,4}; 17 | // printArray(in); //CTE : only Non-primitive type data will be generic 18 | 19 | System.out.println("Integer Array Element: "); 20 | printArray(i); 21 | System.out.println("Double Array Element: "); 22 | printArray(d); 23 | System.out.println("Character Array Element: "); 24 | printArray(ch); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /hello.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /interfaceExamples/Aeroplane.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | public class Aeroplane implements Flyable { 4 | public void fly() { 5 | System.out.println("Aeroplane is flying"); 6 | } 7 | } -------------------------------------------------------------------------------- /interfaceExamples/Bird.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | public class Bird implements Flyable { 3 | public void fly() { 4 | System.out.println("Bird is flying"); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /interfaceExamples/Flyable.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | public interface Flyable { 4 | void fly(); 5 | } 6 | -------------------------------------------------------------------------------- /interfaceExamples/IntefaceRules.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | public class IntefaceRules { 4 | } 5 | 6 | interface ExampleInterface1 { 7 | // By default - public static final. No other modifier allowed 8 | // value1,value2,value3,value4 all are - public static final 9 | int value1 = 10; 10 | public int value2 = 15; 11 | public static int value3 = 20; 12 | public static final int value4 = 25; 13 | 14 | // private int value5 = 10;//COMPILER ERROR 15 | 16 | // By default - public abstract. No other modifier allowed 17 | void method1();// method1 is public and abstract 18 | // private void method6();//COMPILER ERROR! 19 | 20 | public static void m1() {//And we can provide definition for 21 | //static method (From java 8 it's possible) 22 | System.out.println("Static method"); 23 | } 24 | //Interface can have a default definition of method. 25 | //NEW FEATURE 26 | default void method5() { 27 | System.out.println("Method5"); 28 | } 29 | } 30 | 31 | interface ExampleInterface2 { 32 | void method2(); 33 | } 34 | interface test{ 35 | void te(); 36 | } 37 | // An interface can extend another interface 38 | // Class implementing SubInterface1 should 39 | // implement method3 and method1(from ExampleInterface1) 40 | interface SubInterface1 extends ExampleInterface1 { 41 | void method3(); 42 | } 43 | 44 | /* 45 | * //COMPILE ERROR IF UnCommented //Interface cannot extend a Class interface 46 | * SubInterface2 extends InterfaceRules{ void method3(); } 47 | */ 48 | 49 | /* A Class can implement multiple interfaces */ 50 | class SampleImpl implements ExampleInterface1, ExampleInterface2 { 51 | /* 52 | * A class should implement all the methods in an interface. If either of 53 | * method1 or method2 is commented, it would result in compilation error. 54 | */ 55 | 56 | public void method2() { 57 | System.out.println("Sample Implementation for Method2"); 58 | } 59 | 60 | public void method1() { 61 | System.out.println("Sample Implementation for Method1"); 62 | } 63 | 64 | } -------------------------------------------------------------------------------- /interfaceExamples/InterfaceExamples.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | public class InterfaceExamples { 4 | public static void main(String[] args) { 5 | Flyable flyable = new Bird(); 6 | flyable.fly(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /interfaceExamples/InterfaceWithMain.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | 4 | public class InterfaceWithMain { 5 | public static void main(String[] args) { 6 | Bird bird = new Bird(); 7 | bird.fly();// Bird is flying 8 | 9 | Aeroplane aeroplane = new Aeroplane(); 10 | aeroplane.fly();// Aeroplane is flying 11 | 12 | // An interface reference variable can hold 13 | // objects of any implementation of interface 14 | Flyable flyable1 = new Bird(); 15 | Flyable flyable2 = new Aeroplane(); 16 | flyable1.fly(); 17 | flyable2.fly(); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /interfaceExamples/SameType.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | public class SameType { 4 | } 5 | 6 | class SuperClass { 7 | 8 | } 9 | 10 | class SubClass extends SuperClass { 11 | 12 | } 13 | 14 | abstract class Abstract { 15 | abstract SuperClass method1(); 16 | } 17 | 18 | interface Interface { 19 | SuperClass method2(); 20 | } 21 | 22 | class ConcreteClass extends Abstract implements Interface { 23 | 24 | public SubClass method2() { 25 | return null; 26 | } 27 | 28 | SubClass method1() { 29 | return null; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /interfaceExamples/practice1.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | interface A { 4 | void myMethod(); 5 | } 6 | 7 | class B { 8 | public void myMethod() { 9 | System.out.println("My Method"); 10 | } 11 | } 12 | 13 | class C extends B implements A { 14 | 15 | } 16 | 17 | public class practice1 { 18 | public static void main(String[] args) { 19 | A a = new C(); 20 | a.myMethod(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /interfaceExamples/practice2.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | class Aa implements Bb 4 | { 5 | public int methodB(int i) 6 | { 7 | return i =+ i * i; //i = ((+ i) * i) 8 | } 9 | } 10 | 11 | abstract interface fgf{ 12 | 13 | } 14 | 15 | interface Bb 16 | { 17 | int methodB(int i); 18 | } 19 | 20 | 21 | public class practice2 22 | { 23 | public static void main(String[] args) 24 | { 25 | Bb b = new Aa(); 26 | 27 | System.out.println(b.methodB(2)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /interfaceExamples/practice3.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | interface One { 4 | String s = "FINAL"; 5 | String methodONE(); 6 | } 7 | 8 | interface Two { 9 | String methodONE(); 10 | } 11 | 12 | abstract class Three { 13 | String s = "NOT FINAL"; 14 | public abstract String methodONE(); 15 | } 16 | 17 | class Four extends Three implements One, Two { 18 | public String methodONE() { 19 | String s = super.s + One.s; 20 | return s; 21 | } 22 | } 23 | 24 | public class practice3 { 25 | public static void main(String[] args) { 26 | Four four = new Four(); 27 | System.out.println(four.methodONE()); 28 | // One one = four; 29 | System.out.println(One.s); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /interfaceExamples/practice4.java: -------------------------------------------------------------------------------- 1 | package interfaceExamples; 2 | 3 | interface Aaa { 4 | int methodA(); 5 | } 6 | 7 | interface Bbb { 8 | int methodB(); 9 | } 10 | 11 | interface Ccc { 12 | int methodC(); 13 | } 14 | interface Ddd{ 15 | 16 | } 17 | 18 | class D implements Aaa, Bbb, Ccc, Ddd { 19 | int i = 999 + 111; 20 | public int methodA() { 21 | i = +i / i; 22 | return i; 23 | } 24 | 25 | public int methodB() { 26 | i = -i * i; 27 | return i; 28 | } 29 | 30 | public int methodC() { 31 | i = ++i - --i; 32 | return i; 33 | } 34 | } 35 | 36 | public class practice4 { 37 | public static void main(String[] args) { 38 | D d = new D(); 39 | System.out.println(d.i); 40 | System.out.println(d.methodA()); 41 | System.out.println(d.methodB()); 42 | System.out.println(d.methodC()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /loopAndFlow/BreakExamples.java: -------------------------------------------------------------------------------- 1 | package loopAndFlow; 2 | 3 | public class BreakExamples { 4 | public static void main(String[] args) { 5 | // Break statement breaks out of the loop 6 | for (int i = 0; i < 10; i++) { 7 | System.out.print(i); 8 | if (i == 5) { 9 | break; 10 | } 11 | } 12 | // Output is 012345 13 | // Break can be used in a while also 14 | int i = 0; 15 | while (i < 10) { 16 | System.out.print(i); 17 | if (i == 5) { 18 | break; 19 | } 20 | i++; 21 | } 22 | // Output is 012345 23 | System.out.println(); 24 | // Break statement takes execution out of inner most loop 25 | for (int j = 0; j < 2; j++) { 26 | for (int k = 0; k < 10; k++) { 27 | System.out.print(j + "" + k); 28 | if (k == 5) { 29 | break;// Takes out of loop using k 30 | } 31 | } 32 | } 33 | // Output is 000102030405101112131415 34 | 35 | System.out.println(); 36 | 37 | // To get out of an outer for loop, label's need to be used 38 | outer: for (int j = 0; j < 2; j++) { 39 | for (int k = 0; k < 10; k++) { 40 | System.out.print(j + "" + k); 41 | if (k == 5) { 42 | break outer;// Takes out of loop using j 43 | } 44 | } 45 | } 46 | // Output is 000102030405 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /loopAndFlow/ContinueExamples.java: -------------------------------------------------------------------------------- 1 | package loopAndFlow; 2 | 3 | public class ContinueExamples { 4 | public static void main(String[] args) { 5 | // Continue statement skips rest of the statements in the loop 6 | // and starts next iteration 7 | for (int i = 0; i < 10; i++) { 8 | if (i == 5) { 9 | continue; 10 | } 11 | System.out.print(i); 12 | } 13 | // Output is 012346789 14 | // Not that the output does not contain 5 15 | 16 | System.out.println(); 17 | 18 | // Continue can be used in a while also 19 | int i = 0; 20 | while (i < 10) { 21 | i++; 22 | if (i == 5) { 23 | continue; 24 | } 25 | System.out.print(i); 26 | } 27 | // Output is 1234678910 28 | // Not that the output does not contain 5 29 | 30 | System.out.println(); 31 | 32 | // Continue statement takes execution to next iteration of inner most 33 | // loop 34 | for (int j = 0; j < 2; j++) { 35 | for (int k = 0; k < 10; k++) { 36 | if (k == 5) { 37 | continue;// skips to next iteration of k loop 38 | } 39 | System.out.print(j + "" + k); 40 | } 41 | } 42 | // Output is 000102030406070809101112131416171819 43 | // Not that the output does not contain 05,15 44 | 45 | System.out.println(); 46 | 47 | // To get out of an outer for loop, label's need to be used 48 | outer: for (int j = 0; j < 2; j++) { 49 | for (int k = 0; k < 10; k++) { 50 | if (k == 5) { 51 | continue outer;// skips to next iteration of j loop 52 | } 53 | System.out.print(j + "" + k); 54 | } 55 | } 56 | // Output is 00010203041011121314 57 | // Not that the output does not contain anything after 05 and also 15 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /loopAndFlow/DoWhileLoopExamples.java: -------------------------------------------------------------------------------- 1 | package loopAndFlow; 2 | 3 | public class DoWhileLoopExamples { 4 | public static void main(String[] args) { 5 | int count = 0; 6 | // do while also is used when it is not clear how many times loop has to 7 | // be executed. 8 | do { 9 | System.out.print(count); 10 | count++; 11 | } while (count < 5);// while this condn is true, loop is executed. 12 | // output is 01234 13 | 14 | // Code in do while is executed atleast once. 15 | count = 5; 16 | do { 17 | System.out.print(count); 18 | count++; 19 | } while (count < 5); 20 | // output is 5 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /loopAndFlow/ForLoopExample.java: -------------------------------------------------------------------------------- 1 | package loopAndFlow; 2 | 3 | public class ForLoopExample { 4 | public static void main(String[] args) { 5 | // Example of a For Loop 6 | for (int i = 0; i < 10; i++) { 7 | System.out.print(i); 8 | } 9 | // Output is 0123456789 10 | 11 | // For loop statement has 3 parts 12 | // Initialization => int i=0 13 | // Condition => i<10 14 | // Operation (Increment or Decrement usually) => i++ 15 | 16 | // There can be multiple statements in Initialization 17 | // or Operation seperated by commas 18 | for (int i = 0, j = 0; i < 10; i++, j--) { 19 | System.out.print(j); 20 | } 21 | // Output is 0-1-2-3-4-5-6-7-8-9 22 | 23 | // Enhanced For Loop 24 | int[] numbers = { 1, 2, 3, 4, 5 }; 25 | 26 | for (int number : numbers) { 27 | System.out.print(number); 28 | } 29 | // Output is 12345 30 | 31 | // Any of 3 parts in a for can be empty 32 | for (;;) { 33 | System.out.print("I will be looping for ever.."); 34 | } 35 | // Result : Infinite loop 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /loopAndFlow/IfElseExamples.java: -------------------------------------------------------------------------------- 1 | package loopAndFlow; 2 | 3 | public class IfElseExamples { 4 | public static void main(String[] args) { 5 | 6 | // Code inside If is executed only if the condition is true 7 | if (true) { 8 | System.out.println("Will be printed"); 9 | } 10 | 11 | // Statement inside this condition is not executed 12 | // if (false) { //Dead Code 13 | // System.out.println("Will NOT be printed");// Not executed 14 | // } 15 | 16 | // Lets look at an example 17 | int x = 5; 18 | 19 | if (x == 5) { 20 | System.out.println("x is 5");// executed since x==5 is true 21 | } 22 | 23 | x = 6; 24 | if (x == 5) { 25 | System.out.println("x is 5");// Not executed since x==5 is false 26 | } 27 | 28 | // If Else 29 | // If condition is true code in if is executed, else code in else is 30 | // executed 31 | 32 | int y = 10; 33 | 34 | if (y == 10) { 35 | System.out.println("Y is 10");// executed-condn y==10 is true 36 | } else { 37 | System.out.println("Y is Not 10"); 38 | } 39 | 40 | y = 11; 41 | 42 | if (y == 10) { 43 | System.out.println("Y is 10");// NOT executed 44 | } else { 45 | System.out.println("Y is Not 10");// executed-condn y==10 is false 46 | } 47 | 48 | // Nested else if 49 | // The code in the first if condition that is true is executed. 50 | // If none of the if conditions are true, then code in else is executed. 51 | int z = 15; 52 | if (z == 10) { 53 | System.out.println("Z is 10");// NOT executed 54 | } else if (z == 12) { 55 | System.out.println("Z is 12");// NOT executed 56 | } else if (z == 15) { 57 | System.out.println("Z is 15");// executed. Rest of the if else are 58 | // skipped. 59 | } else { 60 | System.out.println("Z is Something Else.");// NOT executed 61 | } 62 | 63 | z = 18; 64 | if (z == 10) { 65 | System.out.println("Z is 10");// NOT executed 66 | } else if (z == 12) { 67 | System.out.println("Z is 12");// NOT executed 68 | } else if (z == 15) { 69 | System.out.println("Z is 15");// NOT executed 70 | } else { 71 | System.out.println("Z is Something Else.");// executed 72 | } 73 | 74 | // Be very careful with formatting 75 | int number = 5; 76 | if (number < 0) // condn is false. So the line in if is not executed. 77 | number = number + 10; // Not executed 78 | number++; // This statement is not part of if 79 | System.out.println(number);// prints 6 80 | 81 | // Guess the output 82 | int k = 15; 83 | if (k > 20) { 84 | System.out.println(1); 85 | } else if (k > 10) { 86 | System.out.println(2); 87 | } else if (k < 20) { 88 | System.out.println(3); 89 | } else { 90 | System.out.println(4); 91 | } 92 | // Output is 2. Once a condition in nested-if-else is true the rest of 93 | // the code is not executed. 94 | 95 | // Guess the output 96 | int l = 15; 97 | 98 | if (l < 20) 99 | System.out.println("l<20"); 100 | if (l > 20) 101 | System.out.println("l>20"); 102 | else 103 | System.out.println("Who am I?"); 104 | // Output is "l<20" followed by "Who am I?" on next line. 105 | // else belong to the last if before it unless brackets ({}) are used. 106 | 107 | // Guess the output 108 | int m = 15; 109 | 110 | if (m > 20) 111 | if (m < 20) 112 | System.out.println("m>20"); 113 | else 114 | System.out.println("Who am I?"); 115 | // Nothing is printed to output. Above code is similar to code below 116 | if (m > 20) {// Condn is false. So, code in if is not executed 117 | if (m < 20) 118 | System.out.println("m>20"); 119 | else 120 | System.out.println("Who am I?"); 121 | } 122 | 123 | //int x1 = 0; 124 | // Condition in if should always be boolean 125 | // if(x1) {} //COMPILER ERROR 126 | // if(x1=0) {}//COMPILER ERROR. Using = instead of == 127 | 128 | boolean isTrue = false; 129 | if (isTrue == true) { 130 | System.out.println("TRUE TRUE");// Will not be printed 131 | } 132 | if (isTrue = true) { 133 | System.out.println("TRUE");// Will be printed. 134 | } 135 | // Condition is isTrue=true. This is assignment. Returns true. So, code 136 | // in if is executed. 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /loopAndFlow/SwitchExamples.java: -------------------------------------------------------------------------------- 1 | package loopAndFlow; 2 | 3 | public class SwitchExamples { 4 | public static void main(String[] args) { 5 | int number = 2; 6 | // Output of below switch is 2. 7 | // The case which is matched is executed. 8 | switch (number) { 9 | case 1: 10 | System.out.println(1); 11 | break; 12 | case 2: 13 | System.out.println(2); 14 | break; 15 | case 3: 16 | System.out.println(3); 17 | break; 18 | default: 19 | System.out.println("Default"); 20 | break; 21 | } 22 | 23 | // Notice that there is not break after each case. 24 | // If there is no break, then all the case's until we find break are 25 | // executed. 26 | number = 2; 27 | switch (number) { 28 | case 1: 29 | System.out.println(1); 30 | case 2: 31 | System.out.println(2); 32 | case 3: 33 | System.out.println(3); 34 | default: 35 | System.out.println("Default"); 36 | } 37 | // Output of above switch is 2 3 Default on separate lines. 38 | 39 | // Notice that there is not break after case 2. So, it fall through 40 | // to case 3. Prints the statement. After that, the break statement 41 | // takes the execution out of the switch. 42 | number = 2; 43 | switch (number) { 44 | case 1: 45 | System.out.println(1); 46 | break; 47 | case 2: 48 | case 3: 49 | System.out.println("Number is 2 or 3"); 50 | break; 51 | default: 52 | System.out.println("Default"); 53 | break; 54 | } 55 | // Output of above switch is Number is 2 or 3. 56 | 57 | // default is executed if none of the case's match 58 | number = 10; 59 | switch (number) { 60 | case 1: 61 | System.out.println(1); 62 | break; 63 | case 2: 64 | System.out.println(2); 65 | break; 66 | case 3: 67 | System.out.println(3); 68 | break; 69 | default: 70 | System.out.println("Default"); 71 | break; 72 | } 73 | // Output of above is Default 74 | 75 | // default doesn't need to be the last case in an switch. 76 | number = 10; 77 | switch (number) { 78 | default: 79 | System.out.println("Default"); 80 | break; 81 | case 1: 82 | System.out.println(1); 83 | break; 84 | case 2: 85 | System.out.println(2); 86 | break; 87 | case 3: 88 | System.out.println(3); 89 | break; 90 | } 91 | // Output of above is Default 92 | // No change in Result 93 | 94 | // Switch can be used only with char, byte, short, int or enum 95 | //long l = 15; 96 | /* 97 | * switch(l){//COMPILER ERROR. Not allowed. } 98 | */ 99 | 100 | // Case value should be a compile time constant. 101 | number = 10; 102 | switch (number) { 103 | // case number>5://COMPILER ERROR. Cannot have a condition 104 | // case number://COMPILER ERROR. Should be constant. 105 | } 106 | 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /loopAndFlow/WhileLoopExamples.java: -------------------------------------------------------------------------------- 1 | package loopAndFlow; 2 | 3 | public class WhileLoopExamples { 4 | public static void main(String[] args) { 5 | int count = 0; 6 | // While is used when it is not clear how many times loop has to be 7 | // executed. 8 | while (count < 5) {// while this condn is true, loop is executed. 9 | System.out.print(count); 10 | count++; 11 | }// output is 01234 12 | 13 | // Depending on the condition, code in while might not be executed at 14 | // all. 15 | count = 5; 16 | while (count < 5) {// condn is false. So, code in while is not executed. 17 | System.out.print(count); 18 | count++; 19 | }// Nothing is printed to output 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /polymorphism/Animal.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | public class Animal { 4 | public String shout() { 5 | return "Don't Know!"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /polymorphism/Cat.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | class Cat extends Animal { 4 | // This is method overriding. 5 | // Method shout in Animal is being overridden. 6 | 7 | // Overriding Class cannot reduce visibility of the method. 8 | // So, shout method cannot be declared as protected. 9 | public String shout() { 10 | return "Meow Meow"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /polymorphism/Dog.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | class Dog extends Animal { 4 | public String shout() { 5 | return "BOW BOW"; 6 | } 7 | 8 | public void run() { 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /polymorphism/TestPolymorphism.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | public class TestPolymorphism { 4 | 5 | public static void main(String[] args) { 6 | Animal animal1 = new Animal(); 7 | System.out.println(animal1.shout()); // Don't Know! 8 | 9 | Animal animal2 = new Dog(); 10 | 11 | // Reference variable type => Animal 12 | // Object referred to => Dog 13 | // Dog's bark method is called. 14 | System.out.println(animal2.shout()); // BOW BOW 15 | 16 | // Even though dog has a method run,it cannot be 17 | // invoked using super class reference variable 18 | // animal2.run();//COMPILE ERROR 19 | } 20 | } --------------------------------------------------------------------------------