Tuesday, May 14, 2013

Tutorial 17: Introduction to Interfaces


 

Code:

SamsungElectronics.java
package InterfaceDemo;

/**
 *
 * @author Ankit
 */
public interface SamsungElectronics {
    void play();
    void stop();
    void pause();
}

SamsungLEDTv.java

package InterfaceDemo;

/**
 *
 * @author Ankit
 */
public class SamsungLEDTv implements SamsungElectronics{

    @Override
    public void play() {
        System.out.println("LED TV is playing now");
    }

    @Override
    public void stop() {
        System.out.println("LED Tv has Stopped now");
    }

    @Override
    public void pause() {
        System.out.println("LED Tv has paused now");
    }
   
}

SamsungLCDTv.java

package InterfaceDemo;

/**
 *
 * @author Ankit
 */
public class SamusungLCDTv implements SamsungElectronics{

    @Override
    public void play() {
        System.out.println("LCD Tv is playing now");
    }

    @Override
    public void stop() {
        System.out.println("LCD Tv is stopped now.");
    }

    @Override
    public void pause() {
        System.out.println("LCD Tv is paused now");
    }
   
}

MainSamsung.java

package InterfaceDemo;

/**
 *
 * @author Ankit
 */
public class MainSamsung {
    public static void main(String[] args) {
        SamsungLEDTv s1 = new SamsungLEDTv();
        s1.play();
        s1.pause();
        s1.stop();
       
        SamusungLCDTv s2 = new SamusungLCDTv();
        s2.play();
        s2.pause();
        s2.stop();
    }
}

Monday, May 13, 2013

Tutorial 16: Abstract Classes in java




Code:

AbstractClass.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AbstractDemo;

/**
 *
 * @author Ankit
 */
public abstract class AbstractClass {
    int x;
    int y;
   
    public void getData(){
        System.out.println("im in getData of Ab Class...");
    }
   
    public abstract void getData(int a, int b);
}


AbstractSubClass.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AbstractDemo;

/**
 *
 * @author Ankit
 */
public class AbstractSubClass extends AbstractClass{

    @Override
    public void getData(int a, int b) {
        System.out.println("I am in getData of subclass...");
    }
   
}

AbstractClassMain.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AbstractDemo;

/**
 *
 * @author Ankit
 */
public class AbstractClassMain {
    public static void main(String[] args) {
        AbstractClass ob1 = new AbstractClass() {

            @Override
            public void getData(int a, int b) {
                    System.out.println("im in getDAta with arguments of Main class....");
            }
        };
       
        ob1.x = 10;
        ob1.y = 20;
        ob1.getData();
        ob1.getData(ob1.x, ob1.y);
       
        AbstractSubClass ob2 = new AbstractSubClass();
        ob2.getData(10, 20);
    }
}


Note:

If a class extends an abstract class, it has to implement (override) all the abstract methods of that abstract class.

Sunday, May 12, 2013

Tutorial 15: Static Methods and Static Blocks in Java




Code for this tutorial:

MainStaticDemo.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package StaticDemo;

/**
 *
 * @author Ankit
 */
public class MainStaticDemo {
    static{
        System.out.println("I am before main....");
    }
   
    //javac MainStatic.java
    //java MainStatic
    public static void main(String[] args) {
       
        StaticVariables.iNo1 = 0;
        StaticVariables o1 = new StaticVariables();
        StaticVariables o2 = new StaticVariables();
           
       
        //o1 has its own iNo2;
        //o2 has its own iNo2;
       
        o1.iNo2 = 10;
        o2.iNo2 = 20;
       
        //o1 and o2 shares iNo1; /// static
       
        System.out.println("o1's no2 is:"+o1.iNo2);
        System.out.println("o2's no2 is:"+o2.iNo2);
        System.out.println("iNo1 is:"+StaticVariables.iNo1);
       
       
        StaticVariables.callMeStatic();
       
       
       
               
    }
}


StaticVariables,java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package StaticDemo;

/**
 *
 * @author Ankit
 */
public class StaticVariables {
    static int iNo1;
    int iNo2;

