Ravi Tutorials Provide Project Training

Java Pragram

Java Pragrams

/*
This is a simple Java program.
Call this file "Example.java".
*/
class Main
{

// Your program begins with a call to main().
public static void main(String args[])
 {
System.out.println("This is a simple Java program.");
}
}


 //area of a circle

class AreaOfCircle
{
public static void main(String s[])
{
double pi, r, a;

pi = 3.14;
r = 10;
a = pi * r * r;

System.out.println("Area of circle = " + a);
}
}
//char data type example
class CharDemo
{
public static void main(String s[])
{
char ch1;

ch1 = 'A';
System.out.println("ch1 = " + ch1);

ch1 = 65; //A
System.out.println("ch1 = " + ch1);

ch1++; // increment ch1
System.out.println("ch1 = " + ch1);
}
}
//Type casting example

class Conversion
{
public static void main(String s[])
{
byte b;
int i = 257;
double d = 3.14;

System.out.println("\n\nConversion of int to byte.");
b = (byte)i;
System.out.println("i = " + i);
System.out.println("b = " + b);

System.out.println("\n\nConversion of double to int.");
i = (int)d;
System.out.println("d = " + d);
System.out.println("i = " + i);
}
}
class Average
{
public static void main(String s[])
{
double a[] = { 10, 20, 30, 40, 50, 60 };
double sum = 0;

for (int i = 0; i < 6; i++)
sum = sum + a[i];

double avg = sum / 6;
System.out.println("Average = " + avg);
}
}
//max, min number from 1-Dim Array

class MaxMin1DimArr
{
public static void main(String s[])
{
int a[] = { 40, 20, 50, 30, 60, 10 };
int i, j, N;
int max, min;

N = a.length;

System.out.println("Array is ");
for (i = 0; i < N; i++)
{
System.out.println(a[i]);
}

max = a[0];
min = a[0];
for (i = 0; i < N; i++)
{
if (max < a[i])
{
max = a[i];
}
if (min > a[i])
{
min = a[i];
}
}
System.out.println("Max = " + max);
System.out.println("Min = " + min);
}
}
//Bubble sort an array

class BubbleSort
{
public static void main(String s[])
{
int a[] = { 40, 20, 50, 30, 60, 10 };
int i, t, j, N;

N = a.length;

System.out.println("Before Sorting : ");
for (i = 0; i < N; i++)
{
System.out.println(a[i]);
}

for (i = 1; i <= N - 1; i++)
{
for (j = 1; j <= N - i; j++)
{
if (a[j - 1] > a[j])
{
t = a[j - 1];
a[j - 1] = a[j];
a[j] = t;
}
}
}

System.out.println("After Sorting:");
for (i = 0; i <= N - 1; i++)
{
System.out.println(a[i]);
}
}
}
//Max, Min number from a Matrix

class MaxMinMatrix
{
public static void main(String s[])
{
int a[][] ={ { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
int i, j, max, min;

max = a[0][0];
min = a[0][0];
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (max < a[i][j])
max = a[i][j];
if (min > a[i][j])
min = a[i][j];

}
}

System.out.println("matrix A : ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + a[i][j]);
}
System.out.println("");
}
System.out.println("Max = " + max);
System.out.println("Min = " + min);
}
}
//Max, Min number from a Matrix

class MaxMinMatrix
{
public static void main(String s[])
{
int a[][] ={ { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
int i, j, max, min;

max = a[0][0];
min = a[0][0];
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (max < a[i][j])
max = a[i][j];
if (min > a[i][j])
min = a[i][j];

}
}

System.out.println("matrix A : ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + a[i][j]);
}
System.out.println("");
}
System.out.println("Max = " + max);
System.out.println("Min = " + min);
}
}
//multiplication of 2 matrices

class MatrixMul
{
public static void main(String s[])
{
int a[][] ={ { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
int b[][] ={ { 2, 3, 4 }, { 3, 4, 5 }, { 5, 6, 7 } };
int c[][] = new int[3][3];
int i, j, k;

System.out.println("matrix A : ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + a[i][j]);
}
System.out.println("");
}

System.out.println("matrix B : ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + b[i][j]);
}
System.out.println("");
}

for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
c[i][j] = 0;
for (k = 0; k < 3; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}

System.out.println("Multiplication of 2 matrix is :");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + c[i][j]);
}
System.out.println("");
}

}
}
//multiplication of 2 matrices

class MatrixMul
{
public static void main(String s[])
{
int a[][] ={ { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
int b[][] ={ { 2, 3, 4 }, { 3, 4, 5 }, { 5, 6, 7 } };
int c[][] = new int[3][3];
int i, j, k;

System.out.println("matrix A : ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + a[i][j]);
}
System.out.println("");
}

System.out.println("matrix B : ");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + b[i][j]);
}
System.out.println("");
}

