Ask Question
18 July, 11:47

Write the definition of a method named add that receives a reference to a Scanner object associated with a stream of input consisting of integers only. The method reads all the integers remaining to be read from the stream and returns their sum. So if the input were 3 51 204 17 1040, the returned value would be 1315. The method must not use a loop of any kind (for, while, do-while) to accomplish its job.

+3
Answers (1)
  1. 18 July, 15:40
    0
    public int add (Scanner input) {

    int sum = input. nextInt ();

    if (input. hasNextInt ()) {

    sum=sum+add (input);

    }

    return sum;

    }

    Explanation:

    Define a method named add that receives a reference to a Scanner object. Take sum as input from the user. Use if statement to check whether it has an integer. Add that integer to sum variable. Return the sum variable.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write the definition of a method named add that receives a reference to a Scanner object associated with a stream of input consisting of ...” in 📘 Engineering if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers