Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Tuesday, April 7, 2015

How to print alphabet using char values in JAVA

First you have to code a java program by using variables and a flow control.

You may need:
  • a char variable to store current char value
  • flow control structure (for,while,or do while loop)
  • command to print current char value
First we create a class program and a main method using java

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

           }
}

Now create a char variable to store a char value and initialize it to zero.

class ascii{
          public static void main(String args[]){
                      char c=0; //char variable named c
          }
}

Now we want a flow control structure to automate printing process. Here I used for loop but you can use while or do while loop also.

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

                     char c=0; //char variable named c

                     for(c=65;c<91;c++){

                     }
          }
}

Here I have initialize c as 65 (c=65) because “A” in ASCII is 65 and terminate value as 91 because here I stop printing process after printing “Z”.

Now we want a command to print this in command prompt screen.

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

                    char c=0; //char variable named c

                    for(c=65;c<91;c++){

                               System.out.print(c+“ ”);

                    }
          }
}

So here print command print ascii value and space. This will run until variable reaches it's terminating value.

Here is the complete code.

Now save your java program with .java extension
After compiling(javac a1.java) run your java program with java “class name”


Then the final output is shown below.


------------------------
Note
------------------------

If you want simple letters you can start c=97 and terminating value as 123
eg: for(c=97;c<123;c++){}


Thanks.

Tuesday, March 31, 2015

Data Types in JAVA

Data Types

Premitive

Object

Integers
  • byte (-27 to 27-1)
  • short (-215 to 215-1)
  • int (231 to 231-1)
  • long (263 to 263-1)
(reference data type or array data type) 

Ex: String
Floating point
  • float (231 to 231-1)
  • double(263 to 263-1)
Characters
  • char (0 to 216-1)
Boolean
  • boolean(true/false)

Sunday, March 29, 2015

Getting started with JAVA programming

Getting started with JAVA programming 

 Are you new to java? If yes here is the way to become a java expert. How to install JAVA Development Kit on your computer 

 Step 1 

      Download java development kit from oracle. 

       Click here to download. (Use the images to get an idea)


Click on accept



Select your download type 64 bit or 32 bit 
and wait until downlaod complete.
Step 2
            After download complete run the setup.
Step 3
            Do not change install location and set it to be default
Step 4
            Wait until installation to be complete.
Step 5
           Now you have JAVA installed in your computer.

Step 6
           Then you need to set path to compile a java program. Here is the way.

Go to your java installed location. In my case it may be c:\program files\java\jdk1.7.0\ (may vary due to java version)

create a new folder named “My docs”.



Open that folder.
Now you are inside the folder named “My docs”
Now open notepad and type start and save it as “cmd.bat” in “My docs” folder.



Now go to c:\program files\java\jdk1.7.0\ bin (your may be vary)


Now copy the path in your address bar

if you are a window 8 user follow these steps;

  1. Drag the Mouse pointer to the Right bottom corner of the screen
  2. Click on the Search icon and type: Control Panel
  3. Click on -> Control Panel -> System -> Advanced System Settings
  4. Click on Environment Variables, under System Variables, find PATH, and double click on it.

  5. Now click on variable value field, put a ; at end of the value.

  6. Now paste copied path after the semi colon
  7. click on ok then again ok and ok.

Now go to c:\program files\java\jdk1.7.0\My docs
Now open CMD.bat file
Then type “javac” and press enter
If you get text like in the image below you are successful.


Now it is time to code java.

How to start java coding

Now you can start to code java. Here is an example
Open notepad .Type these text
class a1{

         public static void main(String args[]){

                     System.out.println("Hello JAVA");

        }

}
Now save this as "a1.java" in "My docs" folder.
Now open "CMD.bat" and type javac a1.java and press enter
Then if it was successful type java a1 and press enter.
Now in cmd you can see a text "hello JAVA".
Now do not close cmd keep it minimized and code some more java program.then save it.
Now again type javac and then type name of your saved java program with .java extension .
Then type java and  saved java program and press enter.


-------------------------------------------------------------

RECOMMENDED BY AUTHOR