PROGRAM 1:
import [Link]; // Import Scanner class
// Class that handles input and addition
class Adder {
void add() {
Scanner sc = new Scanner([Link]); // Create Scanner object
[Link]("Enter first number: ");
int a = [Link](); // Read first number
[Link]("Enter second number: ");
int b = [Link](); // Read second number
int sum = a + b; // Addition
[Link]("The sum is: " + sum);
[Link](); // Close scanner
}
}
// Main class
class AddNumbers1 {
public static void main(String[] args) {
// Create object of Adder using 'new'
Adder obj = new Adder();
// Call add() method (which handles input + sum)
[Link]();
}
}
OUTPUT:
PROGRAM 2:
class Adder {
void add(int x, int y) {
int sum = x + y; // calculate sum
[Link]("The sum is: " + sum);
}
}
// Main class
class AddNumbers2 {
public static void main(String[] args) {
Adder obj = new Adder(); // create object of Adder class
int a = 15;
int b = 25;
[Link](a, b); // call add() method using object
}
}
OUTPUT:
PROGRAM 3:
class Add1{
public static void main(String[] args) {
int a = 10; // first number
int b = 20; // second number
int sum = a + b; // addition
[Link]("The sum is: " + sum);
}
}
OUTPUT: