Thursday, March 16, 2017

UnCaught Exception

Exception is a run time error which is caused due to Programmer error,Input error and others abnormal condition which leads to termination of the program.

UnCaught Exception is that which is handled by java run time system by default handler and leads to abnormal termination of program.

Code::

import java.util.*;
public class Exception 
{

    
    public static void main(String[] nt) 
    {
        Scanner in =new Scanner(System.in);
        System.out.print("Enter any Number:");
        int a=in.nextInt();
        int b=12345/a;
        System.out.println("Result is :"+b);
    }
    
}

Output::

Friday, March 10, 2017

Classes and Objects Python

Following are Some Examples to illustrate the concept:

Code::
1.

class rectangle:
    def __init__(self,w,h):
        self.w=w
        self.h=h
   
    def area(self):
        self.area=self.w*self.h
        return self.area
   
    def perimeter(self):
        self.perimeter=2*(self.w+self.h)
        return self.perimeter
   
    def issquare(self):
        if(self.w is self.h):
            return True
        else:
            return False
   
    def getw(self):
        return self.w
   
    def geth(self):
        return self.h
   
r=rectangle(5,5)
print("Width:",r.getw(),"Height:",r.geth())
print("Area is:",r.area())
print("Perimeter is:",r.perimeter())
print("IsSquare:",r.issquare())

Output::
2.

class complex:
    def __init__ (self,a,b):
        self.r=a
        self.i=b
   
    def add(self,sec):
        r=complex(0,0)
        r.r=self.r+sec.r
        r.i=self.i+sec.i
        return r
       
    def sub(self,sec):
        r=complex(0,0)
        r.r=self.r-sec.r
        r.i=self.i-sec.i
        return r
   
    def mul(self,sec):
        r=complex(0,0)
        r.r=self.r*sec.r
        r.i=self.i*sec.i
        return r
   
    def div(self,sec):
        r=complex(0,0)
        r.r=self.r/sec.r
        r.i=self.i/sec.i
        return r
   
    def __str__ (self):
        return(str(self.r)+"+"+str(self.i)+"i")
   
c1=complex(2,3)
c2=complex(3,1)
print("Addition is:",c1.add(c2))
print("Subtraction is:",c1.sub(c2))
print("Multiplication is:",c1.mul(c2))

print("Division is:",c1.div(c2))

Output::

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::