Saturday, August 27, 2011

Class Object Example in java


//ClientClass.java
/*This is the main class from where we call a method [sum(parameter1,parameter2)] of another class named [MethodSuplierClass] by creating a object of that class[MethodSuplierClass] and use the "."(dot) operator. */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ClientClass
{
public static void main(String args[]) throws IOException
{

int a,b;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter first numbers:");
a=Integer.valueOf(br.readLine()).intValue();

System.out.println("Enter second numbers:");
b=Integer.valueOf(br.readLine()).intValue();

MethodSuplierClass MethodSuplierClassObject = new MethodSuplierClass();
System.out.println("Sumassion of two numbers is :");
MethodSuplierClassObject.sum(a,b);
}
}


//MethodSuplierClass.java
public class MethodSuplierClass
{
public void sum(int a,int b)
{
System.out.println(a+"+"+b+"="+(a+b));
}
}


output =>
Enter first numbers:
15
Enter second numbers:
36
Sumassion of two numbers is :
15+36=51

No comments:

Post a Comment