Java Unit 1 Pratical
Java Unit 1 Pratical
/* program to implement methods with no arg, one arg and mul ple arguments
it implements method overloading...(polymorphism) */
class demo1
{
// func on prototypes
sta c void area() // method declara on and defini on
{
System.out.println("Welcome to area calcula on..");
}
sta c void area(int l)
{
System.out.println("Area of square = " + l*l);
}
sta c void area(double r)
{
System.out.println("Area of circle = " + (3.14* (r*r)) );
}
sta c void area(int l, int b)
{
System.out.println("Area of rectangle = " + (l*b) );
}
public sta c void main(String args[])
{
// method calling
area(); // no arg... welcome only
area(10); // square
area(3.78); // circle
area(55,60); // rectangle
} //main over
} // class over
class demo2
{
public sta c void main(String args[])
{
emp e1 = new emp();
//e1.empid=101;
e1.setdata();
e1.showdata();
} //main over
} // class over
/* program to input two numbers from command line and display maximum number using
ternary operator.
*/
class max
{
public sta c void main(String args[])
{
int a, b,c;
a = Integer.parseInt( args[0] );
b = Integer.parseInt( args[1] );
if ( a == b)
{
System.out.println("both numbers are equal");
}
else
{
c = (a>b) ? a : b ;
System.out.println("maximum = " + c );
}
} // main over
} // class over
//*overload_demo
public class Overload_demo
{
sta c void calc_area()
{
int a,l,b;
l=10;
b= 5;
a = 2*(l*b);
System.out.println("Area is : " + a);
}
sta c void calc_area(int l)
{
int a = l*l;
System.out.println("Area of square is : " + a);
}
sta c void calc_area(int l, int b)
{
int a = 2*l*b;
System.out.println("Area of rectangle is : " + a);
}
sta c void calc_area(float l, int b)
{
float a = 2*(l*b);
System.out.println("Area of rectangle with float is : " + a);
}
class productdemo
{
public sta c void main(String args[])
{
product p1 = new product(); // default constructor
p1.display();
}//main over
}//class over
// use of sta c method and sta c variable
// and sta c block
class emp
{
private int empid;
public sta c int count=0;
public emp(int empid)
{ this.empid=empid; //solves ambiguity (confusion) between same name of
//class variable and func on argument variable
// this refers to current object
count++;
System.out.println(count + " object created");
}
void display()
{
System.out.println("\n Empd id = " + empid);
}
}
class sta cdemo1
{
public sta c void main(String args[])
{
emp e1 = new emp(101);
e1.display();
emp e2 = new emp(233);
e2.display();
class u1p2
{
public sta c void main(String args[])
{
int amt, years;
float rate, total, si, emi;
amt = Integer.parseInt(args[0]);
rate = 12.50f;
years = 5;
si = (amt*rate*years)/100;
total = amt + si;
emi = total/60;
System.out.println("Loan of " + amt + " at the rate of " + rate + "%" );
System.out.println("simple interest = " + si);
System.out.println("Total amount = " + total);
System.out.println("EMI for "+ years + " years = " +
String.format("%.2f",emi) );
}//main over
}//class over
class u1p4
{
public sta c void main(String args[])
{
int a,b,c, max;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
if ( a == b && a == c && b == c)
System.out.println("All are euqal");
else
{
max = (a >= b && a >= c) ? a :
(b >= a && b >= c)? b : c;
System.out.println("max = " + max);
}
}//main over
}//class over
Note:
compelx number is like
a+bi
where,
a = real part (integer)
b = imagenary part (integer)
i = posi ve (non-nega ve) integer
e.g.
c1 = 2+3i
c2 = 45+13i
c3 = 47+16i // c3 = c1 + c2
(-12) + (-5)i
*/
class complex
{
int a,b;
complex()
{
a = 10;
b = 10;
//System.out.println("object created with default values");
}
complex(int x, int y)
{
a = x; b =y;
System.out.println("object created with argument values");
}
void disp()
{
System.out.println(a + " + " + b + " i");
}
complex add(complex c2)
{
complex dummy = new complex();
dummy.a = a + c2.a;
dummy.b = b + c2.b;
return dummy;
}
class u1p7
{
public sta c void main(String args[])
{
complex c1 = new complex(2,3);
c1.disp();
complex c2 = new complex(45,13);
c2.disp();
complex c3 = new complex();
c3 = c1.add(c2); // c3 = dummy
c3.disp();
complex c4 = new complex();
c4 = c1.sub(c2);
c4.disp();
complex c5 = new complex();
c5 = c1.mul(c2);
c5.disp();
} // main over
} // class u1p7 over
/*
data types
primi ve derived
int array, structure,
char linked list, stack, queue
long class, typedef, enum
float
double
boolean
*/