for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
c[i][j] = 0;
for (k = 0; k < 3; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}

System.out.println("Multiplication of 2 matrix is :");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + c[i][j]);
}
System.out.println("");
}

}
}
//Create an identity Matrix

class CreateIdentityMatrix
{
public static void main(String s[])
{
int a[][] = new int[3][3];
int i, j;

for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (i == j)
{
a[i][j] = 1;
}
else
{
a[i][j] = 0;
}
}
}

System.out.println("Identity matrix is :");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + a[i][j]);
}
System.out.println("");
}

}
}
//Matrix is identity or not

class IsMatrixIdentity
{
public static void main(String s[])
{
int a[][] ={ { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
int i, j;

int flg = 0;
outer: for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (i == j && a[i][j] != 1)
{
flg = 1;
break outer;
}
else if (i != j && a[i][j] != 0)
{
flg = 1;
break outer;
}
}
}


if (flg == 1)
System.out.println("Given matrix is not identity");
else
System.out.println("Given matrix is identity");


System.out.println("Given matrix is :");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + a[i][j]);
}
System.out.println("");
}

}
}
//Matrix is identity or not

class IsMatrixIdentity
{
public static void main(String s[])
{
int a[][] ={ { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
int i, j;

int flg = 0;
outer: for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (i == j && a[i][j] != 1)
{
flg = 1;
break outer;
}
else if (i != j && a[i][j] != 0)
{
flg = 1;
break outer;
}
}
}


if (flg == 1)
System.out.println("Given matrix is not identity");
else
System.out.println("Given matrix is identity");


System.out.println("Given matrix is :");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(" " + a[i][j]);
}
System.out.println("");
}

}
}
//Result of a student using percentage (else if ladder)

class Result
{
public static void main(String s[])
{
int per = 67;
String Res = "";

if (per >= 70)
Res = "Distinction";
else if (per >= 60)
Res = "First Class";
else if (per >= 50)
Res = "Second Class";
else if (per >= 40)
Res = "Pass Class";
else
Res = "Fail";

System.out.println("Per = " + per);
System.out.println("Result = " + Res);
}
}
//Result of a student using Percentage (switch case)

class ResultSwitch
{
public static void main(String s[])
{
int per = 67;
String Res = "";

int t = per / 10;
switch (t)
{
case 10:
case 9:
case 8:
case 7:
Res = "Distinction";
break;
case 6:
Res = "First Class";
break;
case 5:
Res = "Second Class";
break;
case 4:
Res = "Pass Class";
break;
default:
Res = "Fail";
break;
}

System.out.println("Per = " + per);
System.out.println("Result = " + Res);
}
}
//Factorial of a number

class Factorial
{
public static void main(String s[])
{
int f, n, i;
f = 1;
n = 5;
for (i = 1; i <= n; i++)
{
f = f * i;
}
System.out.println("factorial of " + n + " = " + f);
}
}
//Fibonacci series

class Fibonacci
{
public static void main(String s[])
{
int a, b, c, i;
a = 0;
b = 1;

System.out.println("Fibonacci series is ");
for (i = 1; i <= 10; i++)
{
c = a + b;
System.out.print(a + " ");
a = b;
b = c;
}
}
}
//number is prime or not

class Prime
{
public static void main(String s[])
{
int i, n = 17;

int flg = 0;
for (i = 2; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
flg = 1;
break;
}
}

if (flg == 1)
System.out.println(n + " is not a prime number");
else
System.out.println(n + " is a prime number");
}
}
//Given number is perfect number or not

class Perfect
{
public static void main(String s1[])
{
int i, s = 0, n = 28;

for (i = 1; i <= n - 1; i++)
{
if (n % i == 0)
s = s + i;
}
System.out.println("N = " + n);
System.out.println("Sum = " + s);

if (s == n)
System.out.println(n + " is perfect number");
else
System.out.println(n + " is not perfect number");
}
}
//reverse number of a given number

