Java Tutorial

Java Programs

Package details:

Every program in JAVA must be developed with respect to class.

Data member's declaration represents the type of data members which we use as a part of the class.

User defined methods represents the type of methods which we use as a part of the class to perform some meaningful operation by making use of the data members of class.

Here main represents the name of the method where the program execution starts and void represents return type of main method which indicates main method does not return anything.

Since main method is executing only once hence it must be static method. Since main method can be called/accessed by everybody and hence it belongs to public method.

Block of statements represents the valid executable statements of JAVA which will call the user defined methods.

Syntax:

class <clsname>
{
    Data member's declaration; User defined methods;
    Public static void main (string k [])
    {
        Block of statements ();
    }
}

Write a JAVA program to display a message "welcome to JAVA"?

Answer:

class First
{
    Public static void main (string k []);
    {    
        System.out.println ("welcome to JAVA");
    }
} 

Note:

While giving the file name to a JAVA program we must give the file name as name of the class with an extension ".java" in which ever the class main method presents.

Whenever we give println it will prints in the new line whereas print prints in the same line.

Requirments for java program

Write a JAVA program which illustrates the concept of instance methods and static methods?

Answer:

class Second
{
    Void disp1 ()
    {
        System.out.println ("disp1-instance");
    }
    Static void disp2 ()
    {
        System.out.println ("disp2-static");
    }
    Public static void main (string k [])
    {
        System.out.println ("main-beginning");
        Second so = new Second ();
        so. disp1 ();
        Second. disp2 ();
        System.out.println ("main-end");
    }
} //second

Note:

  1. One static method can call another static method directly provide both the static method belongs to same class.
  2. One instance method can call another instance method directly provide both the static instance method belongs to same class.

Write a JAVA program for printing the data which we pass from command prompt?

Answer:

class Print
{
    public static void main (String k [])
    {
        System.out.println ("NUMBER OF VALUES ="+k.length);
        for (int i=0; i<;k.length; i++)
        {
            System.out.println (k[i] +" ");
        }
    }
};

Write a JAVA program which computes sum of two numbers by accepting the data from command prompt (DOS)?

Answer:

class Sum
{
    int a, b, c; //a,b,c are data members
    void assign (int x, int y) //x,y are formal parameters
    {
        a=x;
        b=y;
    }
    void add () //add () is business logic method
    {
        c=a+b;
    }
    void disp () //disp () is business logic method
    {
        System.out.println ("SUM OF "+a+" AND "+b+" = "+c);
    }
};
class SumDemo
{
    public static void main (String k [])
    {
        int x=Integer.parseInt (k [0]);
        int y=Integer.parseInt (k [1]);
        Sum so=new Sum ();
        so. assign(x, y);
        so. add ();
        so.disp ();
    }
};

Note:

The data which we pass through command prompt is called command line arguments.

Write a JAVA program to check weather the given number is prime or not?

Answer:

class Prime
{
    int n;
    void set (int x)
    {
        n=x;
    }
    String decide ()
    {
        int i;
        for (i=2; i&lt;n; i++)
        {
            if (n%i==0)
            { 
                break;
            }
        }
        if (i==n)
        { 
            return "PRIME";
        }
        else
        {
            return "NOT"+"PRIME";
        }
    }
};

class PrimeDemo
{
    public static void main (String k [])
    {
        int n=Integer.parseInt (k [0]);
        Prime po=new Prime ();
        po.set (n);
        String so=po.decide ();
        System.out.println (so);
    }
};

Write a JAVA program which converts an ordinary number into roman number ?

Answer:

class Roman
{
    int n;
    void set (int x)
    {
        n=x;
    }
    void convert ()
    {
        if (n<=0)
        {
            System.out.print ("NO ROMAN FOR THE GIVEN NUMBER");
        }
        else
        {
            while (n>=1000)
            {
                System.out.print ("M"); n=n-1000;
            }
            if (n>=900)
            {
                System.out.print ("CM"); n=n-900;
            }
            if (n>=500)
            {
                System.out.print ("D"); n=n-500;
            }
            if (n>=400)
            {
                System.out.print ("CD"); n=n-400;
            }
            while (n>=100)
            {
                System.out.print ("C"); n=n-100;
            }
            if (n>=90)
            {
                System.out.print ("XC"); n=n-90;
            }
            if (n>=50)
            {
                System.out.print ("L"); n=n-50;
            }
            if (n>=40)
            {
                System.out.print ("XL"); n=n-40;
            }
            while (n>=10)
            {
                System.out.print ("X"); n=n-10;
            }
            if (n>=9)
            {
                System.out.print ("IX"); n=n-9;
            }
            if (n>=5)
            {
                System.out.print ("V"); n=n-5;
            }
            if (n>=4)
            {
                System.out.print ("IV"); n=n-4;
            }
            while (n>=1)
            {
                System.out.print ("I"); n=n-1;
            }
            System.out.println ();
        }
    }
};

class RomanDemo
{
    public static void main (String k [])
    {
        int n=Integer.parseInt (k [0]);
        Roman r=new Roman ();
        r.set (n);
        r.convert ();
    }
};