Pages

get our extension

Search program

Tuesday, 23 July 2013

what is the difference between instance variable and static variable(class variable)

Instance variable

--> instance variable is a variable whose seperate copy is availabe to each object.

--> instance variables are created in the objects on heap memory.





Instance_variable_example.java

class Instance_var
{
      int a=5;
       void display()
      {
            System.out.println("a="+a);
      }
      }
class Instance_variable_example
{
      public static void main(String[] args)
      {
            Instance_static obj1=new Instance_static();
            ++obj1.a;
            obj1.display();
            Instance_static obj2=new Instance_static();
            obj2.display();
           
      }

}

OUTPUT:

a=6
a=5


Static variable

 --> a static variable(also known as class variable) is a variable whose single copy in memory is shared by all objects.

--> static variables are stored on method area.



Static_variable_example.java

class Static_var
{
      static int a=5;
      static void display()
      {
            System.out.println("a="+a);
      }
      }
class Static_variable_example
{
      public static void main(String[] args)
      {
            Instance_static obj1=new Instance_static();
            ++obj1.a;
            obj1.display();
            Instance_static obj2=new Instance_static();
            obj2.display();
           
      }


}

OUTPUT:

a=6
a=6


Wednesday, 17 July 2013

Make Pendrive Bootable --> For Window 7 and Window 8 Operating System

STEPS:-

1.      C:\
2.      C:\> diskpart
                            à  opening a new window, for this click yes
3.      DISKPART > list disk
                       àSee the disk that is a pendrive ie disk 1
4.      DISKPART > select disk 1
       à Disk 1 is now the selected disk
5.      DISKPART > detail disk
6.      DISKPART > clean
       à DiskPart succeeded in cleaning the disk
7.      DISKPART > create partition primary
       àDiskPart succeeded in creating the specified  
                                partition
8.      DISKPART > select  partition 1
9.      DISKPART > active
10.   DISKPART > format fs=fat32
11.   DISKPART > assign
12.   DISKPART > exit
After the format is complete, put your original Windows 7 DVD in your DVD drive, or if you have an .iso image of the DVD, mount that image using any virtual drive software like Power ISO.
- Now go to My Computer, and open the contents of DVD Drive. Press ‘ctrl+a’ to select all contents and copy all the contents by pressing ‘ctrl+c’.
- Now go back to My Computer and open the USB Drive and press ‘ctrl+v’ to paste all the contents of DVD into the USB Disk.

create an entity class "Product" with following properties product_id, product_name, product_details, price. Add some sample data in this product class using collections. and sort and display

create an entity class "Product"  with following properties product_id, product_name, product_details, price. Add some sample data in this product class
using collections. and sort and display  :
-->on default by product_id
-->based on product_name or price
   -->sort on product_name, if two product_name is same sort on product_details basis.
  -->sort on price, if two prices are same consider sorting on product_id basis.


Product.java


package com.test;
import java.util.Comparator;

public class Product implements Comparable<Product>{
    private int productId;
    private String productName;
    private String productDescription;
    private double price;
   
             public Product()
            {     
            }
   
            public Product(int productId, String productName, String        
                             productDescription, double price)
           {
                this.productId = productId;
                this.productName = productName;
                this.productDescription = productDescription;
                this.price = price;
           }
                     public int getProductId()
                     {
                             return productId;
                     }
                     public void setProductId(int productId)
                     {
                           this.productId = productId;
                     }
                     public String getProductName()
                     {
                            return productName;
                     }

    public void setProductName(String productName)
     {
        this.productName = productName;
     }
     public String getProductDescription()
     {
        return productDescription;
     }
     public void setProductDescription(String productDescription)
     {
        this.productDescription = productDescription;
      }
    public double getPrice()
   {
        return price;
    }
    public void setPrice(double price)
    {
        this.price = price;
    }
    @Override
    public int compareTo(Product p)
    {  
        return this.productId-p.productId;
    }
    @Override
    public String toString()
    {
        return "price=        " + price + ", productDescription=           "
                + productDescription + ", productId=           " + productId
                + ", productName=              " + productName;
    }
   
    public static class ProductInnerClass implements Comparator<Product>
    {
        @Override
        public int compare(Product p1, Product p2)
        {
            int i=Double.compare(p1.getPrice(), p2.getPrice());
            if(i==0)
            {
                return p1.getProductId()-p2.getProductId();
            }
            return i;
        }       
    }  

}

ProductMain.java

package com.alok.entity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.alok.client.Product;
public class ProductMain {
    public static void main(String[] args) throws NumberFormatException, IOException {
        ArrayList<Product> productList=new ArrayList<Product>();
        productList.add(new Product(555, "Monitor", "15 inch", 4000.00));
        productList.add(new Product(2222, "Monitor", "17 inch", 5000.00));
        productList.add(new Product(3333, "Del", "Laptop", 4000.00));
        productList.add(new Product(6666, "Mouse", "Optical Mouse", 200.00));
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int i=1;
        while(i<3)
        {
            System.out.println("1 : Sort by id");
            System.out.println("2 : Sort by name");
            System.out.println("3 : Sort by price");
            int ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1 : Collections.sort(productList);
                         System.out.println("Sorted product : ");
                         for(Product p : productList)
                         {
                             System.out.println(p);
                         }
                         break;
                case 2 : Collections.sort(productList, new Comparator<Product>()
                         {
                                @Override
                         public int compare(Product p1, Product p2)
                         {
                         int i=p1.getProductName().compareToIgnoreCase 
                                                  (p2.getProductName());
                            if(i==0)
                            {
                            return p1.getProductDescription(). 
                             compareToIgnoreCase(p2.getProductDescription());
                            }                   
                                       return i;
                         }
                       });  
                System.out.println("Sorted product : ");
                 for(Product p : productList)
                 {
                     System.out.println(p);
                 }
                 break;
               case 3 :
             Collections.sort(productList,new Product.ProductInnerClass());
                    System.out.println("Sorted product : ");
                     for(Product p : productList)
                     {
                         System.out.println(p);
                     }
                     break;
                default : System.out.println("Invalid Option");    
                            System.exit(0);
            }
        }
    }
}


OUTPUT:


1 : Sort by id
2 : Sort by name
3 : Sort by price
1
Sorted product : 
price=        4000.0, productDescription=           15 inch, productId=           555, productName=              Monitor
price=        5000.0, productDescription=           17 inch, productId=           2222, productName=              Monitor
price=        4000.0, productDescription=           Laptop, productId=           3333, productName=              Del
price=        200.0, productDescription=           Optical Mouse, productId=           6666, productName=              Mouse
1 : Sort by id
2 : Sort by name
3 : Sort by price
2
Sorted product : 
price=        4000.0, productDescription=           Laptop, productId=           3333, productName=              Del
price=        4000.0, productDescription=           15 inch, productId=           555, productName=              Monitor
price=        5000.0, productDescription=           17 inch, productId=           2222, productName=              Monitor
price=        200.0, productDescription=           Optical Mouse, productId=           6666, productName=              Mouse
1 : Sort by id
2 : Sort by name
3 : Sort by price
3
Sorted product : 
price=        200.0, productDescription=           Optical Mouse, productId=           6666, productName=              Mouse
price=        4000.0, productDescription=           15 inch, productId=           555, productName=              Monitor
price=        4000.0, productDescription=           Laptop, productId=           3333, productName=              Del
price=        5000.0, productDescription=           17 inch, productId=           2222, productName=              Monitor
1 : Sort by id
2 : Sort by name
3 : Sort by price
4
Invalid Option