Java4teachus

Thursday, January 31, 2019

What is length & Accepting The Dynamic Input to The Java Program

Accepting dynamic input to the Java program 

In Java program we accept the data dynamic we have 4 approaches.
1) through command prompt
2) through keyboard
3) through properties file (or) resources bundle file.
4) through XML document file

LENGTH 

Length is one of the "Implicit variable" created during at the compile time by the "Java environment" and it is available to the entire Java program for two purpose. They are:

Define-Length-&-Accepting-the-dynamic-values-to-the-java-program.jpeg
  1. It finds number of elements present in an "Array" [determine the size of an array like [5] ]
  2. Internally JVM will treat "All Fundamental Arrays" .
Ex:- int [ ], float[ ], char[ ].

Syntax :-     Array name.length 

(Here Array name represents name of the array variables).

Example:-1   int a [4] = { 10,20,30,40};
                       System.out.println ( " Number of elements ="+ a. length ) ;// 4
                       for ( int i=0 ; i< a.length ; i++ )
                        {
                        System.out.println (a [ i ]);
                        }

Example:-2    float x [2] = { 10.1,20.1 };
                        System.out.println ( " Number of elements = "+ x.length);//2
                        for ( int i=0; i< x.length ; i++)
                        {
                        System.out.println( x [ i ] );
                        }

Example:-3   String s [ ] = { "C++" , " Java ", " Oracle" };
                       System.out.println ( "Number of elements = " + s.length ) ; //3
                       for ( int i=0; i< s.length; i++)
                       {
                       System.out.println( s[ i ] );
                       } 


Consider the following statements:

Accepting the data from dynamic form in the below following small example.


EX:-  D://Java >                               Java   MulDemo          10  20
         Command prompt                    Java program             values