class ReverseNo
{
public static void main(String s[])
{
int n = 345, rev = 0, r, n1;
n1 = n;
while (n > 0)
{
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
System.out.println("Original no = " + n1);
System.out.println("Reverse no = " + rev);
}
}
//number is palindrome or not

class Palindrome
{
public static void main(String s[])
{
int n = 515, rev = 0, r, n1;
n1 = n;
while (n > 0)
{
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}

System.out.println("Original = " + n1);
System.out.println("Reverse = " + rev);

if (n1 == rev)
System.out.println(n1 + " is Palindrome");
else
System.out.println(n1 + " is Not Palindrome");
}
}
//number is amstrong or not

class AmstrongNo
{
public static void main(String s1[])
{
int n = 153, s = 0, r, n1;
n1 = n;
while (n > 0)
{
r = n % 10;
s = s + r * r * r;
n = n / 10;
}

System.out.println("Original no = " + n1);
System.out.println("Sum = " + s);
if (n1 == s)
System.out.println(n1 + " is AmstrongNo ");
else
System.out.println(n1 + " is Not AmstrongNo");
}
}
//Convert a given Decimal number to Binary number

class DecToBin
{

public static void main(String s[])
{
int n, n1, i = 0, j;
int b[] = new int[100];

n = 30;
n1 = n;
i = 0;
while (n > 0)
{
b[i] = n % 2;
n = n / 2;
i++;
}

System.out.println("Decimal No = "+ n1);
System.out.print("Binary No = ");
for (j = i - 1; j >= 0; j--)
{
System.out.print(b[j]);
}
}
}
//Convert a given Decimal number to Octal number

class DecToOct
{

public static void main(String s[])
{
int n, n1, i = 0, j;
int b[] = new int[100];

n = 430;
n1 = n;
i = 0;

while (n>0)
{
b[i]=n%8;
n=n/8;
i++;
}

System.out.println("Decimal No = "+n1);
System.out.print("Octal No = ");

for (j=i-1;j>=0;j--)
{
System.out.print(b[j]);
}
}
}
//Convert a given Decimal number to Hexadecimal number

class DecToHex
{

public static void main(String s[])
{
int n, n1, i = 0, j;
int b[] = new int[100];

n = 1250;
n1 = n;
i = 0;


while (n > 0)
{
b[i] = n % 16;
n = n / 16;
i++;
}

System.out.println("Decimal No = "+ n1);
System.out.print("Hexadecimal No = ");

for (j = i - 1; j >= 0; j--)
{
if (b[j] < 10)
{
System.out.print(b[j]);
}
else
{
switch (b[j])
{
case 10:
System.out.print("A");
break;
case 11:
System.out.print("B");
break;
case 12:
System.out.print("C");
break;
case 13:
System.out.print("D");
break;
case 14:
System.out.print("E");
break;
case 15:
System.out.print("F");
break;
}

}
}
}
}
//Pattern123
//1
//2 3
//4 5 6
//7 8 9 10

class Pattern123
{

public static void main(String s[])
{
int i, j, c, N;

N = 5;
c = 1;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= i; j++)
{
System.out.print(c + "  ");
c++;
}
System.out.println("");
}
}
}
//Pattern101
//1
//0 1
//1 0 1
//0 1 0 1

class Pattern101
{
public static void main(String s[])
{
int i, j, N, c;
N = 5;

for (i = 1; i <= N; i++)
{
for (j = 1; j <= i; j++)
{
c = (i + j - 1) % 2;
System.out.print(c + "  ");
}
System.out.println("");
}
}
}
//break label example
class breakLabel
{
public static void main(String s[])
{
F:
for (int i = 1; i <= 3; i++)
{
System.out.print("i=" + i + " : ");
for (int j = 1; j <= 10; j++)
{
if (j == 5)
{
break F;
}
System.out.print(j + " ");
}
System.out.println("This will not print");
}
System.out.println("Loops complete.");
}
}
//Simple class example of getData() and putData()

class Student
{
int rn;
String name;
double per;

void getData(int r,String nm,double p)
{
rn = r;
name = nm;
per = p;
}

void putData()
{
System.out.println("Roll no = " + rn);
System.out.println("Name = " + name);
System.out.println("Per = " + per);
}
}

class SimpleClass
{
public static void main(String s[])
{
Student s1 = new Student();
s1.getData(1, "Piyush", 72.70);
s1.putData();

Student s2;
s2 = new Student();
s2.getData(2, "Shah", 85);
s2.putData();
}
}
//Constructor example

class Student
{
int rn;
String name;
double per;

Student(int r, String n, double p)
{
rn = r;
name = n;
per = p;
}

void putData()
{
System.out.println("Roll no = " + rn);
System.out.println("Name = " + name);
System.out.println("Per = " + per);
}
}

class ConstructorExa
{
public static void main(String s[])
{
Student s1 = new Student(1, "Piyush", 72.70);
s1.putData();

Student s2 = new Student(2, "Shah", 85);
s2.putData();
}
}
//Create instance variable hiding problem

