import java.io.*;
import java.util.Scanner;
public class Addition
{
public static void main(String[]args)
{
int a,b,c;
Scanner s=new Scanner(System.in);
System.out.println("enter first number");
a=s.nextInt();
System.out.println("enter the second number");
b=s.nextInt();
c=a+b;
System.out.println("Sum of"+a+"and"+b+" "+c);
}
}
c
class Add
{
public static void main(String[]args)
{
int a,b,c,d;
Scanner s=new Scanner(System.in);
System.out.println("enter the first number");
a=s.nextInt();
System.out.println("enter the second number");
b=s.nextInt();
System.out.println("enter the Third number");
c=s.nextInt();
d=a+b+c;
System.out.println("sum of"+a+"and"+b+"and"+c+" "+d);
}
}
In the above program we use two class first is Addition and second is Add. But both class have a main function, this is impossible because only one main class we can create in a program or project.So we can create a program using two class like this
import java.io.*;
java.util.Scanner;
public class Addition // first or main class
{
public static void main(String[]args) throws IOException
{
int a,b,c;
Add obj=new Add();// object creation obj.sum();
Scanner s=new Scanner(System.in);
System.out.println("enter first number");
a=s.nextInt();
System.out.println("enter the second number");
b=s.nextInt();
c=a+b;
System.out.println("Sum of"+a+"and"+b+" "+c);
}
}
class Add // second class
{
public void sum()
{
int a,b,c,d;
Scanner s=new Scanner(System.in);
System.out.println("enter the first number");
a=s.nextInt();
System.out.println("enter the second number");
b=s.nextInt();
System.out.println("enter the Third number");
c=s.nextInt();
d=a+b+c;
System.out.println("sum of"+a+"and"+b+"and"+c+" "+d);
}
}
Here, this program have two class but only one main function. So we can create an object of another class and use the Add class in our program.
Save the file as Addition.java. After that we can call this class through of it's object.
Nyc one
ReplyDelete