Using two or more classes

Try to open your JCreator, or whatever third party java application you have. You can also use plain notepad if you want and play it through the use of your CMD.

Let's use Sample as the class name for this one...


class Sample
{
int sum;

Sample()
{
sum = 0;
}

void getSum(int num1, int num2)
{
sum = num1+num2;
}

void display()
{
System.out.println("The sum is:" + sum);
}
}


Try to create a new class with the filename of MClass (without deleting your first class which is the sample), and paste the code below:


import java.io.*;
public class MClass
{
public static void main (String args[]) throws IOException
{
BufferedReader m = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter First Number: ");
int a = Integer.parseInt(m.readLine());
System.out.println("Enter Second Number: ");
int b = Integer.parseInt(m.readLine());

Sample x = new Sample();
x.getSum(a,b);
x.display();
}
}


What's your output? Interesting, isn't it? :D

No comments:

Post a Comment

Extended Search

Custom Search
Hello World