The values passing through the Java program from command  prompt " Command Line Argument " .
When we pass (or) enter the "command line argument" internally the following steps will be executed and makes to an  their makes to an understand arguments passing to which method, residing in which method and how to process.
  • Class Loader Subsystem loads "MulDemo" along with command line arguments [ 10,20 ].
  • JVM takes loaded class first as "MulDemo" along with command line argument [ 10,20 ]  and accounted then ( EX:- length L ) 
  • JVM looks for main ( String k [ 2 ] ) [ 10,20 ];.
  • JVM calls the main  ( String k [ 2 ] ) [ 10,20 ]; as "MulDemo" as main(string k [ 2 ] ) [10,20];
  • Hence all the  command line argument is taken by the JVM and it passing into the "main(String k[] )" and available in the form of "Array of Object of String class". ( EX:- String k( [ ] ) 
NOTE :The reason for taking array of object of string class is that to accept command line  arguments.

Q. write a java program which will accept the dynamic input from command line arguments through the command prompt.

Program

class CommandLineArguements
{
public static void main(Strings k[])
{
System.out.println("Command Line Arguemants");
for (int i=0; i<=k.length;i++)
{
System.out.println(k[i]);
}
}

Q   Write a Java program which will accept the command line arguments .

Program :-

class CmdLine
{
public static void main(String l[])
{
System.out.println("Number of cmd line args ="+l.length);
System.out.println("Command Line Arguments");
System.out.println("========================");
for (int i=0; i<l.length; i++)  //Here For Loop Condition start.
{
System.out.println( l [i] );
}
System.out.println("=========================");
} // main()
} //cmdline class

Q.  Write a Java program which will accept the data & shown student information from the command line arguments .



Program :-

class StudentInfo
{
public static void main(String k[])
{
System.out.println("Number of cmd line args ="+k.length);
System.out.println("Command Line Arguments");
System.out.println("========================");
for (int i=0; i<k.length; i++)  //Here For Loop Condition start.
{
System.out.println( k [i] );
}
System.out.println("=========================");
} // main()
} //StudentInfo class

The below image is shown that the  output for above program.

Command-Line-arguments-program.jpeg

Thursday, January 24, 2019

A Sample Java Programs & compilation

A Sample java programs


Q. write a Java program which will display the sum of two number ?

program:

class Sum                 //Sub class
{
int a,b,c;                     // Data members
void set( )                  // non - static methods
{
a=10;
b=20;
}
void sum( )                 // Method Declaration 
{
c=a+b;
}
void display( )           // Display the values
{
System.out.println("value of a="+a);
System.out.println("value of b="+b);
System.out.println("Sum="+c);
}
}//Business logic

class SumDemo    // Mainclass
{
public static void main(String args[])    // Main Method
{
Sum s=new Sum( );    // Object declaration with sub class called Sum
s.set( );                           
s.sum( );                        // methods callings
s.display( );
}
}// Execution logic

➩ Compile the above java program in command prompt.

cd> javac Sum.java ⤶

cd> java Sum 
Output:-  30.


                                  

Qwrite a Java program which will display the multiply of two number ?.

Program:


class Mul 

{
int a,b,c;
void set()
{
a=10;
b=20;
}
void mul()
{
c=a*b;
}
void display()
{
System.out.println("value of a="+a);
System.out.println("value of b="+b);
System.out.println("multiplication="+c);
}
}
class MulDemo
{
public static void main(String args[])
{
Mul m=new Mul();
m.set();
m.mul();
m.display();
}
}

Q . write a Java program which will display the "Division of two number" ?.

Program:-


class Division 

{
int a,b,c;
void set()
{
a=4;
b=2;
}
void mul()
{
c=a/b;
}
void display()
{
System.out.println("value of a="+a);
System.out.println("value of b="+b);
System.out.println("multiplication="+c);
}
}
class DivisionDemo
{
public static void main(String args[])
{
Division m=new Division();
m.set();
m.mul();
m.display();
}
}

Q. write a Java program which will display the  "Area Of Triangle" ?.

Program :

class Triangle
{
int b,h,a;
void set();
{
b=1;
h=2;
}
void areaOfTriangle()
{
a = 0.5*b*h;
}
void display()
{
System.out.println("value of b="+b);
System.out.println("value of h="+h);
System.out.println("AreaOfTriangle="+a);
}
}
class TriangleDemo
{
public static void main(String args[])
{
Triangle t=new Triangle();
t.set();
t.areaOfTriangle();
t.display();
}
}

Q. write a Java program which will display the  "PerimeterOfRectangle" ?.

program:

class PerimeterOfRectangle
{
int l,b,p;
void set()
{
l=10;
b=20;
}
void perimeterOfRectangle()
{
p=2*l*b;
}
void display()
{
System.out.println("value of l="+l);
System.out.println("value of b="+b);
System..out.println("perimeterofrectangle="+p);
}
}
class PerimeterDemo
{
public static void main(String args[])
{
PerimeterOfRectangle p= new PerimeterOfRectangle();
p.set();
p.perimeterOfRectangle();
p.display();
}
}


Q. write a Java program which will display the  "AreaOfTrapezoid" ?.

program:

class AreaOfTrapezoid
{
int b1,b2,h;
float a;
void set()
{
b1=2;
b2=3;
h=4;
}
void areaOfTrapezoid()
{
a=b1+b2*h*0.5;
}
void display()
{
System.out.println("value of b1="+b1);
System.out.println("value of b2="+b2);
System.out.println("areaoftrapezoid="+a);
}
}
class AreaDemo
{
public static void main(String args[])
{
AreaOfTrapezoid a=new AreaOfTrapezoid();
a.set();
a.areaOfTrapezoid();
a.display();
}
}


Q. write a Java program which will display the  "SlopeOfLine" ?.

Program :

class SlopeOfLine
{
int x1,x2,y1,y2;
float m;
void set()
{
x1=1;
x2=2;
y1=2;
y2=4;
}
void slopeOfLine()
{
m=y2-y1/x2-x1;
}
void display()
{
System.out.println("value of x1="+x1);
System.out.println("value of x2="+x2);
System.out.println("value of y1="+y1);
System.out.println("value of y2="+y2);
System.out.println("slopeofline="+m);
}
}
class SlopeDemo
{
public static void main(String args[])
{
SlopeOfLine s= new SlopeOfLine();
s.set();
s.slopeOfLine();
s.display();
}
}


Q. write a Java program which will display the  "Volume of a Cone" ?.

Program :

class VolumeOfCone
{
int b,h;
float v;
void set()
{
b=2;
h=3;
}
void volumeOfCone()
{
v=b*h*0.3;
}
void display()
{
System.out.println("value of b="+b);
System.out.println("value of h="+h);
System.out.println("volume of a cone="+v);
}
}
class ConeDemo
{
public static void main(String args[])
{
VolumeOfCone= new VolumeOfCone();
s.set();
s.volumeOfCone();
s.display();
}

}


Q. write a Java program which will display the  "SurfaceAreaOfCyclinder" ?.

Program :

class SurfaceAreaOfCyclinder
{
int r,h;
float pi,s;
void set()
{
r=2;
h=3;
pi=3.14f;
}
void surfaceAreaOfCyclinder()
{
s=2*pi*r*h;
}
void display()
{
System.out.println("value of r="+r);
System.out.println("value of h="+h);
System.out.println("SurfaceAreaOfCyclinder="+s);
}
}
class AreaOfCyclinder
{
public static void main(String args[])
{
SurfaceAreaOfCyclinder= new SurfaceAreaOfCyclinder();
s.set();
s.surfaceAreaOfCyclinder();
s.display();
}
}

Compile and set the class path in java:

Now we will discuss about on  how to set the class path for compiling  executing the java program. The  given below points are  understand that how to compile the java program piratically. 

1) Firstly we can create a folder/directory  in any drive option of my computer and save your program in that folder .  Here i will create a folder in  D: (Drive)  with name as Java .

Here i  save the program with file name in that folder with extension  called  ".java".
Example:- D: Java ➡️ FirstProgram.java (File name)

2)  Compile the Java program like this .
 Example:-  D://cd  java ➡️ D://java> Javac FirstProgram.java (file name) 

3) Error will occur like this "Javac is not recognized as internally and externally command ".

4) Now, we have to set the path for javac & java tools .

Example :- D:// java>set path="C:\Program Files\Java\ jdk 1.8.0_144\bin";

5) Again we recompile the java program .
Example :- D:// java> javac  FirstProgram.java

6) To ensure that FirstProgram.class file name is generated when we compile the java program .  

7) Run the java program .
Example:-  D://java>java FirstProgram 

When the above statements is executed the following operations will be performed.
  1. ClassLoader subsystem loads/transfer FirstProgram.class file in to "MainMemmory" .
  2. JVM takes loaded classes (FirstProgram).
  3. JVM looks for main method  in the form of Array Of Object Of String class "(String args [])" .
  4. JVM calls main (String args[]) with respect to  the loaded classes as FirstProgram.java
  5. Finally we get the output of the program.

Wednesday, January 09, 2019

Structure Of a Java Program


The standard format released by language developers and which is followed by the language programmers for developing the real world application is called 'Structure of a Program'.

Structure-Of-a-Java-Program.jpeg

Syntax:

package Information;
class <Class Name>
{
Data Members ;
Methods ;
public static void main(String args[])
{
Block of Statement's
}
}

Explanation 


1) Package Information:  represents collection of  'classes, interfaces & sub packages'. For each sub package contain's collection of classes, interfaces and sub-sub packages etc . If we use predefined of any class interfaces as a part of java program and whose specify the classes & interfaces presents (Other wise we get compile time error's). Out of many packages in java , One of the package is " Java.lang.* " is a default package directly imported for each & every java program . Hence this package is as called " Default package ".

2) Each & every java programmer must be start with the concept of a class . A class is a keyword used for development of  "Programmer Defined Data Type " . 
      
           Ex : - Student  S = new Student( );

3) <class name> is one of the Java Valid Variable Name is treated as a name of the  keyword and it is pro-grammatically consider as " Programmer Defined Data Type ".

4) Data Members represents the either " Instance " or " Static " and that are selected based on the class name.

5) Programmer defined method represents " Instance " or "Static" and meant  for performing different type of operations either repeatedly or only once.

6)  Since each & every java program executes starts from "main(String args[])" and it is called as program drivers. (pro-grammatically it is consider as "Sou-do" method whose information within  java compiler & JVM ).

7)  Since "public static void main(String args[])" method of java execute only once and hence its nature must be "Static".

8)  Since "public static void main(String args[])" method of java can be used by java programmer in any class and hence its access modifier must be "Public".

9) Each & every "public static void main(String args[])" method of java takes  in the form of  "Array Of Object Of String Class" ( String args[] ). 

10) "Block of statements or Set of executable statements", which are internally calling programmer defined method either with object or respect to class.

11)  The file name of a java program is that whatever class name contain main method then the class name must be given with an extension ".java". 


Q . Write a Java program which will display the "Hello World" message ? 

program:

class Sample
{
public static void main(String args[])
{
System.out.println("Hello World");
System.out.println("This is First Program");
System.out.println("BY Java4TeachUs.blogspot.com");
}
}


steps-for-compiling-the-java-program.jpeg


➪Steps for compiling the java program:

The following steps sequence will compile and execute the java program.
  • Write the source code and save it on same file name with an extension called ".java".
          Ex:- Sample.java
  • Compile the java program as follows. 
          Syntax :            javac Sample.java
          Example :        javac Sample.java
  • Is there any error's then find the error's & solve and save the program.
  • If there is no error's are not found .
  • Then java compiler generates on intermediate file with an extends called "class".
          Ex:-Sum.class
  • Finally execute the java program as like this.
          Ex:- java Sum


Get the executable program and output will be shown as below .

Sample-Program.java


The above diagram "Java Javac" are two tools (or) application programs (or) executable files developed by sun micro system developers and available as a part of Java/bin/direction (or) folders and they are used for compiling & executing the Java program executable.

Requirement Analysis for development of Java application.

 1) Software Requirement : Download JDK  1.5 / 1.6 / 1.7 / 1.8
                                                          (Freely download from www.sun.com)
 2) O.S  Requirement         : Any O.S.can be used because Java is plod form independent.
 3) IDE  Requirement        : (Integrated Development Environment)


An IDE is one of the third party developed by third party members which is released to real world and used by each & every programmer for development real world application of obtaining of solve in built facilities.

EX:- 1) DOS Editor
             Note pad(poor IDE)
             word pad etc
         2) Edit plus (- ES Enterprises } Best for learners
         3) Net beans (Best IDE'S)
             My Eclipse
          
In the above IDE are mostly commonly Used in real industry for developing the real time applications .

Thursday, January 03, 2019

System.out.println( ) & System.out.print() in Java

What is System.out.println() & System.out.print() in java. Basically in java these statements are used for print the values in the output . Here we discuss about the 2 statements and about their explanation.

In Java interview point of view this is very useful for fresher learner and beginner who want to know about the meaning of "System.out.println();"

➧ System.out.println( ); ------> 1 statement
➧ System .out.print( );    ------> 2 statement
  1. Statement 1 is used to displaying result of Java program on this console in form of "line/line".
  2. Statement 2 is used for displaying result of Java program in the "same line".

Explanation

  • print( ) & println( ) are the two predefined instance methods present in predefined class called "PrintStream".
  • To access print() & println() methods we need an object of  "PrintStream" class.
  • An object of  PrintStream class called "Out" is created as a static data member in an another predefined class called "System".
  • Hence access print()& println() are two methods then we need to write as follows:
               System.out.println( ) ;
              System.out.print( );


Consider the following old segment which makes understand Technical coding of statements.

Program:-


     class PrintStream
     {
       void print(....)
       {
        ..........
       }
       void println(.....)
       {
        .......
        .......
        }
    class System
      {
       ...........
      Static final PrintStream out = new PrintStream( );
       ...........
      }
   }

Example:- 

Hello Java World

System.out.println( "Hello Java World");

Example:-1 

int a = 10;

System.out.println( a);                                 // 10
System.out.println( "value of a="+a);         // value of a=10
System.out.println(a+"is the value of  a");  // a is the value of 10.

Example:-2 

int a = 10,int b= 20,int c = a+b;

System.out.println( "Sum ="+c);                                 // sum =30
System.out.println(c+' is the sum ");                           // 30 is the sum
System.out.println( " sum of "+a+"&"+ b+ "=" +c);   // sum of 10 and 20 = 30

Example:-3

( Java. 'J' James Gosling )

System.out.println ("Java"+ " ' " + "J" + " ' " + "James Gosling");