    public StaticVariables() {
        iNo1++;
    }
   
    public static void callMeStatic(){
        System.out.println("I am from static method of class StaticVari.");
    }
   
}

Saturday, May 11, 2013

Tutorial 14: Static Variables


Tutorial 14: Static Variables

Sample Code:  

MainStaticDemo.java
package StaticDemo;

/**
 *
 * @author Ankit
 */
public class MainStaticDemo {
    public static void main(String[] args) {
       
        StaticVariables.iNo1 = 0;
        StaticVariables o1 = new StaticVariables();
        StaticVariables o2 = new StaticVariables();
           
       
        //o1 has its own iNo2;
        //o2 has its own iNo2;
       
        o1.iNo2 = 10;
        o2.iNo2 = 20;
       
        //o1 and o2 shares iNo1; /// static
       
        System.out.println("o1's no2 is:"+o1.iNo2);
        System.out.println("o2's no2 is:"+o2.iNo2);
        System.out.println("iNo1 is:"+StaticVariables.iNo1);
       
       
               
    }
}


StaticVariables.java

 package StaticDemo;

/**
 *
 * @author Ankit
 */
public class StaticVariables {
    static int iNo1;
    int iNo2;

    public StaticVariables() {
        iNo1++;
    }
   
}

Friday, May 10, 2013

Tutorial 13: Multilevel Inheritance in java


Tutorial 13: Multilevel Inheritance in Java

Code:

Computer.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MultilevelInheri;

/**
 *
 * @author Ankit
 */
public class Computer {
    private int totalUSBPorts;
    private int totalRAMinGBs;
   
    public void start(){
        System.out.println("yes i can start!!!");
    }
    public void shutdown(){
        System.out.println("yes i can shutdown!!!");
    }

    public int getTotalRAMinGBs() {
        return totalRAMinGBs;
    }

    public void setTotalRAMinGBs(int totalRAMinGBs) {
        this.totalRAMinGBs = totalRAMinGBs;
    }

    public int getTotalUSBPorts() {
        return totalUSBPorts;
    }

    public void setTotalUSBPorts(int totalUSBPorts) {
        this.totalUSBPorts = totalUSBPorts;
    }
   
}

Laptop.java

package MultilevelInheri;

/**
 *
 * @author Ankit
 */
public class Leptop extends Computer{
    private int totalBettryCells;

    public int getTotalBettryCells() {
        return totalBettryCells;
    }

    public void setTotalBettryCells(int totalBettryCells) {
        this.totalBettryCells = totalBettryCells;
    }
   
}

MacBook.java

package MultilevelInheri;

/**
 *
 * @author Ankit
 */
public class MacBook extends Leptop{
   
}

MultiInheri.java

package MultilevelInheri;

/**
 *
 * @author Ankit
 */
public class MainInheri {
    public static void main(String[] args) {
        MacBook myMacBook = new MacBook();
       
        myMacBook.setTotalBettryCells(9);
        myMacBook.setTotalRAMinGBs(8);
        myMacBook.setTotalUSBPorts(4);
       
        System.out.println("my macbook is equipted with "+myMacBook.getTotalBettryCells()+"bettry Cells");
        System.out.println("my macbook is equipeted with "+ myMacBook.getTotalRAMinGBs()+"RAM");
        System.out.println("my macbook is equ with "+ myMacBook.getTotalUSBPorts()+"USBs");
    }
}

Thursday, May 9, 2013

Tutorial 12: Inheritance Getting Started


Tutorial 12: Inheritance Getting Started
Code:

Mom.java

package Inheritance;

/**
 *
 * @author Ankit
 */
public class Mom {
    private String strSkinTone;
    private String strEyes;

    public String getStrEyes() {
        return strEyes;
    }

    public void setStrEyes(String strEyes) {
        this.strEyes = strEyes;
    }

    public String getStrSkinTone() {
        return strSkinTone;
    }

    public void setStrSkinTone(String strSkinTone) {
        this.strSkinTone = strSkinTone;
    }
   
   
}

