├── 1.Introduction_to_java ├── IntroToJava.pdf └── README.md ├── 10.Expressions_and_increment_decrement_operators ├── README.md └── cwh_10_resulting_data_type.java ├── 11.Exercise_1_Solution ├── README.md └── cwh_11_ex1_sol.java ├── 12.Chapter_2_practice_set ├── README.md ├── cwh_12_ps2_pr01.java ├── cwh_12_ps2_pr02.java ├── cwh_12_ps2_pr03.java ├── cwh_12_ps2_pr04.java └── cwh_12_ps2_pr05.java ├── 13.Introduction_to_strings ├── Ch3Strings.pdf ├── README.md └── cwh_13_strings.java ├── 14.String_methods ├── README.md └── cwh_14_string_methods.java ├── 15.Chapter_3_practice_set ├── README.md ├── cwh_15_ps3_pr01.java ├── cwh_15_ps3_pr02.java ├── cwh_15_ps3_pr03.java ├── cwh_15_ps3_pr04.java └── cwh_15_ps3_pr05.java ├── 16.Conditionals_if_else ├── README.md └── ch4conditional.pdf ├── 17.Relational_and_logical_operators ├── README.md └── cwh_17_logical.java ├── 18.Switch_case_statements ├── README.md └── cwh_18_elseif.java ├── 19.Chapter_4_practice_set ├── README.md ├── cwh_19_ch4_ps_pr_01.java ├── cwh_19_ch4_ps_pr_02.java ├── cwh_19_ch4_ps_pr_03.java ├── cwh_19_ch4_ps_pr_04.java ├── cwh_19_ch4_ps_pr_05.java └── cwh_19_ch4_ps_pr_06.java ├── 2.Basic_structure_of_java_program ├── Hello.java ├── IntroToJava.pdf └── README.md ├── 20.Java_Exercise_2 ├── README.md └── RPS.java ├── 21.While_Loops ├── Chapter_5.pdf ├── README.md └── cwh_21_ch5_loops.java ├── 22.Do_While_Loop ├── README.md └── cwh_22_ch4_do_while.java ├── 23.For_Loops ├── README.md └── cwh_23_for_loop.java ├── 24.Break_And_Continue ├── README.md └── cwh_24_break_and_continue.java ├── 25.Chapter_5_Practice_Set ├── Chapter_5__Practice_Set.pdf ├── README.md ├── cwh_25_ch5_ps_pr_01.java ├── cwh_25_ch5_ps_pr_02.java ├── cwh_25_ch5_ps_pr_03.java ├── cwh_25_ch5_ps_pr_04.java ├── cwh_25_ch5_ps_pr_05.java ├── cwh_25_ch5_ps_pr_06.java ├── cwh_25_ch5_ps_pr_07.java ├── cwh_25_ch5_ps_pr_08.java ├── cwh_25_ch5_ps_pr_09.java ├── cwh_25_ch5_ps_pr_10.java └── cwh_25_ch5_ps_pr_11.java ├── 26.Introduction_To_Arrays ├── Chapter_6_Arrays.pdf ├── README.md └── cwh_26_arrays.java ├── 27.For_Each_Loop ├── README.md └── cwh_27_arrays.java ├── 28.Multidimensional_Arrays ├── README.md └── cwh_28_multi_dim_arrays.java ├── 29.Chapter_6_Practice_Set ├── Chapter_6_Practice_Set.pdf ├── README.md ├── cwh_29_ps_pr_01.java ├── cwh_29_ps_pr_02.java ├── cwh_29_ps_pr_03.java ├── cwh_29_ps_pr_04.java ├── cwh_29_ps_pr_05.java ├── cwh_29_ps_pr_06.java └── cwh_29_ps_pr_07.java ├── 3.Variables_and_data_types ├── JavaChapter1.pdf ├── README.md └── Total.java ├── 30.Overview_Of_IntelliJ_IDEA └── README.md ├── 31.Methods ├── Ch7 Methods.pdf ├── README.md └── cwh_31_methods.java ├── 32.Method_Overloading ├── README.md └── cwh_32_method_overloading.java ├── 33.Variable_Arguments ├── README.md └── calculate.java ├── 34.Recursion ├── README.md ├── cwh_34_recursion.java └── recursion.java ├── 35.Chapter_7_Practice_Set ├── Chapter7_PracticeSet.pdf ├── README.md ├── cwh_35_ps_pr_01.java ├── cwh_35_ps_pr_02.java ├── cwh_35_ps_pr_03.java ├── cwh_35_ps_pr_04.java ├── cwh_35_ps_pr_05.java ├── cwh_35_ps_pr_06.java ├── cwh_35_ps_pr_07.java ├── cwh_35_ps_pr_08.java ├── cwh_35_ps_pr_09.java └── cwh_35_ps_pr_10.java ├── 36.Introduction_To_OOPS ├── Ch8 Intro to oops.pdf └── README.md ├── 37.Basic_Terminologies_In_OOPS └── README.md ├── 38.Custom_Class ├── README.md └── cwh_38_custom_class.java ├── 39.Chapter_8_Practice_Set ├── Ch8_PS.pdf ├── README.md ├── cwh_39_ps_pr_01.java ├── cwh_39_ps_pr_02.java ├── cwh_39_ps_pr_03.java ├── cwh_39_ps_pr_04.java ├── cwh_39_ps_pr_05.java └── cwh_39_ps_pr_06.java ├── 4.Literals ├── CWH_04_literals.java └── README.md ├── 40.Access_Modifiers ├── CWH.java ├── CWH1.java ├── Chapter9.pdf ├── README.md └── cwh_40_ch9.java ├── 41.Exercise_2_Solution ├── README.md └── cwh_41_ex2sol.java ├── 42.Constructors ├── CWH_constructors.java ├── README.md ├── cwh_42_constructors.java └── quick_quiz.java ├── 43.Java_Exercise_3 └── README.md ├── 44.Chapter_9_Practice_Set ├── Ch9PS.pdf ├── README.md ├── cwh_44_ps_pr_01.java ├── cwh_44_ps_pr_02.java ├── cwh_44_ps_pr_03.java ├── cwh_44_ps_pr_04.java └── cwh_44_ps_pr_05.java ├── 45.Inheritance ├── Chapter10.pdf ├── README.md └── cwh_45_inheritance.java ├── 46.Constructors_In_Inheritance ├── README.md └── cwh_46_constructors_in_inheritance.java ├── 47.this_and_super_keyword ├── README.md ├── cwh.java ├── cwh1.java └── cwh_47_this_super.java ├── 48.Method_Overriding ├── CWH.java ├── CWH1.java ├── README.md └── cwh_48_method_overriding.java ├── 49.Dynamic_Method_Dispatch ├── README.md └── cwh_49_dynamic_method_dispatch.java ├── 5.Getting_user_input ├── CWH_05_TakingInput.java └── README.md ├── 50.Exercise_3_Solution ├── README.md └── cwh_50_ex3sol.java ├── 6.Java_exercise_1 ├── Percentage_calc.java └── README.md ├── 7.Chapter_1_practice_set ├── Q_1.java ├── Q_2.java ├── Q_3.java ├── Q_4.java ├── Q_5.java └── README.md ├── 8.Operators_and_expressions ├── CWH_Ch2_Operators.java ├── Chapter2.pdf └── README.md ├── 9.Associativity_of_operators ├── README.md └── cwh_09_ch2_op_pre.java ├── LICENSE └── README.md /1.Introduction_to_java/IntroToJava.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/1.Introduction_to_java/IntroToJava.pdf -------------------------------------------------------------------------------- /1.Introduction_to_java/README.md: -------------------------------------------------------------------------------- 1 | # Introduction to Java + Installing Java JDK and IntelliJ IDEA for Java 2 | 3 | - Java is one of the most popular programming languages because it is used in various tech fields like app development, web development, client-server applications, etc. 4 | - Java is an object-oriented programming language developed by Sun Microsystems of the USA in 1991. 5 | - It was originally called Oak by James Goslin. He was one of the inventors of Java. 6 | - Java = Purely Object-Oriented. 7 | 8 | ## How Java Works? 9 | - The source code in Java is first compiled into the bytecode. 10 | - Then the Java Virtual Machine(JVM) compiles the bytecode to the machine code. 11 | 12 | 13 | 14 | ## Java Installation: 15 | 16 | ### Step 1: Downloading JDK 17 | - JDK stands for Java Development Kit. It contains Java Virtual Machine(JVM) and Java Runtime Environment(JRE). 18 | - JDK – Java Development Kit = Collection of tools used for developing and running java programs. 19 | - JRE – Java Runtime Environment = Helps in executing programs developed in JAVA. 20 | - [Click here](https://www.oracle.com/java/technologies/javase-jdk16-downloads.html), and you will be redirected to the official download page of JDK. 21 | - Select your operating system and download the file(executable file in case of Windows). 22 | 23 | ### Step 2: Installing JDK 24 | - Once the executable file is downloaded successfully, right-click and open the file. 25 | - The executable file will start executing. 26 | - Keep clicking on the Next button to install the JDK in default settings. 27 | 28 | ### Step 3: Downloading IntelliJ IDEA : 29 | - We need an Integrated Development Environment(IDE) to write and debug our code easily. 30 | - IntelliJ IDEA is the best-suited IDE for writing Java code. 31 | - [Click here](https://www.jetbrains.com/idea/download/#section=windows), and you will be redirected to the official download page of IntelliJ IDEA. 32 | - Download the Community Version of the IntelliJ IDEA as it is free to use. 33 | 34 | ### Step 4: Installing IntelliJ IDEA : 35 | - Once the download completes, open the .exe file, and the installation process will begin. 36 | - Click on the **"Next"** button to install the IntelliJ IDEA in the default location. 37 | - Do not forget to check **"Add launchers dir to the PATH,"** as shown in the below image. 38 | 39 | 40 | 41 | - After this, click on the "Next" button and then click on the **"Install"** button. 42 | 43 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-1/IntroToJava.pdf) 44 | 45 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-1/UltimateJavaCheatSheet.pdf) 46 | -------------------------------------------------------------------------------- /10.Expressions_and_increment_decrement_operators/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Data Type of Expressions & Increment/Decrement Operators 2 | 3 | ## Resulting data type after arithmetic operation 4 | 5 | - Result = byte + short -> integer 6 | - Result = short + integer -> integer 7 | - Result = long + float -> float 8 | - Result = integer + float -> float 9 | - Result = character + integer -> integer 10 | - Result = character + short -> integer 11 | - Result = long + double -> double 12 | - Result = float + double -> double 13 | 14 | 15 | ## Increment and Decrement operators 16 | - a++, ++a (Increment Operators) 17 | - a--, --a (Decrement Operators) 18 | - These will operate on all data types except Booleans. 19 | 20 | **Quick Quiz: Try increment and decrement operators on a Java variable** 21 | 22 | - a++ -> first use the value and then increment 23 | - ++a -> first increment the value then use it 24 | 25 | **Quick Quiz: What will be the value of the following expression(x)** 26 | ``` 27 | int y=7; 28 | int x = ++y*8; 29 | value of x? 30 | char a = ‘B’; 31 | a++; (a is not ‘C’) 32 | ``` 33 | 34 | ## Code as Described in the Video 35 | ``` 36 | package com.company; 37 | 38 | public class cwh_10_resulting_data_type { 39 | public static void main(String[] args) { 40 | /* byte x = 5; 41 | int y = 6; 42 | short z = 8; 43 | int a = y + z; 44 | float b = 6.54f + x; 45 | System.out.println(b); */ 46 | 47 | // Increment and Decrement Operators 48 | int i = 56; 49 | // int b = i++; // first b is assigned i (56) then i is incremented 50 | int j = 67; 51 | int c = ++j; // first j is incremented then c is assigned j (68) 52 | System.out.println(i++); 53 | System.out.println(i); 54 | System.out.println(++i); 55 | System.out.println(i); 56 | int y = 7; 57 | System.out.println( ++y *8); 58 | char ch = 'a'; 59 | System.out.println(++ch); 60 | } 61 | } 62 | ``` 63 | 64 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-10/Chapter2.pdf) 65 | 66 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-10/UltimateJavaCheatSheet.pdf) 67 | -------------------------------------------------------------------------------- /10.Expressions_and_increment_decrement_operators/cwh_10_resulting_data_type.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | public class cwh_10_resulting_data_type { 4 | public static void main(String[] args) { 5 | /* byte x = 5; 6 | int y = 6; 7 | short z = 8; 8 | int a = y + z; 9 | float b = 6.54f + x; 10 | System.out.println(b); */ 11 | 12 | // Increment and Decrement Operators 13 | int i = 56; 14 | // int b = i++; // first b is assigned i (56) then i is incremented 15 | int j = 67; 16 | int c = ++j; // first j is incremented then c is assigned j (68) 17 | System.out.println(i++); 18 | System.out.println(i); 19 | System.out.println(++i); 20 | System.out.println(i); 21 | int y = 7; 22 | System.out.println( ++y *8); 23 | char ch = 'a'; 24 | System.out.println(++ch); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /11.Exercise_1_Solution/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Exercise 1 - Solutions + Shoutouts 2 | - Write a program to calculate the percentage of a given student in the CBSE board exam. His marks from 5 subjects must be taken as input from the keyboard. (Marks are out of 100) 3 | 4 | ## Code Solution: 5 | ``` 6 | package com.company; 7 | import java.util.Scanner; 8 | 9 | public class cwh_11_ex1_sol { 10 | 11 | 12 | public static void main(String[] args) { 13 | 14 | Scanner scan = new Scanner(System.in); 15 | System.out.println("Enter your Physics marks : "); 16 | int physics = scan.nextInt(); 17 | System.out.println("Enter your English marks : "); 18 | int English = scan.nextInt(); 19 | System.out.println("Enter your Chemistry marks : "); 20 | int chemistry = scan.nextInt(); 21 | System.out.println("Enter your Mathematics marks : "); 22 | int mathematics = scan.nextInt(); 23 | System.out.println("Enter your Computer Science marks : "); 24 | int computer = scan.nextInt(); 25 | 26 | float percentage = ((physics + English + chemistry + mathematics + computer)/500.0f)*100; 27 | 28 | System.out.println("percentage : "); 29 | System.out.println(percentage); 30 | 31 | 32 | } 33 | } 34 | ``` 35 | 36 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-11/UltimateJavaCheatSheet.pdf) 37 | -------------------------------------------------------------------------------- /11.Exercise_1_Solution/cwh_11_ex1_sol.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | import java.util.Scanner; 3 | 4 | public class cwh_11_ex1_sol { 5 | 6 | 7 | public static void main(String[] args) { 8 | 9 | Scanner scan = new Scanner(System.in); 10 | System.out.println("Enter your Physics marks : "); 11 | int physics = scan.nextInt(); 12 | System.out.println("Enter your English marks : "); 13 | int English = scan.nextInt(); 14 | System.out.println("Enter your Chemistry marks : "); 15 | int chemistry = scan.nextInt(); 16 | System.out.println("Enter your Mathematics marks : "); 17 | int mathematics = scan.nextInt(); 18 | System.out.println("Enter your Computer Science marks : "); 19 | int computer = scan.nextInt(); 20 | 21 | float percentage = ((physics + English + chemistry + mathematics + computer)/500.0f)*100; 22 | 23 | System.out.println("percentage : "); 24 | System.out.println(percentage); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /12.Chapter_2_practice_set/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Chapter 2 - Practice Set (Java Practice Questions) 2 | 3 | 1. What will be the result of the following expression: 4 | 5 | float a = 7/4 * 9/2 6 | 7 | 2. Write a java program to encrypt a grade by adding 8 to it. Decrypt it to show the correct grade. 8 | 3. Use comparison operators to find out whether a given number is greater than the user entered number or not. 9 | 4. Write the following expression in a java program: 10 | 11 | (v^2-u^2)/2as 12 | 13 | 5. Find the value of 'a' in expression given below : 14 | 15 | int x = 7 16 | int a = 7*49/7 + 35/7 17 | 18 | # Code Solution 19 | 20 | ``` 21 | public class cwh_12_ps2_pr01 { 22 | public static void main(String[] args) { 23 | float a = 7/4.0f * 9/2.0f; 24 | System.out.println(a); 25 | } 26 | } 27 | ``` 28 | 29 | 30 | ``` 31 | public class cwh_12_ps2_pr02 { 32 | public static void main(String[] args) { 33 | char grade = 'B'; 34 | grade = (char)(grade + 8); 35 | System.out.println(grade); 36 | // Decrypting the grade 37 | grade = (char)(grade - 8); 38 | System.out.println(grade); 39 | } 40 | } 41 | ``` 42 | 43 | ``` 44 | import java.util.Scanner; 45 | 46 | public class cwh_12_ps2_pr_03 { 47 | public static void main(String[] args) { 48 | Scanner sc = new Scanner(System.in); 49 | int a = sc.nextInt(); 50 | System.out.println(a>8); 51 | } 52 | } 53 | 54 | ``` 55 | 56 | ``` 57 | import java.util.Scanner; 58 | 59 | public class cwh_12_ps2_pr_04 { 60 | public static void main(String[] args) { 61 | Scanner sc = new Scanner(System.in); 62 | 63 | System.out.print("Enter v : "); 64 | int v = sc.nextInt(); 65 | 66 | System.out.print("Enter u : "); 67 | int u = sc.nextInt(); 68 | 69 | System.out.print("Enter s : "); 70 | int s = sc.nextInt(); 71 | 72 | System.out.print("Enter a : "); 73 | int a = sc.nextInt(); 74 | 75 | double result = (v*v - u*u) / (2*a*s); 76 | System.out.print("Result is : " + result); 77 | } 78 | } 79 | ``` 80 | 81 | ``` 82 | import java.util.Scanner; 83 | 84 | public class cwh_12_ps2_pr_05 { 85 | public static void main(String[] args) { 86 | Scanner sc = new Scanner(System.in); 87 | System.out.println(7*49/7+35/7); 88 | } 89 | } 90 | ``` 91 | -------------------------------------------------------------------------------- /12.Chapter_2_practice_set/cwh_12_ps2_pr01.java: -------------------------------------------------------------------------------- 1 | public class cwh_12_ps2_pr01 { 2 | public static void main(String[] args) { 3 | float a = 7/4.0f * 9/2.0f; 4 | System.out.println(a); 5 | } 6 | } -------------------------------------------------------------------------------- /12.Chapter_2_practice_set/cwh_12_ps2_pr02.java: -------------------------------------------------------------------------------- 1 | public class cwh_12_ps2_pr02 { 2 | public static void main(String[] args) { 3 | char grade = 'B'; 4 | grade = (char)(grade + 8); 5 | System.out.println(grade); 6 | // Decrypting the grade 7 | grade = (char)(grade - 8); 8 | System.out.println(grade); 9 | } 10 | } -------------------------------------------------------------------------------- /12.Chapter_2_practice_set/cwh_12_ps2_pr03.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class cwh_12_ps2_pr_03 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int a = sc.nextInt(); 7 | System.out.println(a>8); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /12.Chapter_2_practice_set/cwh_12_ps2_pr04.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class cwh_12_ps2_pr_04 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.print("Enter v : "); 8 | int v = sc.nextInt(); 9 | 10 | System.out.print("Enter u : "); 11 | int u = sc.nextInt(); 12 | 13 | System.out.print("Enter s : "); 14 | int s = sc.nextInt(); 15 | 16 | System.out.print("Enter a : "); 17 | int a = sc.nextInt(); 18 | 19 | double result = (v*v - u*u) / (2*a*s); 20 | System.out.print("Result is : " + result); 21 | } 22 | } -------------------------------------------------------------------------------- /12.Chapter_2_practice_set/cwh_12_ps2_pr05.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class cwh_12_ps2_pr_05 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.println(7*49/7+35/7); 7 | } 8 | } -------------------------------------------------------------------------------- /13.Introduction_to_strings/Ch3Strings.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/13.Introduction_to_strings/Ch3Strings.pdf -------------------------------------------------------------------------------- /13.Introduction_to_strings/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Introduction to Strings 2 | 3 | - A string is a sequence of characters. 4 | - Strings are objects that represent a char array. For example : 5 | 6 | ``` 7 | char[] str = {'H','A','R','R','Y'}; 8 | String s = new String(str); 9 | ``` 10 | 11 | is same as : 12 | 13 | ``` 14 | String s = "Harry"; 15 | ``` 16 | 17 | - Strings are immutable and cannot be changed. 18 | - java.lang.String class is used to create a String object. 19 | - The string is a class but can be used as a data type. 20 | - Syntax of strings in Java : 21 | 22 | ``` 23 | String = ""; 24 | ``` 25 | 26 | Example : 27 | 28 | ``` 29 | String str = "CodeWithHarry"; 30 | ``` 31 | 32 | - In the above example, str is a reference, and “CodeWithHarry” is an object. 33 | 34 | ### Different ways to create a string in Java : 35 | In Java, strings can be created in two ways : 36 | - By using string literal 37 | - By using the new 38 | 39 | ### Creating String using String literal : 40 | ``` 41 | String s1= "String literal" 42 | ``` 43 | 44 | - We use double quotes("") to create string using string literal. Before creating a new string instance, JVM verifies if the same string is already present in the string pool or not. 45 | - If it is already present, then JVM returns a reference to the pooled instance otherwise, a new string instance is created. 46 | 47 | - In the above diagram, notice that string "Harry" is already present in the string pool, which is pointed by the str1. 48 | - When we try to create the same string object using str2, JVM finds that string object with the value "Harry" is already present in the string pool; therefore, instead of creating a new object, a reference to the same object is returned. 49 | 50 | ### Creating String using new : 51 | ``` 52 | String s=new String("Harry"); 53 | ``` 54 | 55 | - When we create a string using "new", a new object is always created in the heap memory. 56 | 57 | ![image](https://user-images.githubusercontent.com/70385488/150810348-c723a98d-104f-4d73-af81-0ef51aa0b357.png) 58 | 59 | - In the above diagram, you can see that although the value of both string objects is the same, i.e., "Harry" still two different objects are created, and they are referred by two different reference variables, i.e., str1 and str2. 60 | - See the examples given below to get a better understanding of String literal and String object : 61 | 62 | ``` 63 | String str1 = "CodeWithHarry"; 64 | String str2 = "CodeWithHarry" 65 | System.out.println(str1 == str2); 66 | ``` 67 | 68 | - Output = True 69 | 70 | Returns true because str1 and str2 are referencing the same object present in the string constant pool. Now, let's see the case of the String object : 71 | 72 | ``` 73 | String str1 = new String("Keep coding"); 74 | String str2 = new String("Keep coding""); 75 | System.out.println(str1 == str2); 76 | ``` 77 | 78 | - Output = False 79 | 80 | Although the value of both the string object is the same, still false is displayed as output because str1 and str2 are two different string objects created in the heap. That's why it is not considered a good practice two compare two strings using the == operator. Always use the equals() method to compare two strings in Java. 81 | 82 | ### Different ways to print in Java : 83 | We can use the following ways to print in Java: 84 | - System.out.print() // No newline at the end 85 | - System.out.println() // Prints a new line at the end 86 | - System.out.printf() 87 | - System.out.format() 88 | 89 | ``` 90 | System.out.printf("%c",ch) 91 | ``` 92 | 93 | - %d for int 94 | - %f for float 95 | - %c for char 96 | - %s for string 97 | 98 | ### Code as written in the video 99 | 100 | ``` 101 | package com.company; 102 | import java.util.Scanner; 103 | 104 | public class cwh_13_strings { 105 | public static void main(String[] args) { 106 | // String name = new String("Harry"); 107 | // String name = "Harry"; 108 | // System.out.print("The name is: "); 109 | // System.out.print(name); 110 | int a = 6; 111 | float b = 5.6454f; 112 | System.out.printf("The value of a is %d and value of b is %8.2f", a, b); 113 | //System.out.format("The value of a is %d and value of b is %f", a, b); 114 | Scanner sc = new Scanner(System.in); 115 | // String st = sc.next(); 116 | // String st = sc.nextLine(); 117 | // System.out.println(st); 118 | 119 | } 120 | } 121 | ``` 122 | 123 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-13/Ch3Strings.pdf) 124 | 125 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-13/UltimateJavaCheatSheet.pdf) 126 | -------------------------------------------------------------------------------- /13.Introduction_to_strings/cwh_13_strings.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | import java.util.Scanner; 3 | 4 | public class cwh_13_strings { 5 | public static void main(String[] args) { 6 | // String name = new String("Harry"); 7 | // String name = "Harry"; 8 | // System.out.print("The name is: "); 9 | // System.out.print(name); 10 | int a = 6; 11 | float b = 5.6454f; 12 | System.out.printf("The value of a is %d and value of b is %8.2f", a, b); 13 | //System.out.format("The value of a is %d and value of b is %f", a, b); 14 | Scanner sc = new Scanner(System.in); 15 | // String st = sc.next(); 16 | // String st = sc.nextLine(); 17 | // System.out.println(st); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /14.String_methods/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: String Methods in Java 2 | 3 | - String Methods operate on Java Strings. They can be used to find the length of the string, convert to lowercase, etc. 4 | - Some of the commonly used String methods are: 5 | 6 | ``` 7 | String name = “Kishan”; 8 | ``` 9 | - (Indexes of the above string are as follows: 0-K, 1-i, 2-s, 3-h, 4-a, 5-n) 10 | 11 | | **Methods** | **Description** | 12 | |:--------------|:------------------| 13 | | **1. length()** | Returns the length of String name. (6 in this case) | 14 | | **2. toLowerCase()** | Converts all the characters of the string to the lower case letters. | 15 | | **3. toUpperCase()** | Converts all the characters of the string to the upper case letters. | 16 | | **4. trim()** | Returns a new String after removing all the leading and trailing spaces from the original string. | 17 | | **5. substring(int start)** | Returns a substring from start to the end. Substring(3) returns “han”. [Note that indexing starts from 0] | 18 | | **6. substring(int start, int end)** | Returns a substring from the start index to the end index. The start index is included, and the end is excluded. | 19 | | **7. replace(‘K’, ‘p’)** | Returns a new string after replacing K with p. pishan is returned in this case. (This method takes char as argument) | 20 | | **8. startsWith(“Ki”)** | Returns true if the name starts with the string “Ki”. (True in this case) | 21 | | **9. endsWith(“an”)** | Returns true if the name ends with the string “an”. (True in this case) | 22 | | **10. charAt(2)** | Returns the character at a given index position. (s in this case) | 23 | | **11. indexOf(“s”)** | Returns the index of the first occurrence of the specified character in the given string. | 24 | | **12. lastIndexOf(“h”)** | Returns the last index of the specified character from the given string. (3 in this case) | 25 | | **13. equals(“Kishan”)** | Returns true if the given string is equal to “Kishan” false otherwise [Case sensitive] | 26 | | **14.equalsIgnoreCase(“kishan”)** | Returns true if two strings are equal, ignoring the case of characters. | 27 | 28 | ### Escape Sequence Characters : 29 | - The sequence of characters after backslash ‘\’ = Escape Sequence Characters 30 | - Escape Sequence Characters consist of more than one character but represent one character when used within the strings. 31 | - Examples: \n (newline), \t (tab), \’ (single quote), \\ (backslash), etc. 32 | 33 | ### Code as described in the video 34 | ``` 35 | public class cwh_14_string_methods { 36 | public static void main(String[] args) { 37 | String name = "Kishan"; 38 | System.out.println(name); 39 | 40 | int value = name.length(); 41 | System.out.println(value); 42 | 43 | String lstring = name.toLowerCase(); 44 | System.out.println(lstring); 45 | 46 | String ustring = name.toUpperCase(); 47 | System.out.println(ustring); 48 | 49 | String nonTrimmedString = " Kishan "; 50 | System.out.println(nonTrimmedString); 51 | 52 | String trimmedString = nonTrimmedString.trim(); 53 | System.out.println(trimmedString); 54 | 55 | System.out.println(name.substring(1)); 56 | System.out.println(name.substring(1,5)); 57 | 58 | System.out.println(name.replace('K', 'p')); 59 | System.out.println(name.replace("n", "ier")); 60 | 61 | System.out.println(name.startsWith("Kis")); 62 | System.out.println(name.endsWith("dd")); 63 | 64 | System.out.println(name.charAt(4)); 65 | 66 | String modifiedName = "Harryrryrry"; 67 | System.out.println(modifiedName.indexOf("rry")); 68 | System.out.println(modifiedName.indexOf("rry", 4)); 69 | System.out.println(modifiedName.lastIndexOf("rry", 7)); 70 | 71 | System.out.println(name.equals("Kishan")); 72 | System.out.println(name.equalsIgnoreCase("KiShAn")); 73 | 74 | System.out.println("I am escape sequence\tdouble quote"); 75 | } 76 | } 77 | ``` 78 | 79 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-14/Ch3Strings.pdf) 80 | 81 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-14/UltimateJavaCheatSheet.pdf) 82 | -------------------------------------------------------------------------------- /14.String_methods/cwh_14_string_methods.java: -------------------------------------------------------------------------------- 1 | public class cwh_14_string_methods { 2 | public static void main(String[] args) { 3 | String name = "Kishan"; 4 | System.out.println(name); 5 | 6 | int value = name.length(); 7 | System.out.println(value); 8 | 9 | String lstring = name.toLowerCase(); 10 | System.out.println(lstring); 11 | 12 | String ustring = name.toUpperCase(); 13 | System.out.println(ustring); 14 | 15 | String nonTrimmedString = " Kishan "; 16 | System.out.println(nonTrimmedString); 17 | 18 | String trimmedString = nonTrimmedString.trim(); 19 | System.out.println(trimmedString); 20 | 21 | System.out.println(name.substring(1)); 22 | System.out.println(name.substring(1,5)); 23 | 24 | System.out.println(name.replace('K', 'p')); 25 | System.out.println(name.replace("n", "ier")); 26 | 27 | System.out.println(name.startsWith("Kis")); 28 | System.out.println(name.endsWith("dd")); 29 | 30 | System.out.println(name.charAt(4)); 31 | 32 | String modifiedName = "Harryrryrry"; 33 | System.out.println(modifiedName.indexOf("rry")); 34 | System.out.println(modifiedName.indexOf("rry", 4)); 35 | System.out.println(modifiedName.lastIndexOf("rry", 7)); 36 | 37 | System.out.println(name.equals("Kishan")); 38 | System.out.println(name.equalsIgnoreCase("KiShAn")); 39 | 40 | System.out.println("I am escape sequence\tdouble quote"); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /15.Chapter_3_practice_set/README.md: -------------------------------------------------------------------------------- 1 | # Java Practice Questions on Strings: Practice Set on Java Strings (Must Solve!) 2 | 3 | - Today we will solve some of the best problems in Java related to strings! Here is the Chapter 3 – Practice Set 4 | 5 | 1. Write a Java program to convert a string to lowercase. 6 | 2. Write a Java program to replace spaces with underscores. 7 | 3. Write a Java program to fill in a letter template which looks like below: 8 | 9 | ``` 10 | // letter = “Dear <|name|>, Thanks a lot” 11 | 12 | // Replace <|name|> with a string (some name) 13 | ``` 14 | 15 | 4. Write a Java program to detect double and triple spaces in a string. 16 | 5. Write a program to format the following letter using escape sequence characters. 17 | 18 | Letter = “Dear Harry, This Java Course is nice. Thanks” 19 | 20 | ## Code Solution: 21 | 22 | ``` 23 | public class cwh_15_ps3_pr01 { 24 | public static void main(String[] args) { 25 | String name = "Peter Parker"; 26 | System.out.println(name); 27 | 28 | name = name.toLowerCase(); 29 | System.out.println(name); 30 | } 31 | } 32 | ``` 33 | 34 | ``` 35 | public class cwh_15_ps3_pr02 { 36 | public static void main(String[] args) { 37 | String name = "My name is Kishan "; 38 | System.out.println(name); 39 | 40 | name = name.replace(" ", "_"); 41 | System.out.println(name); 42 | } 43 | } 44 | ``` 45 | 46 | ``` 47 | import java.util.*; 48 | 49 | public class cwh_15_ps3_pr03 { 50 | public static void main(String[] args) { 51 | Scanner sc = new Scanner(System.in); 52 | String name = sc.next(); 53 | String letter = "Dear <|name|>, Thanks a lot"; 54 | 55 | letter = letter.replace("<|name|>", name); 56 | System.out.println(letter); 57 | } 58 | } 59 | ``` 60 | 61 | ``` 62 | public class cwh_15_ps3_pr04 { 63 | public static void main(String[] args) { 64 | String name = "My name is Kishan"; 65 | System.out.println(name.indexOf(" ")); 66 | System.out.println(name.indexOf(" ")); 67 | } 68 | } 69 | ``` 70 | 71 | ``` 72 | import java.util.*; 73 | 74 | public class cwh_15_ps3_pr05 { 75 | public static void main(String[] args) { 76 | String Letter = ("Dear Harry,\n\t This Java Course is nice.\n\t Thanks"); 77 | System.out.println(Letter); 78 | } 79 | } 80 | ``` 81 | 82 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-15/UltimateJavaCheatSheet.pdf) 83 | -------------------------------------------------------------------------------- /15.Chapter_3_practice_set/cwh_15_ps3_pr01.java: -------------------------------------------------------------------------------- 1 | public class cwh_15_ps3_pr01 { 2 | public static void main(String[] args) { 3 | String name = "Peter Parker"; 4 | System.out.println(name); 5 | 6 | name = name.toLowerCase(); 7 | System.out.println(name); 8 | } 9 | } -------------------------------------------------------------------------------- /15.Chapter_3_practice_set/cwh_15_ps3_pr02.java: -------------------------------------------------------------------------------- 1 | public class cwh_15_ps3_pr02 { 2 | public static void main(String[] args) { 3 | String name = "My name is Kishan "; 4 | System.out.println(name); 5 | 6 | name = name.replace(" ", "_"); 7 | System.out.println(name); 8 | } 9 | } -------------------------------------------------------------------------------- /15.Chapter_3_practice_set/cwh_15_ps3_pr03.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class cwh_15_ps3_pr03 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | String name = sc.next(); 7 | String letter = "Dear <|name|>, Thanks a lot"; 8 | 9 | letter = letter.replace("<|name|>", name); 10 | System.out.println(letter); 11 | } 12 | } -------------------------------------------------------------------------------- /15.Chapter_3_practice_set/cwh_15_ps3_pr04.java: -------------------------------------------------------------------------------- 1 | public class cwh_15_ps3_pr04 { 2 | public static void main(String[] args) { 3 | String name = "My name is Kishan"; 4 | System.out.println(name.indexOf(" ")); 5 | System.out.println(name.indexOf(" ")); 6 | } 7 | } -------------------------------------------------------------------------------- /15.Chapter_3_practice_set/cwh_15_ps3_pr05.java: -------------------------------------------------------------------------------- 1 | public class cwh_15_ps3_pr05 { 2 | public static void main(String[] args) { 3 | String Letter = ("Dear Harry,\n\t This Java Course is nice.\n\t Thanks"); 4 | System.out.println(Letter); 5 | } 6 | } -------------------------------------------------------------------------------- /16.Conditionals_if_else/README.md: -------------------------------------------------------------------------------- 1 | # Java Conditionals: If-else Statement in Java 2 | - Sometimes we want to drink coffee when we feel sleepy. Sometimes, we order junk food if it is our friend’s birthday. 3 | - You might want to buy an umbrella if it’s raining. All these decisions depend on a certain condition being met. 4 | - Similar to real life, we can execute some instructions only when a condition is met in programming also. 5 | - If-else block is used to check conditions and execute a particular section of code for a specific condition. 6 | 7 | ### Flow control of if-else in Java : 8 | 9 | 10 | 11 | ### Decision-making instructions in Java 12 | - If-Else Statement 13 | - Switch Statement 14 | 15 | ### If-Else Statement 16 | - Syntax of If-else statement in Java : 17 | 18 | ``` 19 | /* if (condition-to-be-checked) { 20 | statements-if-condition-true; 21 | } 22 | else { 23 | statements-if-condition-false; 24 | } */ 25 | ``` 26 | - Example: 27 | ``` 28 | int a = 29; 29 | if (a>18) { 30 | System.out.println(“You can drive”); 31 | } 32 | else{ 33 | System.out.println(“You are underage!"); 34 | } 35 | ``` 36 | 37 | - Output : You can drive 38 | 39 | ### If-else ladder : 40 | - Instead of using multiple if statements, we can also use else if along with if thus forming an if-else-if-else ladder. 41 | - Using such kind of logic reduces indents. 42 | - Last else is executed only if all the conditions fail. 43 | 44 | ``` 45 | /* if (condition1) { 46 | //Statements; 47 | } 48 | 49 | else if { 50 | // Statements; 51 | } 52 | 53 | else { 54 | //Statements 55 | } */ 56 | ``` 57 | 58 | - Note that the else block is optional. 59 | 60 | #### Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-16/ch4conditional.pdf) 61 | 62 | #### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-16/UltimateJavaCheatSheet.pdf) 63 | -------------------------------------------------------------------------------- /16.Conditionals_if_else/ch4conditional.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/16.Conditionals_if_else/ch4conditional.pdf -------------------------------------------------------------------------------- /17.Relational_and_logical_operators/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Relational and Logical Operators in Java 2 | 3 | ### Relational Operators in Java : 4 | - Relational operators are used to evaluate conditions (true or false) inside the if statements. Some examples of relational operators are: 5 | - == (equals) 6 | - '>=' (greater than or equals to) 7 | - '>' (greater than) 8 | - < (less than) 9 | - <= (less than or equals to) 10 | - != (not equals) 11 | 12 | **Note:** ‘=’ is used for an assignment whereas ‘==’ is used for equality check. The condition can be either true or false. 13 | 14 | ### Logical Operators : 15 | - Logical operators are used to provide logic to our Java programs. 16 | - There are three types of logical operators in Java : 17 | - && - AND 18 | - || - OR 19 | - ! – NOT 20 | 21 | **1. AND Operator :** 22 | - Evaluates to true if both the conditions are true. 23 | 24 | - Y && Y = Y 25 | - Y && N = N 26 | - N && Y = N 27 | - N && N = N 28 | - Convention: # Y – True and N - False 29 | 30 | **2. OR Operator :** 31 | - Evaluates to true when at least one of the conditions is true. 32 | 33 | - Y || Y = Y 34 | - Y || N = Y 35 | - N || Y = Y 36 | - N || N = N 37 | - Convention: # Y – True and N - False 38 | 39 | **3. NOT Operator :** 40 | - Negates the given logic (true becomes false and vice-versa) 41 | 42 | - !Y = N 43 | - !N = Y 44 | 45 | ### Code as Described in the Video 46 | 47 | ``` 48 | public class cwh_17_logical { 49 | public static void main(String[] args) { 50 | System.out.println("For Logical AND..."); 51 | boolean a = true; 52 | boolean b = false; 53 | // if (a && b){ 54 | // System.out.println("Y"); 55 | // } 56 | // else{ 57 | // System.out.println("N"); 58 | // } 59 | 60 | System.out.println("For Logical OR..."); 61 | 62 | // if (a || b){ 63 | // System.out.println("Y"); 64 | // } 65 | // else{ 66 | // System.out.println("N"); 67 | // } 68 | 69 | System.out.println("For Logical NOT"); 70 | System.out.print("Not(a) is "); 71 | System.out.println(!a); 72 | System.out.print("Not(b) is "); 73 | System.out.println(!b); 74 | } 75 | } 76 | ``` 77 | 78 | #### Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-17/ch4conditional.pdf) 79 | 80 | #### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-17/UltimateJavaCheatSheet.pdf) 81 | -------------------------------------------------------------------------------- /17.Relational_and_logical_operators/cwh_17_logical.java: -------------------------------------------------------------------------------- 1 | public class cwh_17_logical { 2 | public static void main(String[] args) { 3 | System.out.println("For Logical AND..."); 4 | boolean a = true; 5 | boolean b = false; 6 | // if (a && b){ 7 | // System.out.println("Y"); 8 | // } 9 | // else{ 10 | // System.out.println("N"); 11 | // } 12 | 13 | System.out.println("For Logical OR..."); 14 | 15 | // if (a || b){ 16 | // System.out.println("Y"); 17 | // } 18 | // else{ 19 | // System.out.println("N"); 20 | // } 21 | 22 | System.out.println("For Logical NOT"); 23 | System.out.print("Not(a) is "); 24 | System.out.println(!a); 25 | System.out.print("Not(b) is "); 26 | System.out.println(!b); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /18.Switch_case_statements/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Switch Case Statements in Java 2 | 3 | ### Switch Case-Control Instruction 4 | - Switch-Case is used when we have to make a choice between the number of alternatives for a given variable. 5 | - Var can be an integer, character, or string in Java. 6 | - Every switch case must contain a default case. The default case is executed when all the other cases are false. 7 | - Never forget to include the break statement after every switch case otherwise the switch case will not terminate. 8 | - Syntax : 9 | 10 | ``` 11 | Switch(var) { 12 | Case C1: 13 | //Code; 14 | break; 15 | Case C2: 16 | //Code; 17 | break; 18 | Case C3: 19 | //Code 20 | break; 21 | default: 22 | //Code 23 | ``` 24 | 25 | - A switch can occur within another but in practice, this is rarely done. 26 | 27 | ### Flow control of switch case in Java : 28 | 29 | ![image](https://user-images.githubusercontent.com/70385488/151165417-61ea32da-8494-4796-b8eb-cb26f4509d9e.png) 30 | 31 | ### Code as Described in the Video 32 | 33 | ``` 34 | package com.company; 35 | import java.util.Scanner; 36 | 37 | public class cwh_18_elseif { 38 | public static void main(String[] args) { 39 | String var = "Saurabh"; 40 | 41 | 42 | switch (var) { 43 | case "Shubham" -> { 44 | System.out.println("You are going to become an Adult!"); 45 | System.out.println("You are going to become an Adult!"); 46 | System.out.println("You are going to become an Adult!"); 47 | } 48 | case "Saurabh" -> System.out.println("You are going to join a Job!"); 49 | case "Vishaka" -> System.out.println("You are going to get retired!"); 50 | default -> System.out.println("Enjoy Your life!"); 51 | } 52 | System.out.println("Thanks for using my Java Code!"); 53 | 54 | 55 | /* 56 | int age; 57 | System.out.println("Enter Your Age"); 58 | Scanner sc = new Scanner(System.in); 59 | age = sc.nextInt(); 60 | if (age>56){ 61 | System.out.println("You are experienced!"); 62 | } 63 | else if(age>46){ 64 | System.out.println("You are semi-experienced!"); 65 | } 66 | else if(age>36){ 67 | System.out.println("You are semi-semi-experienced!"); 68 | } 69 | else{ 70 | System.out.println("You are not experienced"); 71 | } 72 | if(age>2){ 73 | System.out.println("You are not a baby!"); 74 | } 75 | */ 76 | 77 | } 78 | } 79 | ``` 80 | 81 | #### Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-18/ch4conditional.pdf) 82 | 83 | #### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-18/UltimateJavaCheatSheet.pdf) 84 | -------------------------------------------------------------------------------- /18.Switch_case_statements/cwh_18_elseif.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | import java.util.Scanner; 3 | 4 | public class cwh_18_elseif { 5 | public static void main(String[] args) { 6 | String var = "Saurabh"; 7 | 8 | 9 | switch (var) { 10 | case "Shubham" -> { 11 | System.out.println("You are going to become an Adult!"); 12 | System.out.println("You are going to become an Adult!"); 13 | System.out.println("You are going to become an Adult!"); 14 | } 15 | case "Saurabh" -> System.out.println("You are going to join a Job!"); 16 | case "Vishaka" -> System.out.println("You are going to get retired!"); 17 | default -> System.out.println("Enjoy Your life!"); 18 | } 19 | System.out.println("Thanks for using my Java Code!"); 20 | 21 | 22 | /* 23 | int age; 24 | System.out.println("Enter Your Age"); 25 | Scanner sc = new Scanner(System.in); 26 | age = sc.nextInt(); 27 | if (age>56){ 28 | System.out.println("You are experienced!"); 29 | } 30 | else if(age>46){ 31 | System.out.println("You are semi-experienced!"); 32 | } 33 | else if(age>36){ 34 | System.out.println("You are semi-semi-experienced!"); 35 | } 36 | else{ 37 | System.out.println("You are not experienced"); 38 | } 39 | if(age>2){ 40 | System.out.println("You are not a baby!"); 41 | } 42 | */ 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /19.Chapter_4_practice_set/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Practice Questions On Conditionals & Switch Case 2 | 3 | 1. What will be the output of this program 4 | ``` 5 | int a = 10; 6 | if (a=11) 7 | System.out.println(“I am 11”); 8 | else 9 | System.out.println(“I am not 11”); 10 | ``` 11 | 2. Write a program to find out whether a student is pass or fail; if it requires a total of 40% and at least 33% in each subject to pass. Assume 3 subjects and take marks as input from the user. 12 | 13 | 3. Calculate income tax paid by an employee to the government as per the slabs mentioned below: 14 | 15 | ![image](https://user-images.githubusercontent.com/70385488/151167797-264bcd0d-feb1-4b57-add6-b09d3eb64166.png) 16 | 17 | 4. Write a Java program to find out the day of the week given the number [1 for Monday, 2 for Tuesday … and so on!] 18 | 5. Write a Java program to find whether a year entered by the user is a leap year or not. 19 | 6. Write a program to find out the type of website from the URL: 20 | - .com – commercial website 21 | - .org – organization website 22 | - .in – Indian website 23 | 24 | #### Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-19/ch4practiceset.pdf) 25 | 26 | #### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-19/UltimateJavaCheatSheet.pdf) 27 | -------------------------------------------------------------------------------- /19.Chapter_4_practice_set/cwh_19_ch4_ps_pr_01.java: -------------------------------------------------------------------------------- 1 | public class cwh_19_ch4_ps_pr_01 { 2 | public static void main(String args[]) { 3 | int a = 10; 4 | if (a = 11) { 5 | system.out.println("I am 11"); 6 | } 7 | else { 8 | system.out.println("I am not 11"); 9 | } 10 | // This program will through an error beacuse in if conditon 11 | // a=11 is an assignment operator and we can't put assignment operator in if condition 12 | // if we use a == 11 then will properly run the program 13 | } 14 | } -------------------------------------------------------------------------------- /19.Chapter_4_practice_set/cwh_19_ch4_ps_pr_02.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class cwh_19_ch4_ps_pr_02 { 4 | public static void main(String args[]) { 5 | byte m1, m2, m3; 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter your marks in Physics"); 8 | m1 = sc.nextByte(); 9 | 10 | System.out.println("Enter your marks in Chemistry"); 11 | m2= sc.nextByte(); 12 | 13 | System.out.println("Enter your marks in Mathematics"); 14 | m3 = sc.nextByte(); 15 | float avg = (m1+m2+m3)/3.0f; 16 | System.out.println("Your Overall percentage is: " + avg); 17 | if(avg>=40 && m1>=33 && m2>=33 && m3>=33){ 18 | System.out.println("Congratulations, You have been promoted"); 19 | } 20 | else{ 21 | System.out.println("Sorry, You have not been promoted! Please try again."); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /19.Chapter_4_practice_set/cwh_19_ch4_ps_pr_03.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class cwh_19_ch4_ps_pr_03 { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.println("Enter your income in Lakhs per annum"); 7 | float tax = 0; 8 | float income = sc.nextFloat(); 9 | if(income<=2.5){ 10 | tax = tax + 0; 11 | } 12 | else if(income>2.5f && income <= 5f){ 13 | tax = tax + 0.05f * (income - 2.5f); 14 | } 15 | else if(income>5f && income <= 10.0f){ 16 | tax = tax + 0.05f * (5.0f - 2.5f); 17 | tax = tax + 0.2f * (income - 5f); 18 | } 19 | else if(income>10.0f){ 20 | tax = tax + 0.05f * (5.0f - 2.5f); 21 | tax = tax + 0.2f * (10.0f - 5f); 22 | tax = tax + 0.3f * (income - 10.0f); 23 | } 24 | 25 | System.out.println("The total tax paid by the employee is: " + tax); 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /19.Chapter_4_practice_set/cwh_19_ch4_ps_pr_04.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class cwh_19_ch4_ps_pr_04 { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | int day = sc.nextInt(); 7 | 8 | switch (day){ 9 | case 1 -> System.out.println("Monday"); 10 | case 2 -> System.out.println("Tuesday"); 11 | case 3 -> System.out.println("Wednesday"); 12 | case 4 -> System.out.println("Thursday"); 13 | case 5 -> System.out.println("Friday"); 14 | case 6 -> System.out.println("Saturday"); 15 | case 7 -> System.out.println("Sunday"); 16 | } 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /19.Chapter_4_practice_set/cwh_19_ch4_ps_pr_05.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class cwh_19_ch4_ps_pr_05 { 4 | public static void main(String args[]) { 5 | Scanner input = new Scanner(System.in); 6 | System.out.print("Enter year : "); 7 | int year = input.nextInt(); 8 | 9 | if((year%400==0) || (year%4==0 && year%100 !=0)) 10 | System.out.println(year+" is a Leap year"); 11 | else 12 | System.out.println(year+" is not a leap year"); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /19.Chapter_4_practice_set/cwh_19_ch4_ps_pr_06.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class cwh_19_ch4_ps_pr_06 { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | String website = sc.next(); 7 | if (website.endsWith(".org")) { 8 | System.out.println("This is an organizational website"); 9 | } 10 | else if (website.endsWith(".com")) { 11 | System.out.println("This is a Commercial website"); 12 | } 13 | else if (website.endsWith(".in")) { 14 | System.out.println("This is an Indian website"); 15 | } 16 | else { 17 | System.out.println("Error 404"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /2.Basic_structure_of_java_program/Hello.java: -------------------------------------------------------------------------------- 1 | public class Hello { 2 | public static void main(String args[]) { 3 | System.out.println("Hello World"); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /2.Basic_structure_of_java_program/IntroToJava.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/2.Basic_structure_of_java_program/IntroToJava.pdf -------------------------------------------------------------------------------- /2.Basic_structure_of_java_program/README.md: -------------------------------------------------------------------------------- 1 | # Basic Structure of a Java Program: Understanding our First Java Hello World Program 2 | 3 | ## Basic Structure of a Java Program 4 | 5 | ``` 6 | package com.company; // Groups classes 7 | public class Main{ // Entrypoint into the application 8 | public static void main(String[]args){ 9 | System.out.println(“Hello World”); 10 | } 11 | } 12 | ``` 13 | 14 | ## Working of the "Hello World" program shown above : 15 | 16 | ### 1. package com.company : 17 | - Packages are used to group the related classes. 18 | - The "Package" keyword is used to create packages in Java. 19 | - Here, com.company is the name of our package. 20 | 21 | ### 2. public class Main : 22 | - In Java, every program must contain a class. 23 | - The filename and name of the class should be the same. 24 | - Here, we've created a class named "Main". 25 | - It is the entry point to the application. 26 | 27 | ### 3. public static void main(String[]args){..} : 28 | - This is the main() method of our Java program. 29 | - Every Java program must contain the main() method. 30 | 31 | ### 4. System.out.println("Hello World"): 32 | - The above code is used to display the output on the screen. 33 | - Anything passed inside the inverted commas is printed on the screen as plain text. 34 | 35 | ### 5. Naming Conventions 36 | - For classes, we use Pascal Convention. The first and Subsequent characters from a word are capital letters (uppercase). 37 | - Example: Main, MyScanner, MyEmployee, CodeWithHarry 38 | - For functions and variables, we use camelCaseConvention. Here the first character is lowercase, and the subsequent characters are uppercase like myScanner, myMarks, CodeWithHarry 39 | 40 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-2/IntroToJava.pdf) 41 | 42 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-2/UltimateJavaCheatSheet.pdf) 43 | -------------------------------------------------------------------------------- /20.Java_Exercise_2/README.md: -------------------------------------------------------------------------------- 1 | # Java Programming Exercise 2: Rock, Paper Scissors Game in Java 2 | 3 | ### Exercise 2 4 | 5 | - Create a simple Rock, Paper Scissors game in Java. (#Use Conditional Statements) 6 | 7 | ### Solution 8 | 9 | ``` 10 | import java.util.*; 11 | import java.util.Random; 12 | 13 | public class RPS { 14 | public static void main(String args[]) { 15 | Scanner sc = new Scanner(System.in); 16 | Random random = new Random(); 17 | 18 | int cpu_score = 0; 19 | int player_score = 0; 20 | 21 | while (true) { 22 | 23 | int computer = random.nextInt(3); 24 | 25 | String cpu_choice = ""; 26 | 27 | if (computer == 0) { 28 | cpu_choice += "rock"; 29 | } 30 | else if (computer == 1) { 31 | cpu_choice += "paper"; 32 | } 33 | else { 34 | cpu_choice += "scissor"; 35 | } 36 | 37 | System.out.println("Please enter your choice"); 38 | System.out.println("Enter rock || paper || scissor || end ||"); 39 | String player = sc.next(); 40 | 41 | if (player.equals(cpu_choice)) { 42 | System.out.println("It's Draw"); 43 | } 44 | 45 | else if (player.equals("rock")) { 46 | if (cpu_choice.equals("paper")) { 47 | System.out.println("CPU Wins..."); 48 | cpu_score += 1; 49 | } 50 | else { 51 | System.out.println("Player Wins..."); 52 | player_score += 1; 53 | } 54 | } 55 | 56 | else if (player.equals("paper")) { 57 | if (cpu_choice.equals("rock")) { 58 | System.out.println("Player Wins..."); 59 | player_score += 1; 60 | } 61 | else { 62 | System.out.println("CPU Wins..."); 63 | cpu_score += 1; 64 | } 65 | } 66 | 67 | else if (player.equals("scissor")) { 68 | if (cpu_choice.equals("paper")) { 69 | System.out.println("Player Wins..."); 70 | player_score += 1; 71 | } 72 | else { 73 | System.out.println("CPU Wins..."); 74 | cpu_score += 1; 75 | } 76 | } 77 | else if (player.equals("end")) { 78 | System.out.println("The final scores are as follows : "); 79 | System.out.println("CPU Score : "+ cpu_score); 80 | System.out.println("Player Score : " + player_score); 81 | System.out.println("Thanks for playing my game."); 82 | break; 83 | } 84 | 85 | } 86 | 87 | } 88 | } 89 | ``` 90 | 91 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-20/UltimateJavaCheatSheet.pdf)** 92 | -------------------------------------------------------------------------------- /20.Java_Exercise_2/RPS.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.Random; 3 | 4 | public class RPS { 5 | public static void main(String args[]) { 6 | Scanner sc = new Scanner(System.in); 7 | Random random = new Random(); 8 | 9 | int cpu_score = 0; 10 | int player_score = 0; 11 | 12 | while (true) { 13 | 14 | int computer = random.nextInt(3); 15 | 16 | String cpu_choice = ""; 17 | 18 | if (computer == 0) { 19 | cpu_choice += "rock"; 20 | } 21 | else if (computer == 1) { 22 | cpu_choice += "paper"; 23 | } 24 | else { 25 | cpu_choice += "scissor"; 26 | } 27 | 28 | System.out.println("Please enter your choice"); 29 | System.out.println("Enter rock || paper || scissor || end ||"); 30 | String player = sc.next(); 31 | 32 | if (player.equals(cpu_choice)) { 33 | System.out.println("It's Draw"); 34 | } 35 | 36 | else if (player.equals("rock")) { 37 | if (cpu_choice.equals("paper")) { 38 | System.out.println("CPU Wins..."); 39 | cpu_score += 1; 40 | } 41 | else { 42 | System.out.println("Player Wins..."); 43 | player_score += 1; 44 | } 45 | } 46 | 47 | else if (player.equals("paper")) { 48 | if (cpu_choice.equals("rock")) { 49 | System.out.println("Player Wins..."); 50 | player_score += 1; 51 | } 52 | else { 53 | System.out.println("CPU Wins..."); 54 | cpu_score += 1; 55 | } 56 | } 57 | 58 | else if (player.equals("scissor")) { 59 | if (cpu_choice.equals("paper")) { 60 | System.out.println("Player Wins..."); 61 | player_score += 1; 62 | } 63 | else { 64 | System.out.println("CPU Wins..."); 65 | cpu_score += 1; 66 | } 67 | } 68 | else if (player.equals("end")) { 69 | System.out.println("The final scores are as follows : "); 70 | System.out.println("CPU Score : "+ cpu_score); 71 | System.out.println("Player Score : " + player_score); 72 | System.out.println("Thanks for playing my game."); 73 | break; 74 | } 75 | 76 | } 77 | 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /21.While_Loops/Chapter_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/21.While_Loops/Chapter_5.pdf -------------------------------------------------------------------------------- /21.While_Loops/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: While Loops in Java 2 | - In programming languages, loops are used to execute a particular statement/set of instructions again and again. 3 | - The execution of the loop starts when some conditions become true. 4 | - For example, print 1 to 1000, print multiplication table of 7, etc. 5 | - Loops make it easy for us to tell the computer that a given set of instructions need to be executed repeatedly. 6 | 7 | ### Types of Loops : 8 | - Primarily, there are three types of loops in Java: 9 | - While loop 10 | - do-while loop 11 | - for loop 12 | - Let's look into these, one by one. 13 | 14 | ### While loops : 15 | - The while loop in Java is used when we need to execute a block of code again and again based on a given boolean condition. 16 | - Use a while loop if the exact number of iterations is not known. 17 | - If the condition never becomes false, the while loop keeps getting executed. Such a loop is known as an infinite loop. 18 | ``` 19 | /* 20 | while (Boolean condition) 21 | 22 | { 23 | 24 | // Statements -> This keeps executing as long as the condition is true. 25 | 26 | } 27 | */ 28 | ``` 29 | - Example 30 | ``` 31 | int i=10; 32 | while(i>0){ 33 | System.out.println(i); 34 | i--; 35 | } 36 | ``` 37 | 38 | ### Flow control of while loop : 39 | 40 | ![image](https://user-images.githubusercontent.com/70385488/151346565-307ba28b-49a1-4f2a-8ae1-3fc1241722ee.png) 41 | 42 | **Quick Quiz:** Write a program to print natural numbers from 100 to 200. 43 | 44 | ### Code as described in the video : 45 | 46 | ``` 47 | public class cwh_21_ch5_loops { 48 | public static void main(String[] args) { 49 | System.out.println(1); 50 | System.out.println(2); 51 | System.out.println(3); 52 | 53 | System.out.println("Using Loops:"); 54 | int i = 100; 55 | while(i<=200){ 56 | System.out.println(i); 57 | i++; 58 | } 59 | System.out.println("Finish Running While Loop!"); 60 | 61 | // while(true){ 62 | // System.out.println("I am an infinite while loop!"); 63 | // } 64 | } 65 | } 66 | ``` 67 | 68 | - This is all for this tutorial, and we will discuss the do-while loop in the next tutorial. 69 | 70 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-21/Chapter_5.pdf)** 71 | 72 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-21/UltimateJavaCheatSheet.pdf)** 73 | -------------------------------------------------------------------------------- /21.While_Loops/cwh_21_ch5_loops.java: -------------------------------------------------------------------------------- 1 | public class cwh_21_ch5_loops { 2 | public static void main(String[] args) { 3 | System.out.println(1); 4 | System.out.println(2); 5 | System.out.println(3); 6 | 7 | System.out.println("Using Loops:"); 8 | int i = 100; 9 | while(i<=200){ 10 | System.out.println(i); 11 | i++; 12 | } 13 | System.out.println("Finish Running While Loop!"); 14 | 15 | // while(true){ 16 | // System.out.println("I am an infinite while loop!"); 17 | // } 18 | } 19 | } -------------------------------------------------------------------------------- /22.Do_While_Loop/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: The do-while loop in Java 2 | 3 | ### Do-while loop: 4 | - Do- while loop is similar to a while loop except for the fact that it is guaranteed to execute at least once. 5 | - Use a do-while loop when the exact number of iterations is unknown, but you need to execute a code block at least once. 6 | - After executing a part of a program for once, the rest of the code gets executed on the basis of a given boolean condition. 7 | - Syntax : 8 | ``` 9 | /* do { 10 | 11 | //code 12 | 13 | } while (condition); //Note this semicolon */ 14 | ``` 15 | - Example 16 | ``` 17 | int i=1; 18 | do{ 19 | System.out.println(i); 20 | i++; 21 | }while(i<=10); 22 | ``` 23 | 24 | ### Difference Between while loop and do-while loop : 25 | - while – checks the condition & executes the code. 26 | - do-while – executes the code at least once and then checks the condition. Because of this reason, the code in the do-while loop executes at least once, even if the condition fails. 27 | 28 | ### Flow control of do-while loop : 29 | 30 | ![image](https://user-images.githubusercontent.com/70385488/151350896-9263940f-4225-4c6b-a7b6-180d2b286919.png) 31 | 32 | **Quick Quiz:** Write a program to print first n natural numbers using a do-while loop. 33 | 34 | ### Code as described in the video : 35 | 36 | ``` 37 | public class cwh_22_ch4_do_while { 38 | public static void main(String[] args) { 39 | // int a = 0; 40 | // while(a<5){ 41 | // System.out.println(a); 42 | // a++; 43 | // } 44 | int b = 10; 45 | do { 46 | System.out.println(b); 47 | b++; 48 | }while(b<5); 49 | 50 | int c = 1; 51 | do{ 52 | System.out.println(c); 53 | c++; 54 | }while(c<=45); 55 | 56 | } 57 | } 58 | ``` 59 | 60 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-22/Chapter_5.pdf) 61 | 62 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-22/UltimateJavaCheatSheet.pdf) 63 | -------------------------------------------------------------------------------- /22.Do_While_Loop/cwh_22_ch4_do_while.java: -------------------------------------------------------------------------------- 1 | public class cwh_22_ch4_do_while { 2 | public static void main(String[] args) { 3 | // int a = 0; 4 | // while(a<5){ 5 | // System.out.println(a); 6 | // a++; 7 | // } 8 | int b = 10; 9 | do { 10 | System.out.println(b); 11 | b++; 12 | }while(b<5); 13 | 14 | int c = 1; 15 | do{ 16 | System.out.println(c); 17 | c++; 18 | }while(c<=45); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /23.For_Loops/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: The for Loop in Java 2 | 3 | ### For loop: 4 | - For loop in java is used to iterate a block of code multiple times. 5 | - Use for loop only when the exact number of iterations needed is already known to you. 6 | - Syntax : 7 | ``` 8 | /* for (initialize; check_bool_expression; update){ 9 | //code; 10 | } */ 11 | ``` 12 | - Initializer: Initializes the value of a variable. This part is executed only once. 13 | - check_bool_expression: The code inside the for loop is executed only when this condition returns true. 14 | - update: Updates the value of the initial variable. 15 | - Example : 16 | ``` 17 | for (i=7; i!=0; i--){ 18 | System.out.println(i); 19 | } 20 | ``` 21 | - The above for loop initializes the value of i=7 and keeps printing as well as decrementing the value of i till i do not get equals to 0. 22 | 23 | ### Flow control of for loop : 24 | 25 | ![image](https://user-images.githubusercontent.com/70385488/151354811-2f095c94-83f7-4579-9f65-0898531cdbc5.png) 26 | 27 | **Quick Quiz 1:** Write a program to print first n odd numbers using a for loop. 28 | 29 | **Quick Quiz 2:** Write a program to print first n natural numbers in reverse order. 30 | 31 | ### Code as described in the video : 32 | 33 | ``` 34 | public class cwh_23_for_loop { 35 | public static void main(String[] args) { 36 | // for (int i=1; i<=10; i++){ 37 | // System.out.println(i); 38 | // } 39 | // 2i = Even Numbers = 0, 2, 4, 6, 8 40 | // 2i+1 = Odd Numbers = 1, 3, 5, 7, 9 41 | //int n = 3; 42 | //for (int i =0; i0;i--){ 17 | if(i==7){ 18 | break; //break the loop 19 | } 20 | System.out.println(i); 21 | } 22 | } 23 | } 24 | ``` 25 | - Output 26 | ``` 27 | 10 28 | 9 29 | 8 30 | ``` 31 | 32 | ### Flow control of break statement : 33 | 34 | ![image](https://user-images.githubusercontent.com/70385488/151574136-1ab97039-3cf9-44c6-b08b-e684a4e2fd6a.png) 35 | 36 | ### Continue statement : 37 | - The continue statement is used to immediately move to the next iteration of the loop. 38 | - The control is taken to the next iteration thus skipping everything below ‘continue’ inside the loop for that iteration. 39 | - Syntax : 40 | ``` 41 | continue; 42 | ``` 43 | 44 | Example to demonstrate the use of continue statement inside a for loop : 45 | ``` 46 | public class CWH_continue { 47 | public static void main(String[] args) { 48 | 49 | for(int i=7;i>0;i--){ 50 | if(i==3){ 51 | continue;//continue skips the rest statement 52 | } 53 | System.out.println(i); 54 | } 55 | } 56 | } 57 | ``` 58 | - Output 59 | ``` 60 | 7 61 | 6 62 | 5 63 | 4 64 | 2 65 | 1 66 | ``` 67 | 68 | ### Flow control of continue statement : 69 | 70 | ![image](https://user-images.githubusercontent.com/70385488/151574682-9a133237-a80c-4f69-b65c-7169291ce573.png) 71 | 72 | ### In a Nut Shell.… 73 | - break statement completely exits the loop 74 | - continue statement skips the particular iteration of the loop. 75 | 76 | ### Code as Described in the Video 77 | 78 | ``` 79 | package com.company; 80 | 81 | public class cwh_24_break_and_continue { 82 | public static void main(String[] args) { 83 | // Break and continue using loops! 84 | // for (int i=0;i<50;i++){ 85 | // System.out.println(i); 86 | // System.out.println("Java is great"); 87 | // if(i==2){ 88 | // System.out.println("Ending the loop"); 89 | // break; 90 | // } 91 | // } 92 | // int i=0; 93 | // do{ 94 | // System.out.println(i); 95 | // System.out.println("Java is great"); 96 | // if(i==2){ 97 | // System.out.println("Ending the loop"); 98 | // break; 99 | // } 100 | // i++; 101 | // }while(i<5); 102 | // System.out.println("Loop ends here"); 103 | 104 | 105 | // for(int i=0;i<50;i++){ 106 | // if(i==2){ 107 | // System.out.println("Ending the loop"); 108 | // continue; 109 | // } 110 | // System.out.println(i); 111 | // System.out.println("Java is great"); 112 | // } 113 | int i=0; 114 | do{ 115 | i++; 116 | if(i==2){ 117 | System.out.println("Ending the loop"); 118 | continue; 119 | } 120 | System.out.println(i); 121 | System.out.println("Java is great"); 122 | 123 | }while(i<5); 124 | System.out.println("Loop ends here"); 125 | } 126 | } 127 | ``` 128 | 129 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-24/Chapter_5.pdf)** 130 | 131 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-24/UltimateJavaCheatSheet.pdf)** 132 | -------------------------------------------------------------------------------- /24.Break_And_Continue/cwh_24_break_and_continue.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | public class cwh_24_break_and_continue { 4 | public static void main(String[] args) { 5 | // Break and continue using loops! 6 | // for (int i=0;i<50;i++){ 7 | // System.out.println(i); 8 | // System.out.println("Java is great"); 9 | // if(i==2){ 10 | // System.out.println("Ending the loop"); 11 | // break; 12 | // } 13 | // } 14 | // int i=0; 15 | // do{ 16 | // System.out.println(i); 17 | // System.out.println("Java is great"); 18 | // if(i==2){ 19 | // System.out.println("Ending the loop"); 20 | // break; 21 | // } 22 | // i++; 23 | // }while(i<5); 24 | // System.out.println("Loop ends here"); 25 | 26 | 27 | // for(int i=0;i<50;i++){ 28 | // if(i==2){ 29 | // System.out.println("Ending the loop"); 30 | // continue; 31 | // } 32 | // System.out.println(i); 33 | // System.out.println("Java is great"); 34 | // } 35 | int i=0; 36 | do{ 37 | i++; 38 | if(i==2){ 39 | System.out.println("Ending the loop"); 40 | continue; 41 | } 42 | System.out.println(i); 43 | System.out.println("Java is great"); 44 | 45 | }while(i<5); 46 | System.out.println("Loop ends here"); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /25.Chapter_5_Practice_Set/Chapter_5__Practice_Set.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/25.Chapter_5_Practice_Set/Chapter_5__Practice_Set.pdf -------------------------------------------------------------------------------- /25.Chapter_5_Practice_Set/README.md: -------------------------------------------------------------------------------- 1 | # Java tutorial: Practice Questions on Loops 2 | 3 | **Question 1:** Write a program to print the following pattern : 4 | ``` 5 | **** 6 | 7 | *** 8 | 9 | ** 10 | 11 | * 12 | ``` 13 | **Question 2:** Write a program to sum first n even numbers using a while loop. 14 | 15 | **Question 3:** Write a program to print the multiplication table of a given number n. 16 | 17 | **Question 4:** Write a program to print a multiplication table of 10 in reverse order. 18 | 19 | **Question 5:** Write a program to find the factorial of a given number using for loops. 20 | 21 | **Question 6:** Repeat problem 5 using a while loop. 22 | 23 | **Question 7:** Repeat problem 1 using for/while loop. 24 | 25 | **Question 8:** What can be done using one type of loop can also be done using the other two types of loops - True or False. 26 | 27 | **Question 9:** Write a program to calculate the sum of the numbers occurring in the multiplication table of 8. 28 | 29 | **Question 10:** A do-while loop is executed: 30 | 31 | At least once 32 | At least twice 33 | At most once 34 | **Question 11:** Repeat problem 2 using for loop. 35 | 36 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-25/Chapter_5__Practice_Set.pdf)** 37 | 38 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-25/UltimateJavaCheatSheet.pdf)** 39 | -------------------------------------------------------------------------------- /25.Chapter_5_Practice_Set/cwh_25_ch5_ps_pr_01.java: -------------------------------------------------------------------------------- 1 | public class cwh_25_ch5_ps_pr_01 { 2 | public static void main(String[] args) { 3 | int n = 4; 4 | for (int i=n; i>0; i--){ 5 | for(int j=0;j 8 | 9 | ### Accessing Array Elements : 10 | - Array elements can be accessed as follows, 11 | ``` 12 | /* marks[0] = 100 //Note that index starts from 0 13 | marks[1] = 70 14 | . 15 | . 16 | marks[4] = 98 */ 17 | ``` 18 | 19 | - So in a nut shell, this is how array works: 20 | ``` 21 | int[] marks; //Declaration! 22 | marks = new int[5]; //Memory allocation! 23 | 24 | int[] marks = new int[5]; //Declaration + Memory allocation! 25 | 26 | int[] marks = {100,70,80,71,98} // Declare + Initialize! 27 | ``` 28 | **Note :** Array indices start from 0 and go till (n-1) where n is the size of the array. 29 | 30 | ### Array length : 31 | - Unlike C/C++, we don't need to use the sizeof() operator to get the length of arrays in Java because arrays are objects in Java therefore we can use the length property. 32 | 33 | marks.length //Gives 5 if marks is a reference to an array with 5 elements 34 | 35 | ### Displaying an Array : 36 | - An array can be displayed using a for loop: 37 | ``` 38 | for (int i=0; i=0; i--) { 50 | System.out.println(marks[i]); 51 | } 52 | } 53 | } 54 | ``` 55 | 56 | ### Code as Described in the Video 57 | ``` 58 | package com.company; 59 | 60 | public class cwh_26_arrays { 61 | public static void main(String[] args) { 62 | /* Classroom of 500 students - You have to store marks of these 500 students 63 | You have 2 options: 64 | 1. Create 500 variables 65 | 2. Use Arrays (recommended) 66 | */ 67 | // There are three main ways to create an array in Java 68 | // 1. Declaration and memory allocation 69 | // int [] marks = new int[5]; 70 | 71 | // 2. Declaration and then memory allocation 72 | // int [] marks; 73 | // marks = new int[5]; 74 | // Initialization 75 | // marks[0] = 100; 76 | // marks[1] = 60; 77 | // marks[2] = 70; 78 | // marks[3] = 90; 79 | // marks[4] = 86; 80 | 81 | // 3. Declaration, memory allocation and initialization together 82 | int [] marks = {98, 45, 79, 99, 80}; 83 | 84 | // marks[5] = 96; - throws an error 85 | System.out.println(marks[4]); 86 | } 87 | } 88 | ``` 89 | 90 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-26/Chapter_6_Arrays.pdf)** 91 | 92 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-26/UltimateJavaCheatSheet.pdf)** 93 | -------------------------------------------------------------------------------- /26.Introduction_To_Arrays/cwh_26_arrays.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | public class cwh_26_arrays { 4 | public static void main(String[] args) { 5 | /* Classroom of 500 students - You have to store marks of these 500 students 6 | You have 2 options: 7 | 1. Create 500 variables 8 | 2. Use Arrays (recommended) 9 | */ 10 | // There are three main ways to create an array in Java 11 | // 1. Declaration and memory allocation 12 | // int [] marks = new int[5]; 13 | 14 | // 2. Declaration and then memory allocation 15 | // int [] marks; 16 | // marks = new int[5]; 17 | // Initialization 18 | // marks[0] = 100; 19 | // marks[1] = 60; 20 | // marks[2] = 70; 21 | // marks[3] = 90; 22 | // marks[4] = 86; 23 | 24 | // 3. Declaration, memory allocation and initialization together 25 | int [] marks = {98, 45, 79, 99, 80}; 26 | 27 | // marks[5] = 96; - throws an error 28 | System.out.println(marks[4]); 29 | } 30 | } -------------------------------------------------------------------------------- /27.For_Each_Loop/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: For Each Loop in Java 2 | - For each loop is an enhanced version of for loop. 3 | - It travels each element of the data structure one by one. 4 | - Note that you can not skip any element in for loop and it is also not possible to traverse elements in reverse order with the help of for each loop. 5 | - It increases the readability of the code. 6 | - If you just want to simply traverse an array from start to end then it is recommended to use for each loop. 7 | - Syntax : 8 | ``` 9 | /* for (int element:Arr) { 10 | Sout(element); //Prints all the elements 11 | } */ 12 | ``` 13 | - Example 14 | ``` 15 | class CWH_forEachLoop{ 16 | public static void main(String args[]){ 17 | //declaring an array 18 | int arr[]={1,2,3,3,4,5}; 19 | //traversing the array with for-each loop 20 | for(int i:arr){ 21 | System.out.println(i); 22 | } 23 | } 24 | } 25 | ``` 26 | - Output 27 | ``` 28 | 1 29 | 2 30 | 3 31 | 4 32 | 5 33 | ``` 34 | 35 | ### Code as Described in the Video 36 | ``` 37 | package com.company; 38 | 39 | public class cwh_27_arrays { 40 | public static void main(String[] args) { 41 | 42 | /* 43 | float [] marks = {98.5f, 45.5f, 79.5f, 99.5f, 80.5f}; 44 | String [] students ={"Harry", "Rohan", "Shubham", "Lovish"}; 45 | System.out.println(students.length); 46 | System.out.println(students[2]); 47 | */ 48 | 49 | int [] marks = {98, 45, 79, 99, 80}; 50 | // System.out.println(marks.length); 51 | 52 | // Displaying the Array (Naive way) 53 | System.out.println("Printing using Naive way"); 54 | System.out.println(marks[0]); 55 | System.out.println(marks[1]); 56 | System.out.println(marks[2]); 57 | System.out.println(marks[3]); 58 | System.out.println(marks[4]); 59 | 60 | // Displaying the Array (for loop) 61 | System.out.println("Printing using for loop"); 62 | for(int i=0;i=0;i--){ 69 | System.out.println(marks[i]); 70 | } 71 | 72 | // Quick Quiz: Displaying the Array (for-each loop) 73 | System.out.println("Printing using for-each loop"); 74 | for(int element: marks){ 75 | System.out.println(element); 76 | } 77 | 78 | } 79 | } 80 | ``` 81 | 82 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-27/Chapter_6_Arrays.pdf)** 83 | 84 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-27/UltimateJavaCheatSheet.pdf)** 85 | -------------------------------------------------------------------------------- /27.For_Each_Loop/cwh_27_arrays.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | public class cwh_27_arrays { 4 | public static void main(String[] args) { 5 | 6 | /* 7 | float [] marks = {98.5f, 45.5f, 79.5f, 99.5f, 80.5f}; 8 | String [] students ={"Harry", "Rohan", "Shubham", "Lovish"}; 9 | System.out.println(students.length); 10 | System.out.println(students[2]); 11 | */ 12 | 13 | int [] marks = {98, 45, 79, 99, 80}; 14 | // System.out.println(marks.length); 15 | 16 | // Displaying the Array (Naive way) 17 | System.out.println("Printing using Naive way"); 18 | System.out.println(marks[0]); 19 | System.out.println(marks[1]); 20 | System.out.println(marks[2]); 21 | System.out.println(marks[3]); 22 | System.out.println(marks[4]); 23 | 24 | // Displaying the Array (for loop) 25 | System.out.println("Printing using for loop"); 26 | for(int i=0;i=0;i--){ 33 | System.out.println(marks[i]); 34 | } 35 | 36 | // Quick Quiz: Displaying the Array (for-each loop) 37 | System.out.println("Printing using for-each loop"); 38 | for(int element: marks){ 39 | System.out.println(element); 40 | } 41 | 42 | } 43 | } -------------------------------------------------------------------------------- /28.Multidimensional_Arrays/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Multidimensional Arrays in Java 2 | - Multidimensional Arrays are an Array of Arrays. Each elements of an M-D array is an array itself. Marks in the previous example was a 1-D array. 3 | 4 | ### Multidimensional 2-D Array 5 | - A 2-D array can be created as follows: 6 | 7 | int [][] flats = new int[2][3] //A 2-D array of 2 rows + 3 columns 8 | 9 | - We can add elements to this array as follows 10 | 11 | flats[0][0] = 100 12 | flats[0][1] = 101 13 | flats[0][2] = 102 14 | // … & so on! 15 | 16 | - This 2-D array can be visualized as follows: 17 | 18 | 19 | 20 | - Similarly, a 3-D array can be created as follows: 21 | 22 | String[][][] arr = new String [2][3][4] 23 | 24 | ### Code as Described in the Video 25 | 26 | ``` 27 | package com.company; 28 | 29 | public class cwh_28_multi_dim_arrays { 30 | public static void main(String[] args) { 31 | int [] marks; // A 1-D Array 32 | int [][] flats; // A 2-D Array 33 | flats = new int [2][3]; 34 | flats[0][0] = 101; 35 | flats[0][1] = 102; 36 | flats[0][2] = 103; 37 | flats[1][0] = 201; 38 | flats[1][1] = 202; 39 | flats[1][2] = 203; 40 | 41 | // Displaying the 2-D Array (for loop) 42 | System.out.println("Printing a 2-D array using for loop"); 43 | for(int i=0;imax){ 7 | max = e; 8 | } 9 | } 10 | System.out.println("the value of the maximum element in this array is: "+ max); 11 | } 12 | } -------------------------------------------------------------------------------- /29.Chapter_6_Practice_Set/cwh_29_ps_pr_07.java: -------------------------------------------------------------------------------- 1 | public class cwh_29_ps_pr_07 { 2 | public static void main(String[] args) { 3 | boolean isSorted = true; 4 | int [] arr = {1, 12, 3, 4, 5, 34, 67}; 5 | for(int i=0; i arr[i+1]){ 7 | isSorted = false; 8 | break; 9 | } 10 | } 11 | if(isSorted){ 12 | System.out.println("The Array is sorted"); 13 | } 14 | else{ 15 | System.out.println("The Array is not sorted"); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /3.Variables_and_data_types/JavaChapter1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/3.Variables_and_data_types/JavaChapter1.pdf -------------------------------------------------------------------------------- /3.Variables_and_data_types/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Variables and Data Types in Java Programming 2 | - Just like we have some rules that we follow to speak English (the grammar), we have some rules to follow while writing a Java program. This set of these rules is called syntax. It’s like Vocabulary and Grammar of Java. 3 | 4 | ### Variables 5 | - A variable is a container that stores a value. 6 | - This value can be changed during the execution of the program. 7 | - Example: int number = 8; (Here, int is a data type, the number is the variable name, and 8 is the value it contains/stores). 8 | 9 | ### Rules for declaring a variable name 10 | - We can choose a name while declaring a Java variable if the following rules are followed: 11 | - Must not begin with a digit. (E.g., 1arry is an invalid variable) 12 | - Name is case sensitive. (Harry and harry are different) 13 | - Should not be a keyword (like Void). 14 | - White space is not allowed. (int Code With Harry is invalid) 15 | - Can contain alphabets, $character, _character, and digits if the other conditions are met. 16 | 17 | ### Data Types 18 | Data types in Java fall under the following categories 19 | - Primitive Data Types (Intrinsic) 20 | - Non-Primitive Data Types (Derived) 21 | 22 | ### Primitive Data Types 23 | - Java is statically typed, i.e., variables must be declared before use. Java supports 8 primitive data types: 24 | 25 | ![image](https://user-images.githubusercontent.com/70385488/150118247-b3a99c81-3e86-49b4-b937-980ba0c8dd7c.png) 26 | 27 | ### How to choose data types for our variables 28 | - In order to choose the data type, we first need to find the type of data we want to store. After that, we need to analyze the min & max value we might use. 29 | 30 | 31 | 32 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-3/JavaChapter1.pdf) 33 | 34 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-3/UltimateJavaCheatSheet.pdf) 35 | -------------------------------------------------------------------------------- /3.Variables_and_data_types/Total.java: -------------------------------------------------------------------------------- 1 | public class Total { 2 | public static void main(String args[]) { 3 | int x=10; 4 | int y=25; 5 | int z=63; 6 | int sum=x+y+z; 7 | 8 | System.out.println("Sum of x+y+z = " + sum); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /30.Overview_Of_IntelliJ_IDEA/README.md: -------------------------------------------------------------------------------- 1 | # How to Make IntelliJ IDEA look Amazing! 2 | - We all know that programming is a fascinating thing. When it comes to writing code, most coders prefer to customize their IDE's on their own to feel motivated, and it brings positive vibes from inside. 3 | - So, in this tutorial, I'm going to help you guys to customize IntelliJ IDEA IDE. 4 | - Follow the steps given below to customize your IDE: 5 | 6 | ### Step 1: Go to the File option in the menu and then choose the settings option. 7 | 8 | 9 | 10 | ### In settings, you'll find many options to customize your IntelliJ IDEA. 11 | 12 | 13 | 14 | ### To Change Theme of IDE: 15 | 16 | #### Choose the theme option in the appearance part of the settings. You can select any one of the four themes and can apply it. 17 | 18 | 19 | 20 | - There are many ways to customize the IDE. But for this tutorial purpose, I'll tell you the most efficient and best way to make the IDE look professional and premium by using the Material Theme UI plugin. 21 | - We will use the Material Theme UI plugin, and the steps to use the material UI are given below: 22 | 23 | ### Step 1: Go to File>Settings and then choose the plugins option. 24 | 25 | 26 | 27 | ### Step 2: Go to the Marketplace in plugins and search for "material" and the first result will be "Material Theme UI". Click on the install option to install it. 28 | 29 | 30 | 31 | ### Step 3: Install "Atom Material Icons" to make the IDE icons look attractive and cool. 32 | 33 | 34 | 35 | #### After installing both the plugins, click on the restart IDE option to restart the IDE so that all the changes can take effect. 36 | 37 | 38 | 39 | #### Now go to File > Settings > Appearance & Behaviour > Appearance, and there you'll get many of the new themes that you can use to make your IDE look professional. 40 | 41 | 42 | 43 | #### You have to choose any one of the themes and then click on the apply option to see its effect. 44 | 45 | 46 | 47 | ### To Change the Font: 48 | - You can even change the font type and size of IDE according to your choice. 49 | - In Appearance, you will get the option to use custom font. And there, you can choose any font and can adjust its size accordingly. 50 | 51 | 52 | 53 | ### To Change Icons: 54 | - In Appearance, you'll get the option of "Atom Material Icons Setting", and there you can change the icons of different things of IDE. 55 | 56 | 57 | 58 | - Using the methods mentioned above, you can change the look of the IDE the way you want. 59 | 60 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-30/UltimateJavaCheatSheet.pdf)** 61 | -------------------------------------------------------------------------------- /31.Methods/Ch7 Methods.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/31.Methods/Ch7 Methods.pdf -------------------------------------------------------------------------------- /31.Methods/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Methods in Java 2 | - Sometimes our program grows in size, and we want to separate the logic of the main method from the other methods. 3 | - For instance, if we calculate the average of a number pair 5 times, we can use methods to avoid repeating the logic. [DRY – Don’t Repeat Yourself] 4 | 5 | ### Syntax of a Method 6 | - A method is a function written inside a class. Since Java is an object-oriented language, we need to write the method inside some class. 7 | - Syntax of a method : 8 | ``` 9 | returnType nameOfMethod() { 10 | //Method body 11 | } 12 | ``` 13 | - The following method returns the sum of two numbers 14 | ``` 15 | int mySum(int a, int b) { 16 | int c = a+b; 17 | return c; //Return value 18 | } 19 | ``` 20 | 21 | - In the above method, int is the return data type of the mySum function. 22 | - mySum takes two parameters: int a and int b. 23 | - The sum of two values integer values(a and b) is stored in another integer value named 'c'. 24 | - mySum returns c. 25 | 26 | ### Calling a Method : 27 | - A method can be called by creating an object of the class in which the method exists followed by the method call: 28 | ``` 29 | Calc obj = new Calc(); //Object Creation 30 | 31 | obj.mySum(a , b); //Method call upon an object 32 | ``` 33 | - The values from the method call (a and b) are copied to the a and b of the function mySum. Thus even if we modify the values a and b inside the method, the values in the main method will not change. 34 | 35 | ### Void return type : 36 | - When we don’t want our method to return anything, we use void as the return type. 37 | 38 | ### Static keyword : 39 | - The static keyword is used to associate a method of a given class with the class rather than the object. 40 | - You can call a static method without creating an instance of the class. 41 | - In Java, the main() method is static, so that JVM can call the main() method directly without allocating any extra memory for object creation. 42 | - All the objects share the static method in a class. 43 | 44 | ### Process of method invocation in Java : 45 | - Consider the method Sum of the calculate class as given in the below code : 46 | 47 | ``` 48 | class calculate{ 49 | int sum(int a,int b){ 50 | return a+b; 51 | } 52 | ``` 53 | 54 | - The method is called like this: 55 | 56 | ``` 57 | class calculate{ 58 | int sum(int a,int b){ 59 | return a+b; 60 | } 61 | 62 | public static void main(String[] args) { 63 | 64 | calculate obj = new calculate(); 65 | int c = obj.sum(5,4); 66 | System.out.println(c); 67 | } 68 | } 69 | ``` 70 | ``` 71 | Output : 9 72 | ``` 73 | - Inside the main() method, we've created an object of the calculate class. 74 | - obj is the name of the calculate class. 75 | - Then, we've invoked the sum method and passed 5 and 4 as arguments. 76 | 77 | **Note:** In the case of Arrays, the reference is passed. The same is the case for object passing to methods. 78 | 79 | ### Code as described in the video: 80 | ``` 81 | package com.company; 82 | 83 | public class cwh_31_methods { 84 | 85 | static int logic(int x, int y){ 86 | int z; 87 | if(x>y){ 88 | z = x+y; 89 | } 90 | else { 91 | z = (x +y) * 5; 92 | } 93 | x = 566; 94 | return z; 95 | } 96 | 97 | 98 | public static void main(String[] args) { 99 | int a = 5; 100 | int b = 7; 101 | int c; 102 | // Method invocation using Object creation 103 | //cwh_31_methods obj = new cwh_31_methods(); 104 | //c = obj.logic(a, b); 105 | c = logic(a, b); 106 | System.out.println(a + " "+ b); 107 | int a1 = 2; 108 | int b1 = 1; 109 | int c1; 110 | c1 = logic(a1, b1); 111 | System.out.println(c); 112 | System.out.println(c1); 113 | } 114 | } 115 | ``` 116 | 117 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-31/Ch7.pdf)** 118 | 119 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-31/UltimateJavaCheatSheet.pdf)** 120 | -------------------------------------------------------------------------------- /31.Methods/cwh_31_methods.java: -------------------------------------------------------------------------------- 1 | public class cwh_31_methods { 2 | 3 | static int logic(int x, int y){ 4 | int z; 5 | if(x>y){ 6 | z = x+y; 7 | } 8 | else { 9 | z = (x +y) * 5; 10 | } 11 | x = 566; 12 | return z; 13 | } 14 | 15 | 16 | public static void main(String[] args) { 17 | int a = 5; 18 | int b = 7; 19 | int c; 20 | // Method invocation using Object creation 21 | //cwh_31_methods obj = new cwh_31_methods(); 22 | //c = obj.logic(a, b); 23 | c = logic(a, b); 24 | System.out.println(a + " "+ b); 25 | int a1 = 2; 26 | int b1 = 1; 27 | int c1; 28 | c1 = logic(a1, b1); 29 | System.out.println(c); 30 | System.out.println(c1); 31 | } 32 | } -------------------------------------------------------------------------------- /32.Method_Overloading/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Method Overloading in Java 2 | - In Java, it is possible for a class to contain two or more methods with the same name but with different parameters. Such methods are called Overloaded methods. 3 | - Method overloading is used to increase the readability of the program. 4 | ``` 5 | void foo() 6 | void foo(int a) //Overloaded function foo 7 | int foo(int a, int b) 8 | ``` 9 | 10 | ### Ways to perform method overloading : 11 | - In Java, method overloading can be performed by two ways listed below : 12 | 1.By changing the return type of the different methods 13 | 2. By changing the number of arguments accepted by the method 14 | 15 | - Now, let's have an example to understand the above ways of method overloading : 16 | 17 | ### 1. By changing the return type : 18 | - In the below example, we've created a class named calculate. 19 | - In the calculate class, we've two methods with the same name i.e. multiply 20 | - These two methods are overloaded because they have the same name but their return is different. 21 | - The return type of 1st method is int while the return type of the other method is double. 22 | 23 | ``` 24 | class calculate{ 25 | int multiply(int a,int b){ 26 | return a*b; 27 | } 28 | double multiply(double a,double b){ 29 | return a*b; 30 | } 31 | 32 | public static void main(String[] args) { 33 | 34 | calculate obj = new calculate(); 35 | int c = obj.multiply(5,4); 36 | double d = obj.multiply(5.1,4.2); 37 | System.out.println("Mutiply method : returns integer : " + c); 38 | System.out.println("Mutiply method : returns double : " + d); 39 | 40 | } 41 | } 42 | ``` 43 | 44 | ``` 45 | Output : 46 | Mutiply method : returns integer : 20 47 | Mutiply method : returns double : 21.419999999999998 48 | ``` 49 | 50 | ### 2. By changing the number of arguments passed : 51 | - Again, we've created two methods with the same name i.e., multiply 52 | - The return type of both the methods is int. 53 | - But, the first method 2 arguments and the other method accepts 3 arguments. 54 | - Example : 55 | 56 | ``` 57 | class calculate{ 58 | int multiply(int a,int b){ 59 | return a*b; 60 | } 61 | int multiply(int a,int b,int c){ 62 | return a*b*c; 63 | } 64 | 65 | public static void main(String[] args) { 66 | 67 | calculate obj = new calculate(); 68 | int c = obj.multiply(5,4); 69 | int d = obj.multiply(5,4,3); 70 | System.out.println(c); 71 | System.out.println(d); 72 | 73 | } 74 | } 75 | ``` 76 | 77 | ``` 78 | Output : 79 | 20 80 | 60 81 | ``` 82 | 83 | **Note:** Method overloading cannot be performed by changing the return type of methods. 84 | 85 | ### Code as described in the video: 86 | ``` 87 | public class cwh_32_method_overloading { 88 | static void foo(){ 89 | System.out.println("Good Morning bro!"); 90 | } 91 | 92 | static void foo(int a){ 93 | System.out.println("Good morning " + a + " bro!"); 94 | } 95 | 96 | static void foo(int a, int b){ 97 | System.out.println("Good morning " + a + " bro!"); 98 | System.out.println("Good morning " + b + " bro!"); 99 | } 100 | 101 | static void foo(int a, int b, int c){ 102 | System.out.println("Good morning " + a + " bro!"); 103 | System.out.println("Good morning " + b + " bro!"); 104 | } 105 | 106 | static void change(int a){ 107 | a = 98; 108 | } 109 | 110 | static void change2(int [] arr){ 111 | arr[0] = 98; 112 | } 113 | static void tellJoke(){ 114 | System.out.println("I invented a new word!\n" + 115 | "Plagiarism!"); 116 | } 117 | 118 | public static void main(String[] args) { 119 | // tellJoke(); 120 | 121 | // Case 1: Changing the Integer 122 | //int x = 45; 123 | //change(x); 124 | //System.out.println("The value of x after running change is: " + x); 125 | 126 | // Case 1: Changing the Array 127 | // int [] marks = {52, 73, 77, 89, 98, 94}; 128 | // change2(marks); 129 | // System.out.println("The value of x after running change is: " + marks[0]); 130 | 131 | 132 | // Method Overloading 133 | foo(); 134 | foo(3000); 135 | foo(3000, 4000); 136 | // Arguments are actual! 137 | 138 | 139 | } 140 | } 141 | ``` 142 | 143 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-32/Ch7.pdf)** 144 | 145 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-32/UltimateJavaCheatSheet.pdf)** 146 | -------------------------------------------------------------------------------- /32.Method_Overloading/cwh_32_method_overloading.java: -------------------------------------------------------------------------------- 1 | public class cwh_32_method_overloading { 2 | static void foo(){ 3 | System.out.println("Good Morning bro!"); 4 | } 5 | 6 | static void foo(int a){ 7 | System.out.println("Good morning " + a + " bro!"); 8 | } 9 | 10 | static void foo(int a, int b){ 11 | System.out.println("Good morning " + a + " bro!"); 12 | System.out.println("Good morning " + b + " bro!"); 13 | } 14 | 15 | static void foo(int a, int b, int c){ 16 | System.out.println("Good morning " + a + " bro!"); 17 | System.out.println("Good morning " + b + " bro!"); 18 | } 19 | 20 | static void change(int a){ 21 | a = 98; 22 | } 23 | 24 | static void change2(int [] arr){ 25 | arr[0] = 98; 26 | } 27 | static void tellJoke(){ 28 | System.out.println("I invented a new word!\n" + 29 | "Plagiarism!"); 30 | } 31 | 32 | public static void main(String[] args) { 33 | // tellJoke(); 34 | 35 | // Case 1: Changing the Integer 36 | //int x = 45; 37 | //change(x); 38 | //System.out.println("The value of x after running change is: " + x); 39 | 40 | // Case 1: Changing the Array 41 | // int [] marks = {52, 73, 77, 89, 98, 94}; 42 | // change2(marks); 43 | // System.out.println("The value of x after running change is: " + marks[0]); 44 | 45 | 46 | // Method Overloading 47 | foo(); 48 | foo(3000); 49 | foo(3000, 4000); 50 | // Arguments are actual! 51 | 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /33.Variable_Arguments/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Variable Arguments (VarArgs) in Java 2 | - In the previous tutorial, we discussed how we can [overload the methods in Java.](https://github.com/kishanrajput23/Java-Tutorials/blob/main/32.Method_Overloading/README.md) 3 | - Now, let's suppose you want to overload an "add" method. The "add" method will accept one argument for the first time and every time the number of arguments passed will be incremented by 1 till the number of arguments is equaled to 10. 4 | - One approach to solve this problem is to overload the "add" method 10 times. But is it the optimal approach? What if I say that the number of arguments passed will be incremented by 1 till the number of arguments is equaled to 1000. Do you think that it is good practice to overload a method 1000 times? 5 | - To solve this problem of method overloading, Variable Arguments(Varargs) were introduced with the release of JDK 5. 6 | - With the help of Varargs, we do not need to overload the methods. 7 | - Syntax : 8 | 9 | ``` 10 | /* 11 | public static void foo(int … arr) 12 | { 13 | // arr is available here as int[] arr 14 | } 15 | */ 16 | ``` 17 | 18 | - foo can be called with zero or more arguments like this: 19 | 1. foo(7) 20 | 2. foo(7,8,9) 21 | 3. foo(1,2,7,8,9) 22 | 23 | 24 | - Example of Varargs In Java : 25 | 26 | ``` 27 | class calculate { 28 | 29 | static int add(int ...arr){ 30 | int result = 0; 31 | for (int a : arr){ 32 | result = result + a; 33 | } 34 | return result; 35 | } 36 | 37 | public static void main(String[] args){ 38 | System.out.println(add(1,2)); 39 | System.out.println(add(2,3,4)); 40 | System.out.println(add(4,5,6)); 41 | } 42 | } 43 | ``` 44 | 45 | ``` 46 | Output : 47 | 3 48 | 9 49 | 15 50 | ``` 51 | 52 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-33/Ch7.pdf)** 53 | 54 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-33/UltimateJavaCheatSheet.pdf)** 55 | -------------------------------------------------------------------------------- /33.Variable_Arguments/calculate.java: -------------------------------------------------------------------------------- 1 | class calculate { 2 | 3 | static int add(int ...arr){ 4 | int result = 0; 5 | for (int a : arr){ 6 | result = result + a; 7 | } 8 | return result; 9 | } 10 | 11 | public static void main(String[] args){ 12 | System.out.println(add(1,2)); 13 | System.out.println(add(2,3,4)); 14 | System.out.println(add(4,5,6)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /34.Recursion/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Recursion in Java 2 | 3 | **One does not simply understand RECURSION without understanding RECURSION.** 4 | 5 | - In programming, recursion is a technique through which a function calls itself. 6 | - With the help of recursion, we can break down complex problems into simple problems. 7 | - Example: Factorial of a number 8 | ``` 9 | //factorial(n) = n*factorial(n-1) [n >= 1] 10 | ``` 11 | 12 | - Now, let's see an example to see the beauty of recursion in programming. First, we will print numbers from 1 to n and then n to 1 using recursion. 13 | 14 | ### Program for printing 1 to n : 15 | 16 | ``` 17 | class recursion { 18 | static void fun2(int n){ 19 | if(n>0){ 20 | fun2(n-1); 21 | System.out.println(n); 22 | } 23 | } 24 | 25 | public static void main(String[] args){ 26 | int n = 3; 27 | fun2(n); 28 | } 29 | } 30 | ``` 31 | 32 | - Output : 33 | ``` 34 | 1 35 | 2 36 | 3 37 | ``` 38 | 39 | - In the above code, the print statement is getting executed at returning time. Watch the video given below to get the proper understanding of the recursive tree for the above program : 40 | 41 | ### Program for printing n to 1 : 42 | ``` 43 | class recursion { 44 | static void fun1(int n){ 45 | if(n>0){ 46 | System.out.println(n); 47 | fun1(n-1); 48 | } 49 | } 50 | public static void main(String[] args){ 51 | int n = 3; 52 | fun1(n); 53 | } 54 | } 55 | ``` 56 | 57 | - Output : 58 | ``` 59 | 3 60 | 2 61 | 1 62 | ``` 63 | - In the above recursive code, the print statement is getting executed at the calling time. Before the recursive function is called, printing was done. Watch the video given below to get the proper understanding of the recursive tree for the above program : 64 | 65 | - Notice that by just changing the order of the print statement, the output of the code is completely reversed. This is the beauty of recursion. The same trick can be used to reverse a linked list. 66 | 67 | **Quick Quiz:** Write a program to calculate (recursion must be used) factorial of a number in Java? 68 | 69 | ``` 70 | public class cwh_34_recursion { 71 | // factorial(0) = 1 72 | // factorial(n) = n * n-1 *....1 73 | // factorial(5) = 5 * 4 * 3 * 2 * 1 = 120 74 | // factorial(n) = n * factorial(n-1) 75 | 76 | static int factorial(int n){ 77 | if(n==0 || n==1){ 78 | return 1; 79 | } 80 | else{ 81 | return n * factorial(n-1); 82 | } 83 | } 84 | static int factorial_iterative(int n){ 85 | if(n==0 || n==1){ 86 | return 1; 87 | } 88 | else{ 89 | int product = 1; 90 | for (int i=1;i<=n;i++){ // 1 to n 91 | product *= i; 92 | } 93 | return product; 94 | } 95 | } 96 | public static void main(String[] args) { 97 | int x = 0; 98 | System.out.println("The value of factorial x is: " + factorial(x)); 99 | System.out.println("The value of factorial x is: " + factorial_iterative(x)); 100 | } 101 | } 102 | ``` 103 | 104 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-34/Ch7.pdf)** 105 | 106 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-34/UltimateJavaCheatSheet.pdf)** 107 | -------------------------------------------------------------------------------- /34.Recursion/cwh_34_recursion.java: -------------------------------------------------------------------------------- 1 | public class cwh_34_recursion { 2 | // factorial(0) = 1 3 | // factorial(n) = n * n-1 *....1 4 | // factorial(5) = 5 * 4 * 3 * 2 * 1 = 120 5 | // factorial(n) = n * factorial(n-1) 6 | 7 | static int factorial(int n){ 8 | if(n==0 || n==1){ 9 | return 1; 10 | } 11 | else{ 12 | return n * factorial(n-1); 13 | } 14 | } 15 | static int factorial_iterative(int n){ 16 | if(n==0 || n==1){ 17 | return 1; 18 | } 19 | else{ 20 | int product = 1; 21 | for (int i=1;i<=n;i++){ // 1 to n 22 | product *= i; 23 | } 24 | return product; 25 | } 26 | } 27 | public static void main(String[] args) { 28 | int x = 0; 29 | System.out.println("The value of factorial x is: " + factorial(x)); 30 | System.out.println("The value of factorial x is: " + factorial_iterative(x)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /34.Recursion/recursion.java: -------------------------------------------------------------------------------- 1 | class recursion { 2 | static void fun2(int n){ 3 | if(n>0){ 4 | fun2(n-1); 5 | System.out.println(n); 6 | } 7 | } 8 | 9 | public static void main(String[] args){ 10 | int n = 3; 11 | fun2(n); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/Chapter7_PracticeSet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/35.Chapter_7_Practice_Set/Chapter7_PracticeSet.pdf -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Practice Questions on Java Methods 2 | 3 | 1. Write a Java method to print the multiplication table of a number n. 4 | 2. Write a program using functions to print the following pattern: 5 | ``` 6 | * 7 | ** 8 | *** 9 | **** 10 | ``` 11 | 3. Write a recursive function to calculate the sum of first n natural numbers. 12 | 4. Write a function to print the following pattern: 13 | ``` 14 | **** 15 | *** 16 | ** 17 | * 18 | ``` 19 | 5. Write a function to print the nth term of the Fibonacci series using recursion. 20 | 6. Write a function to find the average of a set of numbers passed as arguments. 21 | 7. Repeat problem 4 using Recursion. 22 | 8. Repeat problem 2 using Recursion. 23 | 9. Write a function to convert Celsius temperature into Fahrenheit. 24 | 10. Repeat problem 3 using an iterative approach. 25 | -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_01.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_01 { 2 | static void multiplication(int n) { 3 | for (int i=1; i<=10; i++) { 4 | System.out.println(n + " * " + i + " = " + (n*i)); 5 | } 6 | } 7 | public static void main(String args[]) { 8 | multiplication(9); 9 | } 10 | } -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_02.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_02 { 2 | static void pattern(int n) { 3 | for (int i = 0; i < n; i++) { 4 | for (int j = 0; j < i + 1; j++) { 5 | System.out.print("* "); 6 | } 7 | System.out.println(); 8 | } 9 | } 10 | public static void main(String args[]) { 11 | pattern(4); 12 | } 13 | } -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_03.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_03 { 2 | static int recursive_sum(int n) { 3 | if (n==1) { 4 | return 1; 5 | } 6 | else { 7 | return n + recursive_sum(n-1); 8 | } 9 | 10 | } 11 | public static void main(String args[]) { 12 | System.out.println(recursive_sum(5)); 13 | } 14 | } -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_04.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_04 { 2 | static void pattern(int n) { 3 | for (int i = n; i > 0; i--) { 4 | for (int j = 0; j < i; j++) { 5 | System.out.print("* "); 6 | } 7 | System.out.println(); 8 | } 9 | } 10 | public static void main(String args[]) { 11 | pattern(5); 12 | } 13 | } -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_05.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_05 { 2 | static int fibonacci(int n) { 3 | if ( n==1 || n == 2) { 4 | return (n-1); 5 | } 6 | else { 7 | return (fibonacci(n-1) + fibonacci(n-2)); 8 | } 9 | } 10 | public static void main(String args[]) { 11 | System.out.println(fibonacci(5)); 12 | } 13 | } -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_06.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_06 { 2 | static int avg(int ...arr) { 3 | int len = arr.length; 4 | int sum = 0; 5 | for (int element:arr) { 6 | sum += element; 7 | } 8 | int average = sum / len; 9 | return average; 10 | } 11 | public static void main(String args[]) { 12 | System.out.println(avg(1,2,3,4,5,34,564,234)); 13 | } 14 | } -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_07.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_07 { 2 | static void pattern_rec(int n) { 3 | if (n > 0) { 4 | for (int i = 0; i < n; i++) { 5 | System.out.print("*"); 6 | } 7 | System.out.println(); 8 | pattern_rec(n - 1); 9 | } 10 | 11 | } 12 | public static void main(String args[]) { 13 | pattern_rec(5); 14 | } 15 | } -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_08.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_08 { 2 | static void pattern_rec(int n) { 3 | if (n > 0) { 4 | pattern_rec(n - 1); 5 | for (int i = 0; i < n; i++) { 6 | System.out.print("*"); 7 | } 8 | System.out.println(); 9 | } 10 | } 11 | public static void main(String args[]) { 12 | pattern_rec(5); 13 | } 14 | } -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_09.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_09 { 2 | static float cel_to_feh(float n) { 3 | float temp = n * (9/5f) + 32; 4 | return temp; 5 | } 6 | public static void main(String args[]) { 7 | System.out.println(cel_to_feh(100.5f)); 8 | } 9 | } -------------------------------------------------------------------------------- /35.Chapter_7_Practice_Set/cwh_35_ps_pr_10.java: -------------------------------------------------------------------------------- 1 | public class cwh_35_ps_pr_10 { 2 | static int sum(int n) { 3 | int sum = 0; 4 | for (int i=1; i<=n; i++) { 5 | sum += i; 6 | } 7 | return sum; 8 | } 9 | public static void main(String args[]) { 10 | System.out.println(sum(5)); 11 | } 12 | } -------------------------------------------------------------------------------- /36.Introduction_To_OOPS/Ch8 Intro to oops.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/36.Introduction_To_OOPS/Ch8 Intro to oops.pdf -------------------------------------------------------------------------------- /36.Introduction_To_OOPS/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Introduction to Object Oriented Programming 2 | - Object-Oriented Programming tries to map code instructions with real-world, making the code short and easier to understand. 3 | - With the help of OOPs, we try to implement real-world entities such as object, inheritance, abstraction, etc. 4 | - OOPs helps us to follow the DRY(Don't Repeat Yourself) approach of programming, which in turn increases the reusability of the code. 5 | 6 | ### Two most important aspects of OOPs - Classes & Objects : 7 | 8 | #### Class : 9 | - A class is a blueprint for creating objects. 10 | - Classes do not consume any space in the memory. 11 | - Objects inherit methods and variables from the class. 12 | - It is a logical component. 13 | 14 | 15 | 16 | #### Objects : 17 | - An object is an instantiation of a class. When a class is defined, a template (info) is defined. 18 | - Every object has some address, and it occupies some space in the memory. 19 | - It is a physical entity. 20 | - Take a look at the below example to get a better understanding of objects and classes : 21 | 22 | ![image](https://user-images.githubusercontent.com/70385488/152324690-d8bc7fce-b9c8-4fe6-b93e-a5b38702af1a.png) 23 | 24 | ### How to model a problem in OOPs 25 | - We identify the following: 26 | 27 | ``` 28 | Noun - Class - Employee 29 | Adjective - Attributes - name, age, salary 30 | Verb - Methods - getSalary(), increment() 31 | ``` 32 | 33 | - This is all for this tutorial. We will do a detailed discussion on every aspect of OOPs in further tutorials. 34 | 35 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-36/Ch8.pdf)** 36 | 37 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-36/UltimateJavaCheatSheet.pdf)** 38 | -------------------------------------------------------------------------------- /37.Basic_Terminologies_In_OOPS/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Basic Terminologies in Object Oriented Programming 2 | 3 | ### Four pillars of Object-Oriented-Programming Language : 4 | 5 | #### 1. Abstraction : 6 | - Let's suppose you want to turn on the bulb in your room. What do you do to switch on the bulb. You simply press the button and the light bulb turns on. Right? Notice that here you're only concerned with your final result, i.e., turning on the light bulb. You do not care about the circuit of the bulb or how current flows through the bulb. The point here is that you press the switch, the bulb turns on! You don't know how the bulb turned on/how the circuit is made because all these details are hidden from you. This phenomenon is known as abstraction. 7 | - More formally, data abstraction is the way through which only the essential info is shown to the user, and all the internal details remain hidden from the user. 8 | - Example : 9 | 10 | 11 | 12 | #### 2. Polymorphism : 13 | - One entity many forms. 14 | - The word polymorphism comprises two words, poly which means many, and morph, which means forms. 15 | - In OOPs, polymorphism is the property that helps to perform a single task in different ways. 16 | - Let us consider a real-life example of polymorphism. A woman at the same time can be a mother, wife, sister, daughter, etc. Here, a woman is an entity having different forms. 17 | - Let's take another example, a smartphone can work like a camera as well as like a calculator. So, you can see the a smartphone is an entity having different forms. Also : 18 | 19 | 20 | 21 | #### 3. Encapsulation : 22 | - The act of putting various components together (in a capsule). 23 | - In java, the variables and methods are the components that are wrapped inside a single unit named class. 24 | - All the methods and variables of a class remain hidden from any other class. 25 | - A automatic cold drink vending machine is an example of encapsulation. 26 | - Cold drinks inside the machine are data that is wrapped inside a single unit cold drink vending machine. 27 | 28 | #### 4. Inheritance : 29 | - The act of deriving new things from existing things. 30 | - In Java, one class can acquire all the properties and behaviours of other some other class 31 | - The class which inherits some other class is known as child class or sub class. 32 | - The class which is inherited is known as parent class or super class. 33 | - Inheritance helps us to write more efficient code because it increases the reusablity of the code. 34 | - Example : 35 | - Rickshaw → E-Rickshaw 36 | - Phone → Smart Phone 37 | 38 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-37/Ch8.pdf)** 39 | 40 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-37/UltimateJavaCheatSheet.pdf)** 41 | -------------------------------------------------------------------------------- /38.Custom_Class/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Creating Our Own Java Class 2 | ### Writing a Custom Class : 3 | - Syntax of a custom class : 4 | 5 | ``` 6 | class { 7 | field; 8 | method; 9 | } 10 | ``` 11 | 12 | - Example : 13 | 14 | ``` 15 | public class Employee { 16 | int id; // Attribute 1 17 | String name; // Attribute 2 18 | } 19 | ``` 20 | 21 | **Note:** The first letter of a class should always be capital. 22 | 23 | - Any real-world object = Properties + Behavior 24 | - Object in OOPs = Attributes + Methods 25 | 26 | ### A Class with Methods : 27 | - We can add methods to our class Employee as follows: 28 | 29 | ``` 30 | /* 31 | public class Employee { 32 | public int id; 33 | public String name; 34 | 35 | public int getSalary(){ 36 | //code 37 | } 38 | public void getDetails(){ 39 | //code 40 | } 41 | }; 42 | */ 43 | ``` 44 | 45 | ### Code as Described in the Video 46 | 47 | ``` 48 | class Employee{ 49 | int id; 50 | int salary; 51 | String name; 52 | public void printDetails(){ 53 | System.out.println("My id is " + id); 54 | System.out.println("and my name is "+ name); 55 | } 56 | 57 | public int getSalary(){ 58 | return salary; 59 | } 60 | } 61 | 62 | public class cwh_38_custom_class { 63 | public static void main(String[] args) { 64 | System.out.println("This is our custom class"); 65 | Employee harry = new Employee(); // Instantiating a new Employee Object 66 | Employee john = new Employee(); // Instantiating a new Employee Object 67 | 68 | // Setting Attributes for Harry 69 | harry.id = 12; 70 | harry.salary = 34; 71 | harry.name = "CodeWithHarry"; 72 | 73 | // Setting Attributes for John 74 | john.id = 17; 75 | john.salary = 12; 76 | john.name = "John Khandelwal"; 77 | 78 | // Printing the Attributes 79 | harry.printDetails(); 80 | john.printDetails(); 81 | int salary = john.getSalary(); 82 | System.out.println(salary); 83 | // System.out.println(harry.id); 84 | // System.out.println(harry.name); 85 | } 86 | } 87 | ``` 88 | 89 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-38/Ch8.pdf)** 90 | 91 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-38/UltimateJavaCheatSheet.pdf)** 92 | -------------------------------------------------------------------------------- /38.Custom_Class/cwh_38_custom_class.java: -------------------------------------------------------------------------------- 1 | class Employee{ 2 | int id; 3 | int salary; 4 | String name; 5 | public void printDetails(){ 6 | System.out.println("My id is " + id); 7 | System.out.println("and my name is "+ name); 8 | } 9 | 10 | public int getSalary(){ 11 | return salary; 12 | } 13 | } 14 | 15 | public class cwh_38_custom_class { 16 | public static void main(String[] args) { 17 | System.out.println("This is our custom class"); 18 | Employee harry = new Employee(); // Instantiating a new Employee Object 19 | Employee john = new Employee(); // Instantiating a new Employee Object 20 | 21 | // Setting Attributes for Harry 22 | harry.id = 12; 23 | harry.salary = 34; 24 | harry.name = "CodeWithHarry"; 25 | 26 | // Setting Attributes for John 27 | john.id = 17; 28 | john.salary = 12; 29 | john.name = "John Khandelwal"; 30 | 31 | // Printing the Attributes 32 | harry.printDetails(); 33 | john.printDetails(); 34 | int salary = john.getSalary(); 35 | System.out.println(salary); 36 | // System.out.println(harry.id); 37 | // System.out.println(harry.name); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /39.Chapter_8_Practice_Set/Ch8_PS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/39.Chapter_8_Practice_Set/Ch8_PS.pdf -------------------------------------------------------------------------------- /39.Chapter_8_Practice_Set/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Basic Questions on Object Oriented Programming 2 | 3 | 1. Create a class Employee with the following properties and methods: 4 | - Salary (property) (int) 5 | - getSalary (method returning int) 6 | - name (property) (String) 7 | - getName (method returning String) 8 | - setName (method changing name) 9 | 10 | 2. Create a class cellphone with methods to print “ringing…”, “vibrating…”, etc. 11 | 3. Create a class Square with a method to initialize its side, calculating area, perimeter etc. 12 | 4. Create a class Rectangle & problem 3. 13 | 5. Create a class TommyVecetti for Rockstar Games capable of hitting (print hitting…), running, firing, etc. 14 | 6. Repeat problem 4 for a circle. 15 | 16 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-39/Ch8_PS.pdf)** 17 | 18 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-39/UltimateJavaCheatSheet.pdf)** 19 | -------------------------------------------------------------------------------- /39.Chapter_8_Practice_Set/cwh_39_ps_pr_01.java: -------------------------------------------------------------------------------- 1 | class Employee { 2 | int salary; 3 | String name; 4 | 5 | public int getSalary() { 6 | return salary; 7 | } 8 | 9 | public String getName() { 10 | return name; 11 | } 12 | 13 | public void setName(String n) { 14 | name = n; 15 | } 16 | } 17 | 18 | public class cwh_39_ps_pr_01 { 19 | public static void main(String args[]) { 20 | Employee kishan = new Employee(); 21 | kishan.setName("Let's Code Together"); 22 | kishan.salary = 50000; 23 | 24 | //kishan.name = "Kishan"; 25 | 26 | System.out.println(kishan.getSalary()); 27 | System.out.println(kishan.getName()); 28 | } 29 | } -------------------------------------------------------------------------------- /39.Chapter_8_Practice_Set/cwh_39_ps_pr_02.java: -------------------------------------------------------------------------------- 1 | class Cellphone { 2 | public void ring() { 3 | System.out.println("ringing..."); 4 | } 5 | 6 | public void vibrate() { 7 | System.out.println("vibrating..."); 8 | } 9 | 10 | public void call() { 11 | System.out.println("calling your friend..."); 12 | } 13 | } 14 | 15 | public class cwh_39_ps_pr_02 { 16 | public static void main(String args[]) { 17 | Cellphone apple = new Cellphone(); 18 | apple.ring(); 19 | apple.vibrate(); 20 | apple.call(); 21 | } 22 | } -------------------------------------------------------------------------------- /39.Chapter_8_Practice_Set/cwh_39_ps_pr_03.java: -------------------------------------------------------------------------------- 1 | class Square { 2 | int side; 3 | 4 | public int perimeter() { 5 | return (4 * side); 6 | } 7 | 8 | public int area() { 9 | return side * side; 10 | } 11 | } 12 | 13 | public class cwh_39_ps_pr_03 { 14 | public static void main(String args[]) { 15 | Square sq = new Square(); 16 | sq.side = 5; 17 | System.out.println(sq.perimeter()); 18 | System.out.println(sq.area()); 19 | } 20 | } -------------------------------------------------------------------------------- /39.Chapter_8_Practice_Set/cwh_39_ps_pr_04.java: -------------------------------------------------------------------------------- 1 | class Rectangle { 2 | int length; 3 | int breadth; 4 | 5 | public int perimeter() { 6 | return (2 * length * breadth); 7 | } 8 | 9 | public int area() { 10 | return length * breadth; 11 | } 12 | } 13 | 14 | public class cwh_39_ps_pr_04 { 15 | public static void main(String args[]) { 16 | Rectangle rec = new Rectangle(); 17 | rec.length = 5; 18 | rec.breadth = 4; 19 | System.out.println(rec.perimeter()); 20 | System.out.println(rec.area()); 21 | } 22 | } -------------------------------------------------------------------------------- /39.Chapter_8_Practice_Set/cwh_39_ps_pr_05.java: -------------------------------------------------------------------------------- 1 | class TommyVecetti{ 2 | public void hit(){ 3 | System.out.println("Hitting the enemy"); 4 | } 5 | public void run(){ 6 | System.out.println("Running from the enemy"); 7 | } 8 | public void fire(){ 9 | System.out.println("Firing on the enemy"); 10 | } 11 | } 12 | 13 | public class cwh_39_ps_pr_05 { 14 | public static void main(String args[]) { 15 | TommyVecetti player = new TommyVecetti(); 16 | player.hit(); 17 | player.run(); 18 | player.fire(); 19 | } 20 | } -------------------------------------------------------------------------------- /39.Chapter_8_Practice_Set/cwh_39_ps_pr_06.java: -------------------------------------------------------------------------------- 1 | class Circle { 2 | double radius; 3 | 4 | public double perimeter() { 5 | return (2 * radius * Math.PI); 6 | } 7 | 8 | public double area() { 9 | return (Math.PI * radius * radius); 10 | } 11 | } 12 | 13 | public class cwh_39_ps_pr_06 { 14 | public static void main(String args[]) { 15 | Circle cir = new Circle(); 16 | cir.radius = 5.5; 17 | System.out.println(cir.perimeter()); 18 | System.out.println(cir.area()); 19 | } 20 | } -------------------------------------------------------------------------------- /4.Literals/CWH_04_literals.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | public class CWH_04_literals { 4 | public static void main(String[] args) { 5 | byte age = 34; 6 | int age2 = 56; 7 | short age3 = 87; 8 | long ageDino = 5666666666666L; 9 | char ch = 'A'; 10 | float f1 = 5.6f; 11 | double d1 = 4.66; 12 | boolean a = true; 13 | String str = "Harry"; 14 | 15 | System.out.print(age); 16 | System.out.println(str); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /4.Literals/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Literals in Java 2 | 3 | ### Literals 4 | - A constant value that can be assigned to the variable is called a literal. 5 | - 101 – Integer literal 6 | - 10.1f – float literal 7 | - 10.1 – double literal (default type for decimals) 8 | - ‘A’ – character literal 9 | - true – Boolean literal 10 | - “Harry” – String literal 11 | 12 | ### Keywords 13 | - Words that are reserved and used by the Java compiler. They cannot be used as an Identifier. 14 | - {You can visit docs.oracle.com for a comprehensive list} 15 | 16 | ### Code as Described in the Video 17 | 18 | ``` 19 | package com.company; 20 | 21 | public class CWH_04_literals { 22 | public static void main(String[] args) { 23 | byte age = 34; 24 | int age2 = 56; 25 | short age3 = 87; 26 | long ageDino = 5666666666666L; 27 | char ch = 'A'; 28 | float f1 = 5.6f; 29 | double d1 = 4.66; 30 | boolean a = true; 31 | 32 | System.out.print(age); 33 | 34 | String str = "Harry"; 35 | 36 | System.out.println(str); 37 | } 38 | } 39 | ``` 40 | 41 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-4/JavaChapter1.pdf) 42 | 43 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-4/UltimateJavaCheatSheet.pdf) 44 | -------------------------------------------------------------------------------- /40.Access_Modifiers/CWH.java: -------------------------------------------------------------------------------- 1 | class Employee { 2 | 3 | private int id; 4 | private String name; 5 | 6 | } 7 | 8 | public class CWH { 9 | public static void main(String[] args) { 10 | Employee emp1 = new Employee(); 11 | emp1.id = 3; 12 | emp1.name = "Shubham"; 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /40.Access_Modifiers/CWH1.java: -------------------------------------------------------------------------------- 1 | class Employee { 2 | 3 | private int id; 4 | private String name; 5 | 6 | public String getName(){ 7 | return name; 8 | } 9 | public void setName(String n){ 10 | name = n; 11 | } 12 | public void setId(int i){ 13 | id = i; 14 | } 15 | public int getId(){ 16 | return id; 17 | } 18 | } 19 | 20 | public class CWH { 21 | public static void main(String[] args) { 22 | Employee emp1 = new Employee(); 23 | 24 | emp1.setName("Shubham"); 25 | System.out.println(emp1.getName()); 26 | emp1.setId(1); 27 | System.out.println(emp1.getId()); 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /40.Access_Modifiers/Chapter9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/40.Access_Modifiers/Chapter9.pdf -------------------------------------------------------------------------------- /40.Access_Modifiers/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Access modifiers, getters & setters in Java 2 | 3 | ### Access Modifiers 4 | - Access Modifiers specify where a property/method is accessible. There are four types of access modifiers in java : 5 | 1. private 6 | 2. default 7 | 3. protected 8 | 4. public 9 | 10 | | **Access Modifier** | **within class** | **within package** | **outside package by subclass only** | **outside package** | 11 | |:---------------------:|:------------------:|:---------------------:|:---------------------------------------:|:----------------------:| 12 | | **public** | Y | Y | Y | Y | 13 | |**protected** | Y | Y | Y | N | 14 | |**Default** | Y | Y | N | N | 15 | |**private** | Y | N | N | N | 16 | 17 | - From the above table, notice that the private access modifier can only be accessed within the class. So, let's try to access private modifiers outside the class : 18 | 19 | ``` 20 | class Employee { 21 | 22 | private int id; 23 | private String name; 24 | 25 | } 26 | 27 | public class CWH { 28 | public static void main(String[] args) { 29 | Employee emp1 = new Employee(); 30 | emp1.id = 3; 31 | emp1.name = "Shubham"; 32 | 33 | } 34 | } 35 | ``` 36 | ``` 37 | Output : 38 | java: id has private access in Employee 39 | ``` 40 | 41 | - You can see that the above code produces an error that we're trying to access a private variable outside the class. So, is there any way by which we can access the private access modifiers outside the class? The answer is Yes! We can access the private access modifiers outside the class with the help of getters and setters. 42 | 43 | ### Getters and Setters : 44 | - Getter ➼ Returns the value [accessors] 45 | - setter ➼ Sets / updates the value [mutators] 46 | - In the below code, we've created total 4 methods: 47 | 1. setName(): The argument passed to this method is assigned to the private variable name. 48 | 2. getName(): The method returns the value set by the setName() method. 49 | 3. setId(): The integer argument passed to this method is assigned to the private variable id. 50 | 4. getId): This method returns the value set by the setId() method. 51 | 52 | ``` 53 | class Employee { 54 | 55 | private int id; 56 | private String name; 57 | 58 | public String getName(){ 59 | return name; 60 | } 61 | public void setName(String n){ 62 | name = n; 63 | } 64 | public void setId(int i){ 65 | id = i; 66 | } 67 | public int getId(){ 68 | return id; 69 | } 70 | } 71 | 72 | public class CWH1 { 73 | public static void main(String[] args) { 74 | Employee emp1 = new Employee(); 75 | 76 | emp1.setName("Shubham"); 77 | System.out.println(emp1.getName()); 78 | emp1.setId(1); 79 | System.out.println(emp1.getId()); 80 | 81 | } 82 | } 83 | ``` 84 | ``` 85 | Output : 86 | Shubham 87 | 1 88 | ``` 89 | 90 | - As you can see that we've got our expected output. So, that's how we use the getters and setters method to get and set the values of private access modifiers outside the class. 91 | 92 | ### Code as described in the video: 93 | 94 | ``` 95 | class MyEmployee{ 96 | private int id; 97 | private String name; 98 | 99 | public String getName(){ 100 | return name; 101 | } 102 | public void setName(String n){ 103 | this.name = n; 104 | } 105 | public void setId(int i){ 106 | this.id = i; 107 | } 108 | public int getId(){ 109 | return id; 110 | } 111 | } 112 | public class cwh_40_ch9 { 113 | public static void main(String[] args) { 114 | MyEmployee harry = new MyEmployee(); 115 | // harry.id = 45; 116 | // harry.name = "CodeWithHarry"; --> Throws an error due to private access modifier 117 | harry.setName("CodeWithHarry"); 118 | System.out.println(harry.getName()); 119 | harry.setId(234); 120 | System.out.println(harry.getId()); 121 | } 122 | } 123 | ``` 124 | 125 | **Quick Quiz:** use the getters and setters from the main method 126 | 127 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-40/Chapter9.pdf)** 128 | 129 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-40/UltimateJavaCheatSheet.pdf)** 130 | -------------------------------------------------------------------------------- /40.Access_Modifiers/cwh_40_ch9.java: -------------------------------------------------------------------------------- 1 | class MyEmployee{ 2 | private int id; 3 | private String name; 4 | 5 | public String getName(){ 6 | return name; 7 | } 8 | public void setName(String n){ 9 | this.name = n; 10 | } 11 | public void setId(int i){ 12 | this.id = i; 13 | } 14 | public int getId(){ 15 | return id; 16 | } 17 | } 18 | public class cwh_40_ch9 { 19 | public static void main(String[] args) { 20 | MyEmployee harry = new MyEmployee(); 21 | // harry.id = 45; 22 | // harry.name = "CodeWithHarry"; --> Throws an error due to private access modifier 23 | harry.setName("CodeWithHarry"); 24 | System.out.println(harry.getName()); 25 | harry.setId(234); 26 | System.out.println(harry.getId()); 27 | } 28 | } -------------------------------------------------------------------------------- /41.Exercise_2_Solution/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Exercise 2 - Solution 2 | 3 | ### Code as described in the video: 4 | 5 | ``` 6 | import java.util.Random; 7 | import java.util.Scanner; 8 | 9 | public class cwh_41_ex2sol { 10 | public static void main(String[] args) { 11 | // 0 for Rock 12 | // 1 for Paper 13 | // 2 for Scissor 14 | 15 | Scanner sc = new Scanner(System.in); 16 | System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissor "); 17 | int userInput = sc.nextInt(); 18 | 19 | Random random = new Random(); 20 | int computerInput = random.nextInt(3); 21 | 22 | if (userInput == computerInput) { 23 | System.out.println("Draw"); 24 | } 25 | else if (userInput == 0 && computerInput == 2 || userInput == 1 && computerInput == 0 26 | || userInput == 2 && computerInput == 1) { 27 | System.out.println("You Win!"); 28 | } else { 29 | System.out.println("Computer Win!"); 30 | } 31 | // System.out.println("Computer choice: " + computerInput); 32 | if(computerInput==0){ 33 | System.out.println("Computer choice: Rock"); 34 | } 35 | else if(computerInput==1){ 36 | System.out.println("Computer choice: Paper"); 37 | } 38 | else if(computerInput==2){ 39 | System.out.println("Computer choice: Scissors"); 40 | } 41 | } 42 | } 43 | ``` 44 | 45 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-41/UltimateJavaCheatSheet.pdf)** 46 | -------------------------------------------------------------------------------- /41.Exercise_2_Solution/cwh_41_ex2sol.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | import java.util.Scanner; 3 | 4 | public class cwh_41_ex2sol { 5 | public static void main(String[] args) { 6 | // 0 for Rock 7 | // 1 for Paper 8 | // 2 for Scissor 9 | 10 | Scanner sc = new Scanner(System.in); 11 | System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissor "); 12 | int userInput = sc.nextInt(); 13 | 14 | Random random = new Random(); 15 | int computerInput = random.nextInt(3); 16 | 17 | if (userInput == computerInput) { 18 | System.out.println("Draw"); 19 | } 20 | else if (userInput == 0 && computerInput == 2 || userInput == 1 && computerInput == 0 21 | || userInput == 2 && computerInput == 1) { 22 | System.out.println("You Win!"); 23 | } else { 24 | System.out.println("Computer Win!"); 25 | } 26 | // System.out.println("Computer choice: " + computerInput); 27 | if(computerInput==0){ 28 | System.out.println("Computer choice: Rock"); 29 | } 30 | else if(computerInput==1){ 31 | System.out.println("Computer choice: Paper"); 32 | } 33 | else if(computerInput==2){ 34 | System.out.println("Computer choice: Scissors"); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /42.Constructors/CWH_constructors.java: -------------------------------------------------------------------------------- 1 | class Employee { 2 | // First constructor 3 | Employee(String s, int i){ 4 | System.out.println("The name of the first employee is : " + s); 5 | System.out.println("The id of the first employee is : " + i); 6 | } 7 | // Constructor overloaded 8 | Employee(String s, int i, int salary){ 9 | System.out.println("The name of the second employee is : " + s); 10 | System.out.println("The id of the second employee is : " + i); 11 | System.out.println("The salary of second employee is : " + salary); 12 | } 13 | 14 | } 15 | public class CWH_constructors { 16 | public static void main(String[] args) { 17 | Employee shubham = new Employee("Shubham",1); 18 | Employee harry = new Employee("Harry",2,70000); 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /42.Constructors/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Constructors in Java 2 | 3 | ### Constructors in Java : 4 | - Constructors are similar to methods,, but they are used to initialize an object. 5 | - Constructors do not have any return type(not even void). 6 | - Every time we create an object by using the new() keyword, a constructor is called. 7 | - If we do not create a constructor by ourself, then the default constructor(created by Java compiler) is called. 8 | 9 | ### Rules for creating a Constructor : 10 | 1. The class name and constructor name should be the same. 11 | 2. It must have no explicit return type. 12 | 3. It can not be abstract, static, final, and synchronized. 13 | 14 | ### Types of Constructors in Java : 15 | - There are two types of constructors in Java : 16 | 17 | **1. Defaut constructor :** A constructor with 0 parameters is known as default constructor. 18 | - Syntax : 19 | 20 | ``` 21 | (){ 22 | //code to be executed on the execution of the constructor 23 | } 24 | ``` 25 | - Example : 26 | 27 | ``` 28 | class CWH { 29 | CWH(){ 30 | System.out.println("This is the default constructor of CWH class."); 31 | } 32 | } 33 | public class CWH_constructors { 34 | public static void main(String[] args) { 35 | CWH obj1 = new CWH(); 36 | } 37 | } 38 | ``` 39 | 40 | ``` 41 | Output : 42 | This is the default constructor of CWH class. 43 | ``` 44 | 45 | - In the above code, CWH() is the constructor of class CWH The CWH() constructor is invoked automatically with the creation of object ob1. 46 | 47 | **2. Paramerterized constructor :** A constructor with some specified number of parameters is known as a parameterized constructor. 48 | - Syntax : 49 | 50 | ``` 51 | ( param1, param2,......){ 52 | //code to be executed on the invocation of the constructor 53 | } 54 | ``` 55 | 56 | - Example : 57 | 58 | ``` 59 | class CWH { 60 | CWH(String s, int b){ 61 | 62 | System.out.println("This is the " +b+ "th video of "+ " "+ s); 63 | } 64 | } 65 | public class CWH_constructors { 66 | public static void main(String[] args) { 67 | CWH obj1 = new CWH("CodeWithHarry Java Playlist",42); 68 | } 69 | } 70 | ``` 71 | 72 | ``` 73 | Output : 74 | This is the 42th video of CodeWithHarry Java Playlist 75 | ```` 76 | 77 | - In the above example, CWH() constructor accepts two parameters i.e., string s and int b. 78 | 79 | ### Constructor Overloading in Java : 80 | - Just like methods, constructors can also be overloaded in Java. We can overload the Employe constructor like below: 81 | 82 | ``` 83 | public Employee (String n) 84 | name = n; 85 | } 86 | ``` 87 | 88 | **Note:** 89 | 1. Constructors can take parameters without being overloaded 90 | 2. There can be more than two overloaded constructors 91 | 92 | - Let's take an example to understand the concept of constructor overloading. 93 | 94 | **Example :** 95 | 96 | - In the below example, the class Employee has a constructor named Employee(). It takes two argument,i.e., string s & int i. The same constructor is overloaded and then it accepts three arguments i.e., string s, int i & int salary. 97 | 98 | ``` 99 | class Employee { 100 | // First constructor 101 | Employee(String s, int i){ 102 | System.out.println("The name of the first employee is : " + s); 103 | System.out.println("The id of the first employee is : " + i); 104 | } 105 | // Constructor overloaded 106 | Employee(String s, int i, int salary){ 107 | System.out.println("The name of the second employee is : " + s); 108 | System.out.println("The id of the second employee is : " + i); 109 | System.out.println("The salary of second employee is : " + salary); 110 | } 111 | 112 | } 113 | public class CWH_constructors { 114 | public static void main(String[] args) { 115 | Employee shubham = new Employee("Shubham",1); 116 | Employee harry = new Employee("Harry",2,70000); 117 | 118 | 119 | 120 | } 121 | } 122 | ``` 123 | 124 | ``` 125 | Output : 126 | 127 | The name of the first employee is : Shubham 128 | The id of the first employee is : 1 129 | The name of the second employee is : Harry 130 | The id of the second employee is : 2 131 | The salary of second employee is : 70000 132 | ``` 133 | 134 | **Quick quiz:** Overloaded the employee constructor to initialize the salary to Rs 10,000 135 | 136 | ``` 137 | class Employee { 138 | // First constructor 139 | Employee(String s, int i){ 140 | System.out.println("The name of the first employee is : " + s); 141 | System.out.println("The id of the first employee is : " + i); 142 | } 143 | // Constructor overloaded 144 | Employee(String s, int i, int salary){ 145 | System.out.println("The name of the second employee is : " + s); 146 | System.out.println("The id of the second employee is : " + i); 147 | System.out.println("The salary of second employee is : " + salary); 148 | } 149 | // Overloaded constructor with salary 10,000 150 | Employee(int salary){ 151 | System.out.println("The salary of third employee is : " + salary); 152 | } 153 | 154 | } 155 | public class quick_quiz { 156 | public static void main(String[] args) { 157 | Employee shubham = new Employee("Shubham",1); 158 | Employee harry = new Employee("Harry",2,70000); 159 | Employee kishan = new Employee(10000); 160 | } 161 | } 162 | ``` 163 | 164 | ### Code as described in the video: 165 | 166 | ``` 167 | class MyMainEmployee{ 168 | private int id; 169 | private String name; 170 | 171 | public MyMainEmployee(){ 172 | id = 0; 173 | name = "Your-Name-Here"; 174 | } 175 | public MyMainEmployee(String myName, int myId){ 176 | id = myId; 177 | name = myName; 178 | } 179 | public MyMainEmployee(String myName){ 180 | id = 1; 181 | name = myName; 182 | } 183 | public String getName(){ 184 | return name; 185 | } 186 | public void setName(String n){ 187 | this.name = n; 188 | } 189 | public void setId(int i){ 190 | this.id = i; 191 | } 192 | public int getId(){ 193 | return id; 194 | } 195 | } 196 | 197 | public class cwh_42_constructors { 198 | public static void main(String[] args) { 199 | //MyMainEmployee harry = new MyMainEmployee("ProgrammingWithHarry", 12); 200 | MyMainEmployee harry = new MyMainEmployee(); 201 | //harry.setName("CodeWithHarry"); 202 | //harry.setId(34); 203 | System.out.println(harry.getId()); 204 | System.out.println(harry.getName()); 205 | } 206 | } 207 | ``` 208 | 209 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-42/Chapter9.pdf)** 210 | 211 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-42/UltimateJavaCheatSheet.pdf)** 212 | -------------------------------------------------------------------------------- /42.Constructors/cwh_42_constructors.java: -------------------------------------------------------------------------------- 1 | class MyMainEmployee{ 2 | private int id; 3 | private String name; 4 | 5 | public MyMainEmployee(){ 6 | id = 0; 7 | name = "Your-Name-Here"; 8 | } 9 | public MyMainEmployee(String myName, int myId){ 10 | id = myId; 11 | name = myName; 12 | } 13 | public MyMainEmployee(String myName){ 14 | id = 1; 15 | name = myName; 16 | } 17 | public String getName(){ 18 | return name; 19 | } 20 | public void setName(String n){ 21 | this.name = n; 22 | } 23 | public void setId(int i){ 24 | this.id = i; 25 | } 26 | public int getId(){ 27 | return id; 28 | } 29 | } 30 | 31 | public class cwh_42_constructors { 32 | public static void main(String[] args) { 33 | //MyMainEmployee harry = new MyMainEmployee("ProgrammingWithHarry", 12); 34 | MyMainEmployee harry = new MyMainEmployee(); 35 | //harry.setName("CodeWithHarry"); 36 | //harry.setId(34); 37 | System.out.println(harry.getId()); 38 | System.out.println(harry.getName()); 39 | } 40 | } -------------------------------------------------------------------------------- /42.Constructors/quick_quiz.java: -------------------------------------------------------------------------------- 1 | class Employee { 2 | // First constructor 3 | Employee(String s, int i){ 4 | System.out.println("The name of the first employee is : " + s); 5 | System.out.println("The id of the first employee is : " + i); 6 | } 7 | // Constructor overloaded 8 | Employee(String s, int i, int salary){ 9 | System.out.println("The name of the second employee is : " + s); 10 | System.out.println("The id of the second employee is : " + i); 11 | System.out.println("The salary of second employee is : " + salary); 12 | } 13 | // Overloaded constructor with salary 10,000 14 | Employee(int salary){ 15 | System.out.println("The salary of third employee is : " + salary); 16 | } 17 | 18 | } 19 | public class quick_quiz { 20 | public static void main(String[] args) { 21 | Employee shubham = new Employee("Shubham",1); 22 | Employee harry = new Employee("Harry",2,70000); 23 | Employee kishan = new Employee(10000); 24 | } 25 | } -------------------------------------------------------------------------------- /43.Java_Exercise_3/README.md: -------------------------------------------------------------------------------- 1 | # Java Exercise 3: Guess the Number (OOPs Edition) 2 | 3 | ### Create a class Game, which allows a user to play "Guess the Number" game once. 4 | - Game should have the following methods: 5 | - Constructor to generate the random number 6 | - takeUserInput() to take a user input of number 7 | - isCorrectNumber() to detect whether the number entered by the user is true 8 | - getter and setter for noOfGuesses 9 | - Use properties such as noOfGuesses(int), etc to get this task done! 10 | 11 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-43/UltimateJavaCheatSheet.pdf)** 12 | -------------------------------------------------------------------------------- /44.Chapter_9_Practice_Set/Ch9PS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/44.Chapter_9_Practice_Set/Ch9PS.pdf -------------------------------------------------------------------------------- /44.Chapter_9_Practice_Set/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Exercise on Access Modifiers and Constructors 2 | 3 | 1. create a class cylinder and use getter and setters to set its radius and height 4 | 2. use ➊ to calculate surface and volume of the cylinder 5 | 3. Use a constructor and repeat ➊ 6 | 4. Overload a constructor used to initialize a rectangle of length and breath 5 for using custom parameters 7 | 5. Repeat ➊ for a sphere 8 | 9 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-44/Ch9PS.pdf)** 10 | 11 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-44/UltimateJavaCheatSheet.pdf)** 12 | -------------------------------------------------------------------------------- /44.Chapter_9_Practice_Set/cwh_44_ps_pr_01.java: -------------------------------------------------------------------------------- 1 | class Cylinder { 2 | private double height; 3 | private double radius; 4 | 5 | public void setRadius(double r) { 6 | this.radius = r; 7 | } 8 | public double getRadius() { 9 | return radius; 10 | } 11 | 12 | public void setHeight(double h) { 13 | this.height = h; 14 | } 15 | public double getHeight() { 16 | return height; 17 | } 18 | } 19 | 20 | public class cwh_44_ps_pr_01 { 21 | public static void main(String args[]) { 22 | Cylinder rad = new Cylinder(); 23 | Cylinder hgt = new Cylinder(); 24 | 25 | rad.setRadius(2.5); 26 | hgt.setHeight(5); 27 | 28 | System.out.println(rad.getRadius()); 29 | System.out.println(hgt.getHeight()); 30 | } 31 | } -------------------------------------------------------------------------------- /44.Chapter_9_Practice_Set/cwh_44_ps_pr_02.java: -------------------------------------------------------------------------------- 1 | class Cylinder { 2 | private double height; 3 | private double radius; 4 | 5 | public void setRadius(double r) { 6 | this.radius = r; 7 | } 8 | public double getRadius() { 9 | return radius; 10 | } 11 | 12 | public void setHeight(double h) { 13 | this.height = h; 14 | } 15 | public double getHeight() { 16 | return height; 17 | } 18 | 19 | public double surfaceArea() { 20 | return (2* Math.PI * radius * radius) + (2*Math.PI * radius * height); 21 | } 22 | public double volume(){ 23 | return Math.PI * radius * radius * height; 24 | } 25 | } 26 | 27 | public class cwh_44_ps_pr_02 { 28 | public static void main(String args[]) { 29 | Cylinder mycylinder = new Cylinder(); 30 | 31 | mycylinder.setRadius(2.5); 32 | mycylinder.setHeight(5); 33 | 34 | System.out.println(mycylinder.getRadius()); 35 | System.out.println(mycylinder.getHeight()); 36 | 37 | System.out.println(mycylinder.surfaceArea()); 38 | System.out.println(mycylinder.volume()); 39 | } 40 | } -------------------------------------------------------------------------------- /44.Chapter_9_Practice_Set/cwh_44_ps_pr_03.java: -------------------------------------------------------------------------------- 1 | class Cylinder { 2 | private double height; 3 | private double radius; 4 | 5 | public Cylinder(double radius, double height) { 6 | this.radius = radius; 7 | this.height = height; 8 | } 9 | 10 | public double surfaceArea() { 11 | return (2* Math.PI * radius * radius) + (2*Math.PI * radius * height); 12 | } 13 | public double volume(){ 14 | return Math.PI * radius * radius * height; 15 | } 16 | } 17 | 18 | public class cwh_44_ps_pr_03 { 19 | public static void main(String args[]) { 20 | Cylinder mycylinder = new Cylinder(5, 9.8); 21 | System.out.println(mycylinder.surfaceArea()); 22 | System.out.println(mycylinder.volume()); 23 | } 24 | } -------------------------------------------------------------------------------- /44.Chapter_9_Practice_Set/cwh_44_ps_pr_04.java: -------------------------------------------------------------------------------- 1 | class Rectangle { 2 | private int length; 3 | private int breadth; 4 | 5 | public Rectangle() { 6 | length = 5; 7 | breadth = 5; 8 | } 9 | 10 | public Rectangle(int length, int breadth) { 11 | this.length = length; 12 | this.breadth = breadth; 13 | } 14 | 15 | public int getLength() { 16 | return length; 17 | } 18 | 19 | public int getBreadth() { 20 | return breadth; 21 | } 22 | } 23 | 24 | public class cwh_44_ps_pr_04 { 25 | public static void main(String args[]) { 26 | Rectangle rect = new Rectangle(80, 40); 27 | System.out.println(rect.getLength()); 28 | System.out.println(rect.getBreadth()); 29 | } 30 | } -------------------------------------------------------------------------------- /44.Chapter_9_Practice_Set/cwh_44_ps_pr_05.java: -------------------------------------------------------------------------------- 1 | class Sphere { 2 | private double radius; 3 | 4 | public void setRadius(double radius) { 5 | this.radius = radius; 6 | } 7 | public double getRadius() { 8 | return radius; 9 | } 10 | public double volume() { 11 | return (4 * Math.PI * radius * radius * radius) / 3; 12 | } 13 | } 14 | 15 | public class cwh_44_ps_pr_05 { 16 | public static void main(String args[]) { 17 | Sphere rad = new Sphere(); 18 | 19 | rad.setRadius(2.5); 20 | 21 | System.out.println(rad.getRadius()); 22 | System.out.println(rad.volume()); 23 | } 24 | } -------------------------------------------------------------------------------- /45.Inheritance/Chapter10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/45.Inheritance/Chapter10.pdf -------------------------------------------------------------------------------- /45.Inheritance/README.md: -------------------------------------------------------------------------------- 1 | # Inheritance in Java 2 | - You might have heard people saying your nose is similar to your father or mother. Or, more formally, we can say that you've inherited the genes from your parents due to which you look similar to them. 3 | - The same phenomenon of inheritance is also valid in programming. 4 | - In Java, one class can easily inherit the attributes and methods from some other class. This mechanism of acquiring objects and properties from some other class is known as inheritance in Java. 5 | - Inheritance is used to borrow properties & methods from an existing class. 6 | - Inheritance helps us create classes based on existing classes, which increases the code's reusability. 7 | - Examples : 8 | 9 | 10 | 11 | 12 | 13 | ### Important terminologies used in Inheritance : 14 | - Parent class/superclass: The class from which a class inherits methods and attributes is known as parent class. 15 | - Child class/sub-class: The class that inherits some other class's methods and attributes is known as child class. 16 | 17 | 18 | 19 | ### Extends keyword in inheritance : 20 | - The extends keyword is used to inherit a subclass from a superclass. 21 | - Syntax : 22 | 23 | ``` 24 | class Subclass-name extends Superclass-name 25 | { 26 | //methods and fields 27 | } 28 | ``` 29 | 30 | - Example : 31 | 32 | ``` 33 | public class dog extends Animal { 34 | // code 35 | } 36 | ``` 37 | 38 | **Note:** [Java doesn't support multiple inheritances](https://codewithharry.com/videos/java-tutorials-for-beginners-56), i.e., two classes cannot be the superclass for a subclass. 39 | 40 | **Quick quiz:** Create a class Animal and Derive another class dog from it. 41 | 42 | ``` 43 | class Animal{ 44 | public void printMe(){ 45 | System.out.println("I am a Animal Class."); 46 | } 47 | } 48 | 49 | class Derived extends Dog{ 50 | public void bark() { 51 | System.out.println("barking..."); 52 | } 53 | } 54 | 55 | public class cwh_45_inheritance { 56 | public static void main(String[] args) { 57 | } 58 | } 59 | ``` 60 | 61 | ### Code as described in video 62 | 63 | ``` 64 | class Base{ 65 | public int x; 66 | 67 | public int getX() { 68 | return x; 69 | } 70 | 71 | public void setX(int x) { 72 | System.out.println("I am in base and setting x now"); 73 | this.x = x; 74 | } 75 | 76 | public void printMe(){ 77 | System.out.println("I am a constructor"); 78 | } 79 | } 80 | 81 | class Derived extends Base{ 82 | public int y; 83 | 84 | public int getY() { 85 | return y; 86 | } 87 | 88 | public void setY(int y) { 89 | this.y = y; 90 | } 91 | } 92 | 93 | public class cwh_45_inheritance { 94 | public static void main(String[] args) { 95 | // Creating an Object of base class 96 | Base b = new Base(); 97 | b.setX(4); 98 | System.out.println(b.getX()); 99 | 100 | // Creating an object of derived class 101 | Derived d = new Derived(); 102 | d.setY(43); 103 | System.out.println(d.getY()); 104 | } 105 | } 106 | ``` 107 | 108 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-45/Chapter10.pdf)** 109 | 110 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-45/UltimateJavaCheatSheet.pdf)** 111 | -------------------------------------------------------------------------------- /45.Inheritance/cwh_45_inheritance.java: -------------------------------------------------------------------------------- 1 | class Base{ 2 | public int x; 3 | 4 | public int getX() { 5 | return x; 6 | } 7 | 8 | public void setX(int x) { 9 | System.out.println("I am in base and setting x now"); 10 | this.x = x; 11 | } 12 | 13 | public void printMe(){ 14 | System.out.println("I am a constructor"); 15 | } 16 | } 17 | 18 | class Derived extends Base{ 19 | public int y; 20 | 21 | public int getY() { 22 | return y; 23 | } 24 | 25 | public void setY(int y) { 26 | this.y = y; 27 | } 28 | } 29 | 30 | public class cwh_45_inheritance { 31 | public static void main(String[] args) { 32 | // Creating an Object of base class 33 | Base b = new Base(); 34 | b.setX(4); 35 | System.out.println(b.getX()); 36 | 37 | // Creating an object of derived class 38 | Derived d = new Derived(); 39 | d.setY(43); 40 | System.out.println(d.getY()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /46.Constructors_In_Inheritance/README.md: -------------------------------------------------------------------------------- 1 | # Constructors in Inheritance in Java 2 | ### Constructors in Inheritance: 3 | - When a drived class is extended from the base class, the constructor of the base class is executed first followed by the constructor of the derived class. For the following Inheritance hierarchy , the constructors are executed in the order: 4 | 5 | 1. C1- Parent 6 | 2. C2 - Child 7 | 3. C3 - Grandchild 8 | 9 | ### Constructors during constructor overloading : 10 | - When there are multiple constructors in the parent class, the constructor without any parameters is called from the child class. 11 | - If we want to call the constructor with parameters from the parent class, we can use the super keyword. 12 | - super(a, b) calls the constructor from the parent class which takes 2 variables 13 | 14 | ``` 15 | class Base1{ 16 | Base1(){ 17 | System.out.println("I am a constructor"); 18 | } 19 | Base1(int x){ 20 | System.out.println("I am an overloaded constructor with value of x as: " + x); 21 | } 22 | } 23 | 24 | class Derived1 extends Base1{ 25 | Derived1(){ 26 | //super(0); 27 | System.out.println("I am a derived class constructor"); 28 | } 29 | Derived1(int x, int y){ 30 | super(x); 31 | System.out.println("I am an overloaded constructor of Derived with value of y as: " + y); 32 | } 33 | } 34 | 35 | class ChildOfDerived extends Derived1{ 36 | ChildOfDerived(){ 37 | System.out.println("I am a child of derived constructor"); 38 | } 39 | ChildOfDerived(int x, int y, int z){ 40 | super(x, y); 41 | System.out.println("I am an overloaded constructor of Derived with value of z as: " + z); 42 | } 43 | } 44 | public class cwh_46_constructors_in_inheritance { 45 | public static void main(String[] args) { 46 | // Base1 b = new Base1(); 47 | // Derived1 d = new Derived1(); 48 | // Derived1 d = new Derived1(14, 9); 49 | // ChildOfDerived cd = new ChildOfDerived(); 50 | ChildOfDerived cd = new ChildOfDerived(12, 13, 15); 51 | } 52 | } 53 | ``` 54 | 55 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-46/JavaChapter10.pdf)** 56 | 57 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-46/UltimateJavaCheatSheet.pdf)** 58 | -------------------------------------------------------------------------------- /46.Constructors_In_Inheritance/cwh_46_constructors_in_inheritance.java: -------------------------------------------------------------------------------- 1 | class Base1{ 2 | Base1(){ 3 | System.out.println("I am a constructor"); 4 | } 5 | Base1(int x){ 6 | System.out.println("I am an overloaded constructor with value of x as: " + x); 7 | } 8 | } 9 | 10 | class Derived1 extends Base1{ 11 | Derived1(){ 12 | //super(0); 13 | System.out.println("I am a derived class constructor"); 14 | } 15 | Derived1(int x, int y){ 16 | super(x); 17 | System.out.println("I am an overloaded constructor of Derived with value of y as: " + y); 18 | } 19 | } 20 | 21 | class ChildOfDerived extends Derived1{ 22 | ChildOfDerived(){ 23 | System.out.println("I am a child of derived constructor"); 24 | } 25 | ChildOfDerived(int x, int y, int z){ 26 | super(x, y); 27 | System.out.println("I am an overloaded constructor of Derived with value of z as: " + z); 28 | } 29 | } 30 | public class cwh_46_constructors_in_inheritance { 31 | public static void main(String[] args) { 32 | // Base1 b = new Base1(); 33 | // Derived1 d = new Derived1(); 34 | // Derived1 d = new Derived1(14, 9); 35 | // ChildOfDerived cd = new ChildOfDerived(); 36 | ChildOfDerived cd = new ChildOfDerived(12, 13, 15); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /47.this_and_super_keyword/README.md: -------------------------------------------------------------------------------- 1 | # this and super keyword in Java 2 | 3 | ### this keyword in Java : 4 | - this is a way for us to reference an object of the class which is being created/referenced. 5 | - It is used to call the default constructor of the same class. 6 | - this keyword eliminates the confusion between the parameters and the class attributes with the same name. Take a look at the example given below : 7 | 8 | ``` 9 | class cwh{ 10 | int x; 11 | 12 | // getter of x 13 | public int getX(){ 14 | return x; 15 | } 16 | 17 | // Constructor with a parameter 18 | cwh(int x) { 19 | x = x; 20 | } 21 | 22 | // Call the constructor 23 | public static void main(String[] args) { 24 | cwh obj1 = new cwh(65); 25 | System.out.println(obj1.getX()); 26 | 27 | } 28 | } 29 | ``` 30 | 31 | ``` 32 | Output : 33 | 0 34 | ``` 35 | 36 | - In the above example, the expected output is 65 because we've passed x=65 to the constructor of the cwh class. But the compiler fails to differentiate between the parameter 'x' & class attribute 'x.' Therefore, it returns 0. 37 | 38 | - Now, let's see how we can handle this situation with the help of this keyword. Take a look at the below code : 39 | 40 | ``` 41 | class cwh1{ 42 | int x; 43 | 44 | // getter of x 45 | public int getX(){ 46 | return x; 47 | } 48 | 49 | // Constructor with a parameter 50 | cwh(int x) { 51 | this.x = x; 52 | } 53 | 54 | // Call the constructor 55 | public static void main(String[] args) { 56 | cwh obj1 = new cwh(65); 57 | System.out.println(obj1.getX()); 58 | 59 | } 60 | } 61 | ``` 62 | 63 | ``` 64 | Output : 65 | 65 66 | ``` 67 | 68 | - Now, you can see that we've got the desired output 69 | 70 | ### Super keyword 71 | - A reference variable used to refer immediate parent class object. 72 | - It can be used to refer immediate parent class instance variable. 73 | - It can be used to invoke the parent class method. 74 | 75 | 76 | ### Code as described in the video: 77 | 78 | ``` 79 | import javax.print.Doc; 80 | class EkClass{ 81 | int a; 82 | public int getA() { 83 | return a; 84 | } 85 | 86 | EkClass(int a) { 87 | this.a = a; 88 | } 89 | 90 | public int returnone() { 91 | return 1; 92 | } 93 | } 94 | 95 | class DoClass extends EkClass { 96 | DoClass(int c) { 97 | super(c); 98 | System.out.println("I am a constructor"); 99 | } 100 | } 101 | 102 | public class cwh_47_this_super { 103 | public static void main(String[] args) { 104 | EkClass e = new EkClass(65); 105 | DoClass d = new DoClass(5); 106 | System.out.println(e.getA()); 107 | } 108 | } 109 | ``` 110 | 111 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-47/JavaChapter10.pdf)** 112 | 113 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-47/UltimateJavaCheatSheet.pdf)** 114 | -------------------------------------------------------------------------------- /47.this_and_super_keyword/cwh.java: -------------------------------------------------------------------------------- 1 | class cwh{ 2 | int x; 3 | 4 | // getter of x 5 | public int getX(){ 6 | return x; 7 | } 8 | 9 | // Constructor with a parameter 10 | cwh(int x) { 11 | x = x; 12 | } 13 | 14 | // Call the constructor 15 | public static void main(String[] args) { 16 | cwh obj1 = new cwh(65); 17 | System.out.println(obj1.getX()); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /47.this_and_super_keyword/cwh1.java: -------------------------------------------------------------------------------- 1 | class cwh1{ 2 | int x; 3 | 4 | // getter of x 5 | public int getX(){ 6 | return x; 7 | } 8 | 9 | // Constructor with a parameter 10 | cwh(int x) { 11 | this.x = x; 12 | } 13 | 14 | // Call the constructor 15 | public static void main(String[] args) { 16 | cwh obj1 = new cwh(65); 17 | System.out.println(obj1.getX()); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /47.this_and_super_keyword/cwh_47_this_super.java: -------------------------------------------------------------------------------- 1 | import javax.print.Doc; 2 | class EkClass{ 3 | int a; 4 | public int getA() { 5 | return a; 6 | } 7 | 8 | EkClass(int a) { 9 | this.a = a; 10 | } 11 | 12 | public int returnone() { 13 | return 1; 14 | } 15 | } 16 | 17 | class DoClass extends EkClass { 18 | DoClass(int c) { 19 | super(c); 20 | System.out.println("I am a constructor"); 21 | } 22 | } 23 | 24 | public class cwh_47_this_super { 25 | public static void main(String[] args) { 26 | EkClass e = new EkClass(65); 27 | DoClass d = new DoClass(5); 28 | System.out.println(e.getA()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /48.Method_Overriding/CWH.java: -------------------------------------------------------------------------------- 1 | class A{ 2 | public void meth1() { 3 | System.out.println("I am method 1 of class A"); 4 | } 5 | } 6 | 7 | class B extends A { 8 | 9 | } 10 | public class CWH { 11 | public static void main(String[] args) { 12 | A a = new A(); 13 | a.meth1(); 14 | 15 | B b = new B(); 16 | b.meth1(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /48.Method_Overriding/CWH1.java: -------------------------------------------------------------------------------- 1 | class A{ 2 | public void meth1() { 3 | System.out.println("I am method 1 of class A"); 4 | } 5 | } 6 | 7 | class B extends A { 8 | @Override 9 | public void meth1(){ 10 | System.out.println("I am method 1 of class B"); 11 | } 12 | } 13 | public class CWH1 { 14 | public static void main(String[] args) { 15 | A a = new A(); 16 | a.meth1(); 17 | 18 | B b = new B(); 19 | b.meth1(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /48.Method_Overriding/README.md: -------------------------------------------------------------------------------- 1 | # Method Overriding in Java 2 | 3 | ### Method Overriding in Java: 4 | - If the child class implements the same method present in the parent class again, it is know as method overriding. 5 | - Method overriding helps us to classify a behavior that is specific to the child class. 6 | - The subclass can override the method of the parent class only when the method is not declared as final. 7 | - Example : 8 | - In the below code, we've created two classes: class A & class B. 9 | - Class B is inheriting class A. 10 | - In the main() method, we've created one object for both classes. We're running the meth1() method on class A and B objects separately, but the output is the same because the meth1() is defined in the parent class, i.e., class A. 11 | 12 | ``` 13 | class A{ 14 | public void meth1() { 15 | System.out.println("I am method 1 of class A"); 16 | } 17 | } 18 | 19 | class B extends A { 20 | 21 | } 22 | public class CWH { 23 | public static void main(String[] args) { 24 | A a = new A(); 25 | a.meth1(); 26 | 27 | B b = new B(); 28 | b.meth1(); 29 | } 30 | } 31 | ``` 32 | 33 | ``` 34 | Output : 35 | I am method 1 of class A 36 | I am method 1 of class A 37 | ``` 38 | 39 | - Now, let's see how we can override the meth1() for class B : 40 | 41 | ``` 42 | class A{ 43 | public void meth1() { 44 | System.out.println("I am method 1 of class A"); 45 | } 46 | } 47 | 48 | class B extends A { 49 | @Override 50 | public void meth1(){ 51 | System.out.println("I am method 1 of class B"); 52 | } 53 | } 54 | public class CWH1 { 55 | public static void main(String[] args) { 56 | A a = new A(); 57 | a.meth1(); 58 | 59 | B b = new B(); 60 | b.meth1(); 61 | } 62 | } 63 | ``` 64 | 65 | ``` 66 | Output : 67 | I am method 1 of class A 68 | I am method 1 of class B 69 | ``` 70 | 71 | ### Code as described in the video: 72 | 73 | ``` 74 | class A { 75 | public int a; 76 | public int harry() { 77 | return 4; 78 | } 79 | public void meth2() { 80 | System.out.println("I am method 2 of class A"); 81 | } 82 | } 83 | 84 | class B extends A { 85 | @Override 86 | public void meth2() { 87 | System.out.println("I am method 2 of class B"); 88 | } 89 | public void meth3() { 90 | System.out.println("I am method 3 of class B"); 91 | } 92 | } 93 | 94 | public class cwh_48_method_overriding { 95 | public static void main(String[] args) { 96 | A a = new A(); 97 | a.meth2(); 98 | 99 | B b = new B(); 100 | b.meth2(); 101 | } 102 | } 103 | ``` 104 | 105 | ``` 106 | Output: 107 | I am method 2 of class A 108 | I am method 2 of class B 109 | ``` 110 | 111 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-48/JavaChapter10.pdf)** 112 | 113 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-48/UltimateJavaCheatSheet.pdf)** 114 | -------------------------------------------------------------------------------- /48.Method_Overriding/cwh_48_method_overriding.java: -------------------------------------------------------------------------------- 1 | class A { 2 | public int a; 3 | public int harry() { 4 | return 4; 5 | } 6 | public void meth2() { 7 | System.out.println("I am method 2 of class A"); 8 | } 9 | } 10 | 11 | class B extends A { 12 | @Override 13 | public void meth2() { 14 | System.out.println("I am method 2 of class B"); 15 | } 16 | public void meth3() { 17 | System.out.println("I am method 3 of class B"); 18 | } 19 | } 20 | 21 | public class cwh_48_method_overriding { 22 | public static void main(String[] args) { 23 | A a = new A(); 24 | a.meth2(); 25 | 26 | B b = new B(); 27 | b.meth2(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /49.Dynamic_Method_Dispatch/README.md: -------------------------------------------------------------------------------- 1 | # Dynamic Method Dispatch in Java 2 | - Dynamic method dispatch is also known as run time polymorphism. 3 | - It is the process through which a call to an overridden method is resolved at runtime. 4 | - This technique is used to resolve a call to an overridden method at runtime rather than compile time. 5 | - To properly understand Dynamic method dispatch in Java, it is important to understand the concept of upcasting because dynamic method dispatch is based on upcasting. 6 | 7 | ### Upcasting : 8 | - It is a technique in which a superclass reference variable refers to the object of the subclass. 9 | - Example : 10 | 11 | ``` 12 | class Animal{} 13 | class Dog extends Animal{} 14 | Animal a=new Dog();//upcasting 15 | ``` 16 | 17 | - In the above example, we've created two classes, named Animal(superclass) & Dog(subclass). While creating the object 'a', we've taken the reference variable of the parent class(Animal), and the object created is of child class(Dog). 18 | 19 | ### Example to demonstrate the use of Dynamic method dispatch : 20 | - In the below code, we've created two classes: Phone & SmartPhone. 21 | - The Phone is the parent class and the SmartPhone is the child class. 22 | - The method on() of the parent class is overridden inside the child class. 23 | - Inside the main() method, we've created an object obj of the Smartphone() class by taking the reference of the Phone() class. 24 | - When obj.on() will be executed, it will call the on() method of the SmartPhone() class because the reference variable obj is pointing towards the object of class SmartPhone(). 25 | 26 | ``` 27 | class Phone{ 28 | public void showTime() { 29 | System.out.println("Time is 8 am"); 30 | } 31 | public void on(){ 32 | System.out.println("Turning on Phone..."); 33 | } 34 | } 35 | 36 | class SmartPhone extends Phone { 37 | public void music() { 38 | System.out.println("Playing music..."); 39 | } 40 | public void on() { 41 | System.out.println("Turning on SmartPhone..."); 42 | } 43 | } 44 | 45 | public class CWH { 46 | public static void main(String[] args) { 47 | 48 | Phone obj = new SmartPhone(); // Yes it is allowed 49 | // SmartPhone obj2 = new Phone(); // Not allowed 50 | 51 | obj.showTime(); 52 | obj.on(); 53 | // obj.music(); Not Allowed 54 | } 55 | } 56 | ``` 57 | 58 | ``` 59 | Output : 60 | Time is 8 am 61 | Turning on SmartPhone... 62 | ``` 63 | 64 | **Note:** The data members can not achieve the run time polymorphism. 65 | 66 | ### Code as described/written in the video : 67 | 68 | ``` 69 | class Phone { 70 | public void showTime() { 71 | System.out.println("Time is 8 am"); 72 | } 73 | public void on() { 74 | System.out.println("Turning on Phone..."); 75 | } 76 | } 77 | 78 | class SmartPhone extends Phone { 79 | public void music() { 80 | System.out.println("Playing music..."); 81 | } 82 | public void on() { 83 | System.out.println("Turning on SmartPhone..."); 84 | } 85 | } 86 | public class cwh_49_dynamic_method_dispatch { 87 | public static void main(String[] args) { 88 | // Phone obj = new Phone(); // Allowed 89 | // SmartPhone smobj = new SmartPhone(); // Allowed 90 | // obj.name(); 91 | 92 | Phone obj = new SmartPhone(); // Yes it is allowed 93 | // SmartPhone obj2 = new Phone(); // Not allowed 94 | 95 | obj.showTime(); 96 | obj.on(); 97 | // obj.music(); Not Allowed 98 | } 99 | } 100 | ``` 101 | 102 | **Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-49/JavaChapter10.pdf)** 103 | 104 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-49/UltimateJavaCheatSheet.pdf)** 105 | -------------------------------------------------------------------------------- /49.Dynamic_Method_Dispatch/cwh_49_dynamic_method_dispatch.java: -------------------------------------------------------------------------------- 1 | class Phone { 2 | public void showTime() { 3 | System.out.println("Time is 8 am"); 4 | } 5 | public void on() { 6 | System.out.println("Turning on Phone..."); 7 | } 8 | } 9 | 10 | class SmartPhone extends Phone { 11 | public void music() { 12 | System.out.println("Playing music..."); 13 | } 14 | public void on() { 15 | System.out.println("Turning on SmartPhone..."); 16 | } 17 | } 18 | public class cwh_49_dynamic_method_dispatch { 19 | public static void main(String[] args) { 20 | // Phone obj = new Phone(); // Allowed 21 | // SmartPhone smobj = new SmartPhone(); // Allowed 22 | // obj.name(); 23 | 24 | Phone obj = new SmartPhone(); // Yes it is allowed 25 | // SmartPhone obj2 = new Phone(); // Not allowed 26 | 27 | obj.showTime(); 28 | obj.on(); 29 | // obj.music(); Not Allowed 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /5.Getting_user_input/CWH_05_TakingInput.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | import java.util.Scanner; 3 | 4 | public class CWH_05_TakingInpu { 5 | public static void main(String[] args) { 6 | System.out.println("Taking Input From the User"); 7 | Scanner sc = new Scanner(System.in); 8 | // System.out.println("Enter number 1"); 9 | // int a = sc.nextInt(); 10 | // float a = sc.nextFloat(); 11 | // System.out.println("Enter number 2"); 12 | // int b = sc.nextInt(); 13 | // float b = sc.nextFloat(); 14 | 15 | // int sum = a +b; 16 | // float sum = a +b; 17 | // System.out.println("The sum of these numbers is"); 18 | // System.out.println(sum); 19 | // boolean b1 = sc.hasNextInt(); 20 | // System.out.println(b1); 21 | // String str = sc.next(); 22 | String str = sc.nextLine(); 23 | System.out.println(str); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /5.Getting_user_input/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Getting User Input in Java 2 | 3 | ### Reading data from the Keyboard : 4 | - Scanner class of java.util package is used to take input from the user's keyboard. 5 | - The Scanner class has many methods for taking input from the user depending upon the type of input. 6 | - To use any of the methods of the Scanner class, first, we need to create an object of the Scanner class as shown in the below example : 7 | 8 | ``` 9 | import java.util.Scanner; // Importing the Scanner class 10 | Scanner sc = new Scanner(System.in); //Creating an object named "sc" of the Scanner class. 11 | ``` 12 | 13 | - Taking an integer input from the keyboard : 14 | 15 | ``` 16 | Scanner S = new Scanner(System.in); //(Read from the keyboard) 17 | int a = S.nextInt(); //(Method to read from the keyboard) 18 | ``` 19 | 20 | ### Code as Described in the Video: 21 | 22 | ``` 23 | package com.company; 24 | import java.util.Scanner; 25 | 26 | public class CWH_05_TakingInpu { 27 | public static void main(String[] args) { 28 | System.out.println("Taking Input From the User"); 29 | Scanner sc = new Scanner(System.in); 30 | // System.out.println("Enter number 1"); 31 | // int a = sc.nextInt(); 32 | // float a = sc.nextFloat(); 33 | // System.out.println("Enter number 2"); 34 | // int b = sc.nextInt(); 35 | // float b = sc.nextFloat(); 36 | 37 | // int sum = a +b; 38 | // float sum = a +b; 39 | // System.out.println("The sum of these numbers is"); 40 | // System.out.println(sum); 41 | // boolean b1 = sc.hasNextInt(); 42 | // System.out.println(b1); 43 | // String str = sc.next(); 44 | String str = sc.nextLine(); 45 | System.out.println(str); 46 | 47 | } 48 | } 49 | ``` 50 | 51 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-5/JavaChapter1.pdf) 52 | 53 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-5/UltimateJavaCheatSheet.pdf) 54 | -------------------------------------------------------------------------------- /50.Exercise_3_Solution/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Exercise 3 - Solutions & Shoutouts 2 | 3 | #### Create a class Game, which allows a user to play "Guess the Number" game once. 4 | 5 | - Game should have the following methods: 6 | - Constructor to generate the random number 7 | - takeUserInput() to take a user input of number 8 | - isCorrectNumber() to detect whether the number entered by the user is true 9 | - getter and setter for noOfGuesses 10 | 11 | **Use properties such as noOfGuesses(int), etc to get this task done!** 12 | 13 | ``` 14 | import java.util.Random; 15 | import java.util.Scanner; 16 | 17 | class Game{ 18 | public int number; 19 | public int inputNumber; 20 | public int noOfGuesses = 0; 21 | 22 | public int getNoOfGuesses() { 23 | return noOfGuesses; 24 | } 25 | 26 | public void setNoOfGuesses(int noOfGuesses) { 27 | this.noOfGuesses = noOfGuesses; 28 | } 29 | 30 | Game(){ 31 | Random rand = new Random(); 32 | this.number = rand.nextInt(100); 33 | } 34 | void takeUserInput(){ 35 | System.out.println("Guess the number"); 36 | Scanner sc = new Scanner(System.in); 37 | inputNumber = sc.nextInt(); 38 | } 39 | boolean isCorrectNumber(){ 40 | noOfGuesses++; 41 | if (inputNumber==number){ 42 | System.out.format("Yes you guessed it right, it was %d\nYou guessed it in %d attempts", number, noOfGuesses); 43 | return true; 44 | } 45 | else if(inputNumbernumber){ 49 | System.out.println("Too high..."); 50 | } 51 | return false; 52 | } 53 | 54 | } 55 | public class cwh_50_ex3sol { 56 | public static void main(String[] args) { 57 | /* 58 | Create a class Game, which allows a user to play "Guess the Number" 59 | game once. Game should have the following methods: 60 | 1. Constructor to generate the random number 61 | 2. takeUserInput() to take a user input of number 62 | 3. isCorrectNumber() to detect whether the number entered by the user is true 63 | 4. getter and setter for noOfGuesses 64 | Use properties such as noOfGuesses(int), etc to get this task done! 65 | */ 66 | 67 | Game g = new Game(); 68 | boolean b= false; 69 | while(!b){ 70 | g.takeUserInput(); 71 | b = g.isCorrectNumber(); 72 | } 73 | 74 | } 75 | } 76 | ``` 77 | 78 | **Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-50/UltimateJavaCheatSheet.pdf)** 79 | -------------------------------------------------------------------------------- /50.Exercise_3_Solution/cwh_50_ex3sol.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | import java.util.Scanner; 3 | 4 | class Game{ 5 | public int number; 6 | public int inputNumber; 7 | public int noOfGuesses = 0; 8 | 9 | public int getNoOfGuesses() { 10 | return noOfGuesses; 11 | } 12 | 13 | public void setNoOfGuesses(int noOfGuesses) { 14 | this.noOfGuesses = noOfGuesses; 15 | } 16 | 17 | Game() { 18 | Random rand = new Random(); 19 | this.number = rand.nextInt(100); 20 | } 21 | void takeUserInput() { 22 | System.out.println("Guess the number"); 23 | Scanner sc = new Scanner(System.in); 24 | inputNumber = sc.nextInt(); 25 | } 26 | boolean isCorrectNumber() { 27 | noOfGuesses++; 28 | if (inputNumber == number){ 29 | System.out.format("Yes you guessed it right, it was %d\nYou guessed it in %d attempts", number, noOfGuesses); 30 | return true; 31 | } 32 | else if(inputNumbernumber){ 36 | System.out.println("Too high..."); 37 | } 38 | return false; 39 | } 40 | 41 | } 42 | public class cwh_50_ex3sol { 43 | public static void main(String[] args) { 44 | /* 45 | Create a class Game, which allows a user to play "Guess the Number" 46 | game once. Game should have the following methods: 47 | 1. Constructor to generate the random number 48 | 2. takeUserInput() to take a user input of number 49 | 3. isCorrectNumber() to detect whether the number entered by the user is true 50 | 4. getter and setter for noOfGuesses 51 | Use properties such as noOfGuesses(int), etc to get this task done! 52 | */ 53 | 54 | Game g = new Game(); 55 | boolean b= false; 56 | while(!b) { 57 | g.takeUserInput(); 58 | b = g.isCorrectNumber(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /6.Java_exercise_1/Percentage_calc.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class MyClass { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | int a, b, c, d, e; 7 | float result, percentage; 8 | System.out.println("Enter your marks respectively"); 9 | 10 | System.out.println("Enter your marks in Hindi Subject"); 11 | a = sc.nextInt(); 12 | 13 | System.out.println("Enter your marks in English Subject"); 14 | b = sc.nextInt(); 15 | 16 | System.out.println("Enter your marks in Math Subject"); 17 | c = sc.nextInt(); 18 | 19 | System.out.println("Enter your marks in Physics Subject"); 20 | d = sc.nextInt(); 21 | 22 | System.out.println("Enter your marks in Chemistry Subject"); 23 | e = sc.nextInt(); 24 | 25 | result = a + b + c + d + e; 26 | percentage = (result/500) * 100; 27 | 28 | System.out.println("Your percentage is " + percentage); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /6.Java_exercise_1/README.md: -------------------------------------------------------------------------------- 1 | # Java Programming Exercise 1: CBSE Board Percentage Calculator 2 | 3 | ### Exercise 1.1 4 | 5 | - Write a program to calculate the percentage of a given student in the CBSE board exam. His marks from 5 subjects must be taken as input from the keyboard. (Marks are out of 100) 6 | 7 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-6/JavaChapter1.pdf) 8 | 9 | ### Ultimate Java Cheatsheet:[Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-6/UltimateJavaCheatSheet.pdf) 10 | -------------------------------------------------------------------------------- /7.Chapter_1_practice_set/Q_1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Q_1 { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | int a = 40; 7 | int b = 23; 8 | int c = 45; 9 | int sum = a + b + c; 10 | System.out.println("The sum is " + sum); 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /7.Chapter_1_practice_set/Q_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Q_2 { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | float subject1 = 45; 7 | float subject2 = 95; 8 | float subject3 = 48; 9 | float cgpa = (subject1 + subject2 +subject3)/30; 10 | System.out.println("The CGPA is " + cgpa); 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /7.Chapter_1_practice_set/Q_3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Q_3 { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.println("What is your name"); 7 | String name = sc.next(); 8 | System.out.println("Hello " + name + " have a good day!"); 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /7.Chapter_1_practice_set/Q_4.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Q_4 { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | double kilometers; 7 | System.out.println("Please enter kilometers:"); 8 | kilometers = sc.nextDouble(); 9 | 10 | double miles = kilometers / 1.6; 11 | 12 | System.out.println(miles + " Miles"); 13 | } 14 | } -------------------------------------------------------------------------------- /7.Chapter_1_practice_set/Q_5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Q_5 { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.println("Enter your number"); 7 | System.out.println(sc.hasNextInt()); 8 | } 9 | } -------------------------------------------------------------------------------- /7.Chapter_1_practice_set/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Chapter 1- Practice Set | Java Practice Problems With Solution 2 | 3 | ### Chapter 1 – Practice Set 4 | 5 | 1. Write a program to sum three numbers in Java. 6 | 2. Write a program to calculate CGPA using marks of three subjects (out of 100) 7 | 3. Write a Java program that asks the user to enter his/her name and greets them with “Hello , have a good day” text. 8 | 4. Write a Java program to convert Kilometers to miles. 9 | 5. Write a Java program to detect whether a number entered by the user is an integer or not. 10 | 11 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-7/JavaChapter1PracticeSet.pdf) 12 | 13 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-7/UltimateJavaCheatSheet.pdf) 14 | -------------------------------------------------------------------------------- /8.Operators_and_expressions/CWH_Ch2_Operators.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | public class CWH_Ch2_Operators { 4 | public static void main(String[] args) { 5 | // 1. Arithmetic Operators 6 | int a = 4; 7 | // int b = 6 % a; // Modulo Operator 8 | // 4.8%1.1 --> Returns Decimal Remainder 9 | 10 | // 2. Assignment Operators 11 | int b = 9; 12 | b *= 3; 13 | System.out.println(b); 14 | 15 | // 3. Comparison Operators 16 | // System.out.println(64<6); 17 | 18 | // 4. Logical Operators 19 | // System.out.println(64>5 && 64>98); 20 | System.out.println(64>5 || 64>98); 21 | 22 | // 5. Bitwise Operators 23 | System.out.println(2&3); 24 | // 10 25 | // 11 26 | // ---- 27 | // 10 28 | } 29 | } -------------------------------------------------------------------------------- /8.Operators_and_expressions/Chapter2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/Java-Tutorials/fbb23054f7666cfda1e590e660f19a6a064ef8fa/8.Operators_and_expressions/Chapter2.pdf -------------------------------------------------------------------------------- /8.Operators_and_expressions/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Operators, Types of Operators & Expressions in Java 2 | - An operator is a symbol that the compiler to perform a specific operation on operands. 3 | - Example : a + b = c 4 | - In the above example, 'a' and 'b' are operands on which the '+' operator is applied. 5 | 6 | ## Types of operators : 7 | ### 1. Arithmetic Operators : 8 | - Arithmetic operators are used to perform mathematical operations such as addition, division, etc on expressions. 9 | - Arithmetic operators cannot work with Booleans. 10 | - % operator can work on floats and doubles. 11 | - Let x=7 and y=2 12 | 13 | ![image](https://user-images.githubusercontent.com/70385488/150535449-0074c214-cb1d-4a29-8306-e9632986a628.png) 14 | 15 | ### 2. Comparison Operators : 16 | - As the name suggests, these operators are used to compare two operands. 17 | - Let x=7 and y=2 18 | 19 | ![image](https://user-images.githubusercontent.com/70385488/150535594-fec270da-1c59-40f3-a0bf-537bf3f9ec5f.png) 20 | 21 | ### 3. Logical Operators : 22 | - These operators determine the logic in an expression containing two or more values or variables. 23 | - Let x = 8 and y =2 24 | 25 | ![image](https://user-images.githubusercontent.com/70385488/150535778-ba37d847-d57a-4840-a84d-7855ad808fc7.png) 26 | 27 | ### 4. Bitwise Operators : 28 | - These operators perform the operations on every bit of a number. 29 | - Let x =2 and y=3. So 2 in binary is 100, and 3 is 011. 30 | 31 | ![image](https://user-images.githubusercontent.com/70385488/150536221-5e7263d7-e2f1-4865-8164-f428bd54c8d4.png) 32 | 33 | ## Precedence of operators 34 | - The operators are applied and evaluated based on precedence. For example, (+, -) has less precedence compared to (*, /). Hence '*' and / are evaluated first. 35 | - In case we like to change this order, we use parenthesis () 36 | 37 | ## Code as Described in the Video 38 | ``` 39 | package com.company; 40 | 41 | public class CWH_Ch2_Operators { 42 | public static void main(String[] args) { 43 | // 1. Arithmetic Operators 44 | int a = 4; 45 | // int b = 6 % a; // Modulo Operator 46 | // 4.8%1.1 --> Returns Decimal Remainder 47 | 48 | // 2. Assignment Operators 49 | int b = 9; 50 | b *= 3; 51 | System.out.println(b); 52 | 53 | // 3. Comparison Operators 54 | // System.out.println(64<6); 55 | 56 | // 4. Logical Operators 57 | // System.out.println(64>5 && 64>98); 58 | System.out.println(64>5 || 64>98); 59 | 60 | // 5. Bitwise Operators 61 | System.out.println(2&3); 62 | // 10 63 | // 11 64 | // ---- 65 | // 10 66 | } 67 | } 68 | ``` 69 | 70 | ### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-8/Chapter2.pdf) 71 | 72 | ### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-8/UltimateJavaCheatSheet.pdf) 73 | -------------------------------------------------------------------------------- /9.Associativity_of_operators/README.md: -------------------------------------------------------------------------------- 1 | # Java Tutorial: Associativity of Operators in Java 2 | ## Associativity 3 | - Associativity tells the direction of the execution of operators. It can either be left to right or vice versa. 4 | - / * -> L to R 5 | - + - -> L to R 6 | - ++, = -> R to L 7 | - Here is the precedence and associativity table which makes it easy for you to understand these topics better: 8 | 9 | 10 | 11 | **Quick Quiz:** How will you write the following expression in Java? 12 | 13 | 14 | 15 | ## Code as Described in the Video 16 | ``` 17 | package com.company; 18 | 19 | public class cwh_09_ch2_op_pre { 20 | public static void main(String[] args) { 21 | // Precedence & Associativity 22 | 23 | //int a = 6*5-34/2; 24 | /* 25 | Highest precedence goes to * and /. They are then evaluated on the basis 26 | of left to right associativity 27 | =30-34/2 28 | =30-17 29 | =13 30 | */ 31 | //int b = 60/5-34*2; 32 | /* 33 | = 12-34*2 34 | =12-68 35 | =-56 36 | */ 37 | 38 | //System.out.println(a); 39 | //System.out.println(b); 40 | 41 | // Quick Quiz 42 | int x =6; 43 | int y = 1; 44 | // int k = x * y/2; 45 | 46 | int b = 0; 47 | int c = 0; 48 | int a = 10; 49 | int k = b*b - (4*a*c)/(2*a); 50 | System.out.println(k); 51 | 52 | } 53 | } 54 | ``` 55 | 56 | ## Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-9/Chapter2.pdf) 57 | 58 | ## Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-9/UltimateJavaCheatSheet.pdf) 59 | -------------------------------------------------------------------------------- /9.Associativity_of_operators/cwh_09_ch2_op_pre.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | public class cwh_09_ch2_op_pre { 4 | public static void main(String[] args) { 5 | // Precedence & Associativity 6 | 7 | //int a = 6*5-34/2; 8 | /* 9 | Highest precedence goes to * and /. They are then evaluated on the basis 10 | of left to right associativity 11 | =30-34/2 12 | =30-17 13 | =13 14 | */ 15 | //int b = 60/5-34*2; 16 | /* 17 | = 12-34*2 18 | =12-68 19 | =-56 20 | */ 21 | 22 | //System.out.println(a); 23 | //System.out.println(b); 24 | 25 | // Quick Quiz 26 | int x =6; 27 | int y = 1; 28 | // int k = x * y/2; 29 | 30 | int b = 0; 31 | int c = 0; 32 | int a = 10; 33 | int k = b*b - (4*a*c)/(2*a); 34 | System.out.println(k); 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Kishan Kumar Rai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java-Tutorials 2 | 3 | ![Java-Tutorials](https://socialify.git.ci/kishanrajput23/Java-Tutorials/image?description=1&font=Bitter&language=1&name=1&owner=1&pattern=Brick%20Wall&theme=Dark) 4 | --------------------------------------------------------------------------------