a) Implementing Class & Objects
Aim: To write a JAVA program to implement class mechanism. – Create a class, methods and invoke them inside main method.
Description:
Programs:
1.no return type and without parameter-list:
class A
{
int l=10,b=20;
void display()
{
System.out.println(l);
System.out.println(b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
a1.display();
}
}
Output:
class A
{
int l=10,b=20;
void display()
{
System.out.println(l);
System.out.println(b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
a1.display();
}
}
Output:
2.no return type and with parameter-list:
class A
{
void display(int l,int b)
{
System.out.println(l);
System.out.println(b);
}
}
class methoddemo1
{
public static void main(String args[])
{
A a1=new A();
a1.display(10,20);
}
}
Output:
class A
{
void display(int l,int b)
{
System.out.println(l);
System.out.println(b);
}
}
class methoddemo1
{
public static void main(String args[])
{
A a1=new A();
a1.display(10,20);
}
}
Output:
3.return type and without parameter-list
class A
{
int l=10,b=20;
int area()
{
return l*b;
}
}
class methoddemo2
{
public static void main(String args[])
{
A a1=new A();
int r=a1.area();
System.out.println("The area is: "+r);
}
}
Output:
class A
{
int l=10,b=20;
int area()
{
return l*b;
}
}
class methoddemo2
{
public static void main(String args[])
{
A a1=new A();
int r=a1.area();
System.out.println("The area is: "+r);
}
}
Output:
No comments