Friday, 13 February 2015

Constructor Creation.

                                    HOW TO CREATE OBJECT OF CLASS??

1. The operator new is use to create a reference to the class.

2. It allocate's a memory the object of the class.

3. Constructors are used  to create Object of class.
     -It has same name as that of class
4. A default constructor is created by the java compiler during run-time,
       If the coder doesn't define any.
5. Code:
     
class Refer{           // class Refer.
  int a;
  
  Refer(int a){          // Parameterized  Constructor.
    System.out.println(a);
  }
  
    public static void main(String[]abc){

 Refer r = new Refer(10);   //passing value to variable a of Contructor.
 
}

}

6. Constructor is a method which have same name with the class and no return type.

7. Parameterized constructor means a value can passed as argument.

8. ScreenShot:


Prompting User For Input.


                                  How To Prompt User For Input??

1. Code :
    import java.util.Scanner; 

class Input{
   
   
   public static void main(String[] args){

   Scanner in = new Scanner(System.in);
     
System.out.println("Enter A String");
String str = in.nextLine();
 
   System.out.println("Enter A Number");
   int num = in.nextInt();
   
   
      System.out.println("Enter A Float Number");
      Float flt = in.nextFloat();
   
    System.out.println("Number is: "+str);
System.out.println("String is: "+num);
 System.out.println("Float Value is: "+flt);
   }
}

2.Scanner is class that helps to read/fetch the user's input.

3.Create reference ("in") to class Scanner to take the input.
   Example: Scanner in = new Scanner(System.in);
    --it can be anything of your choice.

4.import Scanner package from java.util.Scanner;

5.Take String Input at first, then followed by others.

6. nextLine for String input.
   nextFloat for Float input.
   nextInt for int input and so on.

7. ScreenShot with Steps to run:

Wednesday, 11 February 2015

The First Program.

                                                     FIRST  PROGRAM
                                     HELLO                                     JAVA

1.The Filename and class name with main method should be always same.

2.Class name should start with Capital letter.

3.Code.

class MyFirstProgram{
         public static void main(String[] args){
                    System.out.println("HELLO JAVA");
         }
}

4.Filename should be  MyFirstProgram.java.

5. It is the main method :  public static void main(String[] args).

6.main method has string array argument by default.

7.System.out.println();  It is used to print a value/value of a variable;

8.Screen Shot is Below.


How To Compile And Run .java File

                                                 COMPILE AND EXECUTE

1. Open Command Prompt.(windows+R, type "cmd" and Enter).

2.Change the directory to the folder which contains your .java file.

3.As of mine, it is "C:\Users\sahil paudel\Documents\java".

4. Type "cd C:\Users\sahil paudel\Documents\java", without quotes.

5.Now type javac (your .java filename),  As javac MyFirstProgram.java  Enter.

6.A bytecode class file will be created. Eg: MyFirstProgram.class  

7. Type java (your .class filename). Eg: java MyFirstProgram  Enter.

8.And You compiled and run your java program.