Tuesday, 19 August 2014

How To Write Java Program In Eclipse IDE

Create a Java project. Click the drop down arrow beside the first icon "File" at the top left of the screen called “new.” Select “Java Project." Give your new project a name and click finish. I will call my project "stringconcat" You can see that a new folder with your project name appears in the left hand column



Highlight the folder you have just created. Now click the “new” drop down menu once again. This time, pick "Class." Give your new class a name. For this class, we can simply use the default settings. Click finish and Eclipse will open a new Java class in the program window. I called my class "persons.”


Eclipse automatically creates a class in the program window. My class uses a method called read() to sum two values For those who are interested, I have included the code at the bottom  


ItS is time to create our main method. Go back to the drop down menu called “new” and, once again, pick “Class.” This time, check the box that says “public static void main(String[]args).” This indicates to Eclipse that you want to create a main method. Give your main class a name and click finish. I will call mine person. Eclipse now opens a new tab with your main class.


This main class person  will take input from the user . It then passes this information to the persons  class and gets the calculation. Then the program displays the total.
Code sample :

Main class :

package com.abc.com;
import java.util.Scanner;

public class person {

    /**
     * @param args
     */
    @SuppressWarnings("null")
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        persons p1=new persons();
        Integer x1,x2,x;
        @SuppressWarnings("unused")
        Scanner input=new Scanner(System.in);
        System.out.println("enter a value");
        x1=input.nextInt();
        System.out.println("enter a value");
        x2=input.nextInt();
        x=p1.read(x1, x2);
        String string1,string2;      
        string1="hello";
        string2 ="world";
        System.out.println(x);
        System.out.println(string1 + string2);





Another class receiving value from base class :

package com.cnk.com;

public class persons {
   
    public Integer read(Integer a,Integer b)
    {
        Integer c;
        c=a+b;
        return c;
       
       
    }

}



Compile/Test:

Click the "run" button (which looks like a "play" icon) that is located just above your program window. This action will automatically save and compile your code. Your program will run in the console window just below the program window. Check to see if your program works correctly. 





      










No comments:

Post a Comment