├── .gitignore ├── README.md └── src └── javaFastTrack3 ├── day1 ├── ComputeTheChange.java ├── LogicErrors.java ├── StringClass.java ├── SumTheDigits.java ├── SyntaxImportance.java ├── TernaryOperator.java ├── Test.java └── VariablesTypesLiterals.java ├── day2 ├── FnraQuestion.java ├── ForWhileLoops.java ├── Palindrome.java ├── PalindromeWithQestionsMark.java ├── PalindromicPrimes.java ├── PrimeNumbers.java ├── ReviewDay1.java └── StringFunctions.java ├── day3 ├── Arrays.java ├── CallByValueCallByReferance.java ├── Day2Review.java ├── FindTheError.java ├── MethodIntroduction.java └── PalindromicPrimes.java ├── day4 ├── BigONotation.java ├── BinarySearch.java ├── CallByValueCallByRef.java ├── Day3Review.java ├── FindTheMostDuplicatedElement.java ├── SelectionSort.java └── SumTheArray.java ├── day5 ├── ChildOOP.java ├── ChildStaticClass.java ├── OOP.java ├── ReviewDay4.java ├── Singleton.java ├── StaticClass.java ├── StringBufferAndStringBuilder.java ├── Test.java └── TwoSum.java └── day6 ├── CollectionFramework.java ├── CountingTheWords.java ├── CropTheString.java ├── ReviewDay5.java ├── TestCollectionList.java ├── TestCollectionMap.java └── TestCollectionSet.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.o 8 | *.so 9 | 10 | # Packages # 11 | ############ 12 | # it's better to unpack these files and commit the raw source 13 | # git has its own built in compression methods 14 | *.7z 15 | *.dmg 16 | *.gz 17 | *.iso 18 | *.jar 19 | *.rar 20 | *.tar 21 | *.zip 22 | 23 | # Logs and databases # 24 | ###################### 25 | *.log 26 | *.sql 27 | *.sqlite 28 | 29 | # OS generated files # 30 | ###################### 31 | .DS_Store 32 | .DS_Store? 33 | ._* 34 | .Spotlight-V100 35 | .Trashes 36 | ehthumbs.db 37 | Thumbs.d 38 | .settings 39 | .classpath 40 | .project 41 | bin/ 42 | bin 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaFastTrack 2 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day1/ComputeTheChange.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day1; 2 | 3 | public class ComputeTheChange { 4 | public static void main(String[] args) { 5 | /* 6 | * Q2: ComputeChange/63 7 | * 8 | * Enter an amount, for example: 11.56 Your amount 11.56 consists of 11 dollars 9 | * // 100 cents 2 quarters // 25 cents 0 dimes // 10 cents 1 nickels // 5 cents 10 | * 1 pennies // a cent 11 | */ 12 | 13 | double num = 11.56; // if I multiply this guy by 100, i will get something like this 1156 14 | 15 | // two types if casting, explicit (manually), implicit(automatically) 16 | 17 | int numbersInCents = (int) (11.56 * 100); // returns XXXXX1156 18 | 19 | int dollars = numbersInCents / 100; // gives me the dollar amount >> returns 11 20 | 21 | numbersInCents = numbersInCents % 100; // this will return last two digits 22 | 23 | int quarters = numbersInCents / 25; // gives me the quarter amount >> returns 2 24 | 25 | numbersInCents = numbersInCents % 25; // returns the amount less than 25 cents >> returns 6 26 | 27 | int dimes = numbersInCents / 10; // gives me the dimes amount >> returns 0 28 | 29 | numbersInCents = numbersInCents % 10; // returns the amount less than 10 cents >> returns 6 30 | 31 | int nickels = numbersInCents / 5; // gives me the nickels amount >> returns 1 32 | 33 | int pennies = numbersInCents % 5; // gives me the penny ammount 34 | 35 | System.out.println("Dollar amount : " + dollars + "\nQuarter amount : " + quarters + "\nDime amount : " + dimes 36 | + "\nNickel amount : " + nickels + "\nPenny amount : " + pennies); 37 | 38 | // casting example 39 | double db = 20; 40 | int in = 10; 41 | 42 | in = (int) db; 43 | db = in; 44 | 45 | System.out.println("============================="); 46 | 47 | // integer overflow, underflow 48 | int num1 = 2147483647; 49 | int num2 = -2147483648; 50 | // overflow 51 | System.out.println(num1 + 2); 52 | // underflow 53 | System.out.println(num2 - 2); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day1/LogicErrors.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day1; 2 | 3 | public class LogicErrors { 4 | public static void main(String[] args) { 5 | // Q4: What is wrong in the following code? 6 | double score = 90.0; 7 | 8 | if (score >= 90.0) 9 | System.out.println("A"); 10 | else if (score >= 80.0) 11 | System.out.println("B"); 12 | else if (score >= 70.0) 13 | System.out.println("C"); 14 | else if (score >= 60.0) 15 | System.out.println("D"); 16 | else 17 | System.out.println("F"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day1/StringClass.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day1; 2 | 3 | public class StringClass { 4 | public static void main(String[] args) { 5 | // String is a special class in Java, it is an object, it is also a char array 6 | // String is an immutable class 7 | // Immutable means >> once you created an immutable object, you can not change it. 8 | 9 | String str = "hello"; 10 | System.out.println(str.hashCode()); 11 | str = "hola"; 12 | System.out.println(str.hashCode()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day1/SumTheDigits.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day1; 2 | 3 | public class SumTheDigits { 4 | public static void main(String[] args) { 5 | /* 6 | * Q1: (Sum the digits in an integer) Write a program that reads an integer 7 | * between 0 and 1000 and adds all the digits in the integer. For example, if an 8 | * integer is 932, the sum of all its digits is 14. 9 | * 10 | * NOTE: We will need to solve this without using loops. 11 | * 12 | * Hint: Use the % operator to extract digits, and use the / operator to remove 13 | * the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93. 14 | * 15 | * Here is a sample run: Enter a number between 0 and 1000: 999 The sum of the 16 | * digits is 27 17 | */ 18 | // 1 + 1 + 2 + 3 + 5 + 8 19 | int number = 6; // 9 + 9 + 9 = 27 20 | // 233 >> 2 + 3 + 3 = 8 21 | 22 | int thirdDigit = number / 100; // 9 >> add this numbr 23 | 24 | int remainingNumber = number % 100; // 87 >> this is not to add, this is just to find the next number to make operation 25 | 26 | int secondDigit = remainingNumber / 10; // 8 >> add this number 27 | 28 | remainingNumber = remainingNumber % 10; // 7 >> this is not to add, this is just to find the next number to make operation 29 | 30 | int firstDigit = remainingNumber / 1; // 7 add this number 31 | 32 | int sum = firstDigit + secondDigit + thirdDigit; 33 | 34 | System.out.println("The sum of the digits are: " + sum); 35 | 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day1/SyntaxImportance.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day1; 2 | 3 | public class SyntaxImportance { 4 | public static void main(String[] args) { 5 | 6 | /* 7 | * Q3: Importance of using proper syntax 8 | * 9 | * What will be the output of the following program? 10 | * 11 | * class IfExample{ 12 | * 13 | * public static void main(String s[]){ 14 | * 15 | * if( 1 < 2 ){ 16 | * 17 | * System.out.println("1 is less than 2"); } else 18 | * System.out.println("2 is less than 1"); 19 | * System.out.println("Hello From IfExample"); 20 | * 21 | * } } 22 | */ 23 | 24 | if (1 < 2) { 25 | System.out.println("1 is less than 2"); 26 | } else { 27 | System.out.println("2 is less than 1"); 28 | System.out.println("Hello From IfExample"); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day1/TernaryOperator.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day1; 2 | 3 | public class TernaryOperator { 4 | public static void main(String[] args) { 5 | // Q5: Ternary operator 6 | 7 | int applePrice = 5; 8 | int orangePrice = 10; 9 | 10 | System.out.println(applePrice > orangePrice ? "Apple wins" : "Orange wins"); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day1/Test.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day1; 2 | 3 | import java.util.*; 4 | 5 | public class Test { 6 | // What makes the Java special? 7 | // Java is platform independent language, write once, run everywhere 8 | // Java has a great OOP structure 9 | // There is no memory manipulation in the Java 10 | // Latest Java version >> Java 18 11 | // type that in the command line to see your java version >> java --version 12 | // What is the package that comes by default when you create a class. 13 | public static void main(String[] args) { 14 | 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day1/VariablesTypesLiterals.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day1; 2 | 3 | public class VariablesTypesLiterals { 4 | // Types 5 | // Two types we have, 6 | // 1 primitive type, 2 non-primitive type 7 | public static void main(String[] args) { 8 | int number = 10; 9 | // byte, short, int, char, float, long, double, boolean 10 | /* 11 | * Non-primitive data types are called reference types because they refer to 12 | * objects. 13 | * 14 | * The main difference between primitive and non-primitive data types are: 15 | * 16 | * Primitive types are predefined (already defined) in Java. Non-primitive types 17 | * are created by the programmer and is not defined by Java (except for String). 18 | * Non-primitive types can be used to call methods to perform certain 19 | * operations, while primitive types cannot. A primitive type has always a 20 | * value, while non-primitive types can be null. A primitive type starts with a 21 | * lowercase letter, while non-primitive types starts with an uppercase letter. 22 | * The size of a primitive type depends on the data type, while non-primitive 23 | * types have all the same size. 24 | * 25 | */ 26 | 27 | String str = "hello"; 28 | str.charAt(0); 29 | 30 | // A primitive type has always a 31 | // value, while non-primitive types can be null 32 | int[] arr = new int[10]; 33 | for (int i : arr) { 34 | System.out.print(i + " "); 35 | } 36 | 37 | //Variables: byte, short, int, char, float, long, double, boolean 38 | int variableName1 = 10; 39 | double variableName2 = 20.9; 40 | 41 | //Literals >> Numeric, String, Boolean, Char 42 | // You can not change the final class 43 | // You can not extend the final class 44 | System.out.print("whatever"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day2/FnraQuestion.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day2; 2 | 3 | public class FnraQuestion { 4 | public static void main(String[] args) { 5 | /* 6 | * /* Write a function which prints out the numbers from 1 to 30 but for numbers 7 | * which are a multiple of 3, print "FIN" instead of the number and for numbers 8 | * which are a multiple of 5, print "RA" instead of the number. for numbers 9 | * which are a multiple of both 3 and 5, print "FINRA" instead of the number. 10 | */ 11 | 12 | int number = 30; 13 | for (int i = 1; i <= number; i++) { 14 | // if the reminder is zero, that means, it can be divisible by three 15 | // start with the most unique scenario 16 | if(i % 15 == 0) { 17 | System.out.print("FINRA "); 18 | }else if (i % 5 == 0) { 19 | System.out.print("RA "); 20 | }else if(i % 3 == 0) { 21 | System.out.print("FIN "); 22 | }else { 23 | System.out.print(i + " "); 24 | } 25 | 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day2/ForWhileLoops.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day2; 2 | 3 | public class ForWhileLoops { 4 | public static void main(String[] args) { 5 | // after writing some code, you may be asked that 6 | // what is the time complexity of your code 7 | // What is BIG O notation tells you how effective your code is. 8 | // O(n), O(n^3), O(1) 9 | // inner loops are not good for the time complexity. 10 | int number = 10; 11 | for (int i = 0; i < number; i++) { 12 | System.out.println("objection hearsay"); 13 | } 14 | 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day2/Palindrome.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day2; 2 | 3 | import java.util.*; 4 | 5 | public class Palindrome { 6 | public static void main(String[] args) { 7 | // Palindromic numbers or strings 8 | // given a string, find if the string is a Palindrome or not. 9 | // mama, madam, 8899889, SAIPPUAKIVIKAUPPIAS 10 | 11 | Scanner input = new Scanner(System.in); 12 | 13 | System.out.println("Enter a string or a number to check the Palindrome"); 14 | 15 | String str = input.nextLine(); 16 | 17 | //999 18 | int low = 0; 19 | int high = str.length() - 1; 20 | 21 | Boolean isPalindrome = true; 22 | for (int i = 0; i < str.length(); i++) { 23 | if (str.charAt(low) != str.charAt(high)) { 24 | isPalindrome = false; 25 | break; 26 | } 27 | low++; 28 | high--; 29 | 30 | } 31 | 32 | System.out.println("Is that a Palindrome: " + isPalindrome); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day2/PalindromeWithQestionsMark.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PalindromeWithQestionsMark { 6 | public static void main(String[] args) { 7 | /* 8 | * write a function solution that, given a string STR of length N, returns any 9 | * palindrome which can be obtained by replacing all of the question marks in 10 | * STR by lowercase letters ('a'-'z'). if no palindrome can be obtained, the 11 | * function should return the string "no". 12 | */ 13 | // SA?PP?AKIVIKAUPPI?S 14 | // ba?aba??a 15 | 16 | Scanner input = new Scanner(System.in); 17 | System.out.println("Enter a string or a number to check the Palindrome"); 18 | String str = input.nextLine(); 19 | 20 | int low = 0; 21 | int high = str.length() - 1; 22 | 23 | //SA?PP?AKIVIKAUPPI?S 24 | Boolean isPalindrome = true; 25 | for (int i = 0; i < str.length(); i++) { 26 | if (str.charAt(low) != '?' && str.charAt(high) != '?') { 27 | if (str.charAt(low) != str.charAt(high)) { 28 | isPalindrome = false; 29 | break; 30 | } 31 | } 32 | low++; 33 | high--; 34 | 35 | } 36 | 37 | System.out.println("Is that a Palindrome: " + isPalindrome); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day2/PalindromicPrimes.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day2; 2 | 3 | public class PalindromicPrimes { 4 | public static void main(String[] args) { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day2/PrimeNumbers.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day2; 2 | 3 | public class PrimeNumbers { 4 | public static void main(String[] args) { 5 | 6 | // find the prime numbers till 100 7 | // it can only divisible by itself and 1 8 | // 2, 3, 5, 7, 11, 13, 17 etc. 9 | // 1, 2, 3, 4, 5, 6, 7, 8, 9 10 | 11 | int limit = 100; 12 | 13 | for (int prime = 2; prime < limit; prime++) { 14 | Boolean isPrime = true; 15 | for (int divisor = 2; divisor < prime; divisor++) { 16 | if (prime % divisor == 0) { 17 | isPrime = false; 18 | break; 19 | } 20 | } 21 | 22 | if (isPrime) { 23 | System.out.print(prime + " "); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day2/ReviewDay1.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day2; 2 | 3 | public class ReviewDay1 { 4 | public static void main(String[] args) { 5 | // String is a special class in Java, it is an object, it is also a char array 6 | // String is an immutable class 7 | // Immutable means >> once you created an immutable object, you can not change 8 | // it. 9 | int a = 10; // 16bytes 10 | String str = "hello"; 11 | 12 | System.out.println(str.hashCode()); 13 | 14 | str = "hola"; 15 | 16 | System.out.println(str.hashCode()); 17 | 18 | // here is the rule, whenever we create an object, we have to use "new" 19 | // keyword. But that rule does not apply for string. 20 | // using the "new" keyword creates a dif object in the memory 21 | // if we are creating 2 objects with the same value without using "new" keyword 22 | // it will be stored in the same place in the memory 23 | String str2 = "world"; 24 | String str3 = "world"; 25 | 26 | String str4 = new String("world"); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day2/StringFunctions.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | 6 | public class StringFunctions { 7 | public static void main(String[] args) { 8 | 9 | // charAt(), replace(), indexOf(), substring(), 10 | // toUpperCase, toLowerCase(), ***split()*** 11 | // interview question, change the plus with "+", and minus word with "-" 12 | String str = "plusminusplusminusplusminus"; 13 | 14 | // it returns the char of the giving index 15 | char ch = str.charAt(1); 16 | System.out.println(ch); 17 | 18 | //replace the word or char from a given string 19 | String str2 = str.replace("plus", "+"); 20 | str2 = str2.replace("minus", "-"); 21 | System.out.println(str2); 22 | 23 | //Given a time, return only the hours 24 | // 11:00, 9:0, 15:00 25 | String str3 = " 11:00 "; 26 | // trim method cuts the whitespaces from begn of the string and end of the string 27 | str3 = str3.trim(); 28 | // if it does not find index of given char, it returns -1 29 | int indexOfChar = str3.indexOf(":"); 30 | String str4 = str3.substring(0, indexOfChar); 31 | System.out.println("The hour is: " + str4); 32 | 33 | // given a name, please return only last name 34 | // Johnny EightKnight, Mary Light 35 | String str5 = "Johnny EightKnight"; 36 | int indexOfWhiteSpace = str5.indexOf(" "); 37 | String onlyLastName = str5.substring(indexOfWhiteSpace + 1, str5.length()); 38 | System.out.println(onlyLastName); 39 | 40 | //str = "plusminusplusminusplusminUS"; 41 | System.out.println(str.toLowerCase()); 42 | System.out.println(str.toUpperCase()); 43 | 44 | // Interview Question, sort the string, and make the first word start with uppercase letter. 45 | // This is a string. >> A is string this. 46 | 47 | // split method 48 | String str6 = "This is a string"; 49 | String[] strArray = str6.split(" "); // [This, is, a, string] 50 | System.out.println("========================"); 51 | // join() 52 | System.out.println(String.join("*", "This", "is", "a", "string")); 53 | 54 | // What is Collections 55 | // What is Collection 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day3/Arrays.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day3; 2 | 3 | public class Arrays { 4 | public static void main(String[] args) { 5 | // we do have to give a size in the arrays 6 | // but in the array list there no size required. 7 | // in the arrays we have initial value, but arraylist no initial value 8 | 9 | int[] arr = new int[10]; // >> int[0] = 0 10 | 11 | double[] db = new double[9]; 12 | 13 | int[] array = new int[50]; 14 | 15 | for (int i = 0; i < array.length; i++) { 16 | // returns something between 0 and 1, 0.9 >> 99.878897 17 | array[i] = (int)(Math.random() * 100); 18 | System.out.print(array[i] + " "); 19 | } 20 | 21 | // from the array above, find the largest number from that array 22 | // only return the last one 23 | System.out.println("The largest element is: " + findLargestNumber(array)); 24 | } 25 | 26 | // offer the interviewer to sort the array first 27 | static int findLargestNumber(int[] array){ 28 | 29 | int largest = array[0]; 30 | int indexOfLargest = 0; 31 | for (int i = 1; i < array.length; i++) { 32 | if(largest <= array[i]) { 33 | // now we are changing the value of the largest if the next index has a greater value 34 | largest = array[i]; 35 | indexOfLargest = i; 36 | } 37 | } 38 | System.out.println("\nThe index of largest is " + indexOfLargest); 39 | 40 | return largest; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day3/CallByValueCallByReferance.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day3; 2 | 3 | public class CallByValueCallByReferance { 4 | public static void main(String[] args) { 5 | 6 | int[] arr = { 5, 10, 15 }; 7 | 8 | int number = 10; 9 | 10 | System.out.println("The value before the method: " + number); 11 | 12 | changeTheValue(number); 13 | 14 | System.out.println("The value after the method: " + number); 15 | 16 | System.out.println("=================================================="); 17 | 18 | System.out.println("The value before the method: " + arr[0]); 19 | 20 | changeTheValue(arr); 21 | 22 | System.out.println("The value before the method: " + arr[0]); 23 | } 24 | 25 | // this is called call by VALUE, methods do not know the other methods variables. 26 | private static void changeTheValue(int number) { 27 | number = 1000; 28 | } 29 | 30 | // once you sent a reference to method, if you change the value, you are actually changing the value of that object 31 | private static void changeTheValue(int[] arr) { 32 | arr[0] = 100; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day3/Day2Review.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day3; 2 | 3 | public class Day2Review { 4 | public static void main(String[] args) { 5 | /* (Palindromic prime) A palindromic prime is a prime number and also palindro- 6 | * mic. For example, 131 is a prime and also a palindromic prime, as are 313 and 7 | * 757. Write a program that displays the first 100 palindromic prime numbers. 8 | * Dis- play 10 numbers per line, separated by exactly one space, as follows: 2 9 | * 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929 10 | */ 11 | 12 | int limit = 100; 13 | 14 | for (int prime = 2; prime < limit; prime++) { 15 | Boolean isPrime = true; 16 | for (int divisor = 2; divisor < prime; divisor++) { 17 | if (prime % divisor == 0) { 18 | isPrime = false; 19 | break; 20 | } 21 | } 22 | 23 | if (isPrime) { 24 | System.out.print(prime + " "); 25 | } 26 | } 27 | 28 | } 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | /* 58 | * Day 3 59 | * 60 | * Review day2 61 | * 62 | * Palindrome 63 | * 64 | * Methods/Procedures/Functions The purpose is to help main Break them into 65 | * small pieces, manage them, microservices 66 | * 67 | * Passing arguments by values Overloading Methods The scope of variables 68 | * ============================================= 69 | * 70 | * Method has two parts header >> modifier, return type, method name, method 71 | * parameter (no return type) body define the method call/invoke the method 72 | * declare the variable By convention, method body 50 lines 73 | * ============================================= 74 | * 75 | * Scope of Variables/Methods with modifiers Methods do not see other method's 76 | * variables 77 | * 78 | * ============================================= 79 | * 80 | * Find the mistake below; 81 | * 82 | * public static int camel(int n) { if (n > 0) return 1; else if (n == 0) return 83 | * 0; else if (n < 0) return –1; } 84 | * 85 | * ============================================= 86 | * 87 | * Writing Simple Method >> addition 88 | * 89 | * addNumbers(1, 30); 90 | * 91 | * ============================================= 92 | * 93 | * Passing the arguments 94 | * 95 | * public static void nPrintln(String message, int n) { for (int i = 0; i < n; 96 | * i++) System.out.println(message); } nPrintln("Hello", 3); nPrintln(3, 97 | * "Hello"); ============================================= 98 | * 99 | * Pass by value example 100 | * 101 | * Increment example: Before the call, x is 1 n inside the method is 2 After the 102 | * call, x is 1 103 | * 104 | * ============================================= 105 | ** 106 | * Test >> Pass By Value, always creates a copy 107 | * 108 | * //Before using the method 109 | * System.out.println("Before invoking the swap method, num1 is " + num1 + 110 | * " and num2 is " + num2); 111 | * 112 | * // Invoke the swap method to attempt to swap two variables swap(num1, num2); 113 | * 114 | * System.out.println("After invoking the swap method, num1 is " + num1 + 115 | * " and num2 is " + num2); 116 | * 117 | * ============================================= 118 | * 119 | * void method example 120 | * 121 | * ============================================= 122 | * 123 | * Find the GreatCommonDivisor of given two numbers 124 | * 125 | * ============================================= 126 | * 127 | * Prime Numbers and Palindromes with method. 128 | * 129 | * ============================================= 130 | * 131 | * RECURSIVE METHOD, STACKOVERFLOW ERROR 132 | * 133 | * in the output expections, another thread has been created 134 | * 135 | * ============================================= 136 | * 137 | * Method overloading, finding the max with max method. 138 | * 139 | * Overloaded methods must have different parameter lists. You cannot overload 140 | * methods based on different modifiers or return types. 141 | * 142 | * Return type and access modifier is not part of the method signature 143 | * 144 | * ============================================= 145 | * 146 | * 147 | * (Check password) Some websites impose certain rules for passwords. Write a 148 | * method that checks whether a string is a valid password. Suppose the password 149 | * rules are as follows: 150 | * 151 | * ■ A password must have at least eight characters. ■ A password consists of 152 | * only letters and digits. ■ A password must contain at least two digits. 153 | * 154 | * Write a program that prompts the user to enter a password and displays Valid 155 | * Password if the rules are followed or Invalid Password otherwise. 156 | * 157 | * ============================================= 158 | * 159 | * (Palindromic prime) A palindromic prime is a prime number and also palindro- 160 | * mic. For example, 131 is a prime and also a palindromic prime, as are 313 and 161 | * 757. Write a program that displays the first 100 palindromic prime numbers. 162 | * Dis- play 10 numbers per line, separated by exactly one space, as follows: 2 163 | * 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929 164 | * 165 | * ============================================= 166 | */ 167 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day3/FindTheError.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day3; 2 | 3 | public class FindTheError { 4 | public static void main(String[] args) { 5 | camel(10); 6 | } 7 | 8 | // Find the mistake below; 9 | 10 | public static int camel(int n) { 11 | 12 | if (n > 0) { 13 | return 1; 14 | } else if (n == 0) { 15 | return 0; 16 | } else { 17 | return -1; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day3/MethodIntroduction.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day3; 2 | 3 | // if the object is a apple pie, the class is the recipe of the apple pie 4 | public class MethodIntroduction { 5 | 6 | // constructor is a special method that has the same name with the Class name 7 | // and no return type 8 | // there are three constructors, we are doing overloading here. 9 | public MethodIntroduction() { 10 | } 11 | 12 | public MethodIntroduction(int number) { 13 | } 14 | 15 | public MethodIntroduction(String str) { 16 | } 17 | 18 | // default, public, private, protected 19 | // static methods belong to the class 20 | // non-static methods belong to the object 21 | // to be able to use a static method, first use the class name 22 | // to be able to use a non-static method, create an instance from that class 23 | public static void main(String[] args) { 24 | // functions and method is same thing 25 | // by convention a method should not be more than 50(max) lines of code. 26 | //method(10); 27 | 28 | // Math is a final class that we can not extend 29 | Math.addExact(10, 6); 30 | 31 | String str = "hello"; 32 | 33 | recursiveMethod(0); 34 | 35 | // method overloading example 36 | MethodIntroduction.sum(10, 6); 37 | MethodIntroduction.sum(10.5, 6.5); 38 | 39 | } 40 | 41 | public static int sum(int number1, int number2) { 42 | int sum = number1 + number2; 43 | return sum; 44 | } 45 | 46 | public static double sum(double number1, double number2) { 47 | // return type is not a part of the method signature 48 | double sum = number1 + number2; 49 | return sum; 50 | } 51 | 52 | public static void method1() { 53 | System.out.println("Void method does not have a return type"); 54 | } 55 | 56 | // if you call the method from the same method, it is called a recursive method 57 | // and we should use a condition to return back to the main. 58 | public static void recursiveMethod(int number) { 59 | 60 | if(number == 101) { 61 | return; 62 | } 63 | System.out.println(number++); 64 | 65 | recursiveMethod(number); 66 | } 67 | 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day3/PalindromicPrimes.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day3; 2 | 3 | public class PalindromicPrimes { 4 | public static void main(String[] args) { 5 | 6 | // find the prime numbers till 100 7 | // it can only divisible by itself and 1 8 | // 2, 3, 5, 7, 11, 13, 17 etc. 9 | // 1, 2, 3, 4, 5, 6, 7, 8, 9 10 | 11 | int limit = 100000; 12 | 13 | for (int prime = 2; prime < limit; prime++) { 14 | Boolean isPrime = true; 15 | for (int divisor = 2; divisor < prime; divisor++) { 16 | if (prime % divisor == 0) { 17 | isPrime = false; 18 | break; 19 | } 20 | } 21 | 22 | if (isPrime && isPalindrome(prime)) { 23 | // System.out.print(prime + " "); 24 | System.out.println("This is a palindromic prime" + prime); 25 | } 26 | } 27 | 28 | } 29 | 30 | private static boolean isPalindrome(int prime) { 31 | String str = prime + ""; 32 | int low = 0; 33 | int high = str.length() - 1; 34 | 35 | Boolean isPalindrome = true; 36 | for (int i = 0; i < str.length(); i++) { 37 | if (str.charAt(low) != str.charAt(high)) { 38 | isPalindrome = false; 39 | break; 40 | } 41 | low++; 42 | high--; 43 | 44 | } 45 | 46 | return isPalindrome ? true : false; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day4/BigONotation.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day4; 2 | 3 | public class BigONotation { 4 | public static void main(String[] args) { 5 | // Big O notation, this is an expression that states how efficent 6 | // your algorithm is. 7 | // O(n) >> that means, based on the number of ("n") data 8 | // your time complexity will increase 9 | 10 | double[] array1 = new double[1000000]; 11 | 12 | int sum = 0; 13 | 14 | // time complexity is >> O(n) 15 | // 1000 times here 16 | for (int i = 0; i < array1.length; i++) { 17 | array1[i] += 1; 18 | } 19 | 20 | // time complexity is >> O(n ^ 2) 21 | // 1000 x 1000 times here 22 | for (int i = 0; i < array1.length; i++) { 23 | for (int j = 0; j < array1.length; j++) { 24 | array1[j] += 1; 25 | } 26 | } 27 | 28 | // to get the value of the last index of the given array 29 | // what is going to be O(?) >> O(1) 30 | double lastIndex = array1[array1.length - 1]; 31 | 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day4/BinarySearch.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day4; 2 | 3 | import java.util.Arrays; 4 | 5 | public class BinarySearch { 6 | public static void main(String[] args) { 7 | int[] array = {3, 8, 13, 18, 43, 44, 58, 60, 68, 78, 81}; 8 | 9 | System.out.println(binarySearch(array, 43)); 10 | } 11 | 12 | static int binarySearch(int[] arr, int key) { 13 | 14 | int low = 0; 15 | int high = arr.length - 1; 16 | 17 | while (high >= low) { 18 | 19 | int mid = (low + high) / 2; 20 | 21 | if (key < arr[mid]) { 22 | high = mid - 1; 23 | } else if (key == arr[mid]) { 24 | return mid; 25 | } else { 26 | low = mid + 1; 27 | } 28 | } 29 | return low; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day4/CallByValueCallByRef.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day4; 2 | 3 | public class CallByValueCallByRef { 4 | public static void main(String[] args) { 5 | 6 | int[] arr = { 5, 10, 15 }; 7 | 8 | int number = 10; 9 | 10 | System.out.println("The value before the method: " + number); 11 | 12 | changeTheValue(number); 13 | 14 | System.out.println("The value after the method: " + number); 15 | 16 | System.out.println("=================================================="); 17 | 18 | System.out.println("The value before the method: " + arr[0]); 19 | 20 | changeTheValue(arr); 21 | 22 | System.out.println("The value before the method: " + arr[0]); 23 | } 24 | 25 | // this is called call by VALUE, methods do not know the other methods 26 | // variables. 27 | private static void changeTheValue(int number) { 28 | number = 1000; 29 | System.out.println("The value of the number inside of the method " + number); 30 | } 31 | 32 | // once you sent a reference to method, if you change the value, you are 33 | // actually changing the value of that object 34 | private static void changeTheValue(int[] arr) { 35 | arr[0] = 100; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day4/Day3Review.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day4; 2 | 3 | import java.util.*; 4 | 5 | public class Day3Review { 6 | public static void main(String[] args) { 7 | // Arrays stores the data in it. 8 | int[] number = new int[5]; 9 | 10 | int[] num1; 11 | 12 | // must be initialized 13 | // num1[0] = 8; 14 | 15 | // assign a random value to the array here 16 | for (int i = 0; i < number.length; i++) { 17 | number[i] = (int)(Math.random() * 100); 18 | } 19 | 20 | // print the array on the console 21 | for (int i = 0; i < number.length; i++) { 22 | System.out.print(number[i] + " "); 23 | } 24 | 25 | System.out.println("\n=========================="); 26 | 27 | // create an array and assign a random value to the array here 28 | int[] number2 = new int[5]; 29 | for (int i = 0; i < number2.length; i++) { 30 | number2[i] = (int) (Math.random() * 100); 31 | } 32 | 33 | for (int i = 0; i < number2.length; i++) { 34 | System.out.print(number2[i] + " "); 35 | } 36 | 37 | // copying the array, this does not make a copy of the array 38 | number2 = number; 39 | 40 | // this is how we can copy the arrays 41 | // reach out to the each index, and assign the value of the array to that index 42 | for (int i = 0; i < number2.length; i++) { 43 | number2[i] = number[i]; 44 | } 45 | 46 | // for each loops are easier to use for the print out statements 47 | for (int each : number2) { 48 | System.out.print(each + " "); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day4/FindTheMostDuplicatedElement.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day4; 2 | 3 | public class FindTheMostDuplicatedElement { 4 | public static void main(String[] args) { 5 | int[] arr = { 8, 1, 8, 4, 5, 4, 5, 8, 5, 5, 5, 5, 5, 7, 7, 8, 1, 1, 1 }; 6 | 7 | // this will count the duplicated numbers 8 | int count = 0; 9 | // this will store the largest duplicated number 10 | int maxCount = 0; 11 | int mostRecurringElement = arr[0]; 12 | 13 | // O(n ^ 2) 14 | for (int i = 0; i < arr.length; i++) { 15 | count = 0; 16 | for (int j = 0; j < arr.length; j++) { 17 | if (arr[i] == arr[j]) { 18 | count++; 19 | } 20 | } 21 | if (count > maxCount) { 22 | maxCount = count; 23 | mostRecurringElement = arr[i]; 24 | } 25 | } 26 | System.out.println(maxCount); 27 | System.out.println("the most recurring element is " + mostRecurringElement); 28 | //System.out.println(mostDuplicate(arr)); 29 | } 30 | 31 | // TODO : RE-check the code 32 | // static int mostDuplicate(int[] arr) { 33 | // int most = 0; 34 | // 35 | // for (int i = 0; i < arr.length; i++) { 36 | // int count = 1; 37 | // for (int j = 1; j < arr.length; j++) { 38 | // if (arr[i] == arr[j]) { 39 | // count++; 40 | // } 41 | // 42 | // } 43 | // if (count > 1) { 44 | // most = arr[i]; 45 | // } 46 | // } 47 | // return most; 48 | // } 49 | } 50 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day4/SelectionSort.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day4; 2 | 3 | import java.util.Arrays; 4 | 5 | public class SelectionSort { 6 | public static void main(String[] args) { 7 | int[] array = new int[50]; 8 | 9 | for (int i = 0; i < array.length; i++) { 10 | array[i] = (int) (Math.random() * 100); 11 | } 12 | System.out.println("Before sorting\n"); 13 | System.out.println(Arrays.toString(array)); 14 | // int[] array = { 6, 8, 3, 5, 2, 1 }; 15 | 16 | // assuming that the array's first index has the min value 17 | int minValue; 18 | int minValueIndex; 19 | 20 | // the O notation of that is O(n ^ 2) which is not efficient 21 | for (int i = 0; i < array.length; i++) { 22 | minValue = array[i]; 23 | minValueIndex = i; 24 | for (int j = i + 1; j < array.length; j++) { 25 | if (minValue > array[j]) { 26 | minValue = array[j]; 27 | minValueIndex = j; 28 | } 29 | } 30 | 31 | // this is swapping 32 | if (minValue != array[i]) { 33 | int temp = array[i]; 34 | array[i] = minValue; 35 | array[minValueIndex] = temp; 36 | } 37 | 38 | } 39 | System.out.println("\nAfter sorting\n"); 40 | // for (int i : array) { 41 | // System.out.print(i + " "); 42 | // } 43 | 44 | System.out.println(Arrays.toString(array)); 45 | } 46 | } 47 | // to practice swapping 48 | // int a = 6; 49 | // int b = 10; 50 | // assign the a's valus to the b 51 | // and assign the b's value to the a 52 | // int temp = a; returns 6 53 | // a = b; returns 10 54 | // b = temp; returns 6 -------------------------------------------------------------------------------- /src/javaFastTrack3/day4/SumTheArray.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day4; 2 | 3 | public class SumTheArray { 4 | public static void main(String[] args) { 5 | // given an array, sum the value of the elements of the array 6 | int[] number = {3, 5, 6, 7}; 7 | 8 | // this is not an array, this is an integer 9 | // sumArray(number[0]); 10 | 11 | System.out.println(sumArray(number)); 12 | } 13 | 14 | 15 | static int sumArray(int[] thisArray) { 16 | 17 | int sum = 0; 18 | 19 | for (int each : thisArray) { 20 | sum += each; 21 | } 22 | 23 | // for (int i = 0; i < thisArray.length; i++) { 24 | // sum += thisArray[i]; 25 | // } 26 | 27 | return sum; 28 | } 29 | 30 | 31 | static int sumArray(int num) { 32 | return 0; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day5/ChildOOP.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day5; 2 | 3 | /* Inheritance (is a relationship), parent class and child class and we use extends keyword, we only extend one class 4 | * Encapsulation >> hiding the data (singleton design patter example) 5 | * Polymorphism >> showing a type with its super type is called Polymorphism, showing the ArrayList with its super type. 6 | * Abstraction >> Abstract class, we do not want anybody to create any instance from that class. 7 | * Interfaces (has a relationship), adding more or new feature to the classes. (it is also called dependency injection) 8 | * */ 9 | public class ChildOOP extends OOP { 10 | 11 | String str1 = "hello"; 12 | 13 | // this is the constractor 14 | private ChildOOP() { 15 | 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | // Polymorphism happens here. 21 | OOP obj = new ChildOOP(); 22 | 23 | System.out.println(obj.getStr()); 24 | obj.setStr("world"); 25 | System.out.println(obj.getStr()); 26 | System.out.println(obj.toString()); 27 | } 28 | 29 | public String toString() { 30 | return "this is from ChildOOP class" + str1; 31 | } 32 | 33 | // What are the differences between the abstract classes and interfaces? 34 | // the purpose of that question is not to hear syntax differences. 35 | // we do not anybody to create any instance from abstract class >> ONLY PURPOSE 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day5/ChildStaticClass.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day5; 2 | 3 | public class ChildStaticClass extends StaticClass { 4 | 5 | public static void main(String[] args) { 6 | StaticClass obj = new StaticClass(); 7 | 8 | double num = obj.number1; 9 | 10 | // this a wrong usage, access anything static with a static way. 11 | double num1 = obj.number; 12 | // Using the class name is a static way reaching out the static members. 13 | double num2 = StaticClass.number; 14 | 15 | // if you are using the reference, we can access everything including static 16 | // members 17 | // but if we are using a class name, we can only access to the static members. 18 | // to be able to access any non-static members, create an instance from that 19 | // class. 20 | // STAR method to answer any interview question 21 | StaticClass.method(); 22 | 23 | // create an instance from the class if you want to use the non-static members 24 | // method1(); 25 | 26 | } 27 | 28 | void method1() { 29 | System.out.println("This is a non static method from the ChildOOP class"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day5/OOP.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day5; 2 | 3 | public class OOP extends Object { 4 | 5 | private String str = "hello"; 6 | 7 | public OOP() { 8 | 9 | } 10 | 11 | public String getStr() { 12 | return str; 13 | } 14 | 15 | public void setStr(String args) { 16 | this.str = args; 17 | } 18 | 19 | public String toString() { 20 | return "this is from OOP class" + str; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day5/ReviewDay4.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day5; 2 | 3 | public class ReviewDay4 { 4 | // Selection Sort 5 | // (Sorting algorithms) 6 | // Searching algorithms 7 | // Find the most duplicated number 8 | // Take home assessments will be like... count the words of a given string 9 | // The red fox is great because the red fox is great. 10 | // the: 2 11 | // red: 2 12 | // fox: 2 etc.. 13 | // given string, crop string with the given index, but do not crop from half of the word 14 | // The red fox is great. (str, 14) 15 | } 16 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day5/Singleton.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day5; 2 | 3 | public class Singleton { 4 | 5 | // The purpose of the singleton is to create one instance from that class 6 | // how do we achieve that 7 | // use encapsulation 8 | 9 | private final Singleton obj = new Singleton(); 10 | 11 | // private final Singleton obj = null; 12 | 13 | // if we do not make the constractor private, it will allow users to create 14 | // instance from that. 15 | private Singleton() { 16 | 17 | } 18 | 19 | public Singleton getInstance() { 20 | return obj; 21 | } 22 | 23 | // public Singleton getInstance() { 24 | // if(this.obj == null) { 25 | // return new Singleton(); 26 | // }else { 27 | // return obj; 28 | // } 29 | // } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day5/StaticClass.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day5; 2 | 3 | public class StaticClass { 4 | // till Object Oriented Programming we are working with functional programming.(array, primitive types, methods, etc...) 5 | // After the OOP, we are working with procedural programming. We are making the framework much easier to maintain with using OOP. 6 | // Great OOP structure makes the Java special. 7 | // keyword is STATIC, after we stop using static keyword, we start to work on OOP. 8 | 9 | /* 10 | * What is static? STATIC is the most important keyword in Java. if anything is 11 | * static that means, that member belongs to that class. if anything is 12 | * non-static that means, that member belongs to the object. we can have a 13 | * static variable, static method, static blocks. once we do something "static" 14 | * that means it is going to be same in every part of the project and by 15 | * convention, if you are making something static, make it "final". financial 16 | * framework, value of the dollar or tax value must be final static. avoid using 17 | * avoid using static keyword if you are working with an object. 18 | * 19 | * We have some classes with static purpose like a Math class. Every member is a static, and we use class name to reach them out. 20 | */ 21 | 22 | //this part is called DATA FIELD, in general, it locates between the class name's block and constructor. 23 | static double number = 3.14; 24 | 25 | double number1 = 3.14; 26 | 27 | public StaticClass() { 28 | 29 | } 30 | 31 | static void method() { 32 | System.out.println("This is a static method"); 33 | } 34 | 35 | // this belongs to the OBJECT, that means, I have to create an instance to be able to use it. 36 | void method1() { 37 | System.out.println("This is a non-static method"); 38 | } 39 | 40 | public static void main(String[] args) { 41 | 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day5/StringBufferAndStringBuilder.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day5; 2 | 3 | public class StringBufferAndStringBuilder { 4 | public static void main(String[] args) { 5 | // not immutable 6 | // strbuffer is trying to be synchronized 7 | // strbuilder is not trying to be synchronized which means, strbuilder is faster 8 | // than strbuffer 9 | StringBuffer strbuffer = new StringBuffer(""); 10 | StringBuilder strbuilder = new StringBuilder(""); 11 | int a = 300000; 12 | 13 | // immutable 14 | String strstring = new String(""); 15 | //////////////////////////////////////////////// 16 | long exeStartTime = System.currentTimeMillis(); 17 | for (int i = 0; i < a; i++) { 18 | strbuffer = strbuffer.append("a"); 19 | } 20 | long exeEndTime = System.currentTimeMillis(); 21 | long totalExecutionTime = exeEndTime - exeStartTime; 22 | System.out.println("Stringbuffer: " + totalExecutionTime); 23 | //////////////////////////////////////////////// 24 | exeStartTime = System.currentTimeMillis(); 25 | for (int i = 0; i < a; i++) { 26 | strbuilder = strbuilder.append("a"); 27 | } 28 | exeEndTime = System.currentTimeMillis(); 29 | totalExecutionTime = exeEndTime - exeStartTime; 30 | System.out.println("Stringbuilder: " + totalExecutionTime); 31 | //////////////////////////////////////////////// 32 | exeStartTime = System.currentTimeMillis(); 33 | for (int i = 0; i < a; i++) { 34 | strstring += "a"; 35 | } 36 | exeEndTime = System.currentTimeMillis(); 37 | totalExecutionTime = exeEndTime - exeStartTime; 38 | System.out.println("String: " + totalExecutionTime); 39 | //////////////////////////////////////////////// 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day5/Test.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day5; 2 | 3 | public interface Test { 4 | 5 | 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day5/TwoSum.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day5; 2 | 3 | // practice with the "leetcode" 4 | public class TwoSum { 5 | public static void main(String[] args) { 6 | // given an integer array, and a target number 7 | // return the only two indexes and numbers that give you the target number by 8 | // adding them 9 | 10 | int[] arr = { 7, 1, 4, 6, 8 }; 11 | int target = 11; 12 | 13 | int[] sol = twoSum(arr, target); 14 | 15 | System.out.println( 16 | "The indexes are " + sol[0] + " " + sol[1] + " and values are " + arr[sol[0]] + " " + arr[sol[1]]); 17 | 18 | } 19 | 20 | static int[] twoSum(int[] arr, int number) { 21 | 22 | // O(n ^ 2) >> this is the time complexity 23 | // brute-force solution 24 | int firstIndex = 0; 25 | int secondIndex = 0; 26 | for (int i = 0; i < arr.length; i++) { 27 | for (int j = i + 1; j < arr.length; j++) { 28 | if (arr[i] + arr[j] == number) { 29 | firstIndex = i; 30 | secondIndex = j; 31 | 32 | } 33 | } 34 | } 35 | 36 | return new int[] { firstIndex, secondIndex }; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day6/CollectionFramework.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day6; 2 | 3 | public class CollectionFramework { 4 | /* 5 | * Collection is a framework that keeps the interfaces, abstract classes, and concrete/regular classes. 6 | * List, Set, Queue 7 | * 8 | * Collections is a class that provides us method for Collection framework 9 | * */ 10 | } 11 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day6/CountingTheWords.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day6; 2 | 3 | import java.util.*; 4 | 5 | public class CountingTheWords { 6 | public static void main(String[] args) { 7 | String text = "The quick brown fox and the quick blue hare"; 8 | 9 | // The quick = 2 10 | // quick brown = 1 11 | // brown fox = 1 etc... 12 | 13 | // using the regex here to split the symbols from the text 14 | String[] str = text.split(" "); 15 | 16 | System.out.println(Arrays.toString(str)); 17 | 18 | List list = new ArrayList<>(); 19 | for (int i = 0; i < str.length; i++) { 20 | // we are adding the elements of the str to the list 21 | list.add(str[i]); 22 | } 23 | 24 | // creating the map structure to count the words and assign them to the key 25 | Map map = new LinkedHashMap<>(); 26 | System.out.println(map); 27 | for (int i = 0; i < list.size(); i++) { 28 | String key = list.get(i).toLowerCase(); 29 | 30 | if (!map.containsKey(list.get(i))) { 31 | map.put(key, 1); 32 | } else { 33 | map.put(key, map.get(key) + 1); 34 | } 35 | } 36 | 37 | System.out.println(map); 38 | 39 | Set> entryset = map.entrySet(); 40 | System.out.println(entryset); 41 | 42 | for (Map.Entry entry : entryset) { 43 | System.out.print(entry.getKey() + "=" + entry.getValue() + " "); 44 | } 45 | 46 | System.out.println(); 47 | 48 | 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day6/CropTheString.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day6; 2 | 3 | import java.util.*; 4 | 5 | public class CropTheString { 6 | public static void main(String[] args) { 7 | // given a string, crop the string without cropping a word based on the given 8 | // index 9 | // Today is Sunday, a sunny Sunday. (Index 10) 10 | // 11 | String str = "Today is Sunday a sunny Sunday"; 12 | System.out.println(cropTheString(str, 30)); 13 | } 14 | 15 | static String cropTheString(String str1, int A) { 16 | 17 | // almost every string manipulation during the interview you are supposed to use 18 | // the split() method 19 | 20 | // to get the every word of that given string as an array, use the split method 21 | String[] str = str1.split(" "); 22 | 23 | System.out.println(Arrays.toString(str)); 24 | 25 | // join method is important 26 | List list = new ArrayList<>(); 27 | Collections.addAll(list, str); 28 | System.out.println(list); 29 | 30 | int len = str1.length(); 31 | System.out.println(len); 32 | if (len <= A) { 33 | return str1; 34 | } else { 35 | for (int i = list.size() - 1; i >= 0; i--) { 36 | list.remove(i); 37 | // join method did solve problem 38 | if (String.join(" ", list).length() <= A) { 39 | return String.join(" ", list); 40 | } 41 | } 42 | } 43 | 44 | return String.join(" ", list); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day6/ReviewDay5.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day6; 2 | 3 | public class ReviewDay5 { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day6/TestCollectionList.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day6; 2 | 3 | import java.util.*; 4 | 5 | public class TestCollectionList { 6 | public static void main(String[] args) { 7 | /* 8 | * ArrayList and LinkedList are defined under the List interface. 9 | * 10 | * insertion ordered, you can have duplicates. 11 | * 12 | * What is the difference between the ArrayList and Arrays. 13 | * 14 | * Arrays >> must have a size, it keeps primitive types and object, to print use 15 | * loops, or convert to toString, arr[i] ArrayList >> you do not have to give a 16 | * size, only object type, we can print the arraylist without converting to the 17 | * string or using a loop, list.get(i) 18 | * 19 | * list.get(), list.contains(), list.remove(), list.add(), list.size(), list.indexOf() 20 | * Collections.sort(list) 21 | * 22 | */ 23 | 24 | List list = new ArrayList<>(); 25 | 26 | // add the new data 27 | list.add("Denver"); 28 | list.add(0, "Sabir"); 29 | list.add("Denver"); 30 | list.add("Houston"); 31 | list.add("LA"); 32 | list.add("New York"); 33 | list.add("New York"); 34 | list.add("New York"); 35 | list.add("New York"); 36 | list.add("San Antonio"); 37 | 38 | // swap the data 39 | list.set(0, "Robinson"); 40 | 41 | // remove the data 42 | list.remove("Denver"); 43 | 44 | // remove all the elements you do not want to have 45 | for (int i = 0; i < list.size(); i++) { 46 | if (list.get(i).equals("Denver")) { 47 | list.remove(i); 48 | } 49 | // do not remove the index while working on for loops, because the size is 50 | // getting smaller 51 | // list.remove(list.indexOf("Denver")); 52 | } 53 | 54 | // checks if you have that element, returns boolean 55 | System.out.println(list.contains("New ")); 56 | 57 | // create a logic that removes the duplicate elements from the list 58 | // how to do that? 59 | List list1 = new ArrayList<>(); 60 | for (int i = 0; i < list.size(); i++) { 61 | if (!list1.contains(list.get(i))) { 62 | list1.add(list.get(i)); 63 | } 64 | } 65 | 66 | System.out.println(list); 67 | System.out.println(list1); 68 | 69 | // returns the last index of given element, returns an integer 70 | System.out.println(list.lastIndexOf("New York")); 71 | 72 | Collections.sort(list); 73 | System.out.println(list); 74 | 75 | System.out.println(Collections.frequency(list, "New York")); 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day6/TestCollectionMap.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day6; 2 | 3 | import java.util.*; 4 | 5 | public class TestCollectionMap { 6 | public static void main(String[] args) { 7 | /* 8 | * Key/value pairs, it stores ("string": 10) (10: "string")("string": "string") 9 | * 10 | * HashMap, LinkedHashMap, TreeMap 11 | * 12 | * HashMap >> not insertion ordered or sorted (just hashing algorithm without being linked), accept "null" value 13 | * 14 | * LinkedHashMap >> insertion ordered (using the hashing algorithm but it is linked), accept "null" value 15 | * 16 | * TreeMap >> sorted (it is using a tree algorithm, please check what tree algorithm is), does not accept "null" value 17 | * 18 | * */ 19 | 20 | Map map = new LinkedHashMap<>(); 21 | 22 | map.put("city", "Paris"); 23 | map.put("animal", "horse"); 24 | map.put("country", "Sweden"); 25 | map.put("team", "Cowboys"); 26 | map.put(null, "Cowboys"); 27 | 28 | map.remove("city"); 29 | 30 | System.out.println(map); 31 | 32 | System.out.println(map.containsKey("team")); 33 | 34 | System.out.println(map.size()); 35 | 36 | System.out.println(map.get("team")); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/javaFastTrack3/day6/TestCollectionSet.java: -------------------------------------------------------------------------------- 1 | package javaFastTrack3.day6; 2 | 3 | import java.util.*; 4 | 5 | public class TestCollectionSet { 6 | public static void main(String[] args) { 7 | /* 8 | * No duplicates allowed 9 | * 10 | * HashSet, LinkedHashSet, TreeSet 11 | * 12 | * HashSet >> not insertion ordered or sorted (just hashing algorithm without 13 | * being linked), accept "null" value 14 | * 15 | * LinkedHashSet >> insertion ordered (using the hashing algorithm but it is 16 | * linked), accept "null" value 17 | * 18 | * TreeSet >> sorted (it is using a tree algorithm, please check what tree 19 | * algorithm is), does not accept "null" value 20 | */ 21 | 22 | Set set = new HashSet<>(); 23 | 24 | // add the new data 25 | set.add("New York"); 26 | set.add("Denver"); 27 | set.add("Houston"); 28 | set.add("LA"); 29 | set.add("Denver"); 30 | set.add("San Antonio"); 31 | set.add(null); 32 | 33 | System.out.println(set); 34 | 35 | System.out.println(set.contains("Denver")); 36 | 37 | // returns a boolean 38 | set.isEmpty(); 39 | } 40 | } 41 | --------------------------------------------------------------------------------