Thursday, March 9, 2017

Inheritance Python

Inheritance is the process of reusing the existing code .
Define new classes from the old one i.e including all features of old classes.

Following Program will illustrate the concept:

Code::


class bankaccount:
    def __init__ (self):
        self.bal=int(input("Enter Initial Balance:"))
   
    def withdraw(self,amt):
        if amt<self.bal:
            self.bal=self.bal-amt
        else:
            raise ValueError ("Insufficient Funds")
   
    def deposit(self,amt):
        self.bal=self.bal+amt
   
    def transfer(self,amt,toacc):
        try:
            self.withdraw(amt)
            toacc.deposit(amt)
        except ValueError as e:
            print("Try Again",e)
    def __str__(self):
        return"Balance is: "+str(self.bal)

class cheque(bankaccount):
    def __init__ (self):
        bankaccount.__init__(self)
        self.cheq={}
    def process(self,amt,number,org):
        try:
            self.withdraw(amt)
        except ValueError as e:
            print("Cheque Returned for",e)
        self.cheq[number]=(org,amt)
    def cheqinfo(self,number):
        if self.cheq.get(number):
            print(self.cheq[number])
        else:
            print("No Such Cheque")
    def withdraw(self,amt):
        print('Withdrawing')
        bankaccount.withdraw(self, amt)

nt=bankaccount()        #object of bankaccount type created
nt.deposit(1500)
print(nt)
nt.withdraw(1500)
print(nt)
nti=bankaccount()          #new object created
nt.transfer(1500, nti)
print(nt,nti)

dn=cheque()
dn.process(1500, 1001, 'nearur')

dn.cheqinfo(1001)

Output::

Wednesday, March 8, 2017

Final Keyword

Final Keyword is used to make any value immutable(not changable),avoid inheritance,or avoid method overriding.

Final To Avoid Method Overriding:

Code::
class Sample
{
  final void show()
    {
        System.out.println("Hello NT");
    }
}
class Test extends Sample
{
    /*void show()
    {
        Sytsem.out.println("Hello NTI");
    }*/          //Compile Time Error
}
public class FinalKeyword 
{

    public static void main(String[] nt) 
    {
      Test t=new Test();
      t.show();
    }
    

}
Output::


Final To avoid Inheritance :

Code::
final class Sample
{
     void show()
    {
        System.out.println("Hello NT");
    }
}
/*class Test extends Sample
{
  
}*/ //compile time error
public class FinalKeyword 
{

    public static void main(String[] nt) 
    {
      
    }
    
}

Abstract Class

Abstract Class is used to make some methods compulsory for subclasses to redefine them according to the subclass.
Thus it provide basic about the functions which an object has to perform .

Syntax:    abstract type name(parameter list);

If a method is abstract then the class must be declared abstract. All the subclasses have to define that method.

Following will illustrate the concept:

Code::

abstract class Base
{
    abstract void show();
}
class Derived extends Base
{
    void show()
    {
        System.out.println("Derived Show");
    }
}
public class AbstractUse 
{
    public static void main(String [] nt)
    {
       // Base b=new Base();  Compile time error
        Derived d=new Derived();
        d.show();
        Base n;     //abstract class reference can be created
        n=d;
        n.show();
    }
}

Output:: 

Dynamic Method Dispatch

Dynamic Method Dispatch is an important feature of java in which the call to a overridden method is resolved at runtime i.e:runtime polymorphism.

In this case the Superclass reference object is assigned the subclass objects hence according to the object referenced the version of overridden method is called.

Following example will illustrate the concept:

Code::

import java.util.*;
class Figure
{
    double a,b;
    Figure(double c,double d)
    {
        a=c;
        b=d;
    }
    void area()
    {
        System.out.println("Area: 0");
    }
}
class Rectangle extends Figure
{
    Rectangle(double c,double d)
    {
        super(c,d);
    }
    void area()
    {
        System.out.println("Area of Rectangle:"+a*b+" square meters");
    }
}
class Triangle extends Figure
{
    Triangle(double c,double d)
    {
        super(c,d);
    }
    void area()
    {
        System.out.println("Area of Triangle:"+0.5*a*b+" square meters");
    }
}
class Circle extends Figure
{
    Circle(double c)
    {
        super(c,c);
    }
    void area()
    {
        System.out.println("Area of Circle:"+3.142*a*a+" square meters");
    }
}
class Square extends Figure
{
    Square(double c)
    {
        super(c,c);
    }
    void area()
    {
        System.out.println("Area of Square: "+a*b+" square meters");
    }
}
public class Area 
{
       public static void main(String[] nt)
       {
           Rectangle r=new Rectangle(5,4);
           Circle c=new Circle(10);
           Square s=new Square(5);
           Triangle t=new Triangle(4,5);
           Figure f;
           f=r;
           f.area();         //rectangle area
           f=c;
           f.area();          // circle area
           f=s;
           f.area();         //square area
           f=t;
           f.area();        //triangle area
       }
}

Output::

Tuesday, March 7, 2017

Electrical Resistance of Wire

The electrical resistance R of a cylindrical wire with length l (in meter) and diameter d (in meter) can be computed from the area A of its diameter (m2) and the  resistivity Ρ of the material (rho, meter times Ohm). The formula:

R=P*(l/A)

Code::

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mrdishant
 */
import java.util.*;
public class Resistance {
    public static void main(String[] nt)
    {
        Scanner in=new Scanner(System.in);
        System.out.print("Enter Length of wire:");
        double length=in.nextDouble();
        System.out.print("Enter Diameter of Wire:");
        double d=in.nextDouble();
        System.out.print("Enter Resistivity of wire:");
        double p=in.nextDouble();
        double r=p*(4*length/3.142*d*d);
        System.out.println("Resistance of Wire:"+r);
 
    }
    
}

Output::

Metropolis City or Not

Question::  We define a Metropolis to be a city, that is either a capital city with more than 100 000 citizens or more than 200 000 citizens and an average income of 720 000 000 per year.

Give a boolean expression with all three variables that is true if and only if the city is a metropolis.

Code::

import java.util.*;
public class NT 
{
    public static void main(String [] nt)
    {
        boolean iscapital=false;
        long nofcitizens;
        double taxpercitizen;
        Scanner in=new Scanner(System.in);
        System.out.print("Press Y if City is a Capital Else anything:");
        char a=in.next().charAt(0);
        if(a=='y'||a=='Y')
        {
            iscapital=true;
        }
        System.out.print("Enter Number of Citizens:");
        nofcitizens=in.nextLong();
        System.out.print("Enter Tax Per Citizen:");
        taxpercitizen=in.nextDouble();
        if(iscapital  && nofcitizens>=100000||nofcitizens>=200000&&taxpercitizen*12*nofcitizens>720000000)
        {
            System.out.println("Yes This city is a Metropolis");
        }
        else
        {
            System.out.println(" This city is not a Metropolis");
        }
    }
    

}

Output:: 

Sunday, March 5, 2017

Method Overloading In Inheritance Java

If there is a method in subclass having same name as that of method in superclass but these have some difference in number and type of arguments then Method Overloading Takes place.

Code::
class d
{
    void show()
    {
        System.out.println("D's method");
    }
}
class e extends d
{
    void show(int x)
    {
        System.out.println("E's method");
    }
}
public class MethodOverriding {

    public static void main(String[] nt)
    {
        e ob=new e();
        ob.show();
        ob.show(67);
    }

}
Output: