accounting+ inventory + remuneration management

Java Programs

You must set path variable to compile and run java programs as follows:
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Java\jdk1.7.0_25\bin
then must restart your computer.

to run java program type :
c:\myjavaprog>javac hello.java

The javac compiler creates a file called hello.class that contains the bytecode version of the program.
The java bytecode is the intermediate representation of your progrtam that contains instuction the java interpreter will execute.
So the output of javac, bytecode is not code that can be directly executed.
To run the java program, you must use the java interpreter, called java.exe.
c:\myjavaprog>java hello

Java Comments:
/* this is multiline comment of java,
as like C and C++,
Lot of syntax of java is same as C
but there are lot different at some point.
eg. string and character data is handle differently in java.
Code of java must reside in class.
java is case sensitive language
name of the program file of java and name of the class which has main function, must be same.
multiline comment ends here*/


//this is single line comment.



basic program:
//call the file hello.java
class hello {
public static void main(String args[]) {
int x;
x=3;
System.out.println("Value of x is :" + x);
x = x * 3;
System.out.println("Now Value of x is :" + x);
}
}


to compile:
c:\myjavaprog>javac hello.java
to run this program:
c:\myjavaprog>java hello

output is :
Value of x is :3
Now Value of x is :9



Another java program using if statement:
//must name this program ex.java
class ex {
public static void main(String args[]) {
int x,y,z;
x=3; y=4; z=9;
if(x {
System.out.println("Value of x is less than y. x=" + x +" y=" + y);
}
if(z>y)
{
System.out.println("Value of z is greater than y. z=" + z +" y=" + y);
}
}
}


to compile:
c:\myjavaprog>javac ex.java
to run this program:
c:\myjavaprog>java ex

output is :
Value of x is less than y. x=3 y=4
Value of z is greater than y. z=9 y=4



/* for loop
give more attention on output of println and print in System.out
*/
class fl {
public static void main(String args[]) {
int i,j;

for(i=0; i<=50; i=i+5)
{
System.out.println("Value of i:" + i);
}

for(j=0; j<=10; j++)
{
System.out.print(j);
}

}
}


to compile:
c:\myjavaprog>javac fl.java

to run this program:
c:\myjavaprog>java fl

output is :
Value of i:0
Value of i:5
Value of i:10
Value of i:15
Value of i:20
Value of i:25
Value of i:30
Value of i:35
Value of i:40
Value of i:45
Value of i:50
012345678910



//Programm Name : Armstrong.java
class Armstrong
{
public static void main(String args[])
{
int num=0;
int n=num;
int check=0,remainder;
int i;
String p="";
for(i=1;i<999999;i++)
{
num=i;
n=num;
check=0;
p = ""+ i;

while(num>0)
{
remainder=num%10;
check=check+(int)Math.pow(remainder,p.length());
num=num/10;
}
if(check==n)
{
System.out.println(n+" is an armstrong no");
}

}
}
}

to compile :
javac Armstrong.java
to Run: java Armstrong
output is :
1 is an armstrong no
2 is an armstrong no
3 is an armstrong no
4 is an armstrong no
5 is an armstrong no
6 is an armstrong no
7 is an armstrong no
8 is an armstrong no
9 is an armstrong no
153 is an armstrong no
370 is an armstrong no
371 is an armstrong no
407 is an armstrong no
1634 is an armstrong no
8208 is an armstrong no
9474 is an armstrong no
54748 is an armstrong no
92727 is an armstrong no
93084 is an armstrong no
548834 is an armstrong no


Integers:- Java has four integer types as bellows:
	width		Range
long	64 bits		-9223372036854775808 to 9223372036854775807 
int	32 bits		-2147483648 to 2147483647
short	16 bits		-32768 to 32767
byte	8  bits		-128 to 127

/*Example of long data type
must give speed.java name to the file.
*/
class speed {
  public static void main(String args[]) {
	long distance;

	distance = 365L * 24L * 60L * 60L * 299792L;
	System.out.println("Distance travelled by light in one year :" + distance+ " km");
	}
}

output:
Distance travelled by light in one year :9454240512000 km

Floating point types:
Name	Width		Range
double	64 bits		1.7e-308 to 1.7e+308
float	32 bits		3.4e-038 to 3.4e+038 



Next