Monday 17 December 2012

Read Password from Console

import java.io.*;

class Abc
{
public static void main(String args[])
{
System.out.println("hello");
Console console=System.console();
console.printf("Enter Password\n");

char a[]=console.readPassword();
for(int i=0;i<a.length;i++)
{
console.printf("%c",a[i]);
}
}
}

Sunday 16 December 2012

Contructor Output

 
class MyClass{
 
int x=100;
int y=200;
 
void MyClass(){
System.out.print(x+" ");
System.out.print(y+" ");
x=30;
y=40;
}
}
class Demo{
public static void main (String []args){
MyClass c=new MyClass();
System.out.print(c.x+" ");
System.out.print(c.y+" ");
}
}
 
Thus this is function void MyClass to declare constructor we should not use void .

How java programs store in Heap and Stack.

Stack values are stored within the scope of the function. Once they are returned they are discarded.
JAVA only creates primitives on stack.Objects are created on heap memory.

Stack section contains methods local variables and reference variables.
Heap section contains objects.
Static section contains static data/methods.

public static void main(String args[]){
 
A parent = new A();
 
//more code
 
}
 
class A{
 
B child = new B();
 
int e;
 
//more code
 
}
 
class B{
 
int c;
 
int d;
 
//more code
 
}
 
In this case , the reference variable “child” will be created in heap ,which in turn will be pointing to its object, something like the diagram shown below.
 
 

Friday 14 December 2012

Mean of n numbers using Scanner class

import java.util.Scanner;



public class apples {


    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);

        System.out.println("How many values you want to use: ");

        //get number

         int n = input.nextInt();

        //create array with size n

         int[] array = new int[n];

        //get the input numbers using for loop

        for (int m = 0; m < n; m++) {

            System.out.print("Value nr  " + m + " : ");

            array[m] = input.nextInt();

        }

        System.out.println("Mean is: " + getMean(array));

    }

    public static double getMean(int[] n){

        double sum = 0;

        for (int m = 0; m < n.length; m++) {

        sum+=n[m];

        }

        return ((double)sum) / n.length;



    }

}

Thursday 13 December 2012

Hash Map

How to write a Java method that stores the months of the year and the number of days of the corresponding month in a collection class, then goes through the collection and prints the name of the months that only have 30 days 

It contains unique element.
It is an unordered Set.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class CollectionDemo
{
    public static void main(String[] args)
    {
        HashMap<String, Integer> map=new HashMap<String, Integer>();
        map.put("jan", 31);
        map.put("feb", 28);
        map.put("mar", 31);
        map.put("apr", 30);
        //So on u can add all
        //Now iterate through whili loop
       
        Iterator itr=map.entrySet().iterator();
       
        while (itr.hasNext()) {
            Map.Entry<String, Integer> ob=(Map.Entry<String, Integer>)itr.next();
            if(ob.getValue()==30)
            {
            System.out.println("key->"+ob.getKey());
            System.out.println("value->"+ob.getValue());
            }
       
        }
    }
}

Output:

key->apr
value->30
 

this Keyword

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

public class ThisDemo
{
    int a, b;
    ThisDemo(int a,int b)
    {
        this.a=a;   //a is local and this.a is global
        this.b=b;
    }
    public static void main(String[] args)
    {
        ThisDemo ob=new ThisDemo(10, 20);
        System.out.println(ob.a);
        System.out.println(ob.b);
       
    }
}




Output:
10
20
 


Another Example


public class ThisDemo
{
    int a, b;
    ThisDemo()
    {
        this(1,2);
    }
    public ThisDemo(int i, int j)
    {
        a=i;
        b=j;
    }
    public static void main(String[] args)
    {
        ThisDemo ob=new ThisDemo();
        System.out.println(ob.a);
        System.out.println(ob.b);
      
    }
}
 

Output:
1
2