├── Week-1 ├── Instructions.md ├── Week-1 Java.xlsx ├── While & Do-While Loops │ ├── While.class │ ├── DoWhile.class │ ├── DoWhile.java │ ├── While.java │ └── README.md ├── Java Program through IDE │ ├── Images │ │ ├── Step_1.png │ │ ├── Step_2.png │ │ ├── Step_3.png │ │ ├── Step_4.png │ │ ├── Step_5.png │ │ ├── Step_6.png │ │ └── Step_7.png │ ├── HelloWorld.java │ └── README.md ├── Switch, while and do-while statements │ ├── Switch.class │ ├── Switch.java │ └── README.md ├── How to run a java program using command prompt │ ├── Step 1.png │ ├── Step 2.png │ ├── Step 3.png │ ├── Step 4.png │ ├── Step 5.png │ ├── Step 6.png │ ├── Step 7.png │ ├── helloWorld.java │ └── How to run a java program using command prompt.md ├── if , if- else , if-else-if │ ├── else-if.java │ └── README.md ├── Upcasting and Downcasting │ ├── UpcastingAndDowncasting.java │ └── README.md ├── Array of Object References │ ├── ArrayOfObjects.java │ └── README.md ├── Java Naming Conventions │ └── Naming Conventions.md ├── Assignment Operator │ ├── AssignmentOperator.java │ └── README.md ├── Java Conditions │ └── Conditions.md ├── Non-Primitive Datatypes │ └── Non-Primitive Datatypes.md ├── Operators │ └── Logical Operators.md ├── Break and Continue Statements │ └── BreakContinue.java └── Expressions And Operators in Java │ └── expressions_and_operators_in_java.md ├── Week-2 ├── Inheritance │ ├── Person.class │ ├── Teacher.class │ ├── Inheritance.class │ ├── Inheritance.java │ └── README.md ├── Objects & Methods │ ├── Student.class │ ├── ObjectTest.class │ ├── ObjectTest.java │ └── README.md ├── Classes and Objects │ ├── Blank-Diagram-Page-1-3.png │ ├── Blank-Diagram-Page-1-5.png │ ├── ClassesAndObjects.java │ └── README.md ├── Java Bean & POJO │ ├── Employee.java │ └── Employee1.java ├── Packages │ └── ImportDemo.java ├── Getters and Setters │ ├── Employee.java │ └── Getters and Setters.md ├── Concrete Classes │ ├── ConcreteEx1.java │ ├── ConcreteEx2.java │ └── README.md ├── Defining Class, Instance Variables and Methods │ ├── ClassVariableMethod.java │ └── README.md ├── Compile-time Polymorphism │ ├── CompiletimePolymorphism.java │ ├── README.md │ └── TypePromotion.svg ├── Arithmetic Operators │ └── Arithmetic_Operators.md ├── Strict Typing │ └── Strict_Typing.md ├── Differences Between JAR and WAR Packaging │ └── JARvsWAR.md ├── Abstraction │ ├── Abstraction.java │ └── Abstraction.md ├── Super Class and Sub Class │ ├── SuperclassSubclass.java │ └── README.md ├── Reference variables, local variables, instance variable │ └── Reference variables, local variables, instance variable.MD ├── Rules of Method Overloading and Overriding │ ├── MethodOverloadOverride.java │ └── README.md ├── Protection of data │ └── Protection_of_data.md ├── The JDK Directory Structure │ └── The JDK Directory Structure.md ├── operator Precedence │ └── operator Precedence.md ├── class, variable and method in Java │ └── class, variable and method in Java.md └── Accessing arrays, manipulating arrays │ └── Array_Operations.md └── Week-3 ├── Declaring Exceptions ├── Declaring Exceptions.java └── Declaring Exceptions.md ├── Assertions ├── Assertions.java └── README.md ├── Default and Parameterized Constructor ├── Constructors.java └── README.md ├── Static Concept in Inheritance ├── StaticInheritance.java └── README.md ├── Static Variables and Methods ├── StaticVariableMethod.java └── README.md ├── Memory Allocation in Java ├── MemoryAllocation.java └── README.md ├── Custom Exception ├── CustomException.java └── README.md ├── Defining and Throwing Exceptions ├── ThrowingException.java └── README.md ├── Errors and Runtime Exceptions ├── ExceptionError.java └── README.md ├── Queue Implementation Classes ├── QueueExample.java └── README.md ├── this and super Call for Constructors ├── thisSuperConstructor.java └── README.md ├── Keywords in Exception API: try, catch, finally, throw, throws └── Keywords in Exception API: try, catch, finally, throw, throws.md └── The_Collections_Framework └── The_Collections_Framework.md /Week-1/Instructions.md: -------------------------------------------------------------------------------- 1 | Every PR should have files in .md/.java format. 2 | -------------------------------------------------------------------------------- /Week-1/Week-1 Java.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/Week-1 Java.xlsx -------------------------------------------------------------------------------- /Week-2/Inheritance/Person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-2/Inheritance/Person.class -------------------------------------------------------------------------------- /Week-2/Inheritance/Teacher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-2/Inheritance/Teacher.class -------------------------------------------------------------------------------- /Week-2/Inheritance/Inheritance.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-2/Inheritance/Inheritance.class -------------------------------------------------------------------------------- /Week-2/Objects & Methods/Student.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-2/Objects & Methods/Student.class -------------------------------------------------------------------------------- /Week-1/While & Do-While Loops/While.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/While & Do-While Loops/While.class -------------------------------------------------------------------------------- /Week-2/Objects & Methods/ObjectTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-2/Objects & Methods/ObjectTest.class -------------------------------------------------------------------------------- /Week-1/While & Do-While Loops/DoWhile.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/While & Do-While Loops/DoWhile.class -------------------------------------------------------------------------------- /Week-1/Java Program through IDE/Images/Step_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/Java Program through IDE/Images/Step_1.png -------------------------------------------------------------------------------- /Week-1/Java Program through IDE/Images/Step_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/Java Program through IDE/Images/Step_2.png -------------------------------------------------------------------------------- /Week-1/Java Program through IDE/Images/Step_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/Java Program through IDE/Images/Step_3.png -------------------------------------------------------------------------------- /Week-1/Java Program through IDE/Images/Step_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/Java Program through IDE/Images/Step_4.png -------------------------------------------------------------------------------- /Week-1/Java Program through IDE/Images/Step_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/Java Program through IDE/Images/Step_5.png -------------------------------------------------------------------------------- /Week-1/Java Program through IDE/Images/Step_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/Java Program through IDE/Images/Step_6.png -------------------------------------------------------------------------------- /Week-1/Java Program through IDE/Images/Step_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/Java Program through IDE/Images/Step_7.png -------------------------------------------------------------------------------- /Week-2/Classes and Objects/Blank-Diagram-Page-1-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-2/Classes and Objects/Blank-Diagram-Page-1-3.png -------------------------------------------------------------------------------- /Week-2/Classes and Objects/Blank-Diagram-Page-1-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-2/Classes and Objects/Blank-Diagram-Page-1-5.png -------------------------------------------------------------------------------- /Week-1/Switch, while and do-while statements/Switch.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/Switch, while and do-while statements/Switch.class -------------------------------------------------------------------------------- /Week-1/How to run a java program using command prompt/Step 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/How to run a java program using command prompt/Step 1.png -------------------------------------------------------------------------------- /Week-1/How to run a java program using command prompt/Step 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/How to run a java program using command prompt/Step 2.png -------------------------------------------------------------------------------- /Week-1/How to run a java program using command prompt/Step 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/How to run a java program using command prompt/Step 3.png -------------------------------------------------------------------------------- /Week-1/How to run a java program using command prompt/Step 4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/How to run a java program using command prompt/Step 4.png -------------------------------------------------------------------------------- /Week-1/How to run a java program using command prompt/Step 5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/How to run a java program using command prompt/Step 5.png -------------------------------------------------------------------------------- /Week-1/How to run a java program using command prompt/Step 6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/How to run a java program using command prompt/Step 6.png -------------------------------------------------------------------------------- /Week-1/How to run a java program using command prompt/Step 7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn-Write-Repeat/java/HEAD/Week-1/How to run a java program using command prompt/Step 7.png -------------------------------------------------------------------------------- /Week-1/While & Do-While Loops/DoWhile.java: -------------------------------------------------------------------------------- 1 | class DoWhile{ 2 | public static void main(String args[]) 3 | { 4 | int i=1; 5 | do 6 | { 7 | System.out.println(i); 8 | i=i+1; 9 | }while(i<=5); 10 | } 11 | } -------------------------------------------------------------------------------- /Week-1/How to run a java program using command prompt/helloWorld.java: -------------------------------------------------------------------------------- 1 | public class helloWorld{ 2 | public static void main(String args[]){ 3 | System.out.println("Hello World!This is my first Java Program"); 4 | } 5 | } -------------------------------------------------------------------------------- /Week-2/Classes and Objects/ClassesAndObjects.java: -------------------------------------------------------------------------------- 1 | // Create a Class 2 | //Main.java 3 | public class ClassesAndObjects { 4 | int x=10; 5 | public static void main(String[] args) { 6 | ClassesAndObjects myObj = new ClassesAndObjects(); 7 | System.out.println(myObj.x); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Week-1/While & Do-While Loops/While.java: -------------------------------------------------------------------------------- 1 | class While { // class name 2 | public static void main(String args[]) { 3 | int i = 1; 4 | while (i <= 5) // while loop starts 5 | { 6 | System.out.println(i); 7 | i = i + 1; 8 | } // while loop ends 9 | } 10 | } -------------------------------------------------------------------------------- /Week-2/Java Bean & POJO/Employee.java: -------------------------------------------------------------------------------- 1 | //Employee.java 2 | 3 | package mypack; 4 | public class Employee implements java.io.Serializable{ 5 | private int id; 6 | private String name; 7 | public Employee(){} 8 | public void setId(int id){this.id=id;} 9 | public int getId(){return id;} 10 | public void setName(String name){this.name=name;} 11 | public String getName(){return name;} 12 | } 13 | -------------------------------------------------------------------------------- /Week-2/Java Bean & POJO/Employee1.java: -------------------------------------------------------------------------------- 1 | // POJO class Exmaple 2 | package Jtp.PojoDemo; 3 | public class Employee1 { 4 | private String name; 5 | private String id; 6 | private double sal; 7 | public String getName() { 8 | return name; 9 | } 10 | public void setName(String name) { 11 | this.name = name; 12 | } 13 | public String getId() { 14 | return id; 15 | } 16 | public void setId(String id) { 17 | this.id = id; 18 | } 19 | public double getSal() { 20 | return sal; 21 | } 22 | public void setSal(double sal) { 23 | this.sal = sal; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Week-3/Declaring Exceptions/Declaring Exceptions.java: -------------------------------------------------------------------------------- 1 | // How to Declaring an Exceptions? 2 | // this is an example that shows the decleration of Exceptions 3 | import java.io.*; 4 | 5 | public class ExcepTest { 6 | 7 | public static void main(String args[]) { 8 | try { 9 | int a[] = new int[2]; 10 | System.out.println("Access element three :" + a[3]); 11 | } catch (ArrayIndexOutOfBoundsException e) { 12 | System.out.println("Exception thrown :" + e); 13 | } 14 | System.out.println("Out of the block"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Week-1/Java Program through IDE/HelloWorld.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A simple Hello World program in Java. 3 | * 4 | * It contains a public class HelloWorld and a public 5 | * static method main. 6 | * While compile and run this file the main method inside HelloWorld 7 | * will be automatically execute. 8 | * 9 | * @author nknantha 10 | */ 11 | 12 | // Public class HelloWorld. 13 | public class HelloWorld { 14 | 15 | // Public static method main. 16 | public static void main(String[] args) { 17 | // Below line prints "Hello World" in console. 18 | System.out.println("Hello World"); 19 | } 20 | } 21 | /** 22 | * Output: 23 | * Hello World 24 | * 25 | */ 26 | -------------------------------------------------------------------------------- /Week-2/Packages/ImportDemo.java: -------------------------------------------------------------------------------- 1 | // Java program to demonstrate accessing of members when 2 | // corresponding classes are imported and not imported. 3 | import java.util.Vector; 4 | 5 | public class ImportDemo 6 | { 7 | public ImportDemo() 8 | { 9 | // java.util.Vector is imported, hence we are 10 | // able to access directly in our code. 11 | Vector newVector = new Vector(); 12 | 13 | // java.util.ArrayList is not imported, hence 14 | // we were referring to it using the complete 15 | // package. 16 | java.util.ArrayList newList = new java.util.ArrayList(); 17 | } 18 | 19 | public static void main(String arg[]) 20 | { 21 | new ImportDemo(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week-3/Assertions/Assertions.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the example for assertion in Java. 3 | * 4 | * @author NanthaKumar 5 | * @date 2021/08/18 6 | */ 7 | 8 | // Main class contains main method. 9 | public class Assertions { 10 | 11 | // method to check minimum. 12 | public static void checkMinimum(int num) { 13 | assert num > 21: "Value under minimum"; 14 | System.out.println("Given Value: " + num); 15 | } 16 | 17 | // main() method contains example. 18 | public static void main(String[] args) { 19 | 20 | // Invoking assertion. 21 | checkMinimum(7); 22 | 23 | // Value > 21 24 | checkMinimum(25); 25 | } 26 | } 27 | 28 | /** 29 | * Output: 30 | * Given Value: 7 31 | * Given Value: 25 32 | */ 33 | -------------------------------------------------------------------------------- /Week-2/Getters and Setters/Employee.java: -------------------------------------------------------------------------------- 1 | // Employee class. 2 | class Employee { 3 | // Member variables of the class. 4 | private int id; 5 | private String name; 6 | private String designation; 7 | private String company; 8 | public int getId() { 9 | return id; 10 | } 11 | public void setId(final int id) { 12 | this.id = id; 13 | } 14 | public String getName() { 15 | return name; 16 | } 17 | public void setName(final String name) { 18 | this.name = name; 19 | } 20 | public String getDesignation() { 21 | return designation; 22 | } 23 | public void setDesignation(final String designation) { 24 | this.designation = designation; 25 | } 26 | public String getCompany() { 27 | return company; 28 | } 29 | public void setCompany(final String company) { 30 | this.company = company; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Week-2/Inheritance/Inheritance.java: -------------------------------------------------------------------------------- 1 | class Person{ // parent class 2 | private String name; 3 | private int id; 4 | 5 | public Person(String name,int id) // constructor of parent class 6 | { 7 | this.name=name; 8 | this.id=id; 9 | 10 | } 11 | public void show() 12 | { 13 | System.out.println("Name : " +name); 14 | System.out.println("Id : " +id); 15 | } 16 | } 17 | 18 | class Teacher extends Person // child class extending properties of parent class 19 | { 20 | private double salary; 21 | 22 | public Teacher(String name,int id,double salary) 23 | { 24 | super(name,id); // takes properties of super class 25 | this.salary=salary; 26 | } 27 | 28 | public void show() 29 | { 30 | super.show(); 31 | System.out.println("Salary: " +salary); 32 | }} 33 | 34 | class Inheritance{ // main class 35 | public static void main(String args[]) 36 | { 37 | Teacher t=new Teacher("Amit",10,40000); // object of the child class which extends parent class 38 | t.show(); 39 | System.out.println(""); 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /Week-1/Switch, while and do-while statements/Switch.java: -------------------------------------------------------------------------------- 1 | 2 | // libraries 3 | import java.util.*; 4 | 5 | class Switch { 6 | public static void main(String args[]) { 7 | float res; 8 | Scanner sc = new Scanner(System.in); // to take i/p from user 9 | System.out.println("Enter 1st number: "); 10 | int a = sc.nextInt(); 11 | System.out.println("Enter 2nd number: "); 12 | int b = sc.nextInt(); 13 | 14 | System.out.println("1.Add\t 2.Subtract\t 3.Multiply\t 4.Divide\t 5.Exit: "); 15 | int opt = sc.nextInt(); 16 | switch (opt) // switch case starts 17 | { 18 | case 1: 19 | res = a + b; 20 | System.out.println("Addition = " + res); 21 | break; 22 | 23 | case 2: 24 | res = a - b; 25 | System.out.println("Subtraction = " + res); 26 | break; 27 | 28 | case 3: 29 | res = a * b; 30 | System.out.println("Multiplication = " + res); 31 | break; 32 | 33 | case 4: 34 | res = a / b; 35 | System.out.println("Division = " + res); 36 | break; 37 | 38 | default: 39 | break; 40 | 41 | } // switch case ends 42 | } 43 | } -------------------------------------------------------------------------------- /Week-2/Objects & Methods/ObjectTest.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // Creating Student class 4 | class Student { 5 | 6 | private int phy, chem, maths; // defining fields 7 | 8 | public void setInfo(int phy, int maths, int chem) { 9 | this.phy = phy; 10 | this.maths = maths; 11 | this.chem = chem; 12 | } 13 | 14 | public void avgMks() { 15 | int sum = 0; 16 | double avg = 0; 17 | sum = phy + maths + chem; 18 | avg = sum / 3; 19 | System.out.println("Avg marks: " + avg); 20 | 21 | } 22 | } 23 | 24 | // Creating another class ObjectTest which contains the main method 25 | class ObjectTest { 26 | 27 | public static void main(String args[]) { 28 | Scanner sc = new Scanner(System.in); 29 | System.out.println("Enter physics marks: "); 30 | int phy = sc.nextInt(); 31 | 32 | System.out.println("Enter maths marks: "); 33 | int maths = sc.nextInt(); 34 | 35 | System.out.println("Enter chemistry marks: "); 36 | int chem = sc.nextInt(); 37 | 38 | Student s = new Student(); // Creating an object or instance 39 | s.setInfo(phy, maths, chem); // initializing the value to these objects by invoking the setInfo method 40 | s.avgMks(); // calculates avg using avgMks method 41 | 42 | } 43 | } -------------------------------------------------------------------------------- /Week-1/if , if- else , if-else-if/else-if.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner ; //library 2 | class else-if //class name 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner in =new Scanner(System.in); 7 | int i=in.nextInt(); // Accept the value of i from the user . 8 | 9 | if(i==2) // if condition is i==2 . 10 | { 11 | System.out.println("Even Number"); // if the condition i==2 is true . 12 | } 13 | else if(i==3) // else if condition is i==3 . 14 | { // The specified codition has to be true to perform the following execution . 15 | 16 | System.out.println("\nOdd Number \nExit from else - if block . "); 17 | 18 | } 19 | 20 | else if(i==5) // else if condition is i==5 . 21 | { 22 | System.out.println(" Five : Prime Number ."); // if the condition i==5 is true . 23 | } 24 | 25 | else 26 | // if neither of the conditions are true . 27 | { 28 | System.out.println(" \n else-if conditions are found false . \n Exit from else ."); // else block will be executed . 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Week-2/Concrete Classes/ConcreteEx1.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains example of simple concrete class. 3 | * It contains ConcreteEx1 class, contains all possible methods 4 | * that the program needs. 5 | * 6 | * @author NanthaKumar 7 | * @date 2021/08/08 8 | */ 9 | 10 | // Example - 1 11 | 12 | // A simple concrete public class named ConcreteEx1. 13 | public class ConcreteEx1 { 14 | 15 | // add() method takes 2 integers and return their sum. 16 | static int add(int a, int b) { 17 | return a + b; 18 | } 19 | 20 | // multiply() method takes 2 integers and return their product. 21 | static int multiply(int a, int b) { 22 | return a * b; 23 | } 24 | 25 | // main() method contains driver code. 26 | public static void main(String[] args) { 27 | 28 | // Creating variables to hold integer values. 29 | int num1 = 10, num2 = 5; 30 | 31 | // Calling add() method with num1 and num2 and 32 | // printing the result. 33 | System.out.println("Sum: " + add(num1, num2)); 34 | 35 | // Printing the result of multiply() with num1 and num2. 36 | System.out.println("Product: " + multiply(num1, num2)); 37 | } 38 | } 39 | 40 | /** 41 | * OUTPUT: 42 | * Sum: 15 43 | * Product: 50 44 | */ 45 | -------------------------------------------------------------------------------- /Week-3/Default and Parameterized Constructor/Constructors.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the example of types of constructor. 3 | * 4 | * @author NanthaKumar 5 | * @date 2021/08/14 6 | */ 7 | 8 | // Class Animal contains default and parameterized constructor. 9 | class Animal { 10 | 11 | public String color; 12 | 13 | // Default constructor. 14 | Animal() { 15 | 16 | // Storing default value in instance variable. 17 | this.color = "Red"; 18 | } 19 | 20 | // Parameterized constructor. 21 | Animal(String color) { 22 | 23 | // Storing given value in instance variable. 24 | this.color = color; 25 | } 26 | } 27 | 28 | // Main class contains the main() method. 29 | public class Constructors { 30 | 31 | // main() method contains the driver code. 32 | public static void main(String[] args) { 33 | 34 | // Creating objects for Animal. 35 | Animal myAnimal = new Animal(); 36 | Animal myAnimal2 = new Animal("Orange"); 37 | 38 | // Printing the values of color in objects. 39 | System.out.println("myAnimal: color = " + myAnimal.color); 40 | System.out.println("myAnimal2: color = " + myAnimal2.color); 41 | } 42 | } 43 | 44 | /** 45 | * Output: 46 | * myAnimal: color = Red 47 | * myAnimal2: color = Orange 48 | */ 49 | -------------------------------------------------------------------------------- /Week-3/Static Concept in Inheritance/StaticInheritance.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the example of StaticInheritance in Java. 3 | * 4 | * @author NanthaKumar 5 | * @date 2021/08/14 6 | */ 7 | 8 | class Fruit { 9 | 10 | // Static method. 11 | public static void staticMethod() { 12 | System.out.println("I'm a static method, from Fruit class."); 13 | } 14 | 15 | // Instance method. 16 | public void instanceMethod() { 17 | System.out.println("I'm an instance method, from Fruit class."); 18 | } 19 | } 20 | 21 | class Apple extends Fruit { 22 | 23 | // Static method. 24 | public static void staticMethod() { 25 | System.out.println("I'm a static method, from Apple class."); 26 | } 27 | 28 | // Instance method. 29 | public void instanceMethod() { 30 | System.out.println("I'm an instance method, from Apple class."); 31 | } 32 | } 33 | 34 | // Main class contains main method. 35 | public class StaticInheritance { 36 | 37 | // main() contains driver code. 38 | public static void main(String[] args) { 39 | 40 | Apple myApple = new Apple(); 41 | Fruit myFruit = myApple; 42 | Fruit.staticMethod(); 43 | myFruit.instanceMethod(); 44 | } 45 | } 46 | 47 | /** 48 | * Output: 49 | * I'm a static method, from Fruit class. 50 | * I'm an instance method, from Apple class. 51 | */ 52 | -------------------------------------------------------------------------------- /Week-1/Switch, while and do-while statements/README.md: -------------------------------------------------------------------------------- 1 | ### Switch statement 2 | 3 | Switch statements are used when you have to take multiple cases into consideration. It is like if-else-if ladder statement. 4 | 5 | # Syntax 6 | switch(expression) { 7 | case x: 8 | // code block 9 | break; 10 | case y: 11 | // code block 12 | break; 13 | default: 14 | // code block 15 | } 16 | 17 | *Note: * We need to use a break statement in each case so that the control comes out of the switch statements. 18 | 19 | ## Example 20 | ```java 21 | /**********************************Switch-Section**********************************/ 22 | class SwitchExample { 23 | 24 | // Static method contains example code. 25 | public static void main(String args[]) { 26 | 27 | 28 | Scanner sc= new Scanner(System.in); 29 | 30 | System.out.println("1.To say hello\t 2.To say bye\t 3.Exit: "); 31 | int opt =sc.nextInt(); 32 | switch(opt) // switch case starts 33 | { 34 | case 1: // case 1 runs if opt == 1 is selected 35 | System.out.println("Hello"); 36 | break; 37 | 38 | case 2: res = a-b; // case 2 runs if opt == 2 is selected 39 | System.out.println("Bye"); 40 | break; 41 | 42 | default: break; // else this will run 43 | 44 | } // switch case ends 45 | } 46 | } 47 | 48 | ``` 49 | 50 | 51 | -------------------------------------------------------------------------------- /Week-2/Objects & Methods/README.md: -------------------------------------------------------------------------------- 1 | ## Objects 2 | 3 | An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class. 4 | 5 | Object Definitions: 6 | 7 | - An object is a real-world entity. 8 | - An object is a runtime entity. 9 | - The object is an entity which has state and behavior. 10 | - The object is an instance of a class. 11 | 12 | ## Method in Java 13 | 14 | In Java, a method is like a function which is used to expose the behavior of an object. 15 | 16 | 17 | 18 | ## Example 19 | ```java 20 | /*******************************Object and Methods*******************************/ 21 | 22 | /* 23 | In this example, we are creating the two objects of Student class and initializing the value to these objects by invoking the insertRecord method. Here, we are displaying the state (data) of the objects by invoking the displayInformation() method. 24 | */ 25 | 26 | class Student{ 27 | int rollno; 28 | String name; 29 | 30 | void insertRecord(int r, String n){ 31 | rollno=r; 32 | name=n; 33 | } 34 | 35 | void displayInformation(){System.out.println(rollno+" "+name); 36 | }} 37 | 38 | class TestStudent{ 39 | public static void main(String args[]){ 40 | 41 | Student s1=new Student(); 42 | Student s2=new Student(); 43 | 44 | s1.insertRecord(111,"Karan"); 45 | s2.insertRecord(222,"Aryan"); 46 | s1.displayInformation(); 47 | s2.displayInformation(); 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Week-3/Static Variables and Methods/StaticVariableMethod.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file demonstrate the static variables, methods and blocks. 3 | * 4 | * @author NanthaKumar 5 | * @date 2021/08/13 6 | */ 7 | 8 | // DemoClass contains static attributs, methods and block. 9 | class DemoClass { 10 | 11 | // Static attribute. 12 | static String greet = "Welcome"; 13 | static int a, b; 14 | 15 | // Static block. It'll be loaded when class loaded in JVM. 16 | static { 17 | System.out.println("Static Block Invoked..."); 18 | 19 | // Here the static attributes accessed by a static block. 20 | a = 10; 21 | b = 20; 22 | } 23 | 24 | // Static method. 25 | static void hello() { 26 | System.out.println("Hello, I'm from DemoClass..."); 27 | } 28 | 29 | } 30 | 31 | // Main class contains the main method(). 32 | public class StaticVariableMethod { 33 | 34 | // main() method contains the driver code. 35 | public static void main(String[] args) { 36 | 37 | // Directly accessing DemoClass attributes and printing 38 | // them. 39 | System.out.println("Greet: " + DemoClass.greet); 40 | System.out.println("a: " + DemoClass.a); 41 | System.out.println("b: " + DemoClass.b); 42 | DemoClass.hello(); 43 | } 44 | } 45 | 46 | /** 47 | * Output: 48 | * Static Block Invoked... 49 | * Greet: Welcome 50 | * a: 10 51 | * b: 20 52 | * Hello, I'm from DemoClass... 53 | */ 54 | -------------------------------------------------------------------------------- /Week-3/Memory Allocation in Java/MemoryAllocation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file demonstrate memory allocation in java. It contains the example of 3 | * Stack Memory and Heap Space. 4 | * 5 | * @author NanthaKumar 6 | * @date 2021/08/13 7 | */ 8 | 9 | // Class Fruit contains attributes and a method. 10 | class Fruit { 11 | 12 | // While creating object for this class it acquire its 13 | // memory for attributes and methods in its own Stack memory. 14 | 15 | // Creating attributes. 16 | String color = "Red"; 17 | int weight = 2; 18 | 19 | // method toString() gives color and weight. 20 | public String toString() { 21 | return "Color: " + this.color + ", Weight: " + 2; 22 | } 23 | } 24 | 25 | // Main class MemoryAllocation. 26 | public class MemoryAllocation { 27 | 28 | // main() method contains the driver code. 29 | public static void main(String[] args) { 30 | 31 | // Here we're creating object for Fruit class. 32 | // The object created and stored in Heap memory while 33 | // their attributes, methods are stored in Stack memory. 34 | 35 | // Creating object for fruit. 36 | System.out.println("Creating object for Fruit..."); 37 | Fruit apple = new Fruit(); 38 | 39 | // Printing apple object. 40 | System.out.println("Object Apple: " + apple.toString()); 41 | } 42 | } 43 | 44 | /** 45 | * Output: 46 | * Creating object for Fruit... 47 | * Object Apple: Color: Red, Weight: 2 48 | */ 49 | -------------------------------------------------------------------------------- /Week-3/Custom Exception/CustomException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the example for Custom Exception. 3 | * 4 | * @author NanthaKumar 5 | * @date 2021/08/18 6 | */ 7 | 8 | // Custom Exception class. 9 | class InvalidAgeException extends Exception { 10 | 11 | // Constructor invokes parent constructor. 12 | public InvalidAgeException(String errorMessage) { 13 | super(errorMessage); 14 | } 15 | } 16 | 17 | // Main class contains the main method. 18 | public class CustomException { 19 | 20 | // Method to check the age is adult. 21 | static void validateAge(int age) throws InvalidAgeException { 22 | if (age < 18) { 23 | throw new InvalidAgeException("Age: " + age + " is not belongs to adult"); 24 | } else { 25 | System.out.println("Age: " + age + " is valid."); 26 | } 27 | } 28 | 29 | // main() method contains driver code. 30 | public static void main(String[] args) { 31 | 32 | // Invoking custom exception by passing non adult age. 33 | try { 34 | validateAge(11); 35 | } 36 | catch (InvalidAgeException e) { 37 | System.out.println("Catched Exception: " + e); 38 | } 39 | 40 | // Validating proper adult age. 41 | try { 42 | validateAge(20); 43 | } 44 | catch (InvalidAgeException e) { 45 | System.out.println("Catched Exception: " + e); 46 | } 47 | } 48 | } 49 | 50 | /** 51 | * Output: 52 | * Catched Exception: InvalidAgeException: Age: 11 is not belongs to adult 53 | * Age: 20 is valid. 54 | */ 55 | -------------------------------------------------------------------------------- /Week-2/Defining Class, Instance Variables and Methods/ClassVariableMethod.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the example of Class, Instance Variables and Methods. 3 | * It contains a example class Student and main class ClassVariableMethod. 4 | * 5 | * @author nknantha 6 | * @date 2021/08/09 7 | */ 8 | 9 | // Default class Student contains example attributes and methods. 10 | class Student { 11 | 12 | // Defining private student name instance variable. 13 | private String name; 14 | 15 | // A method to get student name. 16 | public String getName() { 17 | return this.name; 18 | } 19 | 20 | // A method to set student name. Takes a parameter name to get assigned. 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | } 25 | 26 | // Public class ClassVariableMethod for example demonstration. 27 | public class ClassVariableMethod { 28 | 29 | // Public main() contains example code. 30 | public static void main(String[] args) { 31 | 32 | // Creating object for student class. 33 | Student Obj = new Student(); 34 | 35 | // Checking initial name of Obj. 36 | System.out.println("Initially, name is " + Obj.getName()); 37 | 38 | // Changing name to Steve. 39 | // Obj.name = "Steve"; // this creates error because of private modifier. 40 | Obj.setName("Steve"); 41 | 42 | // After changing name accessing name of Obj. 43 | System.out.println("After name change, name is " + Obj.getName()); 44 | } 45 | } 46 | 47 | /** 48 | * Output: 49 | * Initially, name is null 50 | * After name change, name is Steve 51 | * 52 | */ 53 | -------------------------------------------------------------------------------- /Week-3/Defining and Throwing Exceptions/ThrowingException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the demonstration of throwing exception. 3 | * 4 | * @author NanthaKumar 5 | * @date 2021/08/17 6 | */ 7 | 8 | // Main class contains the main method. 9 | public class ThrowingException { 10 | 11 | // Divide method divides the given numbers. 12 | // It may throws ArithmeticException and base Exception. 13 | public static int divide(int a, int b) throws ArithmeticException, Exception { 14 | 15 | // Throwing manual exception if a < b. 16 | if (a < b) { 17 | throw new Exception("Value a: " + a + ", should be greater than value b: " + b); 18 | } 19 | return a / b; 20 | } 21 | 22 | // Execute and manages the exception with try catch. 23 | public static void execDivide(int a, int b) { 24 | try { 25 | System.out.println("Result: " + divide(a, b)); 26 | } 27 | catch (ArithmeticException e) { 28 | System.out.println("Catched Exception: " + e); 29 | } 30 | catch (Exception e) { 31 | System.out.println("Catched Exception: " + e); 32 | } 33 | } 34 | 35 | // main() method contains the driver code. 36 | public static void main(String[] args) { 37 | 38 | // First we try to catch ArithmeticException. 39 | execDivide(5, 0); 40 | 41 | // Second we try to catch manual exception. 42 | execDivide(5, 10); 43 | } 44 | } 45 | 46 | /** 47 | * Output: 48 | * Catched Exception: java.lang.ArithmeticException: / by zero 49 | * Catched Exception: java.lang.Exception: Value a: 5, should be greater than value b: 10 50 | */ 51 | -------------------------------------------------------------------------------- /Week-3/Errors and Runtime Exceptions/ExceptionError.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the example of errors and exceptions. 3 | * 4 | * @author NanthaKumar 5 | * @date 2021/08/18 6 | */ 7 | 8 | // Class for example of ArithmeticException. 9 | class ExceptionExample { 10 | 11 | // Method containing code to make ArithmeticException. 12 | public static void exec() { 13 | int a = 9, b = 0; 14 | 15 | try { 16 | System.out.println("Result: " + a / b); 17 | } 18 | catch (ArithmeticException e) { 19 | System.out.println("Catched Exception: " + e); 20 | } 21 | } 22 | } 23 | 24 | // Class for Error example. 25 | class ErrorExample { 26 | 27 | // Method to make StackOverFlowError. 28 | public static void exec(int i) { 29 | 30 | // Recursive call with incremented value. 31 | exec(++i); 32 | } 33 | } 34 | 35 | // Main class contains the main method. 36 | public class ExceptionError { 37 | 38 | // main() method contains driver code. 39 | public static void main(String[] args) { 40 | 41 | // Calling methods to execute examples. 42 | ExceptionExample.exec(); 43 | ErrorExample.exec(1); 44 | } 45 | } 46 | 47 | /** 48 | * Output: 49 | * Catched Exception: java.lang.ArithmeticException: / by zero 50 | * Exception in thread "ExceptionError" java.lang.StackOverflowError 51 | * at ErrorExample.exec(ExceptionError.java:39) 52 | * at ErrorExample.exec(ExceptionError.java:39) 53 | * at ErrorExample.exec(ExceptionError.java:39) 54 | * at ErrorExample.exec(ExceptionError.java:39) 55 | * at ErrorExample.exec(ExceptionError.java:39) 56 | * ... 57 | */ 58 | -------------------------------------------------------------------------------- /Week-2/Compile-time Polymorphism/CompiletimePolymorphism.java: -------------------------------------------------------------------------------- 1 | /** 2 | * CompiletimePolymorphism 3 | * 4 | * This file contains the demonstration of compile-time polymorphism. 5 | * It contains a main class that contains methods sum() with 3 6 | * signatures and a main method for begin execution. 7 | * 8 | * @author NanthaKumar 9 | * @date 2021/08/08 10 | */ 11 | 12 | // Public main class CompiletimePolymorphism. 13 | public class CompiletimePolymorphism { 14 | 15 | // Signature - 1: 16 | // sum() method with 2 integer parameter. 17 | static int sum(int a, int b) { 18 | return a + b; 19 | } 20 | 21 | // Signature - 2: 22 | // sum() method with 2 float parameter. 23 | static float sum(float a, float b) { 24 | return a + b; 25 | } 26 | 27 | // Signature - 3: 28 | // sum() method with 3 integer parameter. 29 | static int sum(int a, int b, int c) { 30 | return a + b + c; 31 | } 32 | 33 | // main() method contains driver code and demonstration. 34 | public static void main(String[] args) { 35 | 36 | // Calling sum() method with 2 integer values and 37 | // printing the result. 38 | System.out.println(sum(3, 10)); 39 | 40 | // Calling sum() method with 2 float values and 41 | // printing the result. 42 | System.out.println(sum(3.0f, 10.0f)); 43 | 44 | // Calling sum() method with 3 integer values and 45 | // printing the result. 46 | System.out.println(sum(3, 10, 2)); 47 | 48 | // Above different method calls of sum() are resolved at compiletime 49 | // rather than runtime. 50 | } 51 | } 52 | 53 | /** 54 | * OUTPUT: 55 | * 13 56 | * 13.0 57 | * 15 58 | */ 59 | -------------------------------------------------------------------------------- /Week-2/Arithmetic Operators/Arithmetic_Operators.md: -------------------------------------------------------------------------------- 1 | # Arithmetic Operators 2 | 3 | > Symbol for performing operations in Java is called a "operator." As an example, +, -, *, /, etc. can be used. As well as addition, subtraction, multiplication, and division, the Java arithmetic operators can be used. Essentially, they are mathematical operations of the most basic kind. 4 | 5 | - ### **Example 1** 6 | 7 | ```java 8 | //Source Code 9 | 10 | public class DevIncept{ 11 | public static void main(String args[]) 12 | { 13 | int a = 10; 14 | int b = 5; 15 | System.out.println(a+b); //addition operator, returns the sum 16 | System.out.println(a-b); //multiplication operator, returns the difference 17 | System.out.println(a*b); //multiplication operator, returns the product 18 | System.out.println(a/b); //division operator, returns the quotient 19 | System.out.println(a%b); //modulous operator, returns the remainder of an integer division 20 | } 21 | } 22 | ``` 23 | 24 | ```java 25 | //Output 26 | 27 | 15 28 | 5 29 | 50 30 | 2 31 | 0 32 | ``` 33 | > Here you can see ```/``` and ```%``` are two sides of the same coin. 10 ÷ 5 gives 2 as the quotient (given by ```/```) and 0 as the remainder (given by ```%```). Also, the real-life mathematical symbol '✖' or cross is not used in computing language due to its similarity with the English alphabet 'X' or 'x'. Instead, ```*``` is used to denote multiplication. 34 | 35 | - ### **Example 2** 36 | Here we are using an arithmatic expression using all the operators listed above. 37 | 38 | ```java 39 | //Source Code 40 | 41 | public class DevIncept 42 | { 43 | public static void main(String args[]) 44 | { 45 | System.out.println(10*10/5+3-1*4/2); 46 | } 47 | } 48 | ``` 49 | 50 | ```java 51 | //Output 52 | 53 | 21 54 | ``` 55 | -------------------------------------------------------------------------------- /Week-1/Upcasting and Downcasting/UpcastingAndDowncasting.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains example for Upcasting and Downcasting. 3 | * It contains a Parent class, Child class and a main class. 4 | * It demonstrate Upcasting and Downcasting. 5 | * 6 | * @author nknantha 7 | * @date 2021/07/30 8 | */ 9 | 10 | // Parent class contains a method tell() 11 | class Parent { 12 | 13 | // Attribute to explain upcasted access from child. 14 | String name = "Parent"; 15 | 16 | void tell() { 17 | // Prints a msg from parent class. 18 | System.out.println("Parent Method"); 19 | } 20 | } 21 | 22 | // Child class that extends parent class 23 | // and overrides parent class's tell() method. 24 | class Child extends Parent { 25 | 26 | // Overrides the parent's tell() method and 27 | // prints msg from child class. 28 | @Override 29 | void tell() { 30 | System.out.println("Child Method"); 31 | } 32 | } 33 | 34 | // Public class UpcastingAndDowncasting which is a main class. 35 | public class UpcastingAndDowncasting { 36 | 37 | // main() method to run program. 38 | public static void main(String[] args) { 39 | // Creating object for child class and Upcasting it 40 | // to parent. 41 | Parent Obj = new Child(); 42 | 43 | // Accessing and printing name attribute in parent class. 44 | System.out.println(Obj.name); 45 | 46 | // Calling tell() method which is overridden by child class. 47 | Obj.tell(); 48 | 49 | // Downcasting Obj to child class. 50 | Child Obj2 = (Child) Obj; 51 | 52 | // After downcasting calling tell() method. 53 | Obj2.tell(); 54 | } 55 | } 56 | 57 | /** 58 | * Output: 59 | * Parent 60 | * Child Method 61 | * Child Method 62 | * 63 | */ 64 | -------------------------------------------------------------------------------- /Week-2/Strict Typing/Strict_Typing.md: -------------------------------------------------------------------------------- 1 | # Strict Typing 2 | 3 | ### **Strictly typed languages require that all data that is interacted with be typed.** 4 | 5 | ```java 6 | int i = 3 7 | string s = "4" 8 | ``` 9 | 10 | > From now on, whenever you use I you will only be able to interact with it as an integer type. That means you can only use it with methods that work with integers. Strings can only be interacted with as string types. It can be concatenated with other strings, printed, and so on. Despite the fact that it contains the character "4", you cannot add to an integer without first converting the string to an integer type. 11 | 12 | ### F**lexibility in a dynamically typed language** 13 | 14 | ```java 15 | i = 3 16 | s = "4" 17 | ``` 18 | 19 | > Types are inferred, which means they are determined based on the data they are set to. Based on how they were set, I is a number type and s is a string type. However, when you have I + s; type inference is used, and depending on your environment, you may get the result I + s = 7; because the programming environment implicitly converted s to an int. However, if the environment believes that an int + string should be interpreted as a concatenation operation rather than an addition operation, this operation could produce the string "34." 20 | 21 | ### **Present Scenario** 22 | 23 | > Loosely typed languages have become very popular because of their flexibility. This can lead to more bugs if you aren't careful because these type inferences can produce unexpected results. When performing I + s in a typed language, the compiler forces me to convert s to an explicit int first, so I know that by adding I to s, I will get 7 because s had to be converted first. Any string, number, or even an object can be in I or s in a dynamic language, but the results may not be what you expected. Until you run your code and see what happens, you won't know. 24 | -------------------------------------------------------------------------------- /Week-3/Queue Implementation Classes/QueueExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the example for Queue. 3 | * It uses LinkedList and Queue classes. 4 | * 5 | * @author NanthaKumar 6 | * @date 2021/08/18 7 | */ 8 | 9 | // Importing Queue and LinkedList. 10 | import java.util.Queue; 11 | import java.util.LinkedList; 12 | 13 | // Main class contains the main method. 14 | public class QueueExample { 15 | 16 | // main() method contains the example. 17 | public static void main(String[] args) { 18 | 19 | // Creating Queue with List. 20 | Queue q = new LinkedList<>(); 21 | 22 | // Adding elements in queue. 23 | for (int i = 1; i < 8; ++i) { 24 | q.add(i); 25 | } 26 | 27 | // Printing queue. 28 | System.out.println("Queue: " + q.toString()); 29 | 30 | // Printing Peek element. 31 | System.out.println("Peek Element: " + q.peek()); 32 | 33 | // Printing the size and empty. 34 | System.out.println("Size: " + q.size() + ", Empty: " + q.isEmpty()); 35 | 36 | // Removing elements and printing it. 37 | System.out.println("Removing Elements: "); 38 | while (q.size() > 0) { 39 | System.out.println("Removed: " + q.poll()); 40 | } 41 | 42 | // Printing queue. 43 | System.out.println("Queue: " + q.toString()); 44 | 45 | // Printing the size and empty. 46 | System.out.println("Size: " + q.size() + ", Empty: " + q.isEmpty()); 47 | } 48 | } 49 | 50 | /** 51 | * Output: 52 | * Queue: [1, 2, 3, 4, 5, 6, 7] 53 | * Peek Element: 1 54 | * Size: 7, Empty: false 55 | * Removing Elements: 56 | * Removed: 1 57 | * Removed: 2 58 | * Removed: 3 59 | * Removed: 4 60 | * Removed: 5 61 | * Removed: 6 62 | * Removed: 7 63 | * Queue: [] 64 | * Size: 0, Empty: true 65 | */ 66 | -------------------------------------------------------------------------------- /Week-2/Concrete Classes/ConcreteEx2.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains example of concrete class with abtract and interface 3 | * concepts. It contains a interface Calc contains declarations of add(), 4 | * multiply() and a abstract class Add contains implementation of add() and 5 | * a main class contains implementation of multiply() and main() methods. 6 | * 7 | * @author NanthaKumar 8 | * @date 2021/08/08 9 | */ 10 | 11 | // Example - 2 12 | 13 | // Interface Calc contains declarations of method add() and multiply(). 14 | interface Calc { 15 | int add(int a, int b); 16 | int multiply(int a, int b); 17 | } 18 | 19 | // Abstract class add contains the implementation of add() method and 20 | // it implements the Calc interface. 21 | abstract class Add implements Calc { 22 | 23 | // Implementation of add() method 24 | public int add(int a, int b) { 25 | return a + b; 26 | } 27 | } 28 | 29 | // Public main class (Concrete) contains main() method and implemantation of 30 | // multiply() method and it extends the abstract class Add. 31 | public class ConcreteEx2 extends Add { 32 | 33 | // Implemantation of multiply() method. 34 | public int multiply(int a, int b) { 35 | return a * b; 36 | } 37 | 38 | // main() method contains driver code. 39 | public static void main(String[] args) { 40 | 41 | // Creating object for main class. 42 | ConcreteEx2 Obj = new ConcreteEx2(); 43 | 44 | // Variables to hold integers. 45 | int num1 = 10, num2 = 5; 46 | 47 | // Calling add() method in Obj and printing the return value. 48 | System.out.println("Sum: " + Obj.add(num1, num2)); 49 | 50 | // Calling multiply() method in Obj and printing the return value. 51 | System.out.println("Product: " + Obj.multiply(num1, num2)); 52 | } 53 | } 54 | 55 | /** 56 | * OUTPUT: 57 | * Sum: 15 58 | * Product: 50 59 | */ 60 | -------------------------------------------------------------------------------- /Week-1/Array of Object References/ArrayOfObjects.java: -------------------------------------------------------------------------------- 1 | /** 2 | * It's a example program for Array of Objects. 3 | * 4 | * Contains: 5 | * - class Person: example class for demonstration. 6 | * - Public class ArrayOfObjects: main class for running the program. 7 | * 8 | * @author nknantha 9 | * @date 2021/07/29 10 | */ 11 | 12 | // Example class for demonstrating array of objects. 13 | // It contains attributes firstName and lastName. 14 | class Person { 15 | 16 | // Creating attributes. 17 | public String firstName, lastName; 18 | 19 | // Person class constructor 20 | Person(String firstName, String lastName) { 21 | this.firstName = firstName; 22 | this.lastName = lastName; 23 | } 24 | 25 | // Overriding toString() method for object representation. 26 | @Override 27 | public String toString() { 28 | // It represent firstName and lastName of this object. 29 | return "FirstName: " + this.firstName + ", LastName: " + this.lastName; 30 | } 31 | } 32 | 33 | // Public class that contains main method. 34 | public class ArrayOfObjects { 35 | 36 | // Main method for run this program. 37 | public static void main(String[] args) { 38 | // First, we're declaring array for objects. 39 | Person[] personArray; 40 | 41 | // Second, we instantiate it. 42 | personArray = new Person[3]; 43 | 44 | // Third, we creating object and storing it in array. 45 | personArray[0] = new Person("Nikola", "Tesla"); 46 | personArray[1] = new Person("Albert", "Einstein"); 47 | personArray[2] = new Person("Isaac", "Newton"); 48 | 49 | // Finally we're printing the stored element. 50 | for(Person person: personArray) { 51 | System.out.println(person); 52 | } 53 | } 54 | } 55 | 56 | /** 57 | * Output: 58 | * FirstName: Nikola, LastName: Tesla 59 | * FirstName: Albert, LastName: Einstein 60 | * FirstName: Isaac, LastName: Newton 61 | * 62 | */ 63 | -------------------------------------------------------------------------------- /Week-3/this and super Call for Constructors/thisSuperConstructor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the example of this and super call constructor. 3 | * 4 | * @author NanthaKumar 5 | * @date 2021/08/17 6 | */ 7 | 8 | // Base class Fruit. 9 | class Fruit { 10 | 11 | String color; 12 | 13 | Fruit() { 14 | // this keyword used to access the attribute in current class. 15 | this.color = "Red"; 16 | System.out.println("Fruit default constructor invoked..."); 17 | } 18 | 19 | Fruit(String color) { 20 | // this keyword used to access the attribute in current class. 21 | this.color = color; 22 | System.out.println("Fruit parameterized constructor invoked..."); 23 | } 24 | } 25 | 26 | // Derived class Apple from Fruit. 27 | class Apple extends Fruit { 28 | 29 | Apple() { 30 | // super keyword used to invoke the parent class constructor. 31 | super(); 32 | System.out.println("Apple default constructor invoked..."); 33 | } 34 | 35 | Apple(String color) { 36 | // super keyword used to invoke the parent class constructor. 37 | super(color); 38 | System.out.println("Apple parameterized constructor invoked..."); 39 | } 40 | } 41 | 42 | // Main class contains the main method. 43 | public class thisSuperConstructor { 44 | 45 | // main() method contains the driver code. 46 | public static void main(String[] args) { 47 | 48 | // Creating object to invoke constructors. 49 | Apple myApple = new Apple(); 50 | Apple myApple2 = new Apple("Green"); 51 | System.out.println("myApple Color: " + myApple.color); 52 | System.out.println("myApple2 Color: " + myApple2.color); 53 | } 54 | } 55 | 56 | /** 57 | * Output: 58 | * Fruit default constructor invoked... 59 | * Apple default constructor invoked... 60 | * Fruit parameterized constructor invoked... 61 | * Apple parameterized constructor invoked... 62 | * myApple Color: Red 63 | * myApple2 Color: Green 64 | */ 65 | -------------------------------------------------------------------------------- /Week-3/Default and Parameterized Constructor/README.md: -------------------------------------------------------------------------------- 1 | # Default and Parameterized Constructor 2 | 3 | 4 | ## Default Constructor 5 | A constructor having no parameter is known as default constructor. It is used to 6 | assign default value to the object. If there is no constructor in a class the 7 | compiler automatically creates a default constructor. 8 | 9 | ### Syntax 10 | ```java 11 | () { 12 | // body of the constructor. 13 | } 14 | ``` 15 | 16 | 17 | ## Parameterized Constructor 18 | A constructor having specific number of parameter is called parameterized constructor. 19 | It assigns the given value to the object. 20 | 21 | ### Syntax 22 | ```java 23 | (datatype parameter,...) { 24 | // body of the constructor. 25 | } 26 | ``` 27 | 28 | 29 | ## Example 30 | ```java 31 | // Class Animal contains default and parameterized constructor. 32 | class Animal { 33 | 34 | public String color; 35 | 36 | // Default constructor. 37 | Animal() { 38 | 39 | // Storing default value in instance variable. 40 | this.color = "Red"; 41 | } 42 | 43 | // Parameterized constructor. 44 | Animal(String color) { 45 | 46 | // Storing given value in instance variable. 47 | this.color = color; 48 | } 49 | } 50 | 51 | // Main class contains the main() method. 52 | public class Constructors { 53 | 54 | // main() method contains the driver code. 55 | public static void main(String[] args) { 56 | 57 | // Creating objects for Animal. 58 | Animal myAnimal = new Animal(); 59 | Animal myAnimal2 = new Animal("Orange"); 60 | 61 | // Printing the values of color in objects. 62 | System.out.println("myAnimal: color = " + myAnimal.color); 63 | System.out.println("myAnimal2: color = " + myAnimal2.color); 64 | } 65 | } 66 | ``` 67 | > *You can find the above code on [Constructors.java](Constructors.java).* 68 | 69 | 70 | ## Output 71 | ``` 72 | myAnimal: color = Red 73 | myAnimal2: color = Orange 74 | ``` 75 | 76 | 77 | ## Author 78 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 79 | -------------------------------------------------------------------------------- /Week-2/Differences Between JAR and WAR Packaging/JARvsWAR.md: -------------------------------------------------------------------------------- 1 | # **Differences Between JAR and WAR Packaging** 2 | 3 | > **JAR**, or Java Archive, is a file format for storing packages. It is possible for JAR files to contain libraries, resources and metadata files. What it is, in essence, is a zip file containing the compressed versions of Java class files and resources. JAR files can be created with the .jar command or with tools such as Maven. 4 | 5 | ```java 6 | META-INF/ 7 | MANIFEST.MF // There may be additional metadata about files stored in the archive. 8 | com/ 9 | devincept/ 10 | MyApplication.class 11 | ``` 12 | 13 | > **WAR** stands for Web Application Archive or Web Application Resource. These .war files are used to package web applications that we can deploy on any Servlet/JSP container. To create a .war archive, we can use the same tools and commands that we used to create a JAR. 14 | 15 | ```java 16 | META-INF/ 17 | MANIFEST.MF // Holds useful information in the about the web archive. 18 | WEB-INF/ 19 | /* Each of the static web resources is located in the WEB-INF public directory. 20 | This includes HTML pages, images and JS files. Web.xml and servlet classes and 21 | libraries are also included.*/ 22 | web.xml 23 | jsp/ 24 | devincept.jsp 25 | classes/ 26 | static/ 27 | templates/ 28 | application.properties 29 | lib/ 30 | ``` 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 |
JARWAR
It allow us to package multiple files into a library, plugin, or any other type of application that we wish to useA WAR file is only used for web-based software applications.
We are able to create a JAR with any structure that you desire.The WEB-INF and META-INF directories are predefined in WAR.
It is possible to run a JAR directly from the command line if it is built as an executable JARA WAR must be run on a server.
49 | -------------------------------------------------------------------------------- /Week-3/Assertions/README.md: -------------------------------------------------------------------------------- 1 | # Assertions 2 | 3 | 4 | ## Assertions 5 | Assertions is used to test assumption about the program in Java. `assert` keyword 6 | used to make assertion. While executing assertion, it is believed to be true. If 7 | it fails, JVM throws an error named `AssertionError`. It is mainly used for testing 8 | purposes during development. 9 | 10 | ### Syntax 11 | ```java 12 | assert expression; 13 | ``` 14 | OR 15 | ```java 16 | assert expression1: expression2; 17 | ``` 18 | 19 | ### Enabling Assertion 20 | By default assertion is disabled in JVM. Use the following command to enable it. 21 | ``` 22 | java -ea filename.java 23 | ``` 24 | OR 25 | ``` 26 | java --enableassertion filename.java 27 | ``` 28 | For disabling assertion use `-da` or `--disableassertion` instead of `-ea` or 29 | `--enableassertion`. 30 | 31 | ### Where to not use assertion 32 | - Assertion should not be used to check arguments in the public methods because it 33 | should result in appropriate runtime exception. 34 | - Do not use assertion, if you don't want any error in any situation. 35 | 36 | 37 | ## Example 38 | ```java 39 | // Main class contains main method. 40 | public class Assertions { 41 | 42 | // method to check minimum. 43 | public static void checkMinimum(int num) { 44 | assert num > 21: "Value under minimum"; 45 | System.out.println("Given Value: " + num); 46 | } 47 | 48 | // main() method contains example. 49 | public static void main(String[] args) { 50 | 51 | // Invoking assertion. 52 | checkMinimum(7); 53 | 54 | // Value > 21 55 | checkMinimum(25); 56 | } 57 | } 58 | ``` 59 | > *Note: You can find the above code on [Assertions.java](Assertions.java).* 60 | 61 | 62 | ## Output 63 | On assertion disabled, 64 | ``` 65 | Given Value: 7 66 | Given Value: 25 67 | ``` 68 | On assertion enabled, 69 | ``` 70 | Exception in thread "main" java.lang.AssertionError: Value under minimum 71 | at Assertions.checkMinimum(Assertions.java:13) 72 | at Assertions.main(Assertions.java:21) 73 | ``` 74 | 75 | 76 | **Reference:** 77 | https://www.geeksforgeeks.org/assertions-in-java/ 78 | https://www.javatpoint.com/assertion-in-java 79 | ## Author 80 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 81 | -------------------------------------------------------------------------------- /Week-2/Abstraction/Abstraction.java: -------------------------------------------------------------------------------- 1 | // Java program to illustrate the 2 | // concept of Abstraction 3 | abstract class Shape { 4 | String color; 5 | 6 | // these are abstract methods 7 | abstract double area(); 8 | public abstract String toString(); 9 | 10 | // abstract class can have constructor 11 | public Shape(String color) 12 | { 13 | System.out.println("Shape constructor called"); 14 | this.color = color; 15 | } 16 | 17 | // this is a concrete method 18 | public String getColor() { return color; } 19 | } 20 | class Circle extends Shape { 21 | double radius; 22 | 23 | public Circle(String color, double radius) 24 | { 25 | 26 | // calling Shape constructor 27 | super(color); 28 | System.out.println("Circle constructor called"); 29 | this.radius = radius; 30 | } 31 | 32 | @Override double area() 33 | { 34 | return Math.PI * Math.pow(radius, 2); 35 | } 36 | 37 | @Override public String toString() 38 | { 39 | return "Circle color is " + super.getColor() 40 | + "and area is : " + area(); 41 | } 42 | } 43 | class Rectangle extends Shape { 44 | 45 | double length; 46 | double width; 47 | 48 | public Rectangle(String color, double length, 49 | double width) 50 | { 51 | // calling Shape constructor 52 | super(color); 53 | System.out.println("Rectangle constructor called"); 54 | this.length = length; 55 | this.width = width; 56 | } 57 | 58 | @Override double area() { return length * width; } 59 | 60 | @Override public String toString() 61 | { 62 | return "Rectangle color is " + super.getColor() 63 | + "and area is : " + area(); 64 | } 65 | } 66 | public class Test { 67 | public static void main(String[] args) 68 | { 69 | Shape s1 = new Circle("Red", 2.2); 70 | Shape s2 = new Rectangle("Yellow", 2, 4); 71 | 72 | System.out.println(s1.toString()); 73 | System.out.println(s2.toString()); 74 | } 75 | } 76 | // Output: 77 | // Shape constructor called 78 | // Circle constructor called 79 | // Shape constructor called 80 | // Rectangle constructor called 81 | // Circle color is Redand area is : 15.205308443374602 82 | // Rectangle color is Yellowand area is : 8.0 83 | -------------------------------------------------------------------------------- /Week-3/Static Concept in Inheritance/README.md: -------------------------------------------------------------------------------- 1 | # Static Concept in Inheritance 2 | 3 | 4 | ## Static Methods 5 | If a static method defined in parent class, that is overridden by a static method 6 | in its subclass makes the parent static method to be hidden. 7 | It has 2 implications, 8 | - The instance method invokes the one in subclass not the superclass. 9 | - The hidden method gets invoked depend on where it's invoked from either superclass 10 | or subclass. 11 | 12 | 13 | ## Example 14 | Consider an example that contains 3 classes. 15 | 16 | Class Fruit: 17 | ```java 18 | class Fruit { 19 | 20 | // Static method. 21 | public static void staticMethod() { 22 | System.out.println("I'm a static method, from Fruit class."); 23 | } 24 | 25 | // Instance method. 26 | public void instanceMethod() { 27 | System.out.println("I'm an instance method, from Fruit class."); 28 | } 29 | } 30 | ``` 31 | 32 | Class Apple: 33 | ```java 34 | class Apple extends Fruit { 35 | 36 | // Static method. 37 | public static void staticMethod() { 38 | System.out.println("I'm a static method, from Apple class."); 39 | } 40 | 41 | // Instance method. 42 | public void instanceMethod() { 43 | System.out.println("I'm an instance method, from Apple class."); 44 | } 45 | } 46 | ``` 47 | The Apple class overrides the instance method of Fruit class and hides the static 48 | method of Fruit class. 49 | 50 | Main Class: 51 | ```java 52 | // Main class contains main method. 53 | public class StaticInheritance { 54 | 55 | // main() contains driver code. 56 | public static void main(String[] args) { 57 | 58 | Apple myApple = new Apple(); 59 | Fruit myFruit = myApple; 60 | Fruit.staticMethod(); 61 | myFruit.instanceMethod(); 62 | } 63 | } 64 | ``` 65 | Creating object for Apple class then upcasting it to parent class Fruit. Then it 66 | invokes the Fruit class method `staticMethod()` and instance method `instanceMethod()`. 67 | 68 | > *You can find the above code on [StaticInheritance.java](StaticInheritance.java).* 69 | 70 | 71 | ## Output 72 | ``` 73 | I'm a static method, from Fruit class. 74 | I'm an instance method, from Apple class. 75 | ``` 76 | 77 | **REFERENCE:** https://docs.oracle.com/javase/tutorial/java/IandI/override.html 78 | 79 | ## Author 80 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 81 | -------------------------------------------------------------------------------- /Week-3/Custom Exception/README.md: -------------------------------------------------------------------------------- 1 | # Custom Exception 2 | 3 | 4 | ## Custom Exception 5 | We can define our own exception called custom exception as per need. It can be defined by 6 | extending the `Exception` base class. It is used to give custom exceptional condition in a 7 | desired project. It is also called user-defined exception. Using custom exception, we can 8 | have our own exception and message. 9 | 10 | ### Syntax 11 | ```java 12 | access_modifier class CustomExceptionName extends Exception { 13 | 14 | // Constructor for custom exception that must invoke parent exception. 15 | public CustomExceptionName(String errorMessage) { 16 | super(errorMessage); 17 | } 18 | } 19 | ``` 20 | 21 | 22 | ## Example 23 | ```java 24 | // Custom Exception class. 25 | class InvalidAgeException extends Exception { 26 | 27 | // Constructor invokes parent constructor. 28 | public InvalidAgeException(String errorMessage) { 29 | super(errorMessage); 30 | } 31 | } 32 | 33 | // Main class contains the main method. 34 | public class CustomException { 35 | 36 | // Method to check the age is adult. 37 | static void validateAge(int age) throws InvalidAgeException { 38 | if (age < 18) { 39 | throw new InvalidAgeException("Age: " + age + " is not belongs to adult"); 40 | } else { 41 | System.out.println("Age: " + age + " is valid."); 42 | } 43 | } 44 | 45 | // main() method contains driver code. 46 | public static void main(String[] args) { 47 | 48 | // Invoking custom exception by passing non adult age. 49 | try { 50 | validateAge(11); 51 | } 52 | catch (InvalidAgeException e) { 53 | System.out.println("Catched Exception: " + e); 54 | } 55 | 56 | // Validating proper adult age. 57 | try { 58 | validateAge(20); 59 | } 60 | catch (InvalidAgeException e) { 61 | System.out.println("Catched Exception: " + e); 62 | } 63 | } 64 | } 65 | ``` 66 | > *Note: You can find the above code on [CustomException.java](CustomException.java).* 67 | 68 | 69 | ## Output 70 | ``` 71 | Catched Exception: InvalidAgeException: Age: 11 is not belongs to adult 72 | Age: 20 is valid. 73 | ``` 74 | 75 | 76 | **Reference:** https://www.javatpoint.com/custom-exception 77 | ## Author 78 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 79 | -------------------------------------------------------------------------------- /Week-2/Getters and Setters/Getters and Setters.md: -------------------------------------------------------------------------------- 1 | # Getters and setters are used to protect your data, particularly when creating classes. 2 | 3 | ### For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively. 4 | ### By convention, getters start with the word "get" and setters with the word "set", followed by a variable name. In both cases the first letter of the variable's name is capitalized: 5 | 6 | ```python 7 | public class Vehicle { 8 | private String color; 9 | ``` 10 | > Getter 11 | ```python 12 | public String getColor() { 13 | return color; 14 | } 15 | ``` 16 | > Setter 17 | ```python 18 | public void setColor(String c) { 19 | this.color = c; 20 | } 21 | } 22 | ``` 23 | ### The getter method returns the value of the attribute. The setter method takes a parameter and assigns it to the attribute. 24 | ### Once the getter and setter have been defined, we use it in our main: 25 | 26 | ```python 27 | public static void main(String[] args) { 28 | Vehicle v1 = new Vehicle(); 29 | v1.setColor("Red"); 30 | System.out.println(v1.getColor()); 31 | } 32 | ``` 33 | > Outputs "Red" 34 | ### Getters and setters allow control over the values. We may validate the given value in the setter before actually setting the value. 35 | 36 | ### Why use getters and setters? 37 | ### Getters and setters allow us to control how important variables are accessed and updated in our code. For example, consider this setter method: 38 | 39 | ```python 40 | public void setNumber(int number) { 41 | if (number < 1 || number > 10) { 42 | throw new IllegalArgumentException(); 43 | } 44 | this.number = num; 45 | } 46 | ``` 47 | ### By using the setNumber method, we can be sure the value of number is always between 1 and 10. This is much better than updating the number variable directly: 48 | 49 | `obj.number = 13;` 50 | 51 | ### If we update number directly, it's possible that we'll cause unintended side effects somewhere else in our code. Here, setting number to 13 violates the 1 to 10 constraint we want to establish. 52 | 53 | ### Making number a private variable and using the setNumber method would prevent this from happening. 54 | 55 | ### On the other hand, the only way to read the value of number is by using a getter method: 56 | 57 | ```python 58 | public int getNumber() { 59 | return this.number; 60 | } 61 | ``` 62 | 63 | 64 | ```python 65 | 66 | ``` 67 | -------------------------------------------------------------------------------- /Week-3/this and super Call for Constructors/README.md: -------------------------------------------------------------------------------- 1 | # this and super Call for Constructors 2 | 3 | 4 | ## `this` Keyword 5 | In Java, `this` keyword refers the current object or instance of a class. It is used 6 | to access current object's constructor, methods and attributes implicitly. 7 | 8 | ## `super` Keyword 9 | In Java, `super` keyword refers the current object's parent class. It is used to 10 | access current object's parent class's constructor, methods and attributes implicitly. 11 | 12 | 13 | ## Example 14 | ```java 15 | // Base class Fruit. 16 | class Fruit { 17 | 18 | String color; 19 | 20 | Fruit() { 21 | // this keyword used to access the attribute in current class. 22 | this.color = "Red"; 23 | System.out.println("Fruit default constructor invoked..."); 24 | } 25 | 26 | Fruit(String color) { 27 | // this keyword used to access the attribute in current class. 28 | this.color = color; 29 | System.out.println("Fruit parameterized constructor invoked..."); 30 | } 31 | } 32 | 33 | // Derived class Apple from Fruit. 34 | class Apple extends Fruit { 35 | 36 | Apple() { 37 | // super keyword used to invoke the parent class constructor. 38 | super(); 39 | System.out.println("Apple default constructor invoked..."); 40 | } 41 | 42 | Apple(String color) { 43 | // super keyword used to invoke the parent class constructor. 44 | super(color); 45 | System.out.println("Apple parameterized constructor invoked..."); 46 | } 47 | } 48 | 49 | // Main class contains the main method. 50 | public class thisSuperConstructor { 51 | 52 | // main() method contains the driver code. 53 | public static void main(String[] args) { 54 | 55 | // Creating object to invoke constructors. 56 | Apple myApple = new Apple(); 57 | Apple myApple2 = new Apple("Green"); 58 | System.out.println("myApple Color: " + myApple.color); 59 | System.out.println("myApple2 Color: " + myApple2.color); 60 | } 61 | } 62 | ``` 63 | > *You can find the above code on [thisSuperConstructor.java](thisSuperConstructor.java).* 64 | 65 | 66 | ## Output 67 | ``` 68 | Fruit default constructor invoked... 69 | Apple default constructor invoked... 70 | Fruit parameterized constructor invoked... 71 | Apple parameterized constructor invoked... 72 | myApple Color: Red 73 | myApple2 Color: Green 74 | ``` 75 | 76 | ## Author 77 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 78 | -------------------------------------------------------------------------------- /Week-2/Super Class and Sub Class/SuperclassSubclass.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file contains the example code for Superclass and Subclass demonstration. 3 | * It contains a parent class Animal and child classes Dog and Cat. It contains 4 | * a main class contains driver code. 5 | * 6 | * @author NanthaKumar 7 | * @date 2021/08/07 8 | */ 9 | 10 | // Base class Animal contains parent functionality. 11 | class Animal { 12 | 13 | // Stores animal name. 14 | public String name = "Base Animal"; 15 | 16 | // Different animals make different sounds so we declaring base class method 17 | // makeSound() to easily override from child classes. 18 | public void makeSound() { 19 | System.out.println("Base Animal Sound..."); 20 | } 21 | } 22 | 23 | // Dog class (subclass) extends the Animal class. 24 | class Dog extends Animal { 25 | 26 | // Constructor that changes the parent name attribute by using 27 | // "super" keyword. 28 | Dog() { 29 | super.name = "Dog"; 30 | } 31 | 32 | // Dogs makes sound "Bow Bow". For that we're overriding parent class 33 | // makeSound() method. 34 | public void makeSound() { 35 | System.out.println("Bow Bow..."); 36 | } 37 | } 38 | 39 | // Cat class (subclass) extends the Animal class. 40 | class Cat extends Animal { 41 | 42 | // Constructor that changes the parent name attribute by using 43 | // "super" keyword. 44 | Cat() { 45 | super.name = "Cat"; 46 | } 47 | 48 | // Overriding parent makeSound() with "Meow Meow". 49 | public void makeSound() { 50 | System.out.println("Meow Meow..."); 51 | } 52 | } 53 | 54 | // public class contains main method. 55 | public class SuperclassSubclass { 56 | 57 | // main() method contains driver code. 58 | public static void main(String[] args) { 59 | 60 | // Objects creation for animals. 61 | Animal baseAnimal = new Animal(); 62 | Dog doggy = new Dog(); 63 | Cat kitty = new Cat(); 64 | 65 | // Calling makeSound() of all Animals. 66 | System.out.println("Making Sounds..."); 67 | 68 | System.out.print("From " + baseAnimal.name + ", "); 69 | baseAnimal.makeSound(); 70 | 71 | System.out.print("From " + doggy.name + ", "); 72 | doggy.makeSound(); 73 | 74 | System.out.print("From " + kitty.name + ", "); 75 | kitty.makeSound(); 76 | } 77 | } 78 | 79 | /** 80 | * OUTPUT: 81 | * Making Sounds... 82 | * From Base Animal, Base Animal Sound... 83 | * From Dog, Bow Bow... 84 | * From Cat, Meow Meow... 85 | */ 86 | -------------------------------------------------------------------------------- /Week-2/Reference variables, local variables, instance variable/Reference variables, local variables, instance variable.MD: -------------------------------------------------------------------------------- 1 | # Types of variables : 2 | There are three types of variables in Java: 3 | 4 | 5 | * Local Variables 6 | * Instance Variables 7 | * Reference variables 8 | 9 | ### Local Variables: 10 | A variable defined within a block or method or constructor is called local variable. 11 | These variable are created when the block is entered or the function is called and destroyed after exiting from the block or when the call returns from the function. 12 | The scope of these variables exists only within the block in which the variable is declared. i.e. we can access these variable only within that block. 13 | Initilisation of Local Variable is Mandatory. 14 | 15 | ### Instance Variables: 16 | Instance variables are non-static variables and are declared in a class outside any method, constructor or block. 17 | As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed. 18 | Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used. 19 | Initilisation of Instance Variable is not Mandatory. Its default value is 0 20 | Instance Variable can be accessed only by creating objects. 21 | ### Reference variables: 22 | When we create an object (instance) of class then space is reserved in heap memory. To access that allocated space we use a pointer variable this pointer variable is also known as reference variable. Classes, interfaces, arrays, enumerations, and, annotations are reference types in Java. Reference variables hold the objects/values of reference types in Java. 23 | Reference variable can also store null value. By default, if no object is passed to a reference variable then it will store a null value. 24 | 25 | 26 | 27 | 28 | ```Example :``` 29 | ``` 30 | public class VariableExample{ 31 | int myVariable; 32 | static int data = 30; 33 | 34 | public static void main(String args[]){ 35 | int a = 100; 36 | VariableExample obj = new VariableExample(); 37 | VariableExample obj1 = obj ; //pointer or Reference variables 38 | 39 | System.out.println("Value of instance variable myVariable: "+obj.myVariable); 40 | System.out.println("Value of Reference variables myVariable: "+obj1.myVariable); 41 | System.out.println("Value of local variable a: "+a); 42 | } 43 | } 44 | ``` 45 | `Output :` 46 | ``` 47 | Value of instance variable myVariable: 0 48 | Value of Reference variables myVariable: 0 49 | Value of local variable a: 100 50 | ``` 51 | -------------------------------------------------------------------------------- /Week-2/Rules of Method Overloading and Overriding/MethodOverloadOverride.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is the example of method overloading and overriding. 3 | * It contains the Animal and Dog classes that is used here for 4 | * demonstrating Overloading. And main class have methods sum() 5 | * that is used for demonstrating Overriding. 6 | * 7 | * @author NanthaKumar 8 | * @date 2021/08/10 9 | */ 10 | 11 | // Base class for Animal. 12 | class Animal { 13 | 14 | // Overridden method makeSound() follows the rules of 15 | // method overriding. Note that here the method is protected (non public). 16 | protected String makeSound() { 17 | return "Base Animal Sound..."; 18 | } 19 | } 20 | 21 | // Dog class that extends the Animal. 22 | class Dog extends Animal { 23 | 24 | // Overriding method makeSound() follows the rules of method overriding. 25 | // Note that here the method is public (not default). 26 | public String makeSound() { 27 | return "Bow Bow..."; 28 | } 29 | } 30 | 31 | // Main class containes driver code and example. 32 | public class MethodOverloadOverride { 33 | 34 | // Creating static methods for overloading. static used for invoke 35 | // method without creating object. 36 | // Method sum() for adding 2 integers and return the sum. 37 | static int sum(int a, int b) { 38 | return a + b; 39 | } 40 | 41 | // Method sum() adds 2 floats and return the sum. 42 | static float sum(float a, float b) { 43 | return a + b; 44 | } 45 | 46 | // main method for execution. 47 | public static void main(String[] args) { 48 | 49 | 50 | // Example of overriding. 51 | 52 | // Creating Object for Dog class and calling the makeSound() method 53 | // and testing it against Animal class object. 54 | System.out.println("From Dog: " + new Dog().makeSound()); 55 | System.out.println("From Animal: " + new Animal().makeSound()); 56 | 57 | 58 | // Example of overloading. 59 | 60 | // Calling sum() methods with intergers and floats then prints the 61 | // result. 62 | System.out.println("Integer Addition, 5 + 4 : " + sum(5, 4)); 63 | System.out.println("Float Addition, 5.0 + 4.0 : " + sum(5.0f, 4.0f)); 64 | // On above code 'f' used after floating numbers. This is because when we 65 | // use decimal point numbers without suffix 'f', Java treat is as double 66 | // datatype. 67 | } 68 | } 69 | 70 | /** 71 | * Output: 72 | * From Dog: Bow Bow... 73 | * From Animal: Base Animal Sound... 74 | * Integer Addition, 5 + 4 : 9 75 | * Float Addition, 5.0 + 4.0 : 9.0 76 | */ 77 | -------------------------------------------------------------------------------- /Week-3/Static Variables and Methods/README.md: -------------------------------------------------------------------------------- 1 | # Static Variables and Methods 2 | 3 | 4 | ## Static Keyword 5 | The keyword `static` gives the static property to the Java class. It is 6 | belonged to class than instance of the class. It can be used with, 7 | - Variables 8 | - Methods 9 | - Blocks 10 | 11 | 12 | ## Static Variables 13 | A variable defined with `static` keyword is known as static variable. 14 | - It belongs to the class not to the instance(object). 15 | - It gives common property across objects created with same class. 16 | - These variables get initialized first than any other instance variables in 17 | the class. 18 | - A static variable inside a class can be accessed without creating an object. 19 | 20 | 21 | ## Static Methods 22 | A method defined with `static` keyword is known as static method. 23 | - It belongs to the class not to the object(instance). 24 | - It only accesses static data member and can change the value of it. 25 | - It can be invoked without creating an object. 26 | 27 | 28 | ## Static Blocks 29 | A block can be defined with `static` keyword is known as static block. 30 | - It's executed when the class loaded in JVM. 31 | - It helps to initialize static data members inside a class. 32 | 33 | 34 | ## Example 35 | ```java 36 | // DemoClass contains static attributs, methods and block. 37 | class DemoClass { 38 | 39 | // Static attribute. 40 | static String greet = "Welcome"; 41 | static int a, b; 42 | 43 | // Static block. It'll be loaded when class loaded in JVM. 44 | static { 45 | System.out.println("Static Block Invoked..."); 46 | 47 | // Here the static attributes accessed by a static block. 48 | a = 10; 49 | b = 20; 50 | } 51 | 52 | // Static method. 53 | static void hello() { 54 | System.out.println("Hello, I'm from DemoClass..."); 55 | } 56 | 57 | } 58 | 59 | // Main class contains the main method(). 60 | public class StaticVariableMethod { 61 | 62 | // main() method contains the driver code. 63 | public static void main(String[] args) { 64 | 65 | // Directly accessing DemoClass attributes and printing 66 | // them. 67 | System.out.println("Greet: " + DemoClass.greet); 68 | System.out.println("a: " + DemoClass.a); 69 | System.out.println("b: " + DemoClass.b); 70 | DemoClass.hello(); 71 | } 72 | } 73 | ``` 74 | > *You can find the above code on [StaticVariableMethod.java](StaticVariableMethod.java).* 75 | 76 | 77 | ## Output 78 | ``` 79 | Static Block Invoked... 80 | Greet: Welcome 81 | a: 10 82 | b: 20 83 | Hello, I'm from DemoClass... 84 | ``` 85 | 86 | 87 | ## Author 88 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 89 | -------------------------------------------------------------------------------- /Week-3/Defining and Throwing Exceptions/README.md: -------------------------------------------------------------------------------- 1 | # Defining and Throwing Exceptions 2 | 3 | 4 | ## Exceptions 5 | An exception is an event, which occurs during the execution of a program, that disrupts the 6 | normal flow of the program's instructions. 7 | 8 | When an error occurs within a method, the method creates an object and hands it 9 | off to the runtime system. The object, called an exception object, contains information 10 | about the error, including its type and the state of the program when the error occurred. 11 | Creating an exception object and handing it to the runtime system is called throwing an exception. 12 | 13 | 14 | ## Throwing Exceptions 15 | `throw` keyword used to throw Exceptions in a custom manner. The method throws an exception 16 | must be specified as such `throws` clause. 17 | 18 | ### Syntax 19 | ```java 20 | throw ; 21 | ``` 22 | ```java 23 | accessModifier returnType methodName(datatype param,...) throws { 24 | // Body of the method. 25 | } 26 | ``` 27 | 28 | 29 | ## Example 30 | ```java 31 | // Main class contains the main method. 32 | public class ThrowingException { 33 | 34 | // Divide method divides the given numbers. 35 | // It may throws ArithmeticException and base Exception. 36 | public static int divide(int a, int b) throws ArithmeticException, Exception { 37 | 38 | // Throwing manual exception if a < b. 39 | if (a < b) { 40 | throw new Exception("Value a: " + a + ", should be greater than value b: " + b); 41 | } 42 | return a / b; 43 | } 44 | 45 | // Execute and manages the exception with try catch. 46 | public static void execDivide(int a, int b) { 47 | try { 48 | System.out.println("Result: " + divide(a, b)); 49 | } 50 | catch (ArithmeticException e) { 51 | System.out.println("Catched Exception: " + e); 52 | } 53 | catch (Exception e) { 54 | System.out.println("Catched Exception: " + e); 55 | } 56 | } 57 | 58 | // main() method contains the driver code. 59 | public static void main(String[] args) { 60 | 61 | // First we try to catch ArithmeticException. 62 | execDivide(5, 0); 63 | 64 | // Second we try to catch manual exception. 65 | execDivide(5, 10); 66 | } 67 | } 68 | ``` 69 | > *Note: You can find the above code on [ThrowingException.java](ThrowingException.java).* 70 | 71 | 72 | ## Output 73 | ``` 74 | Catched Exception: java.lang.ArithmeticException: / by zero 75 | Catched Exception: java.lang.Exception: Value a: 5, should be greater than value b: 10 76 | ``` 77 | 78 | 79 | **Reference:** https://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html 80 | ## Author 81 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 82 | -------------------------------------------------------------------------------- /Week-1/Java Naming Conventions/Naming Conventions.md: -------------------------------------------------------------------------------- 1 | # Java Naming Conventions 2 | 3 | ### Below are some naming conventions of java programming language. They must be followed while developing software in java for good maintenance and readability of code. Java uses CamelCase as a practice for writing names of methods, variables, classes, packages and constants. 4 | 5 | **Camel case in Java Programming :** It consists of compound words or phrases such that each word or abbreviation begins with a capital letter or first word with a lowercase letter, rest all with capital. 6 | 7 | 1. Classes and Interfaces : 8 | * Class names should be nouns, in mixed case with the first letter of each internal word capitalised. Interfaces name should also be capitalised just like class names. 9 | * Use whole words and must avoid acronyms and abbreviations. 10 | 11 | #### Examples: 12 | ```python 13 | interface Bicycle 14 | class MountainBike implements Bicyle 15 | 16 | interface Sport 17 | class Football implements Sport 18 | ``` 19 | 20 | 2. Methods : 21 | * Methods should be verbs, in mixed case with the first letter lowercase and with the first letter of each internal word capitalised. 22 | 23 | #### Examples: 24 | ```python 25 | void changeGear(int newValue); 26 | void speedUp(int increment); 27 | void applyBrakes(int decrement); 28 | ``` 29 | 30 | 3. Variables : Variable names should be short yet meaningful. 31 | * Variables can also start with either underscore(‘_’) or dollar sign characters. 32 | * Should be mnemonic i.e, designed to indicate to the casual observer the intent of its use. 33 | * **One-character variable names should be avoided** except for temporary variables. 34 | * Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters. 35 | 36 | #### Examples: 37 | > variables for MountainBike class 38 | ```python 39 | int speed = 0; 40 | int gear = 1; 41 | ``` 42 | 43 | 4. Constant Variables: 44 | * Should be all uppercase with words separated by underscores (“_”). 45 | * There are various constants used in predefined classes like Float, Long, String etc. 46 | 47 | #### Examples: 48 | `static final int MIN_WIDTH = 4;` 49 | > // Some Constant variables used in predefined Float class 50 | ```python 51 | public static final float POSITIVE_INFINITY = 1.0f / 0.0f; 52 | public static final float NEGATIVE_INFINITY = -1.0f / 0.0f; 53 | public static final float NaN = 0.0f / 0.0f; 54 | ``` 55 | 5. Packages: 56 | * The prefix of a unique package name is always written in **all-lowercase ASCII letters** and should be one of the top-level domain names, like com, edu, gov, mil, net, org. 57 | * Subsequent components of the package name vary according to an organisation’s own internal naming conventions. 58 | 59 | #### Examples: 60 | ```python 61 | com.sun.eng 62 | com.apple.quicktime.v2 63 | ``` 64 | 65 | > java.lang packet in JDK 66 | `java.lang` 67 | 68 | 69 | 70 | ```python 71 | 72 | ``` 73 | -------------------------------------------------------------------------------- /Week-1/Assignment Operator/AssignmentOperator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @title Assignment Operator 3 | * @desc It's an example program for Assignment Operator. 4 | * @author nknantha 5 | * @date 2021/07/29 6 | */ 7 | 8 | // Public class contains main method. 9 | public class AssignmentOperator { 10 | 11 | // main() method to run this program. 12 | public static void main(String[] args) { 13 | 14 | // Declaring integer variable and initializing 10 15 | // using simple assignment operator. 16 | int Value = 1; 17 | System.out.println("Initially, Value = " + Value); 18 | 19 | // Compound additional assignment operator. 20 | Value += 10; 21 | System.out.println("Adding 10, Value = " + Value); 22 | 23 | // Compound subtraction assignment operator. 24 | Value -= 8; 25 | System.out.println("Subracting 8, Value = " + Value); 26 | 27 | // Compound multiplication assignment operator. 28 | Value *= 8; 29 | System.out.println("Multiplying 8, Value = " + Value); 30 | 31 | // Compound division assignment operator. 32 | Value /= 4; 33 | System.out.println("Dividing 4, Value = " + Value); 34 | 35 | // Compound Modulo Assignment operator. 36 | Value %= 99; 37 | System.out.println("Modulo with 99, Value = " + Value); 38 | 39 | // Compound bitwise AND Assignment operator. 40 | Value &= 7; 41 | System.out.println("Bitwise AND with 7, Value = " + Value); 42 | 43 | // Compound bitwise OR Assignment operator. 44 | Value |= 13; 45 | System.out.println("Bitwise OR with 13, Value = " + Value); 46 | 47 | // Compound bitwise XOR Assignment operator. 48 | Value ^= 17; 49 | System.out.println("Bitwise XOR with 17, Value = " + Value); 50 | 51 | // Changing value to 2021 for easy understanding of left and right shift. 52 | Value = 2021; 53 | 54 | // Compound left shift assignment operator. 55 | Value <<= 2; 56 | System.out.println("Left shit with 2, Value = " + Value); 57 | 58 | // Compound right shift assignment operator. 59 | Value >>= 2; 60 | System.out.println("Right shift with 2, Value = " + Value); 61 | 62 | // Compound right shift assignment (Unsigned) operator. 63 | Value >>>= 2; 64 | System.out.println("Right shift (unsigned) with 2, Value = " + Value); 65 | } 66 | } 67 | 68 | /** 69 | * Output: 70 | * Initially, Value = 1 71 | * Adding 10, Value = 11 72 | * Subracting 8, Value = 3 73 | * Multiplying 8, Value = 24 74 | * Dividing 4, Value = 6 75 | * Modulo with 99, Value = 6 76 | * Bitwise AND with 7, Value = 6 77 | * Bitwise OR with 13, Value = 15 78 | * Bitwise XOR with 17, Value = 30 79 | * Left shit with 2, Value = 8084 80 | * Right shift with 2, Value = 2021 81 | * Right shift (unsigned) with 2, Value = 505 82 | * 83 | */ 84 | -------------------------------------------------------------------------------- /Week-1/Java Program through IDE/README.md: -------------------------------------------------------------------------------- 1 | # Java Program through IDE 2 | 3 | Hey Learners, In this topic we can learn about IDE's and how can we use it with example. 4 | 5 | 6 | ## IDE 7 | 8 | - An IDE or Integrated Development Environment is a software application that provides multiple features to enhance programming activity. 9 | 10 | - It makes coding easier and increase productivity for developers. 11 | 12 | - It contains features like syntax highlighting, code completion, debugger, version control, etc. 13 | 14 | ### Popular Java IDE's 15 | 16 | - [IntelliJ IDEA](https://www.jetbrains.com/idea/) 17 | - [Eclipse](https://www.eclipse.org/eclipseide/) 18 | - [Xcode](https://developer.apple.com/xcode/) 19 | - [NetBeans](https://netbeans.apache.org/) 20 | 21 | 22 | ## Setup & Run 23 | 24 | For understanding, We can see an example of creating a Hello World program in an IDE. 25 | 26 | > Note: Major IDE's have same style setup while creating a project. 27 | 28 | I'm taking IntelliJ IDEA. You can download it from [above](#popular-java-ides). 29 | 30 | And I'm assuming that you are installed JDK already. If you've not done before checkout [here](https://www.wikihow.com/Install-the-Java-Software-Development-Kit). 31 | 32 | ### Steps 33 | 34 | Let's START, 35 | 36 | 1) Open IntelliJ IDEA and Click on New Project. 37 | ![Step_1](Images/Step_1.png) 38 | 39 | 2) Select your SDK and Click Next. I have SDK 15, it is choosen by default. 40 | ![Step_2](Images/Step_2.png) 41 | 42 | 3) Click & check Create project from template.(Optional) 43 | ![Step_3](Images/Step_3.png) 44 | 45 | 4) Fill your Project name, location and Base package. And click Finish. 46 | ![Step_4](Images/Step_4.png) 47 | 48 | 5) If you've done Step 3 you'll see a template of code in editor. So you just need to type below code after //write your code here. 49 | 50 | ```java 51 | System.out.println("Hello World"); 52 | ``` 53 | 54 | If you can't see that simply copy paste the below code which is a Hello World program in Java. 55 | ```java 56 | public class Main { 57 | 58 | public static void main(String[] args) { 59 | // Below statement prints "Hello World" 60 | System.out.println("Hello World"); 61 | } 62 | } 63 | ``` 64 | ![Step_5](Images/Step_5.png) 65 | 66 | 6) Click on the Play button, located left side of the main() method in Main class. And click Run. 67 | ![Step_6](Images/Step_6.png) 68 | 69 | ### Output 70 | 71 | - Unlike on command-line, you don't need any steps involving compiling and running java files in IDE's. 72 | 73 | - You have to just click Run, the IDE will compile and run the file for you. 74 | 75 | - After clicking Run you'll see output like this, 76 | ![Step_7](Images/Step_7.png) 77 | 78 | That's all about Java program through IDE. 79 | 80 | > I added a file [HelloWorld.java](HelloWorld.java) for example. You can use it either in IDE or command-line. 81 | 82 | ## Author 83 | [NanthaKumar](https://github.com/nknantha) 84 | -------------------------------------------------------------------------------- /Week-2/Compile-time Polymorphism/README.md: -------------------------------------------------------------------------------- 1 | # Compile-time Polymorphism 2 | 3 | 4 | ## Polymorphism 5 | Polymorphism is the ability to take many forms instead of one. It describes the 6 | concept that different classes can be used with same interface. It occurs when 7 | multiple classes related each other by inheritance. 8 | There are 2 types of polymorphism, 9 | - Compile-time Polymorphism. 10 | - Run-time Polymorphism. 11 | 12 | 13 | ## Compile-time Polymorphism 14 | An object's functionality resolved at compile time rather than run-time is known 15 | as Compile-time Polymorphism. Compile-time polymorphism is achieved through method 16 | overloading. It provides fast execution because the method that needs to be 17 | executed is known early at the compile time. It is also known as static polymorphism, 18 | early binding and overloading. 19 | 20 | 21 | ## Method Overloading 22 | In a namespace, more than one method having same name and different datatype of parameters 23 | or different number of parameters is known as Method overloading. On execution particular 24 | method signature chosen by given arguments. If matching method signature not found then 25 | the argument datatype promoted to another data type. Method overloading provides greater 26 | readability and productivity. It is related to compile-time polymorphism. 27 | 28 | ### Type Promotion 29 | Type promotion happens in this way, 30 | ![](TypePromotion.svg) 31 | 32 | 33 | ## Example 34 | ```java 35 | // Public main class CompiletimePolymorphism. 36 | public class CompiletimePolymorphism { 37 | 38 | // Signature - 1: 39 | // sum() method with 2 integer parameter. 40 | static int sum(int a, int b) { 41 | return a + b; 42 | } 43 | 44 | // Signature - 2: 45 | // sum() method with 2 float parameter. 46 | static float sum(float a, float b) { 47 | return a + b; 48 | } 49 | 50 | // Signature - 3: 51 | // sum() method with 3 integer parameter. 52 | static int sum(int a, int b, int c) { 53 | return a + b + c; 54 | } 55 | 56 | // main() method contains driver code and demonstration. 57 | public static void main(String[] args) { 58 | 59 | // Calling sum() method with 2 integer values and 60 | // printing the result. 61 | System.out.println(sum(3, 10)); 62 | 63 | // Calling sum() method with 2 float values and 64 | // printing the result. 65 | System.out.println(sum(3.0f, 10.0f)); 66 | 67 | // Calling sum() method with 3 integer values and 68 | // printing the result. 69 | System.out.println(sum(3, 10, 2)); 70 | 71 | // Above different method calls of sum() are resolved at compiletime 72 | // rather than runtime. 73 | } 74 | } 75 | ``` 76 | > *You can find the above on [CompiletimePolymorphism.java](CompiletimePolymorphism.java).* 77 | 78 | 79 | ## Output 80 | ``` 81 | 13 82 | 13.0 83 | 15 84 | ``` 85 | 86 | 87 | ## Author 88 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 89 | -------------------------------------------------------------------------------- /Week-1/How to run a java program using command prompt/How to run a java program using command prompt.md: -------------------------------------------------------------------------------- 1 | ### How To Run A Java Program Using Command Prompt 2 | 3 | Before we get started, we need to install the Java Runtime Environment(JRE) in order to run java programs in your local system. 4 | 5 | Click on this link and click on jdk download 6 | 7 | 8 | 9 | There are four platforms of the Java programming language(Java Platform Standard Edition (Java SE),Java Platform, Enterprise Edition (Java EE),Java Platform, Micro Edition (Java ME),JavaFX) but we will be using Java SE. 10 | 11 | Choose a download link based on the configuration of your device(I use Windows-64 bit version and hence I chose this). 12 | 13 | 14 | 15 | Click on the check box and give download. 16 | 17 | Once downloaded,open the file. 18 | 19 | 20 | 21 | Go through the installation procedure and install it.It will be installed in the ProgramFiles of your C Drive by default. In case you have given an alternate folder path,it will be downloaded there. 22 | 23 | If you have completed till this,great job!Let's get started!! 24 | 25 | Open notepad(type notepad in your search bar or windows key + r and then type notepad in case of Windows user) 26 | 27 | Type the following to write your first ever Java program 28 | 29 | 30 | 31 | Once done we have to save it. 32 | 33 | Notice that we will be saving our Java files with the same name as our class name with the extension java 34 | 35 | 36 | 37 | Now that we saved our Java file,we have to run it. 38 | 39 | Open command prompt and run cd followed by the path to the folder where you have saved your java file.(I saved mine in my Desktop) 40 | 41 | 42 | 43 | Now run java your_class_name.java 44 | 45 | Et Voila, you will see the output. 46 | 47 | 48 | 49 | If you have completed all these steps,congratulations on running your first ever Java program😎 50 | -------------------------------------------------------------------------------- /Week-2/Abstraction/Abstraction.md: -------------------------------------------------------------------------------- 1 | # Abstraction in Java 2 | > Data Abstraction is the property by virtue of which only the essential details are displayed to the user. 3 | > The trivial or the non-essentials units are not displayed to the user. 4 | > Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects. 5 | > Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car, but he does not know about how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc in the car. This is how data abstraction works. 6 | #### Note: 7 | * An abstract class is a class that is declared with abstract keyword. 8 | * An abstract method is a method that is declared without implementation. 9 | * An abstract class may or may not have all abstract methods. Some of them can be concrete methods 10 | * A method defined abstract must always be redefined in the subclass, thus making overriding compulsory OR either make subclass itself abstract. 11 | * Any class that contains one or more abstract methods must also be declared with abstract keyword. 12 | * There can be no object of an abstract class. That is, an abstract class can not be directly instantiated with the new operator. 13 | * An abstract class can have parameterized constructors and default constructor is always present in an abstract class. 14 | ## When to use abstract classes and abstract methods 15 | > There are situations in which we will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. 16 | > That is, sometimes we will want to create a superclass that only defines a generalization form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. 17 | ## Example: 18 | > Consider a classic “shape” example, perhaps used in a computer-aided design system or game simulation. The base type is “shape” and each shape has a color, size and so on. From this, specific types of shapes are derived(inherited)-circle, square, triangle and so on — each of which may have additional characteristics and behaviors. For example, certain shapes can be flipped. Some behaviors may be different, such as when you want to calculate the area of a shape. The type hierarchy embodies both the similarities and differences between the shapes. 19 | ![](https://media.geeksforgeeks.org/wp-content/uploads/Abstract-classes-and-methods-Page-1.png) 20 | 21 | ## Advantages of Abstraction 22 | 1. It reduces the complexity of viewing the things. 23 | 2. Avoids code duplication and increases reusability. 24 | 3. Helps to increase security of an application or program as only important details are provided to the user. 25 | -------------------------------------------------------------------------------- /Week-1/While & Do-While Loops/README.md: -------------------------------------------------------------------------------- 1 | # While loops 2 | 3 | The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops. 4 | 5 | Java while loop is called an entry control loop because condition is specified at the start itself. 6 | 7 | Loops consists of 3 parts: 8 | - initialization expression (initializing the loop variable) 9 | - conditional expression (limit for the loop to run ) 10 | - increment/decrement of looping variable 11 | 12 | ### *initialization expression* 13 | Initialization occurs once, as the loop begins. 14 | Initialization usually consists of a single variable declaration 15 | Eg: int x = 0 16 | 17 | ### *conditional expression/ termination expression* 18 | The termination expression is evaluated and if true, execution continues with the statement (loop body). 19 | The loop body will never be executed if the termination expression is immediately false. 20 | The loop terminates when the termination expression evaluates to false. 21 | 22 | ### *increment/decrement of looping variable* 23 | The increment expression is invoked after each iteration of the loop. 24 | 25 | 26 | ### Syntax 27 | ```java 28 | while(condition){ 29 | 30 | ----body statements---- 31 | 32 | }; 33 | ``` 34 | # Do while loops 35 | 36 | The Java do-while loop is used to iterate a part of the program repeatedly, until the specified condition is true. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use a do-while loop. 37 | 38 | Java do-while loop is called an exit control loop because condition is specified at the end of the loop. 39 | 40 | ### Syntax 41 | ```java 42 | do{ 43 | 44 | ----body statements---- 45 | 46 | }while(condition); 47 | ``` 48 | 49 | ## Example 50 | ```java 51 | /**********************************While loop-Section**********************************/ 52 | 53 | // class name (While) 54 | class While { 55 | 56 | // Static method contains example code. 57 | public static void run() { 58 | int i=1; // initialization expression 59 | while(i<=3) // while starts(conditional expression in the brackets) 60 | { 61 | System.out.println("HI"); // prints 'HI' three times since i iterates from 1 to 3 62 | i=i+1; //increment of looping variable 63 | } 64 | 65 | } 66 | } 67 | 68 | // class name (DoWhile) 69 | class DoWhile { 70 | 71 | // Static method contains example code. 72 | public static void run() { 73 | int i=1; // initialization expression 74 | do // dowhile starts 75 | { 76 | System.out.println("HI"); // prints 'HI' three times since i iterates from 1 to 3 77 | i=i+1; //increment of looping variable 78 | }while(i<=3); // conditional expression in the brackets 79 | 80 | } 81 | } 82 | ``` 83 | -------------------------------------------------------------------------------- /Week-1/Upcasting and Downcasting/README.md: -------------------------------------------------------------------------------- 1 | # Upcasting and Downcasting 2 | 3 | 4 | ## Type Casting 5 | Type casting is a method or process of converting one data type to another 6 | data type in both ways automatically and manually. Implicit conversion done 7 | by the compiler and explicit conversion done by the programmer. 8 | 9 | 10 | ## Upcasting 11 | A child object typecasted to its parent class object is known as Upcasting. 12 | By upcasting an object to it's parent, we can access parent attributes and 13 | methods. We can also access some specified methods of child class. Upcasting 14 | can be done implicitly. 15 | 16 | ### Syntax 17 | ```java 18 | ParentClass Obj = new ChildClass(); 19 | // Here child object created and typecasted automatically 20 | // to its parent object. 21 | ``` 22 | 23 | 24 | ## Downcasting 25 | A parent object typecasted to its child class object is known as Downcasting. 26 | Downcasting cannot be done implicitly. When we forcefully typecast them, the 27 | compiler checks in the background if this typecasting possible or not and 28 | throws a ClassCastException if it is not possible. 29 | 30 | ### Syntax 31 | ```java 32 | // Here we're using Upcasted Obj for downcasting. 33 | ChildClass Obj2 = (ChildClass) Obj; 34 | // Note that we're explicitly typecasting parent object 35 | // to its child by '(ChildClass)'. 36 | ``` 37 | 38 | 39 | ## Example 40 | ```java 41 | // Parent class contains a method tell() 42 | class Parent { 43 | 44 | // Attribute to explain upcasted access from child. 45 | String name = "Parent"; 46 | 47 | void tell() { 48 | // Prints a msg from parent class. 49 | System.out.println("Parent Method"); 50 | } 51 | } 52 | 53 | // Child class that extends parent class 54 | // and overrides parent class's tell() method. 55 | class Child extends Parent { 56 | 57 | // Overrides the parent's tell() method and 58 | // prints msg from child class. 59 | @Override 60 | void tell() { 61 | System.out.println("Child Method"); 62 | } 63 | } 64 | 65 | // Public class UpcastingAndDowncasting which is a main class. 66 | public class UpcastingAndDowncasting { 67 | 68 | // main() method to run program. 69 | public static void main(String[] args) { 70 | // Creating object for child class and Upcasting it 71 | // to parent. 72 | Parent Obj = new Child(); 73 | 74 | // Accessing and printing name attribute in parent class. 75 | System.out.println(Obj.name); 76 | 77 | // Calling tell() method which is overridden by child class. 78 | Obj.tell(); 79 | 80 | // Downcasting Obj to child class. 81 | Child Obj2 = (Child) Obj; 82 | 83 | // After downcasting calling tell() method. 84 | Obj2.tell(); 85 | } 86 | } 87 | ``` 88 | > *Note: You can also find above code on [UpcastingAndDowncasting.java](UpcastingAndDowncasting.java).* 89 | 90 | 91 | ## Output 92 | ``` 93 | Parent 94 | Child Method 95 | Child Method 96 | ``` 97 | 98 | 99 | ## Author 100 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 101 | -------------------------------------------------------------------------------- /Week-3/Errors and Runtime Exceptions/README.md: -------------------------------------------------------------------------------- 1 | # Errors and Runtime Exceptions 2 | 3 | 4 | ## Exceptions 5 | An exception is an event, which occurs during the execution of a program, that disrupts the 6 | normal flow of the program's instructions. It may occur at compile time and runtime. It can 7 | be thrown manually by developer. It can be handled by `try{}catch{}` blocks. Exception 8 | may be checked and unchecked. 9 | 10 | 11 | ## Errors 12 | Errors are the problem that happens at runtime unexpectedly. It cannot be handled or caught. 13 | It indicates a serious problem. These are all unchecked. Example: `Assertion­Error`, 14 | `Linkage­Error`. 15 | 16 | | Exceptions | Errors | 17 | |:---|:---| 18 | | We can recover from exceptions using `try` `catch` blocks. | Impossible to recover. | 19 | | It has checked and unchecked classification. | All errors are unchecked. | 20 | | It occurs at compile and runtime. | It occurs only at runtime. | 21 | | Base class: `java.lang.Exception`. | Base class: `java.lang.Error`. | 22 | | Checked exceptions are known to compiler. | Errors cannot be known to compiler. | 23 | | Examples:
**Checked Exceptions:** `Class­Not­Found­Exception`, `Illegal­Access­Exception`.
**Unchecked Exceptions:** `Arithmetic­Exception`, `Array­Store­Exception`. | Examples: `Out­Of­Memory­Error`, `Stack­Overflow­Error`. | 24 | 25 | 26 | ## Example 27 | ```java 28 | // Class for example of ArithmeticException. 29 | class ExceptionExample { 30 | 31 | // Method containing code to make ArithmeticException. 32 | public static void exec() { 33 | int a = 9, b = 0; 34 | 35 | try { 36 | System.out.println("Result: " + a / b); 37 | } 38 | catch (ArithmeticException e) { 39 | System.out.println("Catched Exception: " + e); 40 | } 41 | } 42 | } 43 | 44 | // Class for Error example. 45 | class ErrorExample { 46 | 47 | // Method to make StackOverFlowError. 48 | public static void exec(int i) { 49 | 50 | // Recursive call with incremented value. 51 | exec(++i); 52 | } 53 | } 54 | 55 | // Main class contains the main method. 56 | public class ExceptionError { 57 | 58 | // main() method contains driver code. 59 | public static void main(String[] args) { 60 | 61 | // Calling methods to execute examples. 62 | ExceptionExample.exec(); 63 | ErrorExample.exec(1); 64 | } 65 | } 66 | ``` 67 | > *Note: You can find the above code on [ExceptionError.java](ExceptionError.java).* 68 | 69 | 70 | ## Output 71 | ``` 72 | Catched Exception: java.lang.ArithmeticException: / by zero 73 | Exception in thread "ExceptionError" java.lang.StackOverflowError 74 | at ErrorExample.exec(ExceptionError.java:39) 75 | at ErrorExample.exec(ExceptionError.java:39) 76 | at ErrorExample.exec(ExceptionError.java:39) 77 | at ErrorExample.exec(ExceptionError.java:39) 78 | at ErrorExample.exec(ExceptionError.java:39) 79 | ... 80 | ``` 81 | 82 | **Reference:** 83 | https://www.geeksforgeeks.org/errors-v-s-exceptions-in-java/ 84 | https://www.javatpoint.com/exception-vs-error-in-java 85 | 86 | 87 | ## Author 88 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 89 | -------------------------------------------------------------------------------- /Week-2/Concrete Classes/README.md: -------------------------------------------------------------------------------- 1 | # Concrete Classes 2 | 3 | 4 | ## Concrete Class 5 | A concrete class is a class that has an implementation for all of its methods 6 | that were inherited from abstract or implemented via interfaces. It is a 7 | complete class and can be instantiated. It cannot be abstract. Furthermore, 8 | it is opposite to abstract. 9 | 10 | 11 | ## Example - 1 12 | ```java 13 | // Example - 1 14 | 15 | // A simple concrete public class named ConcreteEx1. 16 | public class ConcreteEx1 { 17 | 18 | // add() method takes 2 integers and return their sum. 19 | static int add(int a, int b) { 20 | return a + b; 21 | } 22 | 23 | // multiply() method takes 2 integers and return their product. 24 | static int multiply(int a, int b) { 25 | return a * b; 26 | } 27 | 28 | // main() method contains driver code. 29 | public static void main(String[] args) { 30 | 31 | // Creating variables to hold integer values. 32 | int num1 = 10, num2 = 5; 33 | 34 | // Calling add() method with num1 and num2 and 35 | // printing the result. 36 | System.out.println("Sum: " + add(num1, num2)); 37 | 38 | // Printing the result of multiply() with num1 and num2. 39 | System.out.println("Product: " + multiply(num1, num2)); 40 | } 41 | } 42 | ``` 43 | > *You can find the above code on [ConcreteEx1.java](ConcreteEx1.java).* 44 | 45 | 46 | ## Output 47 | ``` 48 | Sum: 15 49 | Product: 50 50 | ``` 51 | 52 | 53 | ## Example - 2 54 | ```java 55 | // Example - 2 56 | 57 | // Interface Calc contains declarations of method add() and multiply(). 58 | interface Calc { 59 | int add(int a, int b); 60 | int multiply(int a, int b); 61 | } 62 | 63 | // Abstract class add contains the implementation of add() method and 64 | // it implements the Calc interface. 65 | abstract class Add implements Calc { 66 | 67 | // Implementation of add() method 68 | public int add(int a, int b) { 69 | return a + b; 70 | } 71 | } 72 | 73 | // Public main class (Concrete) contains main() method and implemantation of 74 | // multiply() method and it extends the abstract class Add. 75 | public class ConcreteEx2 extends Add { 76 | 77 | // Implemantation of multiply() method. 78 | public int multiply(int a, int b) { 79 | return a * b; 80 | } 81 | 82 | // main() method contains driver code. 83 | public static void main(String[] args) { 84 | 85 | // Creating object for main class. 86 | ConcreteEx2 Obj = new ConcreteEx2(); 87 | 88 | // Variables to hold integers. 89 | int num1 = 10, num2 = 5; 90 | 91 | // Calling add() method in Obj and printing the return value. 92 | System.out.println("Sum: " + Obj.add(num1, num2)); 93 | 94 | // Calling multiply() method in Obj and printing the return value. 95 | System.out.println("Product: " + Obj.multiply(num1, num2)); 96 | } 97 | } 98 | ``` 99 | > *You can find the above code on [ConcreteEx2.java](ConcreteEx2.java).* 100 | 101 | 102 | ## Output 103 | ``` 104 | Sum: 15 105 | Product: 50 106 | ``` 107 | 108 | 109 | ## Author 110 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 111 | -------------------------------------------------------------------------------- /Week-2/Defining Class, Instance Variables and Methods/README.md: -------------------------------------------------------------------------------- 1 | # Defining Class, Instance Variables and Methods 2 | 3 | 4 | ## Defining Class 5 | Class is a blueprint or prototype from which objects are created. It is a logical entity. 6 | A class can have attributes and methods. 7 | 8 | ### Syntax 9 | ```java 10 | class { 11 | // body of the class. 12 | } 13 | ``` 14 | - access_modifier: Specifies the accessibility of the class. It takes default if we don't specify anything. 15 | - class_name: The name of the class. The name should be start with an initial letter, it capitalized for convention. 16 | 17 | 18 | ## Defining Instance Variables 19 | Variables created inside class and outside methods known as instance variables. It doesn't get memory at 20 | compile time, it gets memory only when object being created. It is also known as attributes. 21 | 22 | ### Syntax 23 | ```java 24 | class ClassName { 25 | ; 26 | } 27 | ``` 28 | - data_type: The data type of the instance variable. 29 | - variable_name: The name of the instance variable. 30 | 31 | Like variables, we initialize instance variables at the time of declaring. 32 | 33 | 34 | ## Defining Methods 35 | A method is a block of code which only runs when it's called. It's like a function which is used to 36 | expose behavior of an object. It provides code reusability and code optimization. 37 | 38 | ### Syntax 39 | ```java 40 | class ClassName { 41 | () { 42 | // body of the method. 43 | } 44 | } 45 | ``` 46 | - access_modifier: Specifies the accessibility of the class. It takes default if we don't specify anything. 47 | - return_type: Defines the return type of the method. 48 | - method_name: Name of the function. By convention method name should be in camelCase. 49 | - parameters: List of all parameters and their data types. 50 | 51 | 52 | ## Example 53 | ```java 54 | // Default class Student contains example attributes and methods. 55 | class Student { 56 | 57 | // Defining private student name instance variable. 58 | private String name; 59 | 60 | // A method to get student name. 61 | public String getName() { 62 | return this.name; 63 | } 64 | 65 | // A method to set student name. Takes a parameter name to get assigned. 66 | public void setName(String name) { 67 | this.name = name; 68 | } 69 | } 70 | 71 | // Public class ClassVariableMethod for example demonstration. 72 | public class ClassVariableMethod { 73 | 74 | // Public main() contains example code. 75 | public static void main(String[] args) { 76 | 77 | // Creating object for student class. 78 | Student Obj = new Student(); 79 | 80 | // Checking initial name of Obj. 81 | System.out.println("Initially, name is " + Obj.getName()); 82 | 83 | // Changing name to Steve. 84 | // Obj.name = "Steve"; // this creates error because of private modifier. 85 | Obj.setName("Steve"); 86 | 87 | // After changing name accessing name of Obj. 88 | System.out.println("After name change, name is " + Obj.getName()); 89 | } 90 | } 91 | ``` 92 | > *Note: You can find above code on [ClassVariableMethod.java](ClassVariableMethod.java)* 93 | 94 | 95 | ## Output 96 | ``` 97 | Initially, name is null 98 | After name change, name is Steve 99 | ``` 100 | 101 | 102 | ## Author 103 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 104 | -------------------------------------------------------------------------------- /Week-3/Keywords in Exception API: try, catch, finally, throw, throws/Keywords in Exception API: try, catch, finally, throw, throws.md: -------------------------------------------------------------------------------- 1 | # Keywords in Exception API: try, catch, finally, throw, throws 2 | ### Exceptions 3 | > In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. 4 | ### What is Exception Handling? 5 | > Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. 6 | ### Java Exception Keywords 7 | > The following table describes the keywords used in `Exception Handling`: 8 | 9 | |Keyword|Description| 10 | |-------|-----------| 11 | | try | The `try` keyword is used to specify a block where we should place an exception code. It means we can't use try block alone. The try block must be followed by either catch or finally.| 12 | | catch | The `catch` block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later.| 13 | | finally | The `finally` block is used to execute the necessary code of the program. It is executed whether an exception is handled or not. | 14 | | throw | The `throw` keyword is used to throw an exception. | 15 | | throws | The `throws` keyword is used to declare exceptions. It specifies that there may occur an exception in the method. It doesn't throw an exception. It is always used with method signature. | 16 | 17 | #### Difference between throw and throws 18 | 19 | | throw | throws | 20 | | ------|--------| 21 | |throw keyword is used to throw an exception explicitly.| throws keyword is used to declare an exception possible during its execution.| 22 | | throw keyword is followed by an instance of Throwable class or one of its sub-classes.|throws keyword is followed by one or more Exception class names separated by commas.| 23 | | throw keyword is declared inside a method body.| throws keyword is used with method signature (method declaration). | 24 | | We cannot throw multiple exceptions using throw keyword.| We can declare multiple exceptions (separated by commas) using throws keyword. | 25 | 26 | ### Example throw Exception 27 | ``` 28 | class Test 29 | { 30 | static void avg() 31 | { 32 | try 33 | { 34 | throw new ArithmeticException("demo"); 35 | } 36 | catch(ArithmeticException e) 37 | { 38 | System.out.println("Exception caught"); 39 | } 40 | } 41 | 42 | public static void main(String args[]) 43 | { 44 | avg(); 45 | } 46 | } 47 | ``` 48 | ### Example throws Keyword 49 | 50 | ``` 51 | class Test 52 | { 53 | static void check() throws ArithmeticException 54 | { 55 | System.out.println("Inside check function"); 56 | throw new ArithmeticException("demo"); 57 | } 58 | 59 | public static void main(String args[]) 60 | { 61 | try 62 | { 63 | check(); 64 | } 65 | catch(ArithmeticException e) 66 | { 67 | System.out.println("caught" + e); 68 | } 69 | } 70 | } 71 | ``` 72 | ### Example finally Block With try , catch 73 | ``` 74 | class Demo 75 | { 76 | public static void main(String[] args) 77 | { 78 | int a[] = new int[2]; 79 | try 80 | { 81 | System.out.println("Access invalid element"+ a[3]); 82 | /* the above statement will throw ArrayIndexOutOfBoundException */ 83 | } 84 | catch(ArrayIndexOutOfBoundsException e) { 85 | System.out.println("Exception caught"); 86 | } 87 | finally 88 | { 89 | System.out.println("finally is always executed."); 90 | } 91 | } 92 | } 93 | ``` 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /Week-2/Classes and Objects/README.md: -------------------------------------------------------------------------------- 1 | # Classes and Objects in Java 2 | 3 | Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities. 4 | ## Class 5 | A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order: 6 | 1. _Modifiers:_ A class can be public or has default access (Refer this for details). 7 | 2. _class keyword:_ class keyword is used to create a class. 8 | 3. _Class name:_ The name should begin with an initial letter (capitalized by convention). 9 | 4. _Superclass(if any):_ The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. 10 | 5. _Interfaces(if any):_ A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. 11 | 6. _Body:_ The class body surrounded by braces, { }. 12 | Constructors are used for initializing new objects. Fields are variables that provides the state of the class and its objects, and methods are used to implement the behavior of the class and its objects. 13 | There are various types of classes that are used in real time applications such as nested classes, anonymous classes, lambda expressions. 14 | ## Object 15 | It is a basic unit of Object-Oriented Programming and represents the real life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of : 16 | 1. *State:* It is represented by attributes of an object. It also reflects the properties of an object. 17 | 2. *Behavior:* It is represented by methods of an object. It also reflects the response of an object with other objects. 18 | 3. *Identity:* It gives a unique name to an object and enables one object to interact with other objects. 19 | Declaring Objects (Also called instantiating a class) 20 | When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances. 21 | 22 | ### Example: 23 | 24 | ![sometext](Blank-Diagram-Page-1-5.png) 25 | 26 | Objects correspond to things found in the real world. For example, a graphics program may have objects such as “circle”, “square”, “menu”. An online shopping system might have objects such as “shopping cart”, “customer”, and “product”. 27 | 28 | 29 | Declaring Objects (Also called instantiating a class) 30 | When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances. 31 | 32 | ### Example: 33 | ![sometext0](Blank-Diagram-Page-1-3.png) 34 | As we declare variables like (type name;). This notifies the compiler that we will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, type must be strictly a concrete class name. In general, we can’t create objects of an abstract class or an interface. 35 | 36 | Dog tuffy; 37 | If we declare reference variable(tuffy) like this, its value will be undetermined(null) until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object. -------------------------------------------------------------------------------- /Week-1/Java Conditions/Conditions.md: -------------------------------------------------------------------------------- 1 | # Java Conditions and If Statements 2 | 3 | ### Java supports the usual logical conditions from mathematics: 4 | 5 | * Less than: a < b 6 | * Less than or equal to: a <= b 7 | * Greater than: a > b 8 | * Greater than or equal to: a >= b 9 | * Equal to a == b 10 | * Not Equal to: a != b 11 | 12 | ### Java has the following conditional statements: 13 | 14 | * Use if to specify a block of code to be executed, if a specified condition is true 15 | * Use else to specify a block of code to be executed, if the same condition is false 16 | * Use else if to specify a new condition to test, if the first condition is false 17 | * Use switch to specify many alternative blocks of code to be executed 18 | 19 | # The if Statement 20 | ### The if statement is used to specify a block of Java code to be executed if a condition is true. 21 | ## **Syntax** 22 | ```python 23 | if (condition) { 24 | ``` 25 | > block of code to be executed if the condition is true 26 | `}` 27 | ## Example: 28 | ```python 29 | public class Main { 30 | public static void main(String[] args) { 31 | if (20 > 18) { 32 | System.out.println("20 is greater than 18"); // obviously 33 | } 34 | } 35 | } 36 | ``` 37 | ### Output: 38 | `20 is greater than 18` 39 | 40 | # The else Statement 41 | ### We use the else statement to specify a block of code to be executed if the condition is false. 42 | ## **Syntax** 43 | ```python 44 | if (condition) { 45 | ``` 46 | > block of code to be executed if the condition is true 47 | `}` 48 | ```python 49 | else { 50 | ``` 51 | > block of code to be executed if the condition is false 52 | `}` 53 | ## Example: 54 | ```python 55 | public class Main { 56 | public static void main(String[] args) { 57 | int time = 20; 58 | if (time < 18) { 59 | System.out.println("Good day."); 60 | } else { 61 | System.out.println("Good evening."); 62 | } 63 | } 64 | } 65 | ``` 66 | ### Output: 67 | `Good evening` 68 | 69 | # The else if Statement 70 | ### The else if statement is used to specify a new condition if the first condition is false. 71 | ## **Syntax** 72 | ```python 73 | if (condition1) { 74 | ``` 75 | > block of code to be executed if condition1 is true 76 | `}` 77 | ```python 78 | else if (condition2) { 79 | ``` 80 | > block of code to be executed if the condition1 is false and condition2 is true 81 | `}` 82 | ```python 83 | else { 84 | ``` 85 | > block of code to be executed if the condition1 is false and condition2 is false 86 | `}` 87 | ## Example: 88 | ```python 89 | public class Main { 90 | public static void main(String[] args) { 91 | int time = 22; 92 | if (time < 10) { 93 | System.out.println("Good morning."); 94 | } else if (time < 20) { 95 | System.out.println("Good day."); 96 | } else { 97 | System.out.println("Good evening."); 98 | } 99 | } 100 | } 101 | ``` 102 | ### Output: 103 | `Good evening` 104 | 105 | # Short Hand If...Else (Ternary Operator) 106 | ### There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements: 107 | ## **Syntax** 108 | `variable = (condition) ? expressionTrue : expressionFalse;` 109 | ## Example: 110 | ```python 111 | public class Main { 112 | public static void main(String[] args) { 113 | int time = 20; 114 | if (time < 18) { 115 | System.out.println("Good day."); 116 | } else { 117 | System.out.println("Good evening."); 118 | } 119 | } 120 | } 121 | ``` 122 | ### Output: 123 | `Good evening` 124 | 125 | 126 | ```python 127 | 128 | ``` 129 | -------------------------------------------------------------------------------- /Week-1/Array of Object References/README.md: -------------------------------------------------------------------------------- 1 | # Array of Object References 2 | 3 | 4 | ## Array 5 | - Array is a data structure. 6 | - It is a collection of items stored in contiguous memory locations. 7 | - It is used to store multiple items of the same data type together. 8 | 9 | 10 | ## Object 11 | - An object is an instance of a class. 12 | - It contains state and behavior. 13 | - It is also known as a real-world entity. 14 | 15 | 16 | ## Array of Objects 17 | The Array of Objects the name itself suggests that it stores an array of objects. 18 | Like in traditional array we store integers, floats, boolean, etc. here we store objects 19 | in an array. Storing objects means that we're storing their references. 20 | 21 | 22 | ## Creation of Array of Objects 23 | We can use the object class(Base class) in Java to create Array of Objects. 24 | Object class in java is the root of all classes. 25 | 26 | ### Syntax 27 | ```java 28 | ClassName[] ObjectArrayName; 29 | ``` 30 | OR 31 | ```java 32 | ClassName ObjectArrayName[]; 33 | ``` 34 | ***Explanation:*** 35 | **ClassName:** the class of which objects going to be stored. 36 | **ObjectArrayName:** the name for your object array. 37 | 38 | 39 | ## Instantiating Object Array 40 | We're instantiating array with new keyword along with array size. 41 | 42 | ### Syntax 43 | ```java 44 | ClassName[] ObjectArrayName = new ClassName[size]; 45 | ``` 46 | ***Explanation:*** 47 | **size:** the count of the objects we store in array. 48 | 49 | 50 | ## Initializing Objects Array 51 | After instantiating, We need to initialize them with its values. 52 | For that, first we have to create each object then we store the 53 | reference of those in array. 54 | 55 | ### Syntax of Object Creation 56 | ```java 57 | ClassName ObjectName = new ClassName(); 58 | ``` 59 | 60 | 61 | ## Example 62 | ```java 63 | // Example class for demonstrating array of objects. 64 | // It contains attributes firstName and lastName. 65 | class Person { 66 | 67 | // Creating attributes. 68 | public String firstName, lastName; 69 | 70 | // Person class constructor 71 | Person(String firstName, String lastName) { 72 | this.firstName = firstName; 73 | this.lastName = lastName; 74 | } 75 | 76 | // Overriding toString() method for object representation. 77 | @Override 78 | public String toString() { 79 | // It represent firstName and lastName of this object. 80 | return "FirstName: " + this.firstName + ", LastName: " + this.lastName; 81 | } 82 | } 83 | 84 | // Public class that contains main method. 85 | public class ArrayOfObjects { 86 | 87 | // Main method for run this program. 88 | public static void main(String[] args) { 89 | // First, we're declaring array for objects. 90 | Person[] personArray; 91 | 92 | // Second, we instantiate it. 93 | personArray = new Person[3]; 94 | 95 | // Third, we creating object and storing it in array. 96 | personArray[0] = new Person("Nikola", "Tesla"); 97 | personArray[1] = new Person("Albert", "Einstein"); 98 | personArray[2] = new Person("Isaac", "Newton"); 99 | 100 | // Finally we're printing the stored element. 101 | for(Person person: personArray) { 102 | System.out.println(person); 103 | } 104 | } 105 | } 106 | ``` 107 | > Note: I also added [ArrayOfObjects.java](ArrayOfObjects.java) file which contains above code. 108 | 109 | 110 | ## Output 111 | ``` 112 | FirstName: Nikola, LastName: Tesla 113 | FirstName: Albert, LastName: Einstein 114 | FirstName: Isaac, LastName: Newton 115 | ``` 116 | 117 | 118 | ## Author 119 | [NanthaKumar](https://github.com/nknantha "Nanthakumar's Profile") 120 | -------------------------------------------------------------------------------- /Week-2/Super Class and Sub Class/README.md: -------------------------------------------------------------------------------- 1 | # Super Class and Sub Class 2 | 3 | 4 | ## Super Class 5 | Super class is defined as the base class of a derived class in which it's inherits to. 6 | In other words, it is the top class of a derived class. It contains the base functionality 7 | and behavior of a class. We cannot assign a super class object to its subclass variable type 8 | without explicit type casting. Super class is also known as parent class. `super` keyword is used to 9 | access parent class attributes and methods from a subclass. 10 | 11 | 12 | ## Sub Class 13 | Sub class is the derived class of a base class in which it's inherits from. It has an 'is a' 14 | relationship with its parent class. We can assign a subclass object to its super class 15 | variable type without explicit type casting. Subclass is also known as child class. In child class 16 | we can override the functionality of its parent class. It provides better code reusability. 17 | 18 | 19 | Both super class and subclass concepts are mainly used in Inheritance concept. `extends` keyword 20 | is used to inherit classes. 21 | 22 | ### Syntax 23 | ```java 24 | class ClassName extends ParentClass { 25 | // body of the child class. 26 | // use "super" keyword to access ParentClass attributes and methods. 27 | } 28 | ``` 29 | 30 | 31 | ## Example 32 | ```java 33 | // Base class Animal contains parent functionality. 34 | class Animal { 35 | 36 | // Stores animal name. 37 | public String name = "Base Animal"; 38 | 39 | // Different animals make different sounds so we declaring base class method 40 | // makeSound() to easily override from child classes. 41 | public void makeSound() { 42 | System.out.println("Base Animal Sound..."); 43 | } 44 | } 45 | 46 | // Dog class (subclass) extends the Animal class. 47 | class Dog extends Animal { 48 | 49 | // Constructor that changes the parent name attribute by using 50 | // "super" keyword. 51 | Dog() { 52 | super.name = "Dog"; 53 | } 54 | 55 | // Dogs makes sound "Bow Bow". For that we're overriding parent class 56 | // makeSound() method. 57 | public void makeSound() { 58 | System.out.println("Bow Bow..."); 59 | } 60 | } 61 | 62 | // Cat class (subclass) extends the Animal class. 63 | class Cat extends Animal { 64 | 65 | // Constructor that changes the parent name attribute by using 66 | // "super" keyword. 67 | Cat() { 68 | super.name = "Cat"; 69 | } 70 | 71 | // Overriding parent makeSound() with "Meow Meow". 72 | public void makeSound() { 73 | System.out.println("Meow Meow..."); 74 | } 75 | } 76 | 77 | // public class contains main method. 78 | public class SuperclassSubclass { 79 | 80 | // main() method contains driver code. 81 | public static void main(String[] args) { 82 | 83 | // Objects creation for animals. 84 | Animal baseAnimal = new Animal(); 85 | Dog doggy = new Dog(); 86 | Cat kitty = new Cat(); 87 | 88 | // Calling makeSound() of all Animals. 89 | System.out.println("Making Sounds..."); 90 | 91 | System.out.print("From " + baseAnimal.name + ", "); 92 | baseAnimal.makeSound(); 93 | 94 | System.out.print("From " + doggy.name + ", "); 95 | doggy.makeSound(); 96 | 97 | System.out.print("From " + kitty.name + ", "); 98 | kitty.makeSound(); 99 | } 100 | } 101 | ``` 102 | > *Note: You can find the above code on [SuperclassSubclass.java](SuperclassSubclass.java).* 103 | 104 | 105 | ## Output 106 | ``` 107 | Making Sounds... 108 | From Base Animal, Base Animal Sound... 109 | From Dog, Bow Bow... 110 | From Cat, Meow Meow... 111 | ``` 112 | 113 | 114 | ## Author 115 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 116 | -------------------------------------------------------------------------------- /Week-3/Declaring Exceptions/Declaring Exceptions.md: -------------------------------------------------------------------------------- 1 | # Declaring Exceptions 2 | > An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. 3 | > An exception can occur for many different reasons. Following are some scenarios where an exception occurs. 4 | * A user has entered an invalid data. 5 | * A file that needs to be opened cannot be found. 6 | * A network connection has been lost in the middle of communications or the JVM has run out of memory. 7 | #### How JVM handle an Exception? 8 | * The run-time system searches the call stack to find the method that contains block of code that can handle the occurred exception. The block of the code is called Exception handler. 9 | * The run-time system starts searching from the method in which exception occurred, proceeds through call stack in the reverse order in which methods were called. 10 | * If it finds appropriate handler then it passes the occurred exception to it. Appropriate handler means the type of the exception object thrown matches the type of the exception object it can handle. 11 | * If run-time system searches all the methods on call stack and couldn’t have found the appropriate handler then run-time system handover the Exception Object to default exception handler , which is part of run-time system. This handler prints the exception information in the following format and terminates program abnormally. 12 | ### Types of exceptions 13 | > Java defines several types of exceptions that relate to its various class libraries. Java also allows users to define their own exceptions. 14 | ![](https://media.geeksforgeeks.org/wp-content/cdn-uploads/Exceptions-in-Java.png) 15 | #### Built-in Exceptions 16 | 17 | >Built-in exceptions are the exceptions which are available in Java libraries. These exceptions are suitable to explain certain error situations. Below is the list of important built-in exceptions in Java. 18 | 19 | `ArithmeticException`
20 | > It is thrown when an exceptional condition has occurred in an arithmetic operation. 21 | 22 | `ArrayIndexOutOfBoundsException`
23 | > It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array. 24 | 25 | 26 | `ClassNotFoundException`
27 | > This Exception is raised when we try to access a class whose definition is not found 28 | 29 | `FileNotFoundException`
30 | > This Exception is raised when a file is not accessible or does not open. 31 | 32 | `IOException`
33 | > It is thrown when an input-output operation failed or interrupted 34 | 35 | `InterruptedException`
36 | > It is thrown when a thread is waiting, sleeping, or doing some processing, and it is interrupted. 37 | 38 | `NoSuchFieldException`
39 | > It is thrown when a class does not contain the field (or variable) specified 40 | 41 | `NoSuchMethodException`
42 | > It is thrown when accessing a method which is not found. 43 | 44 | `NullPointerException `
45 | > This exception is raised when referring to the members of a null object. Null represents nothing 46 | 47 | `NumberFormatException `
48 | > This exception is raised when a method could not convert a string into a numeric format. 49 | 50 | `RuntimeException `
51 | > This represents any exception which occurs during runtime. 52 | 53 | `StringIndexOutOfBoundsException `
54 | > It is thrown by String class methods to indicate that an index is either negative or greater than the size of the string 55 | #### Example of Exceptions Declartion 56 | ``` 57 | //Java program to demonstrate FileNotFoundException 58 | import java.io.File; 59 | import java.io.FileNotFoundException; 60 | import java.io.FileReader; 61 | class File_notFound_Demo { 62 | 63 | public static void main(String args[]) { 64 | try { 65 | 66 | // Following file does not exist 67 | File file = new File("E://file.txt"); 68 | 69 | FileReader fr = new FileReader(file); 70 | } catch (FileNotFoundException e) { 71 | System.out.println("File does not exist"); 72 | } 73 | } 74 | } 75 | 76 | ``` 77 | -------------------------------------------------------------------------------- /Week-2/Protection of data/Protection_of_data.md: -------------------------------------------------------------------------------- 1 | # Protection of data 2 | 3 | ### Classes, variables, methods, and constructors can be given different levels of access using Java's various access modifiers for protection of data. 4 | 5 | - ### **Each of the four levels of access is described below.** 6 | - This is the default visibility setting for a given package. There is no need for modifiers. 7 | - Only class members can see this (private). 8 | - Everyone can see you (public). 9 | - The package and all its subclasses can see this information (protected). 10 | - ### **Default Access Modifier - No Keyword** 11 | 12 | > We do not declare an access modifier explicitly for a class, field, method, etc. This is known as a default access modifier. Unprotected variables and methods are available to any other class in the same package. In an interface, the fields and methods are implicitly public static final. Modifierless variables and methods can be declared, as shown in the examples below. 13 | 14 | ```java 15 | String DevIncept= "contribution"; 16 | int DevIncept() 17 | { 18 | return 0; 19 | } 20 | ``` 21 | 22 | - ### **Private Access Modifier - Private** 23 | 24 | > It is only possible to access methods, variables and constructors that are declared private from within the class that they are part of. As the most restrictive access level, the Private Access Modifier (PAM) is enabled. It is impossible for classes and interfaces to be private. Public getter methods in a class allow access to private variables. The private modifier is the primary means by which an object isolates itself from the outside world and hides data. 25 | 26 | ```java 27 | public class DevIncept 28 | { 29 | private String s; 30 | public String OpenSource() 31 | { 32 | return this.s; 33 | } 34 | public void Contributions(String s) 35 | { 36 | this.s = s; 37 | } 38 | } 39 | ``` 40 | 41 | > This class's format variable is private, so other classes cannot directly access or modify its value. For this reason, we defined two public methods: *OpenSource()* and *Contributions(String)*. 42 | 43 | - ### **Public Access Modifier - Public** 44 | 45 | > Any class can access a public class, method, function Object(), interface, etc. Any class in the Java universe can access fields, methods, and blocks declared in public classes. As long as our public class is not in the same package as our private class, it still needs to be imported. By virtue of class inheritance, subclasses inherit all public methods and variables from their parent class. Publicity is required for the *main()* method in an application. As a result, the class could not be run by a Java interpreter. 46 | 47 | ```java 48 | public static void main(String[] arguments) 49 | { 50 | // ... 51 | } 52 | ``` 53 | 54 | - ### **Protected Access Modifier - Protected** 55 | 56 | > The protected members of a superclass can only be accessed by subclasses of the superclass or by classes within the superclass' package. On classes and interfaces, the protected access modifier cannot be applied. Methods and fields in an interface, however, cannot be declared protected. In contrast, protected access prevents non-related classes from trying to access the helper method or variable. 57 | 58 | ```java 59 | class DevIncept 60 | { 61 | protected boolean Suvra(Speaker DI) 62 | { 63 | // code block 64 | } 65 | } 66 | class Contributions extends DevIncept 67 | { 68 | boolean Shaw(Speaker DI) 69 | { 70 | // code block 71 | } 72 | } 73 | ``` 74 | 75 | > *DevIncept* is the only class that can access the *Suvra()* method if it is declared private. As soon as we define it as public, it will be available to the entire outside world for use. As a result of using the protected modifier, this method will only be available to its subclass. 76 | 77 | - ### **When it comes to inheritance, the following rules are enforced:** 78 | - Private methods are not inherited at all, so there is no rule for them. 79 | - Methods declared public in a superclass must also be declared public in all subclasses. 80 | - Methods declared protected in a superclass must be public or protected in subclasses; they cannot be private 81 | -------------------------------------------------------------------------------- /Week-3/Queue Implementation Classes/README.md: -------------------------------------------------------------------------------- 1 | # Queue Implementation Classes 2 | 3 | 4 | ## Queue 5 | Queue is an abstract data type, and it is a linear data structure. It works on the 6 | principle FIFO (First In First Out). In Java, `java.util` package contains the interface 7 | of Queue. 8 | The Queue implementations are grouped into general-purpose and concurrent implementations. 9 | 10 | 11 | ## General-Purpose Queue Implementations 12 | - `LinkedList` implements the `Queue` interface, providing first in, first out (FIFO) 13 | queue operations for `add`, `poll`, and so on. 14 | - `PriorityQueue` class is a priority queue based on the heap data structure. This queue 15 | orders elements according to the order specified at construction time, which can be the 16 | elements natural ordering or the ordering imposed by an explicit `Comparator`. 17 | 18 | 19 | ## Concurrent Queue Implementations 20 | The `java.util.concurrent` package contains a set of synchronized Queue interfaces and 21 | classes. 22 | `BlockingQueue` extends Queue with operations that wait for the queue to become nonempty 23 | when retrieving an element and for space to become available in the queue when storing an 24 | element. This interface is implemented by the following classes: 25 | - `LinkedBlockingQueue` \- an optionally bounded FIFO blocking queue backed by linked nodes. 26 | - `ArrayBlockingQueue` \- a bounded FIFO blocking queue backed by an array. 27 | - `PriorityBlockingQueue` \- an unbounded blocking priority queue backed by a heap. 28 | - `DelayQueue` \- a time-based scheduling queue backed by a heap. 29 | - `SynchronousQueue` \- a simple rendezvous mechanism that uses the `BlockingQueue` interface. 30 | 31 | 32 | ## Methods in Queue Interface 33 | | Methods | Description | 34 | |---|---| 35 | | `add(element)` | Adds the given element at the end of the queue. | 36 | | `isEmpty()` | Gives `true` when queue is empty, otherwise `false`. | 37 | | `peek()` | Returns the top element in the Queue without removing. | 38 | | `poll()` | Removes top given element in the queue. Returns `null` if queue is empty. | 39 | | `remove()` | Removes the top element in the queue. Throws `NoSuchElementException` when the queue is empty. | 40 | | `size()` | Returns the size of the queue. | 41 | 42 | 43 | ## Example 44 | Here we can see `LinkedList` implementation, 45 | ```java 46 | // Importing Queue and LinkedList. 47 | import java.util.Queue; 48 | import java.util.LinkedList; 49 | 50 | // Main class contains the main method. 51 | public class QueueExample { 52 | 53 | // main() method contains the example. 54 | public static void main(String[] args) { 55 | 56 | // Creating Queue with List. 57 | Queue q = new LinkedList<>(); 58 | 59 | // Adding elements in queue. 60 | for (int i = 1; i < 8; ++i) { 61 | q.add(i); 62 | } 63 | 64 | // Printing queue. 65 | System.out.println("Queue: " + q.toString()); 66 | 67 | // Printing Peek element. 68 | System.out.println("Peek Element: " + q.peek()); 69 | 70 | // Printing the size and empty. 71 | System.out.println("Size: " + q.size() + ", Empty: " + q.isEmpty()); 72 | 73 | // Removing elements and printing it. 74 | System.out.println("Removing Elements: "); 75 | while (q.size() > 0) { 76 | System.out.println("Removed: " + q.poll()); 77 | } 78 | 79 | // Printing queue. 80 | System.out.println("Queue: " + q.toString()); 81 | 82 | // Printing the size and empty. 83 | System.out.println("Size: " + q.size() + ", Empty: " + q.isEmpty()); 84 | } 85 | } 86 | ``` 87 | > *Note: For compile & run use file [QueueExample.java](QueueExample.java).* 88 | 89 | 90 | ## Output 91 | ``` 92 | Queue: [1, 2, 3, 4, 5, 6, 7] 93 | Peek Element: 1 94 | Size: 7, Empty: false 95 | Removing Elements: 96 | Removed: 1 97 | Removed: 2 98 | Removed: 3 99 | Removed: 4 100 | Removed: 5 101 | Removed: 6 102 | Removed: 7 103 | Queue: [] 104 | Size: 0, Empty: true 105 | ``` 106 | 107 | 108 | **Reference:** 109 | https://docs.oracle.com/javase/tutorial/collections/implementations/queue.html 110 | ## Author 111 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 112 | -------------------------------------------------------------------------------- /Week-1/if , if- else , if-else-if/README.md: -------------------------------------------------------------------------------- 1 | # if 2 | * Java uses if statement , the execution should has to follow the specific condition . 3 | * If the condition specified is true then only the code of the block gets executed . 4 | * If the condition is false program stop . 5 | #### Syntax 6 | ```java 7 | if (condition) 8 | { 9 | ---- body statements ----- 10 | } 11 | ``` 12 | 13 | # if -else 14 | * Java uses if - else statement to perform conditions . 15 | *The condition specified is true then only the code of if block gets executed . Otherwise the code of else gets executed . 16 | * else statement does not work without existence of its corresponding if statement . 17 | * else gets executed if the condition is false . 18 | 19 | #### Syntax 20 | ```java 21 | if (condition) 22 | { 23 | ---- body statements ----- 24 | } 25 | else 26 | { 27 | ---- body statements ------ 28 | } 29 | ``` 30 | 31 | ## Example 32 | ```java 33 | import java.util.Scanner ; 34 | class Main //class name 35 | { 36 | public static void main(String[] args) { 37 | Scanner in =new Scanner(System.in); 38 | int i=in.nextInt(); // Accept the value of i from the user . 39 | if(i==1) // if condition i==1 40 | { 41 | System.out.println("Hello World ! "); // if the condition i==1 is true . 42 | } 43 | else // if the condition i==1 is false . 44 | { 45 | System.out.println(" Exit from else block . "); // This code will get executed . 46 | } 47 | } 48 | } 49 | ``` 50 | 51 | # if else -if 52 | * The java uses else if statements like another if condition . 53 | * It's used in the program when if statement having multiple decisions. 54 | * The condition of if is true then only the execution of that block inside the else can be there . 55 | 56 | #### Syntax 57 | ```java 58 | if (condition 1) 59 | { 60 | ---- body statements ----- 61 | } 62 | else 63 | { 64 | if (condition 2) 65 | { 66 | ------ body statements ------ 67 | } 68 | } 69 | ``` 70 | 71 | 72 | 73 | ```java 74 | if (condition 1) 75 | { 76 | ---- body statements ----- 77 | } 78 | else 79 | { 80 | if (condition 2) 81 | { 82 | ------ body statements ------ 83 | } 84 | else 85 | { 86 | ------ body statements ------ 87 | } 88 | } 89 | ``` 90 | 91 | 92 | ## Example 93 | ```java 94 | import java.util.Scanner ; //library 95 | class else-if //class name 96 | { 97 | public static void main(String[] args) { 98 | Scanner in =new Scanner(System.in); 99 | int i=in.nextInt(); // Accept the value of i from the user . 100 | if(i==2) // if condition i==2 . 101 | { 102 | System.out.println("Hello World ! "); // if the condition i==2 is true . 103 | } 104 | else // if the condition i==2 is false . 105 | { // It will enter the else block . 106 | if (i<10) // The specified codition has to be true to perform the following execution . 107 | { 108 | System.out.println(" Exit from else - if block . "); // This will be executed if the value of i is . 109 | // lesser than 10 only . 110 | } 111 | } //if the condition i<10 is not true . No execution . 112 | } 113 | } 114 | ``` 115 | # if -else - if 116 | 117 | * Java uses else if to add multiple conditions . 118 | * Code block gets executed only if the condition is true . 119 | * It is used to test the conditions . 120 | * You can use multiple else- if conditions . Before using else-if , condition of if has to be there . 121 | * At the end if neither of the conditions are true , execution of else block 122 | can be there . 123 | 124 | #### Syntax 125 | ```java 126 | if (condition 1) 127 | { 128 | ---- body statements ----- 129 | } 130 | else if (condition 2) 131 | { 132 | 133 | ------ body statements ------ 134 | } 135 | else if (condition 3) 136 | { 137 | ------ body statements ------ 138 | } 139 | else // if conditions are found false , the execution of else will be there . 140 | { 141 | ------ body statements ------ 142 | } 143 | ``` 144 | 145 | ##### Author 146 | ###### [MayuriMahajan](https://github.com/Mayuri-cell) 147 | -------------------------------------------------------------------------------- /Week-3/The_Collections_Framework/The_Collections_Framework.md: -------------------------------------------------------------------------------- 1 | # The Collections Framework 2 | The `Collection in Java` is a framework that provides an architecture to store and manipulate the group of objects. 3 | Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. 4 | 5 | Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet). 6 | 7 | ## What is Collection in Java 8 | A Collection represents a single unit of objects, i.e., a group. 9 | 10 | ## What is a framework in Java 11 | It provides readymade architecture. 12 | It represents a set of classes and interfaces. 13 | It is optional. 14 | ## What is Collection framework 15 | The Collection framework represents a unified architecture for storing and manipulating a group of objects. It has: 16 | 17 | 1. Interfaces and its implementations, i.e., classes 18 | 2. Algorithm 19 | 20 | ## Hierarchy of Collection Framework 21 | Let us see the hierarchy of Collection framework. The java.util package contains all the classes and interfaces for the Collection framework. 22 | ![](https://static.javatpoint.com/images/java-collection-hierarchy.png) 23 | 24 | ## Methods of Collection interface 25 | 26 | 27 | |No.| Method |Description| 28 | |----|-------|---------| 29 | |1 |public boolean add(E e)| It is used to insert an element in this collection.| 30 | |2 |public boolean addAll(Collection c)| It is used to insert the specified collection elements in the invoking collection.| 31 | |3 |public boolean remove(Object element)| It is used to delete an element from the collection.| 32 | |4 |public boolean removeAll(Collection c)| It is used to delete all the elements of the specified collection from the invoking collection.| 33 | |5 |default boolean removeIf(Predicate filter)| It is used to delete all the elements of the collection that satisfy the specified predicate.| 34 | |6 |public boolean retainAll(Collection c)| It is used to delete all the elements of invoking collection except the specified collection.| 35 | |7 |public int size()| It returns the total number of elements in the collection.| 36 | |8 |public void clear()| It removes the total number of elements from the collection.| 37 | |9 |public boolean contains(Object element)| It is used to search an element.| 38 | |10 |public boolean containsAll(Collection c)| It is used to search the specified collection in the collection.| 39 | |11 |public Iterator iterator() |It returns an iterator.| 40 | |12 |public Object[] toArray() |It converts collection into array.| 41 | |13|public T[] toArray(T[] a)| It converts collection into array. Here, the runtime type of the returned array is that of the specified array.| 42 | |14|public boolean isEmpty()| It checks if collection is empty.| 43 | |15 |default Stream parallelStream()| It returns a possibly parallel Stream with the collection as its source.| 44 | |16 |default Stream stream() |It returns a sequential Stream with the collection as its source.| 45 | |17 |default Spliterator spliterator()| It generates a Spliterator over the specified elements in the collection.| 46 | |18 |public boolean equals(Object element)| It matches two collections.| 47 | |19 |public int hashCode()| It returns the hash code number of the collection.| 48 | 49 | ## Iterator interface 50 | Iterator interface provides the facility of iterating the elements in a forward direction only. 51 | Methods of Iterator interface 52 | There are only three methods in the Iterator interface. They are: 53 | 54 | |No.|Method |Description| 55 | |---|------|-------| 56 | |1 |public boolean hasNext() |It returns true if the iterator has more elements otherwise it returns false.| 57 | |2 |public Object next() |It returns the element and moves the cursor pointer to the next element.| 58 | |3 |public void remove() |It removes the last elements returned by the iterator. It is less used.| 59 | 60 | ## Iterable Interface 61 | The Iterable interface is the root interface for all the collection classes. The Collection interface extends the Iterable interface and therefore all the subclasses of Collection interface also implement the Iterable interface. 62 | 63 | It contains only one abstract method. i.e., 64 | 65 | > Iterator iterator() 66 | It returns the iterator over the elements of type T. 67 | 68 | ## Conclusion: 69 | Collection is a Framework that contains all the important data structors and the user don't need to code it from the scratch. 70 | -------------------------------------------------------------------------------- /Week-1/Non-Primitive Datatypes/Non-Primitive Datatypes.md: -------------------------------------------------------------------------------- 1 | # Non-Primitive Data types in Java 2 | 3 | ### We know that data types in Java have been categorized into primitive and non-primitive data types. 4 | 5 | ### Primitive data types are those data types that are used by programmers when creating variables in their programs. It is used to store a single entity (value). For example, we take an “int” type that can store only one integer value. “boolean” can store only one value either true or false. There are eight primitive data types in Java. They are boolean, char, byte, short, int, long, float, and double. 6 | 7 | ### Another data type supported by Java known as non-primitive data types or advanced data types. They are not predefined in java like primitive data types. These data types are used to store a group of values or several values. 8 | ### For example, we take an array. It can store a group of values. Similarly, another example is a class that can store different values. Therefore, these data types are also known as advanced data types in Java. When we define a variable of non-primitive data types, it references a memory location where data is stored in the heap memory. That is, it references a memory where an object is actually placed. Therefore, a non-primitive data type variable is also called referenced data type in Java or simply object reference variable. This object reference variable lives on the stack memory and the object to which it points always lives on the heap memory. The stack holds a pointer to the object on the heap. In Java programming, all non-primitive data types are simply called objects which are created by instantiating a class. 9 | 10 | ## Key points: 11 | 1. The default value of any reference variable is null. 12 | 2. Whenever you will pass a non-primitive data type to a method, you are actually passing an address of that object where data is stored. 13 | 14 | ## How to Declare Non-primitive type Data types in Java? 15 | ### In primitive data type, we declare like this: 16 | `int p = 100;` 17 | > // p is an int data type which can store the only integer value. 18 | 19 | ### In reference data types, an object reference variable ( or simply called reference variable) is declared just like we declare a primitive variable. 20 | `School sc;` 21 | ### Here, School is the name of a class, and “sc” is the name of a reference variable. No object has yet been created. 22 | 23 | ### We create an object of a class using new keyword. For example, the following statement creates an object of a class School and assigns it to the reference variable “sc”. 24 | `sc = new School();` 25 | #### where, 26 | 27 | #### School ➞ name of the class. 28 | #### sc ➞ Object reference. An object reference is a variable that stores the address of an object in the computer’s memory. An object represents an instance through which we can access member. 29 | 30 | #### School() ➞ Constructor of the class. The constructor of a class is generally used to initialize an object. 31 | #### new ➞ is a special keyword that creates the memory in java. 32 | 33 | #### Now an object of class School lives on the heap and the object reference variable “sc” refers to it. 34 | 35 | ### The declaration of an object reference variable, object creation, and initialization of reference variable can also be done in a single line statement like this: 36 | `School sc = new School();` 37 | 38 | ## Types of Non-primitive Data types 39 | ### There are four types of non-primitive data types in Java. They are as follows: 40 | 1. **Class and objects**: Every class is data type and it is also considered as user-defined data types. This is because a user creates a class. 41 | 42 | 2. **String**: A string represents a sequence of characters like India, ABC123, etc. The simplest way to create a string object is by storing sequence of characters into string type variable like this: 43 | String str = “Universe”; 44 | Here, string type variable str contains “Universe”. A string is also a class. 45 | 46 | 3. **Array**: An array in java is an object which is used to store multiple variables of the same type. These variables can be primitive or non-primitive data types. 47 | The example of declaring an array variable of primitive data type int is as follows: int [ ] scores; 48 | The example of declaring an array variable of non-primitive data type is Student [ ] students; // Student is a name of class. 49 | 50 | 4. **Interface**: An interface is declared like a class but the only difference is that it contains only final variables and method declarations. It is a fully abstract class. 51 | 52 | 53 | ```python 54 | 55 | ``` 56 | -------------------------------------------------------------------------------- /Week-2/The JDK Directory Structure/The JDK Directory Structure.md: -------------------------------------------------------------------------------- 1 | # The JDK Directory Structure 2 | > JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to develop java applications and applets. 3 | >
The JDK allows developers to create Java programs that can be executed and run by the `JVM` and `JRE`. 4 | >
The JVM is the Java platform component that executes programs. 5 | >
The JRE is the on-disk part of Java that creates the JVM. 6 | >
JDK and JRE are different because the JDK is a package of tools for developing Java-based software and the JRE is a package of tools for running Java code. 7 | `So the topic is the organization of files into a hierarchy of folders of Java Development Kit.` 8 | ## Development Files and Directories 9 | Assuming the JDK software is installed at `/jdk1.8.0`, here are some of the most important directories: 10 | 11 | | Directories | Description | 12 | | ------------ | ----------- | 13 | | /jdk1.8.0 | Root directory of the JDK software installation. Contains copyright, license, and README files. Also contains `src.zip`, the archive of source code for the Java platform.| 14 | | /jdk1.8.0/bin | Executables for all the development tools contained in the JDK. The `PATH` environment variable should contain an entry for this directory. | 15 | | /jdk1.8.0/lib | Files used by the development tools. Includes `tools.jar`, which contains non-core classes for support of the tools and utilities in the JDK. Also includes `dt.jar`, the `DesignTime` archive of `BeanInfo` files that tell interactive development environments (IDEs) how to display the Java components and how to let the developer customize them for an application. | 16 | | /jdk1.8.0/jre | Root directory of the Java Runtime Environment (JRE) used by the JDK development tools. The runtime environment is an implementation of the Java platform. This is the directory referred to by the `java.home` system property. | 17 | | /jdk1.8.0/jre/bin | Executable files for tools and libraries used by the Java platform. The executable files are identical to files in `/jdk1.8.0/bin`. The java launcher tool serves as an application launcher (and replaced the earlier `jre` command that shipped with 1.1 releases of the JDK). This directory does not need to be in the `PATH` environment variable. | 18 | | /jdk1.8.0/jre/lib | Code libraries, property settings, and resource files used by the JRE. For example `rt.jar` contains the bootstrap classes, which are the run time classes that comprise the Java platform core API, and `charsets.jar` contains the character-conversion classes. Aside from the `ext` subdirectory, there are several additional resource subdirectories not described here | 19 | | /jdk1.8.0/jre/lib/ext | Default installation directory for extensions to the Java platform. This is where the `JavaHelp` JAR file goes when it is installed, for example. This directory includes the `jfxrt.jar` file, which contains the JavaFX runtime libraries and the `localedata.jar` file, which contains the locale data for the `java.text` and `java.util packages`. | 20 | | /jdk1.8.0/jre/lib/security | Contains files used for security management. These include the security policy `java.policy` and security properties `java.security` files. | 21 | | /jdk1.8.0/jre/lib/sparc | Contains the `.so` (shared object) files used by the Oracle Solaris release of the Java platform. | 22 | | /jdk1.8.0/jre/lib/sparc/client | Contains the `.so` file used by the Java HotSpot VM client, which is implemented with Java HotSpot VM technology. This is the default Java Virtual Machine (JVM). | 23 | | /jdk1.8.0/jre/lib/sparc/server | Contains the `.so` file used by the Java HotSpot VM server. | 24 | | /jdk1.8.0/jre/lib/applet | JAR files that contain support classes for applets can be placed in the `lib/applet/` directory. This reduces startup time for large applets by allowing applet classes to be preloaded from the local file system by the applet class loader and provides the same protections as though they had been downloaded over the Internet. | 25 | | /jdk1.8.0/jre/lib/fonts | Font files used by the platform. | 26 | 27 | ## Additional Files and Directories 28 | Here additional means the directory structure for Java source code, C header files, and other additional directories and files. 29 | 30 | | Directories | Description | 31 | | ------------ | ----------- | 32 | | /jdk1.8.0/src.zip | Archive that contains the source code for the Java platform. | 33 | | /jdk1.8.0/db | Contains Java DB. | 34 | | /jdk1.8.0/include | C-language header files that support native-code programming with the Java Native Interface and the Java Virtual Machine (JVM) Debugger Interface. | 35 | | /jdk1.8.0/man | Contains man pages for the JDK tools. | 36 | -------------------------------------------------------------------------------- /Week-1/Assignment Operator/README.md: -------------------------------------------------------------------------------- 1 | # Assignment Operator 2 | 3 | We can separate Assignment operator into 2 categories, 4 | - [Simple Assignment Operator](#simple-assignment-operator "Simple Assignment Operator") 5 | - [Compound Assignment Operator](#compound-assignment-operator "Compound Assignment Operator") 6 | 7 | 8 | ## Simple Assignment Operator 9 | Simple assignment operator used to assign a value in a variable. 10 | It is used to assign the value on its right to the operand on its left. 11 | 12 | ### Syntax 13 | ```java 14 | VariableName = Value; 15 | ``` 16 | 17 | 18 | ## Compound Assignment Operator 19 | Compound assignment operator is also known as shorthand operator. 20 | It provides a shorter way to assign an expression to a variable. 21 | In other words it's do some operation before the assignment. 22 | 23 | ### Syntax 24 | ``` 25 | VariableName [CompoundAssignmentOperator] Value; 26 | ``` 27 | 28 | For example, 29 | ```java 30 | a += 1; 31 | // Above is equivalent to below. 32 | a = a + 1; 33 | // This type of equivalence also applies for below Compound Assignment 34 | // Operators in table. 35 | ``` 36 | 37 | ### Various Compound Assignment Operator 38 | | Operators | Description | 39 | |:---:|:---:| 40 | | += | Compound additional assignment operator. | 41 | | -= | Compound subtraction assignment operator. | 42 | | *= | Compound multiplication assignment operator. | 43 | | /= | Compound division assignment operator. | 44 | | %= | Compound Modulo Assignment operator. | 45 | | &= | Compound bitwise AND Assignment operator. | 46 | | \|= | Compound bitwise OR Assignment operator. | 47 | | ^= | Compound bitwise XOR assignment operator. | 48 | | >>= | Compound right shift assignment operator. | 49 | | >>>= | Compound right shift assignment (Unsigned) operator. | 50 | | <<= | Compound left shift assignment operator. | 51 | 52 | 53 | ## Example 54 | ```java 55 | // Public class contains main method. 56 | public class AssignmentOperator { 57 | 58 | // main() method to run this program. 59 | public static void main(String[] args) { 60 | 61 | // Declaring integer variable and initializing 10 62 | // using simple assignment operator. 63 | int Value = 1; 64 | System.out.println("Initially, Value = " + Value); 65 | 66 | // Compound additional assignment operator. 67 | Value += 10; 68 | System.out.println("Adding 10, Value = " + Value); 69 | 70 | // Compound subtraction assignment operator. 71 | Value -= 8; 72 | System.out.println("Subracting 8, Value = " + Value); 73 | 74 | // Compound multiplication assignment operator. 75 | Value *= 8; 76 | System.out.println("Multiplying 8, Value = " + Value); 77 | 78 | // Compound division assignment operator. 79 | Value /= 4; 80 | System.out.println("Dividing 4, Value = " + Value); 81 | 82 | // Compound Modulo Assignment operator. 83 | Value %= 99; 84 | System.out.println("Modulo with 99, Value = " + Value); 85 | 86 | // Compound bitwise AND Assignment operator. 87 | Value &= 7; 88 | System.out.println("Bitwise AND with 7, Value = " + Value); 89 | 90 | // Compound bitwise OR Assignment operator. 91 | Value |= 13; 92 | System.out.println("Bitwise OR with 13, Value = " + Value); 93 | 94 | // Compound bitwise XOR Assignment operator. 95 | Value ^= 17; 96 | System.out.println("Bitwise XOR with 17, Value = " + Value); 97 | 98 | // Changing value to 2021 for easy understanding of left and right shift. 99 | Value = 2021; 100 | 101 | // Compound left shift assignment operator. 102 | Value <<= 2; 103 | System.out.println("Left shit with 2, Value = " + Value); 104 | 105 | // Compound right shift assignment operator. 106 | Value >>= 2; 107 | System.out.println("Right shift with 2, Value = " + Value); 108 | 109 | // Compound right shift assignment (Unsigned) operator. 110 | Value >>>= 2; 111 | System.out.println("Right shift (unsigned) with 2, Value = " + Value); 112 | } 113 | } 114 | ``` 115 | > *Note: The above code added in [AssignmentOperator.java](AssignmentOperator.java) file.* 116 | 117 | 118 | ## Output 119 | ``` 120 | Initially, Value = 1 121 | Adding 10, Value = 11 122 | Subracting 8, Value = 3 123 | Multiplying 8, Value = 24 124 | Dividing 4, Value = 6 125 | Modulo with 99, Value = 6 126 | Bitwise AND with 7, Value = 6 127 | Bitwise OR with 13, Value = 15 128 | Bitwise XOR with 17, Value = 30 129 | Left shit with 2, Value = 8084 130 | Right shift with 2, Value = 2021 131 | Right shift (unsigned) with 2, Value = 505 132 | ``` 133 | 134 | 135 | ## Author 136 | [NanthaKumar](https://github.com/nknantha "Nanthakumar's Profile") 137 | -------------------------------------------------------------------------------- /Week-2/Inheritance/README.md: -------------------------------------------------------------------------------- 1 | ### Inheritance 2 | 3 | Inheritance in Java is a concept that acquires the properties from one class to other classes; for example, the relationship between father and son. 4 | 5 | - In Java, a class can inherit attributes and methods from another class. The class that inherits the properties is known as the sub-class or the child class. 6 | - The class from which the properties are inherited from is know as the superclass or the parent class. 7 | - Inheritance is also known as the IS-A relationship. 8 | 9 | ### Types of Inheritance in Java 10 | 11 | - Single Inheritance: Creating subclasses from a single base class is called single inheritance. 12 | - Multiple Inheritance: In this case, there is more than one superclass, and there can be one or more subclasses but it is not available in Java. 13 | - Multilevel Inheritance: In Multi-Level Inheritance in Java, a class extends to another class that is already extended from another class. 14 | - Hierarchical Inheritance: A class can have more than one class derived from it. So we have one base or superclass and more than one subclasses. 15 | Eg: of hierarchical inheritance, we can represent Animal class as a superclass and then have more than one animal like Cat, Dog, Cow, etc. derived from it. 16 | 17 | ### Super keyword 18 | - We can invoke the superclass variables. 19 | - We can invoke the superclass methods. 20 | - We can invoke the superclass constructor. 21 | 22 | 23 | ## Example 24 | ```java 25 | /******************************* Inheritance *******************************/ 26 | 27 | 28 | class superclass 29 | { 30 | // superclass data variables 31 | // superclass member functions 32 | } 33 | 34 | class subclass ​extends​ superclass 35 | { 36 | // subclass data variables 37 | // subclass member functions 38 | } 39 | 40 | // Eg of SIngle Inheritance 41 | class Parent // parent class 42 | { 43 | public void M1() 44 | { 45 | System.out.println("Parent Class Method"); 46 | } 47 | } 48 | class Child extends Parent // child class 49 | { 50 | public void M2() 51 | { 52 | System.out.println("Child Class Method"); 53 | } 54 | } 55 | class Inh_In_Java 56 | { 57 | public static void main(String[] args) 58 | { 59 | Child p=new Child(); // object of child class 60 | p.M1(); // methods 61 | p.M2(); 62 | } 63 | } 64 | 65 | 66 | 67 | // Eg of multilevel inheritance 68 | import java.util.*; 69 | import java.lang.*; 70 | import java.io.*; 71 | //parent class A 72 | class A 73 | { 74 | public void print_A() 75 | { 76 | System.out.print("SoftwareTestingHelp "); 77 | } 78 | } 79 | //Derived class B - intermediary 80 | class B extends A 81 | { 82 | public void print_B() 83 | { 84 | System.out.print("Java Series "); 85 | } 86 | } 87 | //Derived Class C 88 | class C extends B 89 | { 90 | public void print_C() 91 | { 92 | System.out.print("Tutorials"); 93 | } 94 | } 95 | 96 | public class Main 97 | { 98 | public static void main(String[] args) 99 | { 100 | C c_obj = new C(); //create Class C obj 101 | c_obj.print_A(); //call grandparent class method 102 | c_obj.print_B(); //call parent class method 103 | c_obj.print_C(); //call member method 104 | } 105 | } 106 | 107 | 108 | 109 | // Eg of hierarchical inheritance 110 | //class Parent 111 | class Parent 112 | { 113 | public void print_parent() 114 | { 115 | System.out.println("In ::Parent class"); 116 | } 117 | } 118 | //child1 class 119 | class Child1 extends Parent 120 | { 121 | public void print_child1() 122 | { 123 | System.out.println("In ::Child1 class"); 124 | } 125 | } 126 | //child2 class 127 | class Child2 extends Parent 128 | { 129 | public void print_child2() 130 | { 131 | System.out.println("In ::Child2 class"); 132 | } 133 | } 134 | //child3 class 135 | class Child3 extends Parent 136 | { 137 | public void print_child3() 138 | { 139 | System.out.println("In ::Child3 class"); 140 | } 141 | } 142 | 143 | public class Main 144 | { 145 | public static void main(String[] args) 146 | { 147 | Child1 ch1 = new Child1(); //create a Child1 class object 148 | ch1.print_child1(); //call its member method 149 | Child2 ch2 = new Child2(); //create a Child2 class object 150 | ch2.print_child2(); //call its member method 151 | Child3 ch3 = new Child3(); //create a Child3 class object 152 | ch3.print_child3(); //call its member method 153 | ch3.print_parent(); //call parent class method with any object 154 | } 155 | } 156 | 157 | ``` -------------------------------------------------------------------------------- /Week-2/operator Precedence/operator Precedence.md: -------------------------------------------------------------------------------- 1 | # Operator Precedence 2 | 3 | > Operator Precedence is a topic that is linked with Mathematics and Computer programming. The order of operation is collection of rules that reflect convertions about which procedures to perform first in order to evaluate a given mathematical expression. 4 | - ### **For Example (Mathematical point of View:)** 5 | ``` 6 | 4+2/2 must be equal to 5 7 | But 4+2/2 will be equal to 3 if we evaluate it left to right blindly(without) and it is an incorrect output 8 | ``` 9 | So there are some rules or the order of operations that has to be followed in order to get correct output also Known as ``Operator Precedence``. 10 | > **_NOTE_** : 11 | > - **parentheses()** and **Array subscript[]** have the highest precedence in Java 12 | > - **Addition and Subtraction** have higher precedence than the **Left shift and Right shift operators** 13 | 14 | Below is a table defined in which the lowest precedence operator show at the top of it. 15 | 16 | | Precedence | Operator | Associativity | 17 | | ----------- | ----------- | ----------- | 18 | | 1) | ``=`` ``+=`` ``-=`` ``*=`` ``/=`` ``%=`` | Right to left | 19 | | 2) | `` ?: `` | Right to left | 20 | | 3) | `` || `` | Left to right | 21 | | 4) | `` && `` | Left to right | 22 | | 5) | `` | `` | Left to right | 23 | | 6) | `` ^ `` | Left to right | 24 | | 7) | `` != `` `` == ``| Left to right | 25 | | 8) | `` < `` ``<=`` ``>`` ``>=`` ``instanceof (type comparision (objects only))`` | Left to right | 26 | | 9) | `` & ``| Left to right | 27 | | 10) | `` >> `` `` << `` ```>>>```| Left to right | 28 | | 11) | `` - `` `` + `` | Left to right | 29 | | 12) | `` * `` `` / `` `` % ``| Left to right | 30 | | 13) | `` - `` `` + `` `` ~ `` `` ! `` `` (type)-typecast `` | Left to right | 31 | | 14) | `` ++ `` ``--`` | Right to left | 32 | | 15) | ``.`` ``()`` ``[]`` | Left to Right | 33 | ### Precedence order 34 | > When two operators share a single operand, the operator having the highest precedence goes first. 35 | ``` 36 | For example, x + y * z is treated as x + (y * z), whereas x * y + z is treated as (x * y) + z because * operator has highest precedence in comparison of + operator. 37 | ``` 38 | ### Associativity 39 | > Associative is a concept related to the operators applied when two operators with the same precedence come in an expression. The associativity concept is very helpful to goes from that situation. Suppose we have an expression `a + b - c` **(+ and - operators have the same priority)**, and this expression will be treated as `(a + (b - c))` because these operators are right to left-associative. On the other hand, `a+++--b+c++` will be treated as `((a++)+((--b)+(c++)))` because the unary post-increment and decrement operators are right to left-associative. 40 | 41 | > An example is defined below to understand how an expression is evaluated using precedence order and associativity? 42 | 43 | | Steps | Expression | result | Description | 44 | | ----------- | -------------- | ------ |----- | 45 | | 1) | x = 4 / 2 + 8 * 4 - ( 5+ 2 ) % 3 | x = 4 / 2 + 8 * 4 - 7 % 3 | `In the above expression, the highest precedence operator is (). So, the parenthesis goes first and calculates first.` | 46 | | 2) | x = 4 / 2 + 8 * 4 - 7 % 3 | x = 2 + 8 * 4 - 7 % 3 | `Now, /, * and % operators have the same precedence and highest from the + and - Here, we use the associativity concept to solve them. The associative of these operators are from left to right. So, / operator goes first and then * and % simultaneously.` | 47 | | 3) | x = 2 + 32 - 1 | x = 34 - 1
x = 33 | `Now, + and - operators both also have the same precedence, and the associativity of these operators lest to the right. So, + operator will go first, and then - will go.` | 48 | 49 | ### HighestPrecedence.java 50 | ``` 51 | 52 | //java Source Code 53 | //import classes 54 | import java.util.*; 55 | 56 | //creating HighestPrecedence class to evaluate the expression 57 | public class HighestPrecedence { 58 | 59 | //main() method starts 60 | public static void main(String[] args) { 61 | 62 | //initialize variables with default values 63 | int x = 2; 64 | int y = 5; 65 | int z = 12; 66 | 67 | //calculating exp1, exp2, and exp3 68 | int exp1 = x +(z/x+(z%y)*(z-x)^2); 69 | int exp2 = z/x+y*x-(y+x)%z; 70 | int exp3 = 4/2+8*4-(5+2)%3; 71 | 72 | //printing the result 73 | System.out.println(exp1); 74 | System.out.println(exp2); 75 | System.out.println(exp3); 76 | } 77 | } 78 | 79 | ``` 80 | ### Output 81 | ![output](https://static.javatpoint.com/core/images/highest-precedence-in-java2.png) 82 | ### Related Topics :
83 | - 84 | - 85 | -------------------------------------------------------------------------------- /Week-3/Memory Allocation in Java/README.md: -------------------------------------------------------------------------------- 1 | # Memory Allocation in Java. 2 | 3 | 4 | ## Memory Allocation 5 | The memory needed by a process or thread is being allocated in main memory is known as 6 | memory allocation. Java Virtual Machine has 2 types of memories Stack Memory and 7 | Heap Space. 8 | 9 | 10 | ## Stack Memory 11 | Stack memory is used for static memory and thread execution. It contains method specific 12 | values and references of objects in heap. It works on the principle of FIFO (Fist In 13 | First Out). Whenever a method is invoked, a new block of memory get allocated in stack 14 | memory, it holds local primitive values and reference of other objects. After the method 15 | ends, the block of memory is released for another process. 16 | 17 | ### Characteristics of Stack Memory 18 | - It grows and shrinks as new methods are called and returned respectively. 19 | - Variables of the method is only live in stack until it get finished. 20 | - It gets auto-allocation and deallocation on a method execution. 21 | - If the memory is full, JVM throws `java.lang.StackOverFlowError`. 22 | - Accessing memory is faster than heap. 23 | - The memory is thread safe as each thread operates in its own Stack. 24 | 25 | ### Methods in Stack Class 26 | - **Object push(Object element):** Pushes given element onto the stack. 27 | - **Object pop():** Removes and return the top element in the stack. If stack 28 | is empty it throws `EmptyStackException`. 29 | - **Object peek():** Returns the top element in the stack, without removing. 30 | - **Boolean empty():** Returns `true` if stack is empty, else it gives `false`. 31 | - **int search(Object element):** Searches given element inside stack and gives 32 | the position of that element inside stack. If element not found it gives `-1`. 33 | 34 | 35 | ## Heap Space 36 | This memory used to allocate memory for objects in runtime. It is mainly used in 37 | runtime of a program. The discrete function like Garbage Collection, keeps 38 | flushing the memory used by previous objects that hold no reference. The memory 39 | allocated for objects can accessible across all programs. 40 | 41 | ### Characteristics of Heap Space 42 | - Heap space accessed via complex memory allocation techniques that includes 43 | Young Generation, Old or Tenured Generation and Permanent Generation. 44 | - If heap space becomes full, java throws `java.lang.OutOfMemoryError`. 45 | - Access in heap space comparatively slower than Stack Memory access. 46 | - It needs Garbage Collector functions to deallocate memory of unused objects 47 | and keep the efficiency of memory usage. 48 | - It is not thread safe and needs to be guarded by properly synchronizing 49 | the code. 50 | 51 | 52 | ## Stack vs Heap 53 | | Stack | Heap | 54 | | :---------------------------------------------: | :-----------------------------------------: | 55 | | Memory allocation happens in contiguous manner. | Memory allocation happens in random manner. | 56 | | Memory managed automatically by compiler. | Memory managed manually by programmer. | 57 | | Access is fast. | Access is slow. | 58 | | Hard to resize. | Easy to resize. | 59 | | Access only by threads. | Access across applications. | 60 | | Memory based on FIFO. | Memory based on Generations. | 61 | | Static memory allocation. | Dynamic memory allocation. | 62 | 63 | 64 | ## Example 65 | ```java 66 | // Class Fruit contains attributes and a method. 67 | class Fruit { 68 | 69 | // While creating object for this class it acquire its 70 | // memory for attributes and methods in its own Stack memory. 71 | 72 | // Creating attributes. 73 | String color = "Red"; 74 | int weight = 2; 75 | 76 | // method toString() gives color and weight. 77 | public String toString() { 78 | return "Color: " + this.color + ", Weight: " + 2; 79 | } 80 | } 81 | 82 | // Main class MemoryAllocation. 83 | public class MemoryAllocation { 84 | 85 | // main() method contains the driver code. 86 | public static void main(String[] args) { 87 | 88 | // Here we're creating object for Fruit class. 89 | // The object created and stored in Heap memory while 90 | // their attributes, methods are stored in Stack memory. 91 | 92 | // Creating object for fruit. 93 | System.out.println("Creating object for Fruit..."); 94 | Fruit apple = new Fruit(); 95 | 96 | // Printing apple object. 97 | System.out.println("Object Apple: " + apple.toString()); 98 | } 99 | } 100 | ``` 101 | > *You can find the above code on [MemoryAllocation.java](MemoryAllocation.java).* 102 | 103 | 104 | ## Output 105 | ``` 106 | Creating object for Fruit... 107 | Object Apple: Color: Red, Weight: 2 108 | ``` 109 | 110 | 111 | ## Author 112 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 113 | -------------------------------------------------------------------------------- /Week-2/Rules of Method Overloading and Overriding/README.md: -------------------------------------------------------------------------------- 1 | # Rules of Method Overloading and Overriding 2 | 3 | 4 | ## Method Overloading 5 | If a class have more than 1 method with same name with different signature is 6 | known as method overloading. It increases the productivity and provides greater 7 | readability. 8 | 9 | There are 2 ways to overload a method, 10 | - By changing the number of parameter. 11 | - By changing the datatype of parameter. 12 | 13 | ### Rules for Method Overloading 14 | 15 | #### 1. Method Signature 16 | The first rule is to change the method signature. The method signature can be 17 | changed by changing the number of parameters or changing the datatype of the 18 | parameters or changing the position of parameters. You can use any of these 19 | combinations to overload a method. 20 | 21 | #### 2. Method Return Type 22 | By changing the return type we cannot overload a method, because of return type 23 | not a part of a method signature. If you tried to overload with this, the compiler 24 | will throw error. 25 | 26 | 27 | ## Method Overriding 28 | An inherited class or an implemented interface methods have another definition 29 | in its child class known as method overloading. While creating object for child 30 | class, the same methods in parent class will be overridden by the child class 31 | methods. It is used for runtime polymorphism. 32 | 33 | ### Rules for Method Overriding 34 | 35 | #### 1. Location 36 | A method can only be overridden in subclass, not in the same class. The subclass 37 | must inherit the parent class contains the method to be overridden. 38 | 39 | #### 2. Exception 40 | Overriding method cannot throw any checked Exception which is in the hierarchy, 41 | than checked Exception thrown by the overridden method. 42 | 43 | #### 3. Visibility 44 | The overriding method cannot reduce the accessibility of the overridden method. 45 | i.e. the private overriding method cannot override a public overridden method. 46 | You cannot reduce the visibility of inherited method of a class. 47 | 48 | #### 4. Accessibility 49 | Overriding method can increase the visibility of overridden method. This is 50 | opposite to the rule Visibility. i.e. the overridden method declared as private 51 | then the overriding method can be protected or public. 52 | 53 | #### 5. Type of Methods 54 | Access modifier specified as private, final and static methods cannot be overridden 55 | in Java. This provides security to methods. If you tried to override them compiler 56 | throws an error. 57 | 58 | #### 6. Return Type 59 | The return type of the overriding method must be same as the overridden method. 60 | 61 | 62 | ## Example 63 | ```java 64 | // Base class for Animal. 65 | class Animal { 66 | 67 | // Overridden method makeSound() follows the rules of 68 | // method overriding. Note that here the method is protected (non public). 69 | protected String makeSound() { 70 | return "Base Animal Sound..."; 71 | } 72 | } 73 | 74 | // Dog class that extends the Animal. 75 | class Dog extends Animal { 76 | 77 | // Overriding method makeSound() follows the rules of method overriding. 78 | // Note that here the method is public (not default). 79 | public String makeSound() { 80 | return "Bow Bow..."; 81 | } 82 | } 83 | 84 | // Main class containes driver code and example. 85 | public class MethodOverloadOverride { 86 | 87 | // Creating static methods for overloading. static used for invoke 88 | // method without creating object. 89 | // Method sum() for adding 2 integers and return the sum. 90 | static int sum(int a, int b) { 91 | return a + b; 92 | } 93 | 94 | // Method sum() adds 2 floats and return the sum. 95 | static float sum(float a, float b) { 96 | return a + b; 97 | } 98 | 99 | // main method for execution. 100 | public static void main(String[] args) { 101 | 102 | 103 | // Example of overriding. 104 | 105 | // Creating Object for Dog class and calling the makeSound() method 106 | // and testing it against Animal class object. 107 | System.out.println("From Dog: " + new Dog().makeSound()); 108 | System.out.println("From Animal: " + new Animal().makeSound()); 109 | 110 | 111 | // Example of overloading. 112 | 113 | // Calling sum() methods with intergers and floats then prints the 114 | // result. 115 | System.out.println("Integer Addition, 5 + 4 : " + sum(5, 4)); 116 | System.out.println("Float Addition, 5.0 + 4.0 : " + sum(5.0f, 4.0f)); 117 | // On above code 'f' used after floating numbers. This is because when we 118 | // use decimal point numbers without suffix 'f', Java treat is as double 119 | // datatype. 120 | } 121 | } 122 | ``` 123 | > *Note: You can find the above code on [MethodOverloadOverride.java](MethodOverloadOverride.java).* 124 | 125 | 126 | ## Output 127 | ``` 128 | From Dog: Bow Bow... 129 | From Animal: Base Animal Sound... 130 | Integer Addition, 5 + 4 : 9 131 | Float Addition, 5.0 + 4.0 : 9.0 132 | ``` 133 | 134 | 135 | ## Author 136 | [NanthaKumar](https://github.com/nknantha "NanthaKumar's Profile") 137 | -------------------------------------------------------------------------------- /Week-2/class, variable and method in Java/class, variable and method in Java.md: -------------------------------------------------------------------------------- 1 | # Class, Variable and Method in Java 2 | >Java is an Object-Oriented programming Language which means it follows data controlling access to the code approch. 3 | ## **Declaration** 4 | - `Declaration of Class` : A class is declared by use of the class keyword. The class body is enclosed between curly braces { and }. The data or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called **members of the class**. 5 |
The syntax: 6 | 7 | ![](https://www.w3resource.com/w3r_images/java-class-image.png) 8 | - `Declaration of Instance Variables` : Variables defined within a class are called **instance variables** and every object has it's own copy of instance variable and each object contains it's own data. An instance variable can be declared public or private or default (no modifier). When we do not want our variable’s value to be changed out-side our class we should declare them private. public variables can be accessed and changed from outside of the class. 9 |
The syntax: 10 | 11 | ![](https://www.w3resource.com/w3r_images/declaration-of-instance-variables.png) 12 | - `Declaration of Methods` : A method is a program module that contains a series of statements that carry out a task and it is optional to execute or not. To execute a method, you invoke or call it from another method; the calling method makes a method call, which invokes the called method. Any class can contain an unlimited number of methods, and each method can be called an unlimited number of times. 13 |
The syntax: 14 | 15 | ![java method declaration image](https://www.w3resource.com/w3r_images/declaration-of-methodes.png) 16 | ## **Access modifiers** : 17 | Each object has members (members can be variable and methods) which can be declared to have specific access. Java has 4 access level and 3 access modifiers. 18 |
Access levels with there features are given below in the table: 19 | 20 | ![](https://media.geeksforgeeks.org/wp-content/cdn-uploads/Access-Modifiers-in-Java.png) 21 | 22 | `public: (Example code)` 23 | 24 | ``` 25 | public class Vehicle { 26 | public int doors; 27 | public int speed; 28 | public String color; 29 | 30 | public void run(){ 31 | //Run method implementation 32 | } 33 | } 34 | ``` 35 | 36 | `protected: (Example code)` 37 | ``` 38 | public class Vehicle{ 39 | protected int doors; 40 | protected String color; 41 | 42 | protected void run(){ 43 | //Run method implementation 44 | } 45 | } 46 | ``` 47 | `Default (no value): (Example code)` 48 | ``` 49 | class Vehicle { 50 | int doors; 51 | int speed; 52 | String color; 53 | void run(){ 54 | //Run method implementation. 55 | } 56 | } 57 | ``` 58 | `private: (Example code)` 59 | ``` 60 | public class Vehicle { 61 | private int doors; 62 | private int speed; 63 | private String color; 64 | 65 | public void run{ 66 | //Run method implementation 67 | } 68 | } 69 | ``` 70 | > Java has modifiers other than access modifiers listed below: 71 | > `static` : static can be used for members of a class. The static members of the class can be accessed without creating an object of a class. Static members are shared by all the objects of a class which mean it is property of class rather then object. 72 | `Example code` 73 | 74 | ``` 75 | public class Vehicle { 76 | private int doors; 77 | private int speed; 78 | private String color; 79 | 80 | public static void run(){ 81 | // Static Run method implementation. 82 | } 83 | public void stop (){ 84 | //Implementation of Stop method 85 | } 86 | } 87 | class Maruti{ 88 | public void TestVehicleClass(){ 89 | //To Access run() method we don't need object of Vehicle class 90 | Vehicle.run(); 91 | //To Access stop() method we need object of Vehicle class, else compilation fails. 92 | new Vehicle().stop(); 93 | } 94 | } 95 | ``` 96 | >In the Example in the Vehicle class run() is a static method and stop() is a non-static method. In Maruti class we can see how to access static method run () and non-static method stop (). 97 | > `final` : This modifier applicable to class, method, and variables. This modifier tells the compiler not to change the value of a variable once assigned. If applied to class, it cannot be sub-classed. If applied to a method, the method cannot be overridden in sub-class. 98 | `Example code` 99 | ``` 100 | public class Person{ 101 | private String name = "Amit"; 102 | private final int age = 30; 103 | 104 | public void finaldemo(){ 105 | name = "Amit Himani"; 106 | //age = 35 ; this line will give you compile time error 107 | } 108 | } 109 | ``` 110 | > `abstract`: There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. This modifier is applicable to class and methods only. 111 | 112 | -------------------------------------------------------------------------------- /Week-1/Operators/Logical Operators.md: -------------------------------------------------------------------------------- 1 | # Logical operators 2 | 3 | ### Logical operators are generally used for combining two or more relational statements. They return Boolean values. The logical operators are used primarily in the expression evaluation to make a decision. These operators allow the evaluation and manipulation of specific bits within the integer. 4 | 5 | ### The types of Logical operators with their description are tabulated as follows - 6 | 7 | | **Operators** | **Description** | 8 | |----------------------|----------------------------------------------------------------------------------------------------| 9 | | **&& (Logical AND)** | This operator returns true if all relational statements combined with && are true, else it returns | | | false. | 10 | | **|| (Logical OR)** | This operator returns true if at least one of the relational statements combined with || is true, | | | else it returns false. | 11 | | **! (logical NOT)** | It returns the inverse of the statement's result. | 12 | | **xor (Logical XOR** | This operator returns true if either statement one is true or statement two is true but not both. | 13 | | **operators)** | | 14 | | **and (Logical AND)**| This operator returns true if all relational statements combined with it are true, else it returns | 15 | | | false. | 16 | | **or (Logical OR)** | This operator returns true if atleast one of the relational statements combined with it is true, | 17 | | | else it returns false. | 18 | 19 | # && (Logical AND) 20 | ### The logical && operator doesn't check the second condition if first condition is false. It checks the second condition only if the first one is true. The evaluation in logical && expression is left to right. With the help of other Logical operators, we can convert Logical AND to Logical OR. This operator accepts two operands. 21 | ### Suppose we have two conditions A and B. So, we can see the possible values of A && B in the below table:- 22 | | **A** | **B** | **A && B** | 23 | |-------|-------|------------| 24 | | True | True | True | 25 | | False | True | False | 26 | | True | False | False | 27 | | False | False | False | 28 | 29 | # || (Logical OR) 30 | ### The Logical OR operator is represented with the two vertical line symbols. It works opposite to the logical AND operator, as it only gives false when both statements of the expression are not true. Otherwise, it returns true. The logical || operator doesn't check the second condition if first condition is true. It checks the second condition only if the first one is false. The logical || operator accepts two operands. 31 | ### Suppose we have two conditions A and B. We can see the possible values of A || B in the following table:- 32 | | **A** | **B** | **A || B** | 33 | |-------|-------|------------| 34 | | True | True | True | 35 | | False | True | True | 36 | | True | False | True | 37 | | False | False | False | 38 | 39 | # ! (Logical NOT operator) 40 | ### This logical operator is represented as an exclamation sign (!). This operator accepts a single argument and returns the inverse value of the corresponding argument. Suppose we have an operand A. So, the Logical NOT operator will return the opposite possible values of A. 41 | | **A** | **!A** | 42 | |-------|--------| 43 | | True | False | 44 | | False | True | 45 | 46 | # xor (Logical XOR operator) 47 | ### XOR stands for "Exclusive-OR". It returns true when either statemen1 is true, or statement2 is true, but not both. The xor operator will return true only when one of the statements is true and another one is false. If both of the statements are true, the xor operator will return false. The xor operator takes two operands and returns false when both operands have the same value. 48 | ### Suppose we have two conditions A and B. So, we can see the possible values of A xor B. 49 | | **A** | **B** | **A xor B** | 50 | |-------|-------|-------------| 51 | | True | True | False | 52 | | False | True | True | 53 | | True | False | True | 54 | | False | False | False | 55 | 56 | # and operator 57 | ### This operator works similar to the && operator. Although both 'and' and '&&' works similar, the difference is of precedence. Precedence decides which operation should be performed first. The precedence of the '&&' operator is higher than the precedence of the '=' operator, whereas the precedence of 'and' is lower than the '=' operator. 58 | 59 | # or operator 60 | ### This operator works similar to the || operator. Although both 'or' and 'II' works similar, the difference is of precedence. Precedence decides which operation should be performed first. The precedence of the 'II' operator is higher than the precedence of the '=' operator, and the precedence of 'or' is lower than the '=' operator. 61 | 62 | 63 | ``` 64 | 65 | ``` 66 | -------------------------------------------------------------------------------- /Week-1/Break and Continue Statements/BreakContinue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This is the example file for demonstrating break and continue 3 | * statements. 4 | * It contains ... 5 | * 6 | * @author nknantha 7 | * @date 2021/07/30 8 | */ 9 | 10 | 11 | /**********************************Break-Section**********************************/ 12 | 13 | // default class for break example. 14 | class BreakExample { 15 | 16 | // Static method contains example code. 17 | public static void run() { 18 | // Iterating from 1 to 5. 19 | for (int i = 1; i < 6; ++i) { 20 | 21 | // Printing the iteration count. 22 | System.out.println("Iteration: " + i); 23 | 24 | // If iteration reaches 3 then we break the loop. 25 | if (i == 3) { 26 | System.out.println("Executing break on iteation " + i + "..."); 27 | break; 28 | } 29 | } 30 | } 31 | } 32 | 33 | // defalut class for Labelled Break example. 34 | class LabelledBreakExample { 35 | 36 | // Static method contains example code. 37 | public static void run() { 38 | // Creating first layer of code. 39 | label1: 40 | // Iterating from 1 to 3. 41 | for (int i = 1; i < 4; ++i) { 42 | 43 | // Creating second layer of code. 44 | label2: 45 | // Iterating from 1 to 3. 46 | for (int j = 1; j < 4; ++j) { 47 | 48 | // Printing second layer iteration count. 49 | System.out.println("OuterLoop: " + i +", InnerLoop: " + j); 50 | 51 | // break the label1 if j == 3 52 | if (j == 3) { 53 | System.out.println("Breaking OuterLoop..."); 54 | break label1; 55 | } 56 | } 57 | } 58 | } 59 | } 60 | 61 | 62 | /********************************Continue-Section*********************************/ 63 | 64 | // default class for continue example. 65 | class ContinueExample { 66 | 67 | // Static run() method contains continue example code. 68 | public static void run() { 69 | // Iterating from 1 to 5 70 | for (int i = 1; i < 6; ++i) { 71 | 72 | // If iteration reached 2 we trigger continue. 73 | if (i == 2) { 74 | System.out.println("Executing continue..."); 75 | continue; 76 | } 77 | 78 | // Printing iteration count. 79 | System.out.println("Iteration: " + i); 80 | } 81 | } 82 | } 83 | 84 | // default class for labelled continue example. 85 | class LabelledContinueExample { 86 | 87 | // Static run() method contains example code. 88 | public static void run() { 89 | // First layer of code. 90 | label1: 91 | // Iterating from 1 to 3. 92 | for (int i = 1; i < 4; ++i) { 93 | 94 | // Second layer of code. 95 | label2: 96 | // Iterating from 1 to 3. 97 | for (int j = 1; j < 4; ++j) { 98 | 99 | // Executing continue if j == 2. 100 | if (j == 2) { 101 | System.out.println("Executing continue to OuterLoop..."); 102 | continue label1; 103 | } 104 | 105 | // Printing iteration count. 106 | System.out.println("OuterLoop: " + i + ", InnerLoop: " + j); 107 | } 108 | } 109 | } 110 | } 111 | 112 | 113 | /***********************************DRIVER-CODE***********************************/ 114 | 115 | // Public class containing the DRIVER-CODE. 116 | public class BreakContinue { 117 | 118 | // main() method for execution entry. 119 | public static void main(String[] args) { 120 | 121 | // First executing BreakExample. 122 | System.out.println("BreakExample:"); 123 | BreakExample.run(); 124 | 125 | // Second executing LabelledBreakExample. 126 | System.out.println("\nLabelledBreakExample:"); 127 | LabelledBreakExample.run(); 128 | 129 | // Third executing ContinueExample. 130 | System.out.println("\nContinueExample:"); 131 | ContinueExample.run(); 132 | 133 | // Finally executing LabelledContinueExample. 134 | System.out.println("\nLablledContinueExample:"); 135 | LabelledContinueExample.run(); 136 | } 137 | } 138 | 139 | /** 140 | * Output: 141 | * BreakExample: 142 | * Iteration: 1 143 | * Iteration: 2 144 | * Iteration: 3 145 | * Executing break on iteation 3... 146 | * 147 | * LabelledBreakExample: 148 | * OuterLoop: 1, InnerLoop: 1 149 | * OuterLoop: 1, InnerLoop: 2 150 | * OuterLoop: 1, InnerLoop: 3 151 | * Breaking OuterLoop... 152 | * 153 | * ContinueExample: 154 | * Iteration: 1 155 | * Executing continue... 156 | * Iteration: 3 157 | * Iteration: 4 158 | * Iteration: 5 159 | * 160 | * LablledContinueExample: 161 | * OuterLoop: 1, InnerLoop: 1 162 | * Executing continue to OuterLoop... 163 | * OuterLoop: 2, InnerLoop: 1 164 | * Executing continue to OuterLoop... 165 | * OuterLoop: 3, InnerLoop: 1 166 | * Executing continue to OuterLoop... 167 | * 168 | */ 169 | -------------------------------------------------------------------------------- /Week-2/Accessing arrays, manipulating arrays/Array_Operations.md: -------------------------------------------------------------------------------- 1 | # **Array Operations in Java** 2 | 3 | > Array operations are notoriously difficult for Java developers to implement in a clean, efficient manner. We will have to deal with them on a number of occasions. Array operations are limited. For this reason, we'll be using Java's Arrays class and Apache's ArrayUtils class for most of our operations. 4 | 5 | ```java 6 | import java.util.Arrays; 7 | import org.apache.commons.lang3.ArrayUtils; 8 | ``` 9 | 10 | - ### What are arrays in Java? 11 | 12 | > An array in Java is an object that contains elements of the same data type. Furthermore, array elements are stored in a contiguous memory location. It is a data structure in which similar elements are stored. A Java array can only hold a fixed number of elements. This int array will be used in all of our examples (unless otherwise specified) so let's declare it and initialize it: 13 | 14 | ```java 15 | int[] array = new int[] { 3, 5, 2, 5, 14, 4 }; 16 | ``` 17 | 18 | - ### **Obtain the first and last elements of an array** 19 | 20 | > This is easy to figure out once we know that the first item in an array is associated with the index value 0 and that the array has a length attribute that we can use: 21 | 22 | ```java 23 | int firstItem = array[0]; 24 | int lastItem = array[array.length - 1]; 25 | ``` 26 | 27 | - ### **Obtain a value at random from an array** 28 | 29 | > We can easily retrieve any value from our array by using the java.util.Random object: 30 | 31 | ```java 32 | array[new Random().nextInt(array.length)]; 33 | ``` 34 | 35 | - ### **Insert a new item into an array** 36 | 37 | > Arrays, as we all know, have a fixed number of values. As a result, we are unable to simply add an item to our list and exceed this limit. Starting with a larger array, we'll need to copy the elements of the base array into it. There is a method in the Arrays class for replicating the values of an array into a new structure of a different size: 38 | 39 | ```java 40 | **int**[] newArray = Arrays.copyOf(array, array.length + 1); 41 | newArray[newArray.length - 1] = newIem; 42 | ``` 43 | 44 | > A one-line statement can be used to accomplish our goal by using the add method of the ArrayUtils class if it is available in our project: 45 | 46 | ```java 47 | ArrayUtils.add(array, newItem); 48 | ``` 49 | 50 | - ### **Adding a value to the middle of two other values** 51 | 52 | > Due to its indexed-values nature, inserting an item between two other items in an array is not a simple task. To simplify the solution, Apache added a method to its ArrayUtils class: 53 | 54 | ```java 55 | ArrayUtils.insert(2, array, 77); 56 | ``` 57 | 58 | - ### **Compare two arrays** 59 | 60 | > The default implementation of equals is used by arrays, despite the fact that arrays are Objects and therefore have an equals method, which relies on reference equality only. 61 | 62 | ```java 63 | Arrays.equals(array1, array2); 64 | Arrays.deepEquals(array1, array2); // for multi-dimensional or nested arrays 65 | ``` 66 | 67 | - ### **Check to see if an array is empty** 68 | 69 | > Assuming that arrays have a length attribute, this is a simple assignment. 70 | 71 | ```java 72 | boolean isEmpty = array == null || array.length == 0; 73 | ``` 74 | 75 | > ArrayUtils also provides a null-safe method: 76 | 77 | ```java 78 | ArrayUtils.isEmpty(array); 79 | ``` 80 | 81 | - ### **How to rearrange the elements of an array** 82 | 83 | > We can use ArrayUtil's feature to shuffle the items in an array: 84 | 85 | ```java 86 | ArrayUtils.shuffle(array); 87 | ``` 88 | 89 | - ### **Box and unbox arrays** 90 | 91 | > Object-based arrays are often the only array types supported by methods. A boxed version of our primitive array can be obtained by using the ArrayUtils helper class. 92 | 93 | ```java 94 | ArrayUtils.toObject(array); 95 | ``` 96 | 97 | - ### **Get rid of duplicates in an array** 98 | 99 | > Transforming the array into a Set implementation removes duplicates the easiest way possible. Our values will need to be boxed if we're not working with object-based arrays, which is the case in this example. 100 | 101 | ```java 102 | Integer[] list = ArrayUtils.toObject(array); // Remove duplicates 103 | Set set = new HashSet(Arrays.asList(list)); // Create array and unbox 104 | return ArrayUtils.toPrimitive(set.toArray(new Integer[set.size()])); 105 | ``` 106 | 107 | - ### **Array to String** 108 | 109 | > Both the Arrays and ArrayUtils classes include implementations for converting data structures to readable Strings. The most significant difference, aside from the slightly different format, is how they treat multi-dimensional objects. 110 | 111 | - We can use two static methods provided by the **Java** Util class, *toString()* (doesn't work well with multi-dimensional arrays) or *deepToString()* (supports all Object-based arrays but will not compile with primitive array arguments). 112 | - In contrast, **Apache's** implementation provides a single function *toString()* that works in all cases. 113 | 114 | - ### **Values in an Array Can Be Filtered** 115 | 116 | Many of us will have to perform the task of filtering out values from a collection on multiple occasions. We don't know the final size of the array when we create it. In order to do this, we'll have to use the Streams approach once. 117 | -------------------------------------------------------------------------------- /Week-2/Compile-time Polymorphism/TypePromotion.svg: -------------------------------------------------------------------------------- 1 | 3 | 4 | %0 5 | 6 | 7 | 8 | byte 9 | 10 | byte 11 | 12 | 13 | 14 | short 15 | 16 | short 17 | 18 | 19 | 20 | byte->short 21 | 22 | 23 | 24 | 25 | 26 | int 27 | 28 | int 29 | 30 | 31 | 32 | short->int 33 | 34 | 35 | 36 | 37 | 38 | long 39 | 40 | long 41 | 42 | 43 | 44 | int->long 45 | 46 | 47 | 48 | 49 | 50 | float 51 | 52 | float 53 | 54 | 55 | 56 | int->float 57 | 58 | 59 | 60 | 61 | 62 | double 63 | 64 | double 65 | 66 | 67 | 68 | int->double 69 | 70 | 71 | 72 | 73 | 74 | long->float 75 | 76 | 77 | 78 | 79 | 80 | long->double 81 | 82 | 83 | 84 | 85 | 86 | char 87 | 88 | char 89 | 90 | 91 | 92 | char->int 93 | 94 | 95 | 96 | 97 | 98 | float->double 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /Week-1/Expressions And Operators in Java/expressions_and_operators_in_java.md: -------------------------------------------------------------------------------- 1 | ### Expressions 2 | 3 | * A Java expression consists of variables, operators, literals, and method calls. To know more about method calls, visit Java methods. 4 | 5 | * For example : 6 | 7 | ``` 8 | int score; 9 | score = 90; 10 | ``` 11 | * Here, score = 90 is an expression that returns an int 12 | 13 | * Consider another example, 14 | 15 | ``` 16 | Double a = 2.2, b = 3.4, result; 17 | result = a + b - 3.4; 18 | ``` 19 | * Here, a + b - 3.4 is an expression. 20 | 21 | ### Operators 22 | 23 | * Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc. 24 | 25 | * There are many types of operators in Java which are given below: 26 | 27 | * Unary Operator, 28 | 29 | * Arithmetic Operator, 30 | 31 | * Shift Operator, 32 | 33 | * Relational Operator, 34 | 35 | * Bitwise Operator, 36 | 37 | * Logical Operator, 38 | 39 | * Ternary Operator and 40 | 41 | * Assignment Operator. 42 | 43 | #### Unary Operator 44 | 45 | * Java unary operators require only one operand 46 | 47 | * Unary operators are used to perform various operations i.e.: 48 | 49 | * incrementing/decrementing a value by one 50 | 51 | * negating an expression 52 | 53 | * inverting the value of a boolean 54 | 55 | ``` 56 | public class OperatorExample{ 57 | public static void main(String args[]){ 58 | int x=10; 59 | System.out.println(x++);//10 (11) 60 | System.out.println(++x);//12 61 | System.out.println(x--);//12 (11) 62 | System.out.println(--x);//10 63 | }} 64 | ``` 65 | 66 | 67 | #### Arithmetic Operator 68 | 69 | * Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. 70 | 71 | * They act as basic mathematical operations. 72 | 73 | ``` 74 | public class OperatorExample{ 75 | public static void main(String args[]){ 76 | int a=10; 77 | int b=5; 78 | System.out.println(a+b);//15 79 | System.out.println(a-b);//5 80 | System.out.println(a*b);//50 81 | System.out.println(a/b);//2 82 | System.out.println(a%b);//0 83 | }} 84 | ``` 85 | 86 | 87 | #### Shift Operator 88 | 89 | * Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times. 90 | 91 | ``` 92 | public class OperatorExample{ 93 | public static void main(String args[]){ 94 | System.out.println(10<<2);//10*2^2=10*4=40 95 | System.out.println(10<<3);//10*2^3=10*8=80 96 | System.out.println(20<<2);//20*2^2=20*4=80 97 | System.out.println(15<<4);//15*2^4=15*16=240 98 | }} 99 | ``` 100 | 101 | 102 | * Java right shift operator >> is used to move the value of the left operand to right by the number of bits specified by the right operand. 103 | 104 | ``` 105 | public OperatorExample{ 106 | public static void main(String args[]){ 107 | System.out.println(10>>2);//10/2^2=10/4=2 108 | System.out.println(20>>2);//20/2^2=20/4=5 109 | System.out.println(20>>3);//20/2^3=20/8=2 110 | }} 111 | ``` 112 | 113 | 114 | #### Logical && and Bitwise & 115 | 116 | * The logical && operator doesn't check the second condition if the first condition is false. 117 | 118 | * It checks the second condition only if the first one is true. 119 | 120 | * The bitwise & operator always checks both conditions whether first condition is true or false. 121 | 122 | ``` 123 | public class OperatorExample{ 124 | public static void main(String args[]){ 125 | int a=10; 126 | int b=5; 127 | int c=20; 128 | System.out.println(a 133 | 134 | ``` 135 | public class OperatorExample{ 136 | public static void main(String args[]){ 137 | int a=10; 138 | int b=5; 139 | int c=20; 140 | System.out.println(a 147 | 148 | #### Ternary Operator 149 | 150 | * Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in Java programming. 151 | 152 | * It is the only conditional operator which takes three operands. 153 | 154 | ``` 155 | public class OperatorExample{ 156 | public static void main(String args[]){ 157 | int a=2; 158 | int b=5; 159 | int min=(a 164 | 165 | #### Assignment Operator 166 | 167 | * Java assignment operator is one of the most common operators. 168 | 169 | * It is used to assign the value on its right to the operand on its left. 170 | 171 | ``` 172 | public class OperatorExample{ 173 | public static void main(String args[]){ 174 | int a=10; 175 | int b=20; 176 | a+=4;//a=a+4 (a=10+4) 177 | b-=4;//b=b-4 (b=20-4) 178 | System.out.println(a); 179 | System.out.println(b); 180 | }} 181 | ``` 182 | ![image](https://user-images.githubusercontent.com/81172589/127402080-b353e659-2e00-4286-a490-7184e3462760.png) 183 | --------------------------------------------------------------------------------