java objects
java objects
-------------------------
class : A class is collection of different data type elements and the functions
which are going to apply on those data items is called as Class. The data items
specified under the class called as Data members and the functions under the class
can be called as Methods.
The methods and the data members are of the class can be able to access with class
variable called as object. The object of the class can be created with new key
word.
class A
{
int a,b; //data members
void get() // methods
{
a=10;
b=20;
}
void sum()
{
int c=a+b;
System.out.println("The sum is " + c);
}
}
class B
{
public static void main(String [ ] a)
{
A ob = new A();
ob.get();
ob.sum();
}
}
Ex 2:
Area of a rectangle
class B
{
int l,b;
void get()
{
l=10;
b=20;
}
void area()
{
int c=l*b;
System.out.println("The area is " + c);
}
public static void main(String [ ] a)
{
B ob = new B();
ob.get();
ob.area();
}
}
Ex 3:
class ABC
{
int a,b,h;
void get()
{
a=10;
h=30;
b=20;
}
double trep()
{
double d;
d=0.5 * h * (a+b);
return(d);
}
}
class B
{
public static void main(String [ ] a)
{
ABC ob = new ABC();
ob.get();
double x=ob.trep();
System.out.println("The area is " + x);
}
}
Parameters can be specified along with class. The parameters specified in a class
can be called as actual arguments and the parameters specified under methods can
be called as formal arguments. The formal arguments and the actual arguments should
not be the same.
class A
{
int a,b; // actual parameters
void get(int x,int y) // formal parameters
{ 40 50
a=x;
b=y;
}
int sum()
{
int c=a+b;
return (c);
}
}
class B
{
public static void main(String [ ] a)
{
int x=Integer.parseInt(a[0]); 40
int y=Integer.parseInt(a[1]); 50
A ob = new A();
ob.get(x,y);
int c= ob.sum();
System.out.println("The sum is " + c);
}
}
Ex 5:
class ABC
{
int a,b,h;
void get(int x,int y,int z)
{
a=x; 60
h=y; 70
b=z; 90
}
double area()
{
double Ar=(0.5 * h * (a+b));
return(Ar);
}
}
} java B 10 20 30
class B
{
public static void main(String [ ] a)
{
ABC ob = new ABC();
int x=Integer.parseInt(a[0]); 60
int y=Integer.parseInt(a[1]); 70
int z=Integer.parseInt(a[2]); 90
ob.get(x,y,z);
double A=ob.area();
System.out.println("The area is " + A);
}
}
This Operator:
This always represents current object. When actual arguments and formal arguments
are same to differentiate both we refer this to the actual argument.
class A
{
int a,b;
void get (int a,int b)
{
this.a=a;
this.b=b;
}
int sum()
{
int c=a+b;
return (c);
}
}
class B
{
public static void main(String [ ] a)
{
int x=Integer.parseInt(a[0]); 50
int y=Integer.parseInt(a[1]); 70
A ob = new A();
ob.get(x,y);
int c= ob.sum();
System.out.println("The sum is " + c);
}
}
Ex:2
class A
{
int p,n,r;
void get(int p,int n,int r)
{
this.p=p;
this.n=n;
this.r=r;
}
double inter()
{
double I =p*n*r*0.01;
return (I);
}
}
class B
{
public static void main(String [ ] a)
{
int x=Integer.parseInt(a[0]);
int y=Integer.parseInt(a[1]);
int z=Integer.parseInt(a[2]);
A ob = new A();
ob.get(x,y,z);
double c= ob.inter();
System.out.println("The simple interest is " + c);
}
}
----------------------
Constructor : Constructor is the special kind of method whose class name is the
same as method name. Constructor is special because which does not returns any
value and does not consists of any return type. Constructor executes automatically
when we create an object for the class. The purpose of the constructor is to
initial the values at the time of creating object for the class.
class A
{
int a,b;
A()
{
a=10;
b=40;
}
int sum()
{
int c=a+b;
return (c);
}
}
class B
{
public static void main(String [ ] a)
{
A ob = new A();
int c= ob.sum();
System.out.println("The sum is " + c);
}
}
1. Display diagonal of rectangle
2. Display area of triangle with sides
3. Display Compound interest p * ( 1 + r/100) n
class B
{
int l,b;
B()
{
l=10;
b=40;
}
void area()
{
int c=l*b;
System.out.println("The area is "+c);
}
public static void main(String [ ] a)
{
B ob = new B();
ob.area();
}
}
Parameterised constructor:
At the time of creating object for the class we may pass values of the variables at
run time called parameterised constructor.
class A
{
int a, b;
A(int x, int y)
{
a=x;
b=y;
}
int sum()
{
int c=a+b;
return (c);
}
}
class B
{
public static void main(String [ ] a)
{
int x=Integer.parseInt(a[0]); 60
int y=Integer.parseInt(a[1]); 90
A ob = new A(x,y);
int c= ob.sum();
System.out.println("The sum is " + c);
}
}
class A
{
int a,b,c;
A(int x, int y, int z)
{
a=x;
b=y;
c=z;
}
double area()
{
double D = 0.5 * (a+b)*c;
return(D);
}
}
class B
{
public static void main(String [ ] a)
{
int x=Integer.parseInt(a[0]);
int y=Integer.parseInt(a[1]);
int z=Integer.parseInt(a[2]);
A ob = new A(x,y,z);
double D= ob.area();
System.out.println("The area is " + D);
}
}
Copy Constructor : We can also called as Constructor Over Loading. At the time of
creating
object for the class we can able to pass object of the class as parameter.
class A
{
int a,b;
A()
{
a=10;
b=20;
}
A(int x,int y)
{
a=x;
b=y;
}
A( A X)
{
a=X.a;
b=X.b;
}
void sum()
{
int c=a+b;
System.out.println("The sum is " + c);
}
}
class B
{
public static void main(String [ ] a)
{
int x=Integer.parseInt(a[0]); 60
int y=Integer.parseInt(a[1]); 90
A P = new A();
P.sum(); 30
A Q = new A(x,y);
Q.sum(); 150
A R = new A(Q);
R.sum(); 150
}
}
class A
{
int r;
A()
{
r=10;
}
A(int x)
{
r=x;
}
A( A X)
{
r=X.r;
}
void area()
{
System.out.println("The area is " + (3.14 *r*r));
}
}
class B
{
public static void main(String [ ] a)
{
int n=Integer.parseInt(a[0]); 100
A P = new A();
P.area();
A Q = new A(n);
Q.area();
A R = new A(Q);
R.area();
}
}
Ex3:
class A
{
int l,b;
A()
{
l=10;
b=20;
}
A(int x,int y)
{
l=x;
b=y;
}
A( A X)
{
l=X.l;
b=X.b;
}
void area()
{
int c=l*b;
System.out.println("The area of rectangle is " + c);
}
}
class B
{
public static void main(String [ ] a)
{
int x=Integer.parseInt(a[0]); 60
int y=Integer.parseInt(a[1]); 90
A P = new A();
P.area(); 30
A Q = new A(x,y);
Q.area(); 150
A R = new A(Q);
R.area(); 150
}
}
Method Overloading : It is the process of specifying same method name for more than
one
method in the same class called Method Overloading. It may be differ in type
signature. i.e
either datatype may be differ or number of arguments may different or return type
may be
different.
class A
{
void sum(int a,int b)
{
System.out.println("The sum of two numbers is " +(a+b));
}
void sum(double x,double y)
{
System.out.println("The double of two numbers is" + (x+y));
}
void sum(String s1,String s2)
{
System.out.println("The sum is " +(s1+s2));
}
}
class ABC
{
public static void main(String [ ]x)
{
int a=Integer.parseInt(x[0]); 10
int b=Integer.parseInt(x[1]); 20
double p=Double.parseDouble(x[2]); 10.5
double q=Double.parseDouble(x[3]); 20.5
String s1=x[4]; ssi
String s2=x[5]; limited
A ob = new A();
ob.sum(a,b);
ob.sum(p,q);
ob.sum(s1,s2);
}
}
1 . volume of box,sphere,cone
2. area of triangle,circle,trepezium,rectangle
1. accept name and three subject marks display name,total,avg and grade.