java fundamentals examples

      Comments Off on java fundamentals examples

java fundamentals examples

java fundamentals examples

java fundamentals examples is collection of java sample code ready to use and learn basic about java.

//Written By Adarsh Patel
//Sample program to display
class example1
{
public static void main(String args[])
{
System.out.print(“My Name is   : “);
System.out.print(“Adarsh Patel”);
System.out.print(“\n”);
System.out.print(“I am Studing : MCA”);
}
}

//Written By Adarsh Patel
//Sample program to calculate and print answer
class example2
{
public static void main(String args[])
{
int val1,val2,sum;
val1=100;
val2=200;
sum = val1 + val2;
System.out.print(“Sum of ” + val1);
System.out.print(” and ” + val2);
System.out.print(” is := ”  + sum);
}
}

//Written By Adarsh Patel
//Sample program of If Condition
class example3
{
public static void main(String args[])
{
int val1,val2,sum;
val1=400;
val2=200;
if (val1 > val2)
{
System.out.print(“Value 1 is Bigger than Value 2”);
}
else
{
System.out.print(“Value 2 is Bigger than Value 1”);
}

}
}

//Written By Adarsh Patel
//Sample program of For Loop
//and also example of println and print
class example4
{
public static void main(String args[])
{
int i;

System.out.println(“In Below output ‘print’ is used \n”);
for(i=1;i<=10;i++)
{
System.out.print(i);
}

System.out.println(“\n\nIn Below output ‘println’ is used \n”);
for(i=1;i<=10;i++)
{
System.out.println(i);
}

}
}

//Written By Adarsh Patel
//Sample program of Whie loop
//and also example of println and print
class example5
{
public static void main(String args[])
{
int i;
System.out.println(“\t\t\t\t\tExample of While Loop”);

System.out.println(“In Below output ‘print’ is used \n”);
i=1;
while(i<=10)
{
System.out.print(i);
i++;
}

i=1;
System.out.println(“\n\nIn Below output ‘println’ is used \n”);
while(i<=10)
{
System.out.println(i);
i++;
}

}
}

//Written By Adarsh Patel
//Sample program of Character Printing on screen
class example6
{
public static void main(String args[])
{
char ch1,ch2;

ch1=65;
ch2=’A’;
System.out.println(“\t\t\t\t\tExample of Character”);

System.out.println(“Output of ch1 : ” + ch1);
System.out.println(“Output of ch2 : ” + ch2);

}
}

//Written By Adarsh Patel
//Sample program : New Data Types Boolean used for True/False
//Mostly used for Yes/No and True/False
class example7
{
public static void main(String args[])
{
boolean a,b;
int i,j;

a = true;
b = false;
i = 5;
j = 7;

System.out.println(“\t\t\t\t\tExample of Boolean”);

System.out.println(“Output of a : ” + a);
System.out.println(“Output of b : ” + b);
System.out.println(“Value of I : ” + i);
System.out.println(“Value of J : ” + j);
System.out.println(” i > j =” + (i>j) );
System.out.println(” i < j =” + (i<j) );
}
}

 

//Written By Adarsh Patel
//Sample program : Class Example – 1

class sample
{
int a;

void display()
{
System.out.println(“Value of A : ” + a);
}
}
class example_class1
{
public static void main(String args[])
{
sample obj = new sample();

obj.a = 59;

obj.display();

}
}

 

//Written By Adarsh Patel
//Sample program : Class Example – 1 With Function(Public)
class cal
{
int a,b;

void display_add()
{
System.out.println(“Sum of A and B  : ” + (a+b));
}

void display_sub()
{
System.out.println(“Sub of A and B  : ” + (a-b));
}
cal()
{
a = 10;
b = 2;
}
}
class example_class2
{
public static void main(String args[])
{
cal obj = new cal();

obj.display_add();

obj.display_sub();

}
}

 

//Written By Adarsh Patel
//Sample program :

class sample
{
int a;
}
class example_class
{
public static void main(String args[])
{
sample obj = new sample();

obj.a = 59;

System.out.println(obj.a);

}
}

 

// Demonstrate dynamic initialization.
class Dynamic_Init {
public static void main(String args[])
{
double a = 3.0, b = 4.0;

// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);

System.out.println(“Value is :” + c);
}
}

 

public class PassByReference {
int onetoZero(int arg[]) {
int count = 0;

for (int i = 0; i < arg.length; i++) {
if (arg[i] == 1) {
count++;
arg[i] = 0;
}
}
return count;
}

public static void main(String[] arguments) {
int arr[] = { 1, 3, 4, 5, 1, 1, 7 };
PassByReference test = new PassByReference();
int numOnes;

System.out.print(“Values of the array: [ “);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ” “);
}
System.out.println(“]”);

numOnes = test.onetoZero(arr);
System.out.println(“Number of Ones = ” + numOnes);
System.out.print(“New values of the array: [ “);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ” “);
}
System.out.println(“]”);
}
}

 

public class PassByReference {
int onetoZero(int arg[]) {
int count = 0;

for (int i = 0; i < arg.length; i++) {
if (arg[i] == 1) {
count++;
arg[i] = 0;
}
}
return count;
}

public static void main(String[] arguments) {
int arr[] = { 1, 3, 4, 5, 1, 1, 7 };
PassByReference test = new PassByReference();
int numOnes;

System.out.print(“Values of the array: [ “);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ” “);
}
System.out.println(“]”);

numOnes = test.onetoZero(arr);
System.out.println(“Number of Ones = ” + numOnes);
System.out.print(“New values of the array: [ “);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ” “);
}
System.out.println(“]”);
}
}

 

// Demonstrate a three-dimensional array.
class threeDMatrix
{
public static void main(String args[])
{
int threeD[][][] = new int[3][4][5];
int i, j, k;

for(i=0; i<3; i++)
for(j=0; j<4; j++)
for(k=0; k<5; k++)
threeD[i][j][k] = i * j * k;

for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
for(k=0; k<5; k++)
System.out.print(threeD[i][j][k] + ” “);
System.out.println();
}
System.out.println();
}
}
}

 

//Written By Adarsh Patel
//Sample program : Simple Triangle using 2 loop
class triangle_1
{
public static void main(String args[])
{
int i,j;

for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j + ” “);
}
System.out.print(“\n”);
}

}
}

 

//Written By Adarsh Patel
//Sample program : Simple Triangle using 2 loop
class triangle_2
{
public static void main(String args[])
{
int i,j;

for(i=0;i<=10;i = i + 2)
{
for(j=0;j<=i;j = j + 2)
{
System.out.print(j + ” “);
}
System.out.print(“\n”);
}

}
}

 

//Written By Adarsh Patel
//Output of Program is like below
/*
*       *
*       *
*       *
*       *
* * * ** * *
*       *
*       *
*       *
*       *
*       *
*/
class triangle_3
{
public static void main(String args[])
{
int i,j,n;

n=10;

for(i=1;i<=n;i++)
{

System.out.print(“*”);
for(j=1;j<=n;j++)
{
if(i==n/2)
System.out.print(“*”);
else
System.out.print(” “);
}
System.out.print(“*”);
if(i==n/2)
System.out.print(“***”);
System.out.print(“\n”);
}
}
}

 

 

 

for More Java Tutorial and Sample Code Click here