Documente Academic
Documente Profesional
Documente Cultură
INDEX
1. Program for Various data types int,char,bool,double,float.
2. Program for Various variables using light travel and area of circle
class datatypes
{
public static void main(String args[])
{
short s1 = 50;
short s2 = 42;
System.out.println("Value of short variable b1 is :" + s1);
System.out.println("Value of short variable b1 is :" + s2);
int i = 0;
int j = 100;
System.out.println("Value of int variable i is :" + i);
System.out.println("Value of int variable j is :" + j);
float f = 10.4f;
System.out.println("Value of float variable f is :" + f);
double d = 1232.44;
System.out.println("Value of double variable d is :" + d);
byte b = 100;
System.out.println("Value of byte variable b1 is :" + b);
boolean b1 = true;
boolean b2 = false;
boolean b3 = (10 > 2)? true:false;
O/P:-
class light
{
public static void main(String args[])
{
int lightspeed;
long days,seconds,distance;
O/P:-
class area
{
public static void main(String args[])
{
double pi,r,a;
O/P:-
class ArrayApp
{
public static void main(String[] args)
{
long[] arr;
arr = new long[100];
int nElems = 0;
int j;
long searchKey;
arr[0] = 77;
arr[1] = 99;
arr[2] = 44;
arr[3] = 55;
arr[4] = 22;
arr[5] = 88;
arr[6] = 11;
arr[7] = 00;
arr[8] = 66;
arr[9] = 33;
nElems = 10;
searchKey = 66;
for(j=0; j<nElems; j++)
if(arr[j] == searchKey)
break;
if(j == nElems)
System.out.println("Can’t find " + searchKey);
else
System.out.println("Found " + searchKey);
}
O/P:-
Found 66
7
class MatrixSum
{
public static void main(String[] args)
{
int array[][]= {{4,5,6},{6,8,9}};
int array1[][]= {{5,4,6},{5,6,7}};
System.out.println("Number of Row= " + array.length);
System.out.println("Number of Column= " + array[1].length);
int l= array.length;
System.out.println("Matrix 1 : ");
for(int i = 0; i < l; i++)
{
for(int j = 0; j <= l; j++)
{
System.out.print(" "+ array[i][j]);
}
System.out.println();
}
int m= array1.length;
System.out.println("Matrix 2 : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+array1[i][j]);
}
System.out.println();
}
System.out.println("Addition of both matrix : ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(" "+(array[i][j]+array1[i][j]));
}
System.out.println();
}
}
}
8
O/P:-
Number of Row= 2
Number of Column= 3
Matrix 1 :
456
689
Matrix 2 :
546
567
Addition of both matrix :
9 9 12
11 14 16
9
class basicmath
{
public static void main(String args[])
{
// arithmeic using integers
System.out.println("Integer Arithmetic:");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// Modulus operator
System.out.println("\nModulus operator:");
int x = 42;
double y = 42.3;
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}
10
O/P:-
Integer Arithmetic:
a=2
b=6
c=1
d = -1
e=1
Modulus operator:
x mod 10 = 52
y mod 10 = 2.299999999999997
11
class bitlogic
{
public static void main(String args[])
{
String binary[] =
{"0000","0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110","1111"};
O/P:-
a = 0011
b = 0110
a|b = 0111
a&b = 0010
a^b = 0101
~a&b|a&~b = 0101
~a = 1100
12
if (a == b)
System.out.println(a + " and " + b + " are equal\n");
if (c != d && d > c)
{
System.out.println(c + " is not equal to " + d + " AND ");
}
if (b >= a || d >= c)
{
System.out.println(b + " is greater than or equal to " + a + " OR ");
System.out.println(d + " is greater than or equal to " + c);
}
}
}
O/P:-
14 and 14 are equal
13 is not equal to 16 AND
16 is greater than 13
13 is less than 14
13 and 14 are
less than or equal to 14 and 16
14 is greater than or equal to 14 OR
16 is greater than or equal to 13
13
class IfElse
{
public static void main(String args[])
{
int month = 4; // April
String season;
O/P:-
}
}
}
O/P:-
class DoWhile
{
public static void main(String args[])throws java.io.IOException
{
char choice;
do
{
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. while");
System.out.println(" 4. do-while");
System.out.println(" 5. for\n");
System.out.println("Choose one:");
choice = (char) System.in.read();
}while( choice < '1' || choice >'5');
System.out.println("\n");
switch(choice)
{
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case '3':
System.out.println("The while:\n");
System.out.println("while(condition) statement;");
break;
case '4':
System.out.println("The do-while:\n");
System.out.println("do {");
16
System.out.println(" statement;");
System.out.println("} while (condition);");
break;
case '5':
System.out.println("The for:\n");
System.out.print("for(init; condition; iteration)");
System.out.println(" statement;");
break;
}
}
}
O/P:-
Help on:
1.if
2.switch
3.while
4.do-while
5.for
Choose one:1
If(condition)
statement;
else
statement;
Choose one :3
The while :
While(condition)
Statement;
Choose one :5
The for :
For(init;condition;iteration)
Statement;
17
class Whiles
{
public static void main(String args[])
{
int n = 1;
System.out.println("Numbers Squares");
System.out.println("------- -------");
while(n < 11)
{
System.out.println(n + " " + n*n);
n++;
}
}
}
O/P:-
Numbers squares
--------------------------
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
18
class Fors
{
public static void main(String args[])
{
int num;
boolean isPrime = true;
num = 14;
O/P:-
14 is Not Prime
19
class Box
{
double width;
double height;
double depth;
O/P:-
Volume is 3000.0
Volume is 162.0
20
class Box
{
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
class parameter
{
public static void main(String args[])
{
// declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
O/P:-
Volume is 3000.0
Volume is 162.0
21
class OverloadDemo
{
void test()
{
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a)
{
System.out.println("a: " + a);
}
void test(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
double test(double a)
{
System.out.println("double a: " + a);
return a*a;
}
}
class ovrmthds
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
O/P:-
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
22
class Box
{
double width;
double height;
double depth;
class overloading
{
public static void main(String args[])
{
O/P:-
class Test
{
void meth(int i, int j)
{
i *= 2;
j /= 2;
}
}
class cbv
{
public static void main(String args[])
{
Test ob = new Test();
int a = 15, b = 20;
O/P:-
class Test
{
int a, b;
Test(int i, int j)
{
a = i;
b = j;
}
// pass an object
void meth(Test o)
{
o.a *= 2;
o.b /= 2;
}
}
class cbr
{
public static void main(String args[])
{
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +ob.a + " " + ob.b);
}
}
O/P:-
class A
{
int i, j;
void showij()
{
System.out.println("i and j: " + i + " " + j);
}
}
void showk()
{
System.out.println("k: " + k);
}
void sum()
{
System.out.println("i+j+k: " + (i+j+k));
}
}
class inheritance
{
public static void main(String args[])
{
its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}
O/P:-
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
class Box
{
private double width;
private double height;
private double depth;
// default constructor
BoxWeight()
{
super();
weight = -1;
}
class demosuper
{
public static void main(String args[])
{
O/P:-
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
class Box
{
private double width;
private double height;
private double depth;
// Add weight.
// default constructor
BoxWeight()
{
super();
weight = -1;
}
// default constructor
Shipment()
{
super();
cost = -1;
}
class DemoShipment
{
public static void main(String args[])
{
Shipment shipment1 =
new Shipment(10, 20, 15, 10, 3.41);
Shipment shipment2 =
new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is "+ shipment1.weight);
34
O/P:-
class A
{
int i, j;
A(int a, int b)
{
i = a;
j = b;
}
// display i and j
void show()
{
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A
{
int k;
B(int a, int b, int c)
{
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show()
{
System.out.println("k: " + k);
}
}
class Override1
{
public static void main(String args[])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
O/P:-
k: 3
36
class A
{
void callme()
{
System.out.println("Inside A's callme method");
}
}
class B extends A
{
void callme()
{
System.out.println("Inside B's callme method");
}
}
class C extends A
{
void callme()
{
System.out.println("Inside C's callme method");
}
}
class Dispatch
{
public static void main(String args[])
{
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
O/P:-
Inside A's callme method
Inside B's callme method
Inside C's callme method
37
abstract class A
{
abstract void callme();
class B extends A
{
void callme()
{
System.out.println("B's implementation of callme.");
}
}
class AbstractDemo
{
public static void main(String args[])
{
B b = new B();
b.callme();
b.callmetoo();
}
}
O/P:-