Java Program to Implement Class Mechanism

Aim:

To write a JAVA program to implement a class mechanism. – Create a class, methods and invoke
them inside the main method

1. no return type and without parameter-list:

Program:

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:

10
20

2. no return type and with parameter-list:

Program:

class A
{
void display(int l,int b)
{
System.out.println(l);
System.out.println(b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
a1.display(10,20);
}
}

Output:

10
20

3. return type and without parameter-list

Program:

class A
{
int l=10,b=20;
int area()
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
int r=a1.area();
System.out.println("The area is: "+r);
}
}

Output:

The area is:200

4. return type and with parameter-list:

Program:

class A
{
int area(int l,int b)
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
int r=a1.area(10,20);
System.out.println(“The area is:”+r);
}
}

Output:

The area is:200

Leave a Comment