In this article,Categories of Java and Program using two classes explained in detail with examples. we will come to know about the information on how methods are created and categorized with examples provided with output and code file.
Based on return type and parameters,methods in java are categorized into 4 type.
- Methods without return type and without parameters.
- Methods without return type and with parameters.
- Methods with return type and without parameters.
- Methods with return type and with parameters.
//Program based on Categories of Java and Program using two classes.
class example (From the topic Categories of Java and Program using two classes)
{
//without return type and without parameters.
void diff()
{
int a,b,c;
a=100;
b=25;
c=a-b;
System.out.println("Difference is"+c);
}
//without return type and with parameters.
void prod(int a,int b)
{
int c;
c=a*b;
System.out.println("product is"+c);
}
//with return type and without parameters.
int div()
{
int a,b,c;
a=100;
b=12;
c=a/b;
return c;
}
//with return type and with parameters.
int sum(int a,int b,int c)
{
int total;
total=a+b+c;
return total;
}
public static void main(String args[])
{
int t;
example obj=new example();
obj.diff();
obj.prod(12,5);
t=obj.div();
System.out.println("Division is"+t);
System.out.println("Sum is"+obj.sum(96,84,73));
}
}
Output
Difference is75
product is60
Division is8
Sum is253
Java program using two classes.
In a single java program,we can write more than one class(We can use two classes), but before saving the program make sure that the class which contains method, is same as that of the program name.
Ex: (From the topic Categories of Java and Program using two classes)
//Java program using Categories of Java and Program using two classes.
class RectangleArea //here the file should be saved as RectanlgeArea.java
{
public static void main(String args[])
{
int a1,a2;
Rectangle obj1=new Rectangle();
Rectangle obj2=new Rectangle();
obj1.len=5;
obj1.bred=10;
a1=obj1.len*obj1.bred;
obj2.getdata(10,20);
a2=obj2.rectarea();
System.out.println("Area1 is"+a1);
System.out.println("Area2 is"+a2);
}
class Rectangle
{
int len,bred;
void getdata(int x,int y)
{
len=x;
bred=y;
}
int rectarea()
{
int area=len*bred;
return area;
}
}
save the file using classname.java(i.e,RectangleArea.java).
Output
Area1 is50
Area2 is200
Continue to next topic Java variables and program of variables.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises,surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment