google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Write a Program in Java using two and more class.

Write a Program in Java using two and more class.

1
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

Java programming language


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.

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top