Monday, April 3, 2017

Digital Clock Java Using Applet

Following class extends Applet and implements Runnable.

Code::

import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.text.*;
public class DigitalClock extends Applet implements Runnable {
    Thread t;
    String Time="";
    
    public void init() 
    {       setSize(400,200);
            setBackground(Color.CYAN);
            t=new Thread(this);
            t.start();
    }

    public void run()
    {
       try
       {
           while(true)
           {
               Calendar c=Calendar.getInstance();
               SimpleDateFormat f=new SimpleDateFormat("hh:mm:ss "+" dd/MM/yyyy");
               
               Date d=c.getTime();
               Time=f.format(d);
               repaint();
               t.sleep(1000);
           }
       }catch(Exception e)
       {
           System.out.println("Error Occured");
       }
    }
    
    public void paint(Graphics g)
    {
        g.setColor(Color.RED);
        g.setFont(new Font("Dialog",Font.BOLD,50));
        g.drawString(Time, 100,100);
    }
}


Output::

Thursday, March 16, 2017

Exception Handling Using Try Catch Java

The statements which can throw exception are placed in try block and after the exception is thrown the catch block following the try block are executed and the program is not abnormally terminated.

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:");
        try
        {
        int a=in.nextInt();
        int b=12345/a;
        System.out.println("Result is :"+b);
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception: " +e);
        }
        
    }

}

Output::

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