Child.java

package Inheritance;

/**
 *
 * @author Ankit
 */
public class Child extends Mom{
   
}

MainInheri.java

package Inheritance;

/**
 *
 * @author Ankit
 */
public class MainInheri {
    public static void main(String[] args) {
        Child John = new Child();
       
        John.setStrEyes("Brown");
        John.setStrSkinTone("Fair");
       
        System.out.println("John inherited eyes color from his mom: that is:"+John.getStrEyes());
        System.out.println("John inherited skintone from his mom and that is: "+ John.getStrSkinTone());
    }
}

Wednesday, May 8, 2013

Tutorial 11: Method Overloading


Tutorial 11: Method Overloading

Code: 

MethodOverloading.java

package Package2;


public class MethodOverloading {
    void printData(int a){
        System.out.println("value of a is:"+a);
    }
    void printData(int a, int b){
        System.out.println("value of a is:"+a);
        System.out.println("value of b is:"+b);
       
    }
    void printData(int a, int b, int c){
        System.out.println("value of a is:"+a);
        System.out.println("value of b is:"+b);
        System.out.println("value of c is:"+c);
    }
}

Tuesday, May 7, 2013

Tutorial 10: Constructor Overloading


Tutorial 10: Constructor Overloading
Code: 

MainConstOverloading.java 

package Package2;

/**
 *
 * @author Ankit
 */
public class MainConstOverloading {
    public static void main(String[] args) {
        MethodOverloading o1 = new MethodOverloading();
        o1.printData(10);
        o1.printData(20, 30);
        o1.printData(40, 50, 60);
    }
}

Monday, May 6, 2013

Tutorial 9: Introduction to Constructor for Rectangle class


Tutorial 9: Introduction to Constructor for Rectangle class
 Code:

MainRectangle.java 

package tutorial;
import tutorial.Rectangle;
/**
 *
 * @author Ankit
 */
public class MainRectangle {
    public static void main(String[] args) {
        //nameofclass objectname = new nameofclass();
        //instantiating the class
        Rectangle rectFirstRectangle = new Rectangle(10,20);
        Rectangle rectSecondRectangle = new Rectangle(50);
        Rectangle rectThirdRectangle = new Rectangle();
       
        //initialization of the data members of the Rectangle class
        //objname.datamembername;
        //rectFirstRectangle.setiLength(10);
        //rectFirstRectangle.setiWidth(20);
       
        //calculate area
        //access the methods of the class
        //int iAreaOfRectFirst = rectFirstRectangle.calcArea();
       
        //print out
       
        //System.out.println("area of my first rectangle is:"+ iAreaOfRectFirst);
        System.out.print("Area of my first rectangle of dimention:"+rectFirstRectangle.getiLength()+"*"+rectFirstRectangle.getiWidth());
        System.out.println(" is:"+rectFirstRectangle.calcArea());
       
         System.out.print("Area of my second rectangle of dimention:"+rectSecondRectangle.getiLength()+"*"+rectSecondRectangle.getiWidth());
        System.out.println(" is:"+rectSecondRectangle.calcArea());
       
         System.out.print("Area of my third rectangle of dimention:"+rectThirdRectangle.getiLength()+"*"+rectThirdRectangle.getiWidth());
        System.out.println(" is:"+rectThirdRectangle.calcArea());
    }
}

Rectangle.java

package tutorial;


public class Rectangle {
    //data memebers
    //variables of the class
    private int iLength;
    private int iWidth;
   
    //member functions
    //functions of the class
   
    int calcArea(){
        return (iLength*iWidth);
    }

    //constructors
    //two argument constructor with (int, int)
    public Rectangle(int iLength, int iWidth) {
        this.iLength = iLength;
        this.iWidth = iWidth;
    }

    //one argument constructor (int)
    public Rectangle(int iLength) {
        this.iLength = this.iWidth = iLength ;
    }

   
    //no argument constructor
    public Rectangle() {
        this.iLength = 0;
        this.iWidth = 0;
    }

    //do nothing constructor
//    public Rectangle() {
//    }