• Swapping 3 numbers

    Need to be discussed

    : Hash algori

  • Logical Operators

    These operators are used to perform logical “AND”, “OR” and “NOT” operation, i.e. the function similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consideration. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensively to test for several conditions for making a decision. Let’s look at each of the logical operators in a detailed manner:

    1. ‘Logical AND’ Operator(&&): This operator returns true when both the conditions under consideration are satisfied or are true. If even one of the two yields false, the operator results false. For example, cond1 && cond2 returns true when both cond1 and cond2 are true (i.e. non-zero)
      Syntax:
    condition1 && condition2

    Example:

    a = 10, b = 20, c = 20
    
    condition1: a < b
    condition2: b == c
    
    if(condition1 && condition2)
    d = a+b+c
    
    // Since both the conditions are true
    d = 50.
    / Java code to illustrate
    // logical AND operator
    
      
    
      
    class Logical {
        public static void main(String[] args)
        {
            // initializing variables
            int a = 10, b = 20, c = 20, d = 0;
      
            // Displaying a, b, c
            System.out.println("Var1 = " + a);
            System.out.println("Var2 = " + b);
            System.out.println("Var3 = " + c);
      
            // using logical AND to verify
            // two constraints
            if ((a < b) && (b == c)) {
                d = a + b + c;
                System.out.println("The sum is: " + d);
            }
            else
                System.out.println("False conditions");
        }
    }
    Output:
    Var1 = 10
    Var2 = 20
    Var3 = 20
    The sum is: 50
    --------------------------------------------------------------------------------------------------------------------------
    'Logical OR' Operator(||): This operator returns true when one of the two conditions under consideration are satisfied or are true. If even one of the two yields true, the operator results true. To make the result false, both the constraints need to return false.
    Syntax:
    condition1 || condition2
    Example:
    
    a = 10, b = 20, c = 20
    
    condition1: a < b
    condition2: b > c
    
    if(condition1 || condition2)
    d = a+b+c
    
    // Since one of the condition is true
    d = 50.
    
    // Java code to illustrate
    // logical OR operator
      
    
      
    class Logical {
        public static void main(String[] args)
        {
            // initializing variables
            int a = 10, b = 1, c = 10, d = 30;
      
            // Displaying a, b, c
            System.out.println("Var1 = " + a);
            System.out.println("Var2 = " + b);
            System.out.println("Var3 = " + c);
            System.out.println("Var4 = " + d);
      
            // using logical OR to verify
            // two constraints
            if (a > b || c == d)
                System.out.println("One or both"
                                   + " the conditions are true");
            else
                System.out.println("Both the"
                                   + " conditions are false");
        }
    }
    Output:
    Var1 = 10
    Var2 = 1
    Var3 = 10
    Var4 = 30
    --------------------------------------------------------------------------------------------------------------------------
    One or both the conditions are true
    'Logical NOT' Operator(!): Unlike the previous two, this is a unary operator and returns true when the condition under consideration is not satisfied or is a false condition. Basically, if the condition is false, the operation returns true and when the condition is true, the operation returns false.
    Syntax:
    
    !(condition)
    Example:
    
    a = 10, b = 20
    
    !(a<b) // returns false
    !(a>b) // returns true
    
    // Java code to illustrate
    // logical NOT operator
    import java.io.*;
      
    class Logical {
        public static void main(String[] args)
        {
            // initializing variables
            int a = 10, b = 1;
      
            // Displaying a, b, c
            System.out.println("Var1 = " + a);
            System.out.println("Var2 = " + b);
      
            // Using logical NOT operator
            System.out.println("!(a < b) = " + !(a < b));
            System.out.println("!(a > b) = " + !(a > b));
        }
    }
    Output:
    Var1 = 10
    Var2 = 1
    !(a < b) = true
    !(a > b) = false
    
    
    --------------------------------------------------------------------------------------------------------------------------
    
    
    while loop – if the number of iteration is not fixed, it is recommended to use while loop.
    
    package selfpractice;
    
    public class Whileloop {
    
    	public static void main(String[] args) {
    		
    		// TODO Auto-generated method stub
    		
    		int i=1;
    		while(i<=10){
    			System.out.println(i);
    			i++;
    		}
    
    	}
    
    }
    
    output 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    --------------------------------------------------------------------------------------------------------------------------
    
    
    Need to discuss with Sir 
    Nested if condition 
    Nested means within. Nested if condition means if-within-if. Nested if condition comes under decision-making statement in Java. There could be infinite if conditions inside an if condition

    Need to be discussed

    : Hash algori

  • IF Else flow chart

    Need to be discussed

    : Hash algori

  • Getter & Setter

    In Java, getter and setter are two conventional methods that are used for retrieving and updating value of a variable.The following code is an example of simple class with a private variable and a couple of getter/setter methods:

    public class SimpleGetterAndSetter {    
    private int number;     
    public int getNumber()
    {        
    return this.number;    
    }     
    public void setNumber(int num)
    {        
    this.number = num;    
    }
    }

    SimpleGetterAndSetterobj= newSimpleGetterAndSetter();

    obj.number = 10;    // compile error, since number is private

    intnum = obj.number; // same as above


    Instead, the outside code have to invoke the getter, getNumber() and the setter, setNumber() in order to read or update the variable, for example:

    SimpleGetterAndSetter obj = newSimpleGetterAndSetter(); 
    obj.setNumber(10);  // OKint
    num = obj.getNumber();  // fine

    So, a setter is a method that updates value of a variable. And a getter is a method that reads value of a variable.

    ———————————————————————–

    example in layman terms

    Let’s say you have a bank account with some money in it.

    Although that money(private money) belongs to you but still you can’t take it directly from it.

    You can only access it via teller or atm machine which is getter (getMoney()) for your money.

    And you can put money in your bank only via money deposit form which is setter(setMoney(double amount)) for your money.

    Need to be discussed

    : Hash algori

  • Interface

    Interface in Java is a bit like the Class, but with a significant difference: an interface can only have method signatures, fields and default methods. Since Java 8, you can also create default methods.

    (Class is a template but Interface is set of rules — its not inheritance but similar inheritance is a relationship based but Interface — implements )

    public interface Vehicle {
        public String licensePlate = " ";
        public float maxVel
        public void start();
        public void stop();
        default void blowHorn(){-------need to discuss 
          System.out.println("Blowing horn");
       }
    }
    

    The interface above contains two fields, two methods, and a default method. Alone, it is not of much use, but they are usually used along with Classes. Make sure some class implements it.

    public class Car implements Vehicle {
        public void start() {
            System.out.println("starting engine...");
        }
        public void stop() {
            System.out.println("stopping engine...");
        }
    }
    

    Now, there is a ground rule: The Class must implement all of the methods in the Interface. The methods must have the exact same signature (name, parameters and exceptions) as described in the interface. The class does not need to declare the fields though, only the methods.

    Instances of an Interface— need to be discuss

    Need to be discussed

    : Hash algori

  • Abstract in Java

    Abstraction is a general concept that denotes the progress of modeling “real things” into programming language.

    Abstraction is the process of selecting data to show only the relevant information to the user.

    In simple terms, abstraction “displays” only the relevant attributes of objects and “hides” the unnecessary details.

    For example, when we are driving a car, we are only concerned about driving the car like start/stop the car, accelerate/ break, etc. We are not concerned about how the actual start/stop mechanism or accelerate/brake process works internally. We are just not interested in those details.

    What we are concerned about is the “abstract” view of these operations that will help us to propel the car forward and reach our destination. This is a simple example of abstraction.

    Thus the car has all the mechanisms and processes in place but from the end user’s perspective, i.e. car driver’s perspective he/she will be interested only in the abstract view of these processes.

    Abstraction reduces the programming efforts and thereby the complexity. An end-user using the application need not be concerned about how a particular feature is implemented. He/she can just use the features as required.

    Thus in abstraction, we deal with ideas and not the events. This means that we hide the implementation details from the user and expose only the functionality to the end-user. Thereby the user will only know “what it does” rather than “how it does”.

    In a nutshell, an abstract class can be described as shown below.

    abstract class

    An abstract method is a method preceded by an ‘abstract’ keyword without any implementation. An abstract method is declared inside an abstract class.

    An abstract method is the one that makes a class incomplete as it doesn’t have an implementation. Hence when we include an abstract method in the class, naturally the class becomes incomplete.

    We can use the abstract method by implementing it in a subclass i.e. a class inherits the abstract class and then implements or provides the code for all the abstract methods declared in the abstract class by overriding them.

    Thus it becomes compulsory to override the abstract method in the subclass. If the abstract method is not implemented in the subclass as well, then we have to declare the subclass also as “abstract”.

    The general declaration of the abstract method is:

    abstract void methodName (parameter_list);

    While writing the abstract method, we need to remember the following rules:

    • A class containing one or more abstract methods is an abstract class.
    • Certain other keywords should not be used with the abstract keyword.

    following points need to be discussed –Thus, the following combinations are illegal in Java.

    1. final
    2. abstract native
    3. abstract static
    4. abstract private
    5. abstract synchronized
    6. abstract strictfp

    an example of an abstract class and an abstract method.

    //abstract class abstract class Bank{        abstract int getInterestRate();    }    //concrete classclass Citi extends Bank{        int getInterestRate(){return 7;}    }//concrete classclass HSBC extends Bank{        int getInterestRate(){return 6;}    }         class Main{        public static void main(String args[]){            Bank b;          b = new Citi ();      // concrete class object        System.out.println("Citi Rate of Interest is: "+b.getInterestRate()+"%");            b = new HSBC ();        // concrete class object        System.out.println("HSBC Rate of Interest is: "+b.getInterestRate()+"%");        }}   

    Output:

    Example of Abstract Method

    In the above example, we have a Bank class. In this class, we have an abstract method, getInterestRate (). Then we declare two classes – ICICI and BOI that inherit from the Bank class. Both these classes implement the getInterestRate () method by returning their respective interest rates.

    Then in the main method, we create a bank object. First, the bank object contains an object of ICICI class and displays the interest rate. Next, the BOI object is created and it displays the interest rate.

    Thus, we can assume that the Bank class is a sort of a sketch or a structure that allows us to get an interest rate. From this structure, we can create as many concrete classes as we want, and then we can get the respective interest rates for each bank object (this is shown in the main method).

    What Is The Use Of An Abstract Class In Java

    Why do we use an abstract class when in reality it does not have any implementation of its own?

    Along with the answer to the above question, we will also illustrate how to use an abstract class in the example that follows.

    Let’s consider an example of Vehicles. We know that Vehicles can be of many types. We can have Cars, Scooters, bikes, mopeds, buses, etc. Though there are many types of vehicles, they have some properties or attributes that are common to all the vehicles irrespective of their types.

    For example, each vehicle has a model, chassis number, color, etc. Each of them has functions like start, stop, accelerate, brake, etc. Now each vehicle will have the above properties and methods that are common to the others as well. At the same time as a vehicle user, we might not be interested in some aspects.

    For example, if a person is driving a car, what he/she will be interested in is just to start and stop the vehicle or accelerate or brake the vehicle. He/she will not be interested in knowing how the vehicle starts or stop. We are only interested in the abstract working of the functions and not in their details.

    Need to be discussed

    : Hash algori

  • Java protected keyword

    A Java protected keyword is an access modifier. It can be assigned to variables, methods, constructors and inner classes.

    Points to remember

    • The protected access modifier is accessible within the package. However, it can also accessible outside the package but through inheritance only.
    • We can’t assign protected to outer class and interface.
    • If you make any constructor protected, you cannot create the instance of that class from outside the package.
    • If you are overriding any method, overridden method (i.e., declared in the subclass) must not be more restrictive.
    • According to the previous point, if you assign protected to any method or variable, that method or variable can be overridden to sub-class using public or protected access modifier only.

    The main purpose of protected keyword is to have the method or variable can be inherited from sub classes

    Java protected keyword Examples:

    The following class Person, declares a protected variable name, inside package p1:

    package p1; 
    public class Person {    
    protected String name;
    }

    The following class in the same package can access the variable name directly:

    package p1; 
    public class Employer
    {    
    void hireEmployee()
    {        Person p = new Person();        
    p.name = "Nam";    // access protected variable directly    
    }
    }

    The following class is in different package but it extends the Person class so it can access the variable name directly:

    package p2; 
    import p1.Person; 
    class Employee extends Person
    {    
    void doStuff()
    {  
    name = "Bob";    
    }
    }

    But the following class, in different package, cannot access the variable name directly:

    package p2; 
    import p1.Person; 
    class AnotherEmployer {    
    void hire()
    {        
    Person p = new Person(); 
    // compile error, cannot acceess protected variable        
    // from different package       
     p.name = "Nam";    
    }
    }
    Error : error: name has protected access in Person

    What is Inheritance

    Inheritance is the ability of a class inherits data and behaviors from another class. Note that only public and protected members of the superclass are inherited by the subclass. The subclass can freely add new members to extend features of the superclass.

    Why is Inheritance

    Inheritance is for reusing code, when you want to extend features of a class, you can write a subclass to inherit all data and behaviors of that superclass. This saves time on writing code.Another reason for implementing inheritance is for the purpose of extensibility. It’s easier to extend a class and add new features than writing a new class from scratch.And using inheritance promotes the maintainability of the code. Imagine you have a superclass and 5 subclasses. When you want to update a common feature of all these classes, you just update the parent class in one place. That’s much easier than updating every single class in case there is no inheritance.

    How is Inheritance implemented in Java?

    In Java, inheritance can be implemented in three forms:

    • A class inherits another class. The keyword extends is used, for example:

    public class Person { }

    public class Designer extends Person { }

    publicclassProgrammer extends Person{}

    public class Architect extends Programmer { }

    Needs to discuss

    A class implements another interface. The keyword implements is used.

    An interface inherits another interface. The keyword extends

    Need to be discussed

    : Hash algori

  • Encapsulation

    Java has 4 pillars

    1. Inheritance ——- Code reusability
    2. Polymorphism——-Code resuability
    3. Abstraction——– Security
    4. Encapsulation ——Security

    Encapsulation — Data protection (data hiding)

    Encapsulation — is achieved through various access specifiers including private, default,protect& public

    Encapsulation — code flexibility need to be discuss

    variable + method =Object

    (yaaruku enna access kudukaroma — private, public, default — access modifiers or specifiers)

    Its a mechanism of wrapping data variables & code acting on data methods together as a single unit.

    Data hiding — Declare the variables of a class as Private(could access only through methods)– for security purpose

    Getter & setter method need to discuss

    Advantages of Encapsulation in Java

    • Encapsulation is binding the data with its related functionalities. Here functionalities mean “methods” and data means “variables”
    • So we keep variable and methods in one place. That place is “class.” Class is the base for encapsulation.
    • With Java Encapsulation, you can hide (restrict access) to critical data members in your code, which improves security
    • If a data member is declared “private”, then it can only be accessed within the same class. No outside class can access data member (variable) of other class.
    • However, if you need to access these variables, you have to use public “getter” and “setter” methods.

    Need to be discussed

    : Hash algori

  • this Keyword

    this keyword in Java

    There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object.

    java this keyword

    Usage of Java this keyword

    Here is given the 6 usage of java this keyword.

    1. this can be used to refer current class instance variable.
    2. this can be used to invoke current class method (implicitly)
    3. this() can be used to invoke current class constructor.
    4. this can be passed as an argument in the method call.
    5. this can be passed as argument in the constructor call.
    6. this can be used to return the current class instance from the method.

    Suggestion: If you are beginner to java, lookup only three usages of this keyword.

    Usage of Java this keyword

    1) this: to refer current class instance variable

    The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

    Understanding the problem without this keyword

    Let’s understand the problem if we don’t use this keyword by the example given below:

    1. class Student{  
    2. int rollno;  
    3. String name;  
    4. float fee;  
    5. Student(int rollno,String name,float fee){  
    6. rollno=rollno;  
    7. name=name;  
    8. fee=fee;  
    9. }  
    10. void display(){System.out.println(rollno+” “+name+” “+fee);}  
    11. }  
    12. class TestThis1{  
    13. public static void main(String args[]){  
    14. Student s1=new Student(111,”ankit”,5000f);  
    15. Student s2=new Student(112,”sumit”,6000f);  
    16. s1.display();  
    17. s2.display();  
    18. }}  

    Test it Now

    Output:

    0 null 0.0
    0 null 0.0
    

    In the above example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable.

    Solution of the above problem by this keyword

    1. class Student{  
    2. int rollno;  
    3. String name;  
    4. float fee;  
    5. Student(int rollno,String name,float fee){  
    6. this.rollno=rollno;  
    7. this.name=name;  
    8. this.fee=fee;  
    9. }  
    10. void display(){System.out.println(rollno+” “+name+” “+fee);}  
    11. }  
    12.   
    13. class TestThis2{  
    14. public static void main(String args[]){  
    15. Student s1=new Student(111,”ankit”,5000f);  
    16. Student s2=new Student(112,”sumit”,6000f);  
    17. s1.display();  
    18. s2.display();  
    19. }}  

    Test it Now

    Output:

    111 ankit 5000.0
    112 sumit 6000.0
    

    If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program:

    Program where this keyword is not required

    If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program:

    1. class Student{  
    2. int rollno;  
    3. String name;  
    4. float fee;  
    5. Student(int r,String n,float f){  
    6. rollno=r;  
    7. name=n;  
    8. fee=f;  
    9. }  
    10. void display(){System.out.println(rollno+” “+name+” “+fee);}  
    11. }  
    12.   
    13. class TestThis3{  
    14. public static void main(String args[]){  
    15. Student s1=new Student(111,”ankit”,5000f);  
    16. Student s2=new Student(112,”sumit”,6000f);  
    17. s1.display();  
    18. s2.display();  
    19. }}  

    Test it Now

    Output:

    111 ankit 5000.0
    112 sumit 6000.0

    Need to be discussed

    : Hash algori

  • Java Constructors

    A Java constructor is special method that is called when an object is instantiated. In other words, when you use the new keyword. The purpose of a Java constructor is to initializes the newly created object before it is used.

    Example that creates an object, which results in the class constructor being called:

    MyClass myClassObj = new MyClass();
    
    Defining a Constructor in Java
    Here is a simple Java constructor declaration example. The example shows a very simple Java class with a single constructor.
    
    public class MyClass {
    
        public MyClass() {
    
        }
    }
    The constructor is this part:
    
        public MyClass() {
    
        }
    The first part of a Java constructor declaration is an access modifier. The access modifier have the same meanings as for methods and fields. 
    

    Need to be discussed

    : Hash algori

Design a site like this with WordPress.com
Get started