19 February 2014

Coding Tools

Notepad
  • Notepad can be used to write and edit java code.
  • The notepad file must be saved with '.java' extension.
  • Name of the file must be same as that of the class being written in it.
  • This file can be accessed with command prompt and can be compiled and run.
  • cmd to compile java file : javac <space>filename.java
  • If it shows no errors then the command to run java file is : java <space> filename

 Writing java code can be made simple using IDEs. A number of java IDEs are available on internet
Some of the IDEs for programming java are:
  • Eclipse
  • Netbeans
  • BlueJ
  • JCreator
  • IntelliJ Idea
  • JBuilder
If you are a beginner in programming then it is good to start practicing with notepad or else you can go with IDEs.

Out of all these IDEs Eclipse is the most preferrable one.It is a free software.You can download Eclipse at Download Eclipse                         
     

Starting with Eclipse

  • After downloading the eclipse IDE you need to extract the .ZIP file in to a folder in which you can find eclipse loader(eclipse.exe)
  • Now click on eclipse.exe and authorize the security warning(click run). You will be asked to select a workspace where all your eclipse files will be saved. Click browse and select a new empty folder and click OK.
  • Eclipse starts with a welcome screen. now open window > open prespective > java. Now you are ready to create and work with projects.

HOW TO CREATE A NEW PROJECT AND NEW CLASS. 

  • Goto File new Java Project and give some name to your project and click ok.
    New-project

  •  You can see your project created below the package explorer window on the left.
    Package-explorer

  • To create a new class right click on the project you just created select new and again select class.
    New-class

  •  A window appears in which name of the class is given(First letter of the class in CAPITALS). Check the box as shown below and click finish.
    class-details
  • Now a java file is created with a class name same as file name. Main method is also included in it.
  • The code that is to be executed is written inside the main method.

Program to display all prime number below a user entered number

Create a new Class with name Disp_primes:

import java.util.Scanner;

public class Disp_primes {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number : ");
        int num = sc.nextInt();
        System.out.println("prime numbers below "+num+" are :");
        int rem=0;
        if(num==2){
            System.out.print(num+" is even prime");
        }
        else{
            for(int i=num;i>2;i--){
                int count=2;
        while(count<i){
            rem = i%count;
            if(rem==0)
                break;
            else
                count++;
                    }
        if(rem!=0)
            System.out.print(i+",");
        }
            if(num>2){
                System.out.print("2");
            }
        }
    }

}

Output:(Click on image to enlarge):
Program-to-display-all-prime-number-below-a-user-entered-number-output
(Comment if you have any doubts)
>

Program to check if a number is Prime or not

Create a new Class with name Chk_prime :

import java.util.Scanner;
 

public class Chk_prime {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int rem=0,i=2;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number to check if it is prime : ");
        int num = sc.nextInt();
        System.out.println("You entered : "+num);
        if(num==2){
            System.out.print(num+" is even prime");
        }
        else{
        while(i<num){
            rem = num%i;
            if(rem==0){
                System.out.print(num+" is not prime");
            break;}
            else
                i++;
                    }
        if(rem!=0)
            System.out.print(num+" is prime");
        }
        }
    }


Output:(Click on image to enlarge)
Program-to-check-if-a number-is-Prime-or-not-output
(Comment if you have any doubts)

Program to check if a year is leap year or not

Create a new class with name Chk_leapyear

import java.util.Scanner;

public class Chk_leapyear {
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a year to check if it is leap year : ");
        int year = sc.nextInt();
        System.out.println("You entered : "+year);
        if(year%100==0){
            if(year%400==0)
                System.out.println(year+" is a leap year");
            else
                System.out.println(year+" is not a leap year");
        }
        else{
            if(year%4==0)
                System.out.println(year+" is a leap year");
            else
                System.out.println(year+" is not a leap year");
        }
    }

}

Output:(Click on image to enlarge)

Program-to-check-if-a-year-is-leap-year-or-not-output
Enter the year you want to check in the output window as shown above.
(Comment if you have any doubts)

First Java program

A demo file to initialize and display different variables:

  • Create a new class with name VariablesDemo:
public class VariablesDemo {

    public static void main(String[] args) {
   
        /* Declaration of variables  */
         int Id;
         String name;
         char gender;
         double salary;
    
         /* Initialization of variables*/
         Id = 4348;
         name = "Employer";
         gender ='M';
         salary = 25000.0;
    
        /* Display the employee details */
         System.out.println("Id : "+ Id);
         System.out.println("Name : "+ name);
         System.out.println("Gender : "+ gender);
         System.out.println("Salary : "+ salary);

        }
}
NOTE: 
  • The text between "/* " and "*/" will not be executed.They are used to write comments.
  • For single line comments use "//
 Output:(Click on the image to enlarge)
eclipse-output

Demonstration of Continue statement

Create a new class with name ControlContinue:

class ControlContinue {
    public static void main(String args[]) {
        //boolean bool = true;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 10; j++) {
                System.out.print(j + "\t");
                if (j > 5) {
                    System.out.println();
                    continue;
                }
            }
            System.out.println("Outer Loop");
        }
        System.out.println("End");
    }
}

Output:(Click on the image to enlarge)
Demonstration of Continue statement-output

Contact Form

Name

Email *

Message *

Smooth Graphics Java

// Arrange Bricks in Game //java brick breaker game code without flickering // added game levels 1,2,3 in the game //java paddle ball ga...