class Student
{
int rn;
String name;
double per;

Student(int rn, String name, double per)
{
rn = rn;
name = name;
per = per;
}

void putData()
{
System.out.println("Roll no = " + rn);
System.out.println("Name = " + name);
System.out.println("Per = " + per);
}
}

class InstanceVariHideProblem
{
public static void main(String s[])
{
Student s1 = new Student(1, "Piyush", 72.70);
s1.putData();

Student s2 = new Student(2, "Shah", 85);
s2.putData();
}
}
//solution of instance variable hiding problem

class Student
{
int rn;
String name;
double per;

Student(int rn, String name, double per)
{
this.rn = rn;
this.name = name;
this.per = per;
}

void putData()
{
System.out.println("Roll no = " + rn);
System.out.println("Name = " + name);
System.out.println("Per = " + per);
}
}

class InstanceVariHideSol
{
public static void main(String s[])
{
Student s1 = new Student(1, "Piyush", 72.70);
s1.putData();

Student s2 = new Student(2, "Shah", 85);
s2.putData();
}
}
//method overloading example

class Maths
{
int add(int a, int b)
{
return a + b;
}

int add(int a, int b, int c)
{
return a + b + c;
}

double add(double a, double b)
{
return a + b;
}
}

class OverloadMethod
{
public static void main(String args[])
{
Maths obj = new Maths();
double result;

int t1 = obj.add(5, 10);
System.out.println(t1);

int t2 = obj.add(5, 10, 20);
System.out.println(t2);

double t3 = obj.add(5.6, 10.2);
System.out.println(t3);
}
}
//Box constructor overloading exmaple

class Box
{
double w;
double h;
double d;

// constructor used when no dimensions specified
Box()
{
w = -1;  // use -1 to indicate
h = -1; // an uninitialized
d = -1;  // box
}

// constructor used when cube is created
Box(double len)
{
w = h = d = len;
}

// constructor used when all dimensions specified
Box(double w1, double h1, double d1)
{
w = w1;
h = h1;
d = d1;
}

// construct clone of an object
Box(Box ob)
{ // pass object to constructor
w = ob.w;
h = ob.h;
d = ob.d;
}

// compute and return volume
double volume()
{
return w * h * d;
}
}

class OverloadCons
{
public static void main(String args[])
{
// create boxes using the various constructors
Box b1 = new Box();
Box b2 = new Box(5);
Box b3 = new Box(10, 20, 30);
Box b4 = new Box(b1);

double v;

v = b1.volume();
System.out.println("Volume of b1 = " + v);

v = b2.volume();
System.out.println("Volume of b2 = " + v);

v = b3.volume();
System.out.println("Volume of b3 = " + v);

v = b4.volume();
System.out.println("Volume of b4 = " + v);
}
}
//call by value

class Demo
{
void exchange(int x, int y)
{
int z = x;
x=y;
y = z;
}
}

class CallByValue
{
public static void main(String args[])
{
Demo obj = new Demo();
int a = 5, b = 10;

System.out.println("a and b before exchange: " + a + " " + b);

obj.exchange(a, b);

System.out.println("a and b after exchange: " + a + " " + b);
}
}
//call by reference

class Demo
{
int x, y;

Demo(int i, int j)
{
x = i;
y = j;
}

void exchange(Demo ob)
{
int z = ob.x;
ob.x = ob.y;
ob.y = z;
}
}

class CallByRef
{
public static void main(String args[])
{
Demo obj = new Demo(5,10);

System.out.println("Before Exchange ");
System.out.println("a = " + obj.x);
System.out.println("b = " + obj.y);

obj.exchange(obj);

System.out.println("After Exchange ");
System.out.println("a = " + obj.x);
System.out.println("b = " + obj.y);
}
}
//Factorial using recursion

class FactorialRecursion
{
public static void main(String args[])
{
Factorial f1 = new Factorial();

int x=f1.fact(5);
System.out.println("5! = " + x);
}
}


class Factorial
{
int fact(int n)
{
int result;

if (n == 1)
return 1;
else
result = n * fact(n - 1);

return result;
}
}

//Fibonacci series using recursion

class FibonacciRecursion
{
public static void main(String args[])
{
Fibonacci f1 = new Fibonacci();

f1.fibo(0,1,10);
}
}


class Fibonacci
{
void fibo(int a,int b,int n)
{
if (n == 0)
return;

System.out.print(a + " ");
int c = a + b;
a = b;
b = c;
fibo(b, c, n - 1);
}
}

//display all elements of an array using recursion

class ArrayDisp
{
int x[];

ArrayDisp(int i)
{
x = new int[i];
}

//display arrary recursively in ascending order
void print(int i)
{
if (i == 0)
return;
else
print(i - 1);

System.out.println("[" + (i - 1) + "] " + x[i - 1]);
}
}


class ArrayDispRecursion
{
public static void main(String args[])
{
ArrayDisp obj = new ArrayDisp(10);

for (int i = 0; i < 10; i++)
obj.x[i] = 2*i;

obj.print(10);
}

}

//addition of an array using recursion

class ArraySum
{
int x[],sum;

ArraySum(int i)
{
x = new int[i];
sum = 0;
}

// display arrary recursively in ascending order
int findsum(int i)
{
if (i == 0)
return 0;
else
findsum(i - 1);

sum += x[i - 1];
//System.out.println("[" + (i - 1) + "] " + x[i - 1]);

return sum;
}
}


class ArraySumRecursion
{
public static void main(String args[])
{
ArraySum obj = new ArraySum(10);

for (int i = 0; i < 10; i++)
obj.x[i] = i;

int s = obj.findsum(10);
System.out.println("Sum = " + s);
}

}

//stack example, with push and pop operation

class Stack
{
private int s[] = new int[10];
private int top;

Stack()
{
top = -1;
}

void push(int x)
{
if (top == s.length-1)
System.out.println("Stack is full.");
else
s[++top] = x;
}

int pop()
{
if (top < 0)
{
System.out.println("Stack underflow.");
return 0;
}
else
return s[top--];
}
}

class StackDemo
{
public static void main(String args[])
{
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();

for (int i = 0; i <= 10; i++)
mystack1.push(2*i);

for (int i = 10; i <= 20; i++)
mystack2.push(2*i);

System.out.println("Stack in mystack1:");
for (int i = 0; i <= 10; i++)
System.out.println(mystack1.pop());

System.out.println("Stack in mystack2:");
for (int i = 0; i <= 10; i++)
System.out.println(mystack2.pop());
}
}
//static variables, methods in same class

class StaticDemo1
{
static int a = 5;
static int b;

static void disp(int x)
{
b = 10;
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}

public static void main(String args[])
{
disp(50);
System.out.println("In main()");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
//static variables, methods in different class

class StaticDemo
{
static int a = 5;
static int b;

static void disp(int x)
{
b = 10;
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}

class StaticDemo2
{
public static void main(String args[])
{
StaticDemo.disp(15);
System.out.println("StaticDemo.a = " + StaticDemo.a);
System.out.println("StaticDemo.b = " + StaticDemo.b);
}
}

//Inner class (nested class)

class A
{
int x = 10;

// this is an innner class
class B
{
int y = 20; // y is local to Inner
void display()
{
System.out.println("B's display: x = " + x);
System.out.println("B's display: y = " + y);
}
}

void showy()
{
System.out.println("A's showy: x = " + x);
//System.out.println("A's showy: y = " + y);// error, y not known here!
}

void test()
{
B b1 = new B();
b1.display();
}

}

class InnerClass
{
public static void main(String args[])
{
A a1 = new A();
a1.test();
}
}
//Factorial using command line argument

class FactorialCLine
{
public static void main(String s[])
{
int f, n, i;
f = 1;
n = Integer.parseInt(s[0]);
for (i = 1; i <= n; i++)
{
f = f * i;
}
System.out.println("factorial of " + n + " is " + f);
}
}
//Addition of 2 Numbers using Command Line arguments

class Add2NumCLine
{
public static void main(String s[])
{
int a, b, c;
a = Integer.parseInt(s[0]);
b = Integer.parseInt(s[1]);
c = a + b;

System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
//example of inheritance

class A
{
int i, j;

void show2()
{
System.out.println("i = " + i);
System.out.println("j = " + j);
}
}

class B extends A
{
int k;

void show3()
{
System.out.println("i = " + i);
System.out.println("j = " + j);
System.out.println("k = " + k);
}
}

class SimpleInheritance
{
public static void main(String args[])
{
A a1 = new A();
B b1 = new B();

a1.i = 5;
a1.j = 10;
System.out.println("Contents of a1 : ");
a1.show2();
System.out.println();

b1.i = 50;
b1.j = 100;
b1.k = 120;
System.out.println("Contents of b1 : ");
b1.show2();
b1.show3();
System.out.println();
}
}
// Single Inheritence, Box --> BoxWeight

class Box
{
private double w,h,d;

Box()
{
w = h = d = 0;
}

Box(double len)
{
w = h = d = len;
}

Box(double w1, double h1, double d1)
{
w = w1;
h = h1;
d = d1;
}

Box(Box ob)
{
w = ob.w;
h = ob.h;
d = ob.d;
}

double volume()
{
return w * h * d;
}
}


class BoxWeight extends Box
{
double m;

BoxWeight(BoxWeight ob)
{
super(ob);
m = ob.m;
}

BoxWeight(double w1, double h1, double d1, double m1)
{
super(w1, h1, d1);
m = m1;
}

BoxWeight()
{
super();
m = -1;
}

BoxWeight(double len, double m1)
{
super(len);
m = m1;
}
}

class SuperCons
{
public static void main(String args[])
{
BoxWeight b1 = new BoxWeight();
BoxWeight b2 = new BoxWeight(10, 20);
BoxWeight b3 = new BoxWeight(10, 20, 30, 5);
BoxWeight b4 = new BoxWeight(b3);
double vol;

vol = b1.volume();
System.out.println("b1.Volume = " + vol);
System.out.println("b1.m = " + b1.m);

vol = b2.volume();
System.out.println("b2.Volume = " + vol);
System.out.println("b2.m = " + b2.m);

vol = b3.volume();
System.out.println("b3.Volume = " + vol);
System.out.println("b3.m = " + b3.m);

vol = b4.volume();
System.out.println("b4.Volume = " + vol);
System.out.println("b4.m = " + b4.m);
}
}
//Using super to overcome name hiding

class A
{
int i;
}

class B extends A
{
int i;

B(int x, int y)
{
super.i = x;
i = y;
}

void show()
{
System.out.println("super.i = " + super.i);
System.out.println("i = " + i);
}
}

class SuperVariable
{
public static void main(String args[])
{
B b1 = new B(5, 10);

b1.show();
}
}
//Multilevel inheritence, Box --> BoxWeight --> Shipment

class Box
{
private double w, h, d;

Box()
{
w = h = d = 0;
}

Box(double len)
{
w = h = d = len;
}

Box(double w1, double h1, double d1)
{
w = w1;
h = h1;
d = d1;
}

Box(Box ob)
{
w = ob.w;
h = ob.h;
d = ob.d;
}

double volume()
{
return w * h * d;
}
}

// Add weight.
class BoxWeight extends Box
{
double m;

BoxWeight()
{
super();
m = -1;
}

BoxWeight(double len, double m1)
{
super(len);
m = m1;
}

BoxWeight(double w1, double h1, double d1, double m1)
{
super(w1, h1, d1);
m = m1;
}

BoxWeight(BoxWeight ob)
{
super(ob);
m = ob.m;
}

}


class Shipment extends BoxWeight
{
double c; //c for cost

Shipment()
{
super();
c = -1;
}

Shipment(double len, double m1, double c1)
{
super(len, m1);
c = c1;
}

Shipment(double w1, double h1, double d1, double m1, double c1)
{
super(w1, h1, d1, m1);
c = c1;
}

Shipment(Shipment ob)
{
super(ob);
c = ob.c;
}
}

class MultiLevelInheritence
{
public static void main(String args[])
{
Shipment b1 = new Shipment();
Shipment b2 = new Shipment(10, 20,10000);
Shipment b3 = new Shipment(10, 20, 30, 5,20000);
Shipment b4 = new Shipment(b3);

double vol;

vol = b1.volume();
System.out.println("b1.Volume = " + vol);
System.out.println("b1.m = " + b1.m);
System.out.println("b1.c = " + b1.c);

vol = b2.volume();
System.out.println("b2.Volume = " + vol);
System.out.println("b2.m = " + b2.m);
System.out.println("b2.c = " + b2.c);

vol = b3.volume();
System.out.println("b3.Volume = " + vol);
System.out.println("b3.m = " + b3.m);
System.out.println("b3.c = " + b3.c);

vol = b4.volume();
System.out.println("b4.Volume = " + vol);
System.out.println("b4.m = " + b4.m);
System.out.println("b4.c = " + b4.c);
}
}
//How default constructors are called in multilevel inheritence

class A
{
A()
{
System.out.println("AAA");
}
}

class B extends A
{
B()
{
System.out.println("BBB");
}
}

class C extends B
{
C()
{
System.out.println("CCC");
}
}

class ConstructorCall
{
public static void main(String args[])
{
C c = new C();
}
}
//Method overriding

class A
{
int i, j;

A(int a, int b)
{
i = a;
j = b;
}

void show()
{
System.out.println("i = " + i);
System.out.println("j = " + j);
}
}

class B extends A
{
int k;

B(int a, int b, int c)
{
super(a, b);
k = c;
}

void show()
{
super.show();
System.out.println("k = " + k);
}
}

class MethodOverride
{
public static void main(String args[])
{
B subOb = new B(5, 10, 15);

subOb.show();
}
}
//Dynamic method dispatch (run-time polymorphism)
//A --> B, A --> C

class A
{
void show()
{
System.out.println("AAA");
}
}

class B extends A
{
void show()
{
System.out.println("BBB");
}
}

class C extends A
{
void show()
{
System.out.println("CCC");
}
}

class DynamicMethodDispatch
{
public static void main(String args[])
{
A a1 = new A();
B b1 = new B();
C c1 = new C();
A r;

r = a1;
r.show();

r = b1;
r.show();

r = c1;
r.show();
}
}
//Dynamic method dispatch (run-time polymorphism)
//Figure --> Rectangle, Figure --> Triangle

class Figure
{
double d1;
double d2;

Figure(double a, double b)
{
d1 = a;
d2 = b;
}

double area()
{
System.out.println("Area for Figure is undefined.");
return 0;
}
}

class Rectangle extends Figure
{
Rectangle(double a, double b)
{
super(a, b);
}

double area()
{
System.out.println("Inside Area for Rectangle.");
return d1 * d2;
}
}

class Triangle extends Figure
{
Triangle(double a, double b)
{
super(a, b);
}

double area()
{
System.out.println("Inside Area for Triangle.");
return 0.5 * d1 * d2;
}
}

class DMD_Area
{
public static void main(String args[])
{
Figure f1 = new Figure(10, 10);
Rectangle r1 = new Rectangle(9, 5);
Triangle t1 = new Triangle(10, 8);

Figure r;

r = f1;
System.out.println("Figure Area = " + r.area());

r = r1;
System.out.println("Rectangle Area = " + r.area());

r = t1;
System.out.println("Triangle Area = " + r.area());
}
}
//abstract class

abstract class A
{
//abstract method
abstract void f1();

//concrete method
void f2()
{
System.out.println("This is a concrete method");
}
}

class B extends A
{
void f1()
{
System.out.println("B's implementation of f1");
}
}

class AbstractClass
{
public static void main(String args[])
{
B b = new B();

b.f1();
b.f2();
}
}

//Dynamic method dispatch (run-time polymorphism) using abstract method and class
//Figure --> Rectangle, Figure --> Triangle

abstract class Figure
{
double d1;
double d2;

Figure(double a, double b)
{
d1 = a;
d2 = b;
}

abstract double area();
}

class Rectangle extends Figure
{
Rectangle(double a, double b)
{
super(a, b);
}

double area()
{
System.out.println("Inside Area for Rectangle.");
return d1 * d2;
}
}

class Triangle extends Figure
{
Triangle(double a, double b)
{
super(a, b);
}

double area()
{
System.out.println("Inside Area for Triangle.");
return 0.5 * d1 * d2;
}
}

class AbstractFigure
{
public static void main(String args[])
{
Rectangle r1 = new Rectangle(5, 10);
Triangle t1 = new Triangle(5, 10);

Figure r;//reference only(no object is created)

r = r1;
System.out.println("Area is " + r.area());

r = t1;
System.out.println("Area is " + r.area());
}
}
//Final method example (Can't override)

class A
{
final void f1()
{
System.out.println("This is a final method");
}
}

class FinalMethodDemo extends A
{
// ERROR, Can't override
void f1()
{
System.out.println("Inside f1");
}
}
//Final class example (Can't inheritence)

final class A
{

}

//ERROR! Can't subclass A
class FinalClassDemo extends A
{

}
//package example

package p1;

public class C1
{
private int a = 1;
public int b = 2;
protected int c = 3;
int d = 4;

public C1()
{
System.out.println("C1 constructor");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}

class C2 extends C1
{
C2()
{
System.out.println("C2 constructor");

//Private variable class only
//System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}

class C3
{
C3()
{
C1 ob = new C1();
System.out.println("C3 constructor");

//Private variable class only
//System.out.println("ob.a = " + ob.a);
System.out.println("ob.b = " + ob.b);
System.out.println("ob.c = " + ob.c);
System.out.println("ob.d = " + ob.d);
}
}

//package p2;
class C4 extends p1.C1
{
C4()
{
System.out.println("C4 constructor");

//Private variable class only
//System.out.println("a = " + a);

System.out.println("b = " + b);
System.out.println("c = " + c);

//class or package only
System.out.println("d = " + d);
}
}

class C5
{
C5()
{
p1.C1 ob = new p1.C1();
System.out.println("C5 constructor");

//Private variable class only
//System.out.println("ob.a = " + ob.a);
System.out.println("ob.b = " + ob.b);

//class, subclass or package only
System.out.println("ob.c = " + ob.c);

//class or package only
//System.out.println("ob.d = " + ob.d);

}
}

// Run from package p1
//package p1;
public class PkgMain1
{
public static void main(String args[])
{
C1 ob1 = new C1();
C2 ob1 = new C2();
C3 ob1 = new C3();
p2.C4 ob1 = new p2.C4();
p2.C5 ob1 = new p2.C5();
}
}


// Run from package p2
/*
package p2;
public class PkgMain2
{
public static void main(String args[])
{
p1.C1 ob1 = new p1.C1();
p1.C2 ob1 = new p1.C2();
p1.C3 ob1 = new p1.C3();
C4 ob1 = new C4();
C5 ob1 = new C5();
}
}
*/
//Interface Variable

import java.util.Random;

interface I1
{
int YES = 1;
int NO = 0;
}

class C1 implements I1
{
void show()
{
System.out.println(YES);
System.out.println(NO);
}
}

class InterfaceVariable
{
public static void main(String args[])
{
C1 ob = new C1();
ob.show();
}
}
//interface inheritence example

interface I1
{
void f1();
void f2();
}

interface I2 extends I1
{
void f3();
}

// This class must implement all of I1 and I2
class A implements I2
{
public void f1()
{
System.out.println("f1()");
}

public void f2()
{
System.out.println("f2()");
}

public void f3()
{
System.out.println("f3()");
}
}

class InterfaceExtends
{
public static void main(String arg[])
{
A a1 = new A();

a1.f1();
a1.f2();
a1.f3();
}
}

//Arithmetic Exception example

class AriExc
{
public static void main(String s[])
{
int a,b,c;

try
{
b = 0;
c = 10 / b;
System.out.println("This will not be printed.");
}
catch (ArithmeticException e)
{
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}
//multiple catch example

class MultipleCatch
{
public static void main(String s[])
{
try
{
int a = s.length;
System.out.println("a = " + a);
//If a=0 then divide by zero exception
int b = 10 / a;

int c[] = { 1, 2, 3, 4, 5 };
c[10] = 50; //out of bounds exception
}
catch (ArithmeticException e)
{
System.out.println("ArithmeticException = " + e);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException = " + e);
}

System.out.println("After try catch");
}
}
//nested try in main()

class NestedTry
{
public static void main(String s[])
{
try
{
int a = s.length;

//If a=0 then divide by zero exception
int b = 10 / a;

System.out.println("a = " + a);
System.out.println("b = " + b);

try
{
if (a == 1)
b = 1 / 0; //divide by zero

if (a == 2)
{
int c[] = { 1, 2, 3, 4, 5 };
c[10] = 50; //out of bounds exception
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException = " + e);
}

}
catch (ArithmeticException e)
{
System.out.println("ArithmeticException = " + e);
}
}
}
//nested try via function calls

class NestedTry2
{
static void nesttry(int a)
{
int b;

try
{
if (a == 1)
b = 1 / 0; //divide by zero

if (a == 2)
{
int c[] = { 1, 2, 3, 4, 5 };
c[10] = 50; //out-of-bounds exception
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index out-of-bounds: " + e);
}
}

public static void main(String s[])
{
try
{
int a = s.length;
System.out.println("a = " + a);

//If a=0 then divide-by-zero exception
int b = 10 / a;
System.out.println("b = " + b);

nesttry(a);
}
catch (ArithmeticException e)
{
System.out.println("Divide by 0: " + e);
}
}
}
//throw example

class ThrowDemo
{
static void show()
{
try
{
throw new ArithmeticException("demo");
}
catch (ArithmeticException e)
{
System.out.println("Caught inside show");
}
}

public static void main(String s[])
{
show();
}
}
//Throws example

class ThrowsDemo
{
static void show() throws ArithmeticException
{
System.out.println("Inside show");
throw new ArithmeticException("demo");
}
public static void main(String s[])
{
try
{
show();
}
catch (ArithmeticException e)
{
System.out.println("Caught " + e);
}
}
}
//finally example

class FinallyDemo
{
static void f1()
{
try
{
System.out.println("inside f1()");
throw new RuntimeException("demo");
}
catch (Exception e)
{
System.out.println("Exception caught");
}
finally
{
System.out.println("f1() finally");
}
}

static void f2()
{
try
{
System.out.println("inside f2()");
return;
}
finally
{
System.out.println("f2() finally");
}
}

static void f3()
{
try
{
System.out.println("inside f3()");
}
finally
{
System.out.println("f3() finally");
}
}

static void f4()
{
try
{
System.out.println("inside f3()");
throw new RuntimeException("demo");
}
finally
{
System.out.println("f4() finally");
}
}

public static void main(String s[])
{
f1();
f2();
f3();
try
{
f4();
}
catch (Exception e)
{
System.out.println("Exception caught");
}
}
}

More Programs is coming soon..