Write a program using default constructor.
Find sum of two numbers. (Java ClassX icse)
Answers
The given problem is solved using language - Java.
import java.util.Scanner;
public class Constructor{
int a,b,s;
Constructor(){
Scanner sc=new Scanner(System.in);
System.out.print("Enter first number: ");
a=sc.nextInt();
System.out.print("Enter second number: ");
b=sc.nextInt();
sc.close();
}
void add(){
s=a+b;
}
void display(){
System.out.println("Sum of two numbers: "+s);
}
public static void main(String s[]){
Constructor obj=new Constructor();
obj.add();
obj.display();
}
}
#1
Enter first number: 894
Enter second number: 568
Sum of two numbers: 1462
——————————————————————
#2
Enter first number: 55
Enter second number: 45
Sum of two numbers: 100
——————————————————————
#3
Enter first number: 1728
Enter second number: 4472
Sum of two numbers: 6200