Fill the apt code to invoke the addNumbers method, by passing num1 and num2 as attributes.import java.util.*;public class Test{ public static int addNumbers(int a,int b) { return a+b; } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int num1=sc.nextInt(); int num2=sc.nextInt(); Test testObj = new Test(); int result = blank ; }}Test.addNumbers(num1,num2) testObj.addNumbers(num1,num2) testObj.addNumbers(int num1,int num2) Test.addNumbers(int num1,int num2)
Question
Fill the apt code to invoke the addNumbers method, by passing num1 and num2 as attributes.import java.util.*;public class Test{ public static int addNumbers(int a,int b) { return a+b; } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int num1=sc.nextInt(); int num2=sc.nextInt(); Test testObj = new Test(); int result = blank ; }}Test.addNumbers(num1,num2) testObj.addNumbers(num1,num2) testObj.addNumbers(int num1,int num2) Test.addNumbers(int num1,int num2)
Solution
The correct code to invoke the addNumbers method by passing num1 and num2 as attributes is:
int result = Test.addNumbers(num1,num2);
This is because the method addNumbers is a static method and it belongs to the class, not the instance of the class. Therefore, you should call it on the class (Test) itself, not on an instance of the class (testObj).
Similar Questions
Arrange the below code in correct order :import java.util.Scanner;public static void main(String args[]) {Scanner sc=new Scanner(System.in);public class Main{int num1 = sc.nextInt();System.out.println("The value is "+num1);}}
class Calculator { public static int add(int a, int b) { return a + b; } public static int add(int a, int b, int c) { return a + b + c; }}public class Main { public static void main(String[] args) { int result1 = Calculator.add(10, 20); int result2 = Calculator.add(10, 20, 30); System.out.println("Result1: " + result1 + ", Result2: " + result2); }}a.Result1: 30, Result2: 60b.Result1: 20, Result2: 30c.None of the aboved.Compilation Error
Fill with the apt data type for the variable result.public class Main{ public static void main(String args[]) { int number1=10; double number2 = 54.5; Answer result = number1 + number2; }}
Write a Java program to add two binary numbers.
Add two numbersYou have been provided with some code. The code should take in two numbers from the user, add them together, and then display the result to the user. However, the code has bugs!Debug this code!Here are some examples of how the program should work:Example 1Enter a number: 32Enter another number: 64The sum of your two numbers is: 96.00Example 2Enter a number: 1234.5678Enter another number: 9999.99The sum of your two numbers is: 11234.56Example 3Enter a number: 23Enter another number: -532.824The sum of your two numbers is: -509.82
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.