Types of methods:
class Methods
{
public void add()//without returntype/without argument
{
int a=123;
int b=10;
System.out.println(“Addition=” +(a+b));
}
}
class Functiondemo
{
public static void main(String args[])
{
Methods o=new Methods();//classname objectname=new classname();
o.add();
}
}
import java.util.*;
class Methods
{
public void sub(int x,int y)//without returntype/with /arguments
{
System.out.println(“Subtraction=” +(x-y));
}
}
class Functiondemo
{
public static void main(String args[])
{
Methods o=new Methods();
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
o.sub(a,b);
}
}
class Methods
{
public int mul()//with returntype/without arguments
{
int a=123;
int b=10;
return a*b;
}}
class Functiondemo
{
public static void main(String args[])
{
Methods o=new Methods();
int x=o.mul();
System.out.println(x);
}
}
class Methods
{
public float div(int x,int y)//with returntype/with arguments
{
return (x/y);
}
class Functiondemo
{
public static void main(String args[])
{
Methods o=new Methods();
System.out.println(“Division=” +o.div(123,10));
}
}
//Invoking the methods:
class Box
{
int length;
int breadth;
int height;
int volume()
{
return length*breadth*height;
}
}
class Demo
{
public static void main(String args[])
{
Box a=new Box();
a.length=10;
a.breadth=8;
a.height=6;
//a.volume();invoking method
System.out.println(“Volume of SteelBox=” +a.volume());
Box b=new Box();
b.length=30;
b.breadth=24;
b.height=25;
//b.volume();invoking method
System.out.println(“Volume of WoodBox=” +b.volume());
}
}
class Demo
{
int length;
int breadth;
int height; //10 //4 //6
static int boxvolume(int length,int breadth,int height)
{
int vol=length*breadth*height;//10*4*6
return vol;
}
}
class MMain
{
public static void main(String[] args)
{
System.out.println(Demo.boxvolume(10,4,6));
}
}