1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
maxonik [38]
4 years ago
13

Write the definition of a method named countPos 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 in standard input and returns the number that are positive. So if the input were: 19 -5 -3 -251 14 -7 -14 6 the method would return 3 because there are 3 positive integers there. The method must not use a loop of any kind (for, while, do-while) to accomplish its job.

Computers and Technology
1 answer:
NNADVOKAT [17]4 years ago
3 0

Answer:

Here is the method countPos    

  static int countPos(Scanner input){ //method that takes a reference to a Scanner object

   int counter=0;  // to count the positive integers  

   int number; //to store each integer value

   if(input.hasNextInt()){ //checks if next token in this scanner input is an integer value

       number=input.nextInt(); //nextInt() method of a Scanner object reads in a string of digits and converts them into an int type and stores it into number variable

       counter=countPos(input); //calls method by passing the Scanner object and stores it in counter variable

       if(number>0) //if the value is a positive number

           counter++;    } //adds 1 to the counter variable each time a positive input value is enountered

   else    { //if value is not a positive integer

       return counter;    } //returns the value of counter

   return counter; } //returns the total count of the positive numbers

Explanation:

Here is the complete program:

import java.util.Scanner; //to take input from user

public class Main { //class name

//comments with each line of method below are given in Answer section

static int countPos(Scanner input){

   int counter=0;

   int number;

   if(input.hasNextInt()){

       number=input.nextInt();

       counter=countPos(input);

       if(number>0)

           counter++;    }

   else    {

       return counter;    }

   return counter; }

public static void main(String[] args) { //start of main function

       System.out.println("Number of positive integers: " + countPos(new Scanner(System.in)));  } } //prints the number of positive integers by calling countPos method and passing Scanner object to it

The program uses hasNextInt method that returns the next token (next input value) and if condition checks using this method if the input value is an integer. nextInt() keeps scanning the next token of the input as an integer. If the input number is a positive number then the counter variable is incremented to 1 otherwise not. If the use enters anything other than an integer value then the program stops and returns the total number of positive integers input by the user. This technique is used in order to avoid using any loop. The program and its output is attached

You might be interested in
The undo can undo the last command only. Clicking on it more than once will do nothing.
Dmitriy789 [7]
True I hope I helped.
7 0
3 years ago
Read 2 more answers
In performing a SWOT analysis, a firm realized that its managers were untrained to handle the requirements of its new division.
Rufina [12.5K]
SWOT stands for strengths, weaknesses, opportunities, and threats. Being untrained sounds like it would be classified as a weakness.
3 0
4 years ago
What is the maximum tolerance for computer clock synchronization?
zavuch27 [327]
Hey there Shedan9609,

What is the maximum tolerance for computer clock synchronization?

Answer:

5 mins

Hope this helps :D

<em>~Top♥</em>


8 0
3 years ago
Suppose a JPanel with a BorderLayout manager contains two components: component1, which was added to the EAST, and component2, w
Tamiku [17]

Answer:

b) I, II, III

Explanation:

Since the two components are added to the 'east' and 'west', and the JPanel has a BorderLayout manager, then North, South and Center parts of the JPanel will appear.

3 0
3 years ago
Which is developed during the process of technological design?
s2008m [1.1K]

Answer:Steps of the technological design process include: identify a problem, research the problem, generate possible solutions, select the best solution, create a model, test the model, refine and retest the model as needed, and communicate the final solution.

Explanation:

:)

3 0
3 years ago
Other questions:
  • What Are the main reasons students take the SAT/ACT?
    15·1 answer
  • Moving Images are called________.
    15·1 answer
  • Will a large computer monitor have a clearer picture than using my plasma tv screen?
    9·1 answer
  • How do you delete an credit card off an iPhone 7
    5·1 answer
  • How do you join The Meeting on zoom because I have class Tomorrow with My Teacher
    7·2 answers
  • Identify some advantages of using Excel over lists, paper files, or simple word documents?
    6·1 answer
  • You would use the _______ conditional formatting options when analyzing a worksheet in which you want to highlight the highest o
    13·1 answer
  • THERES THIS USER ON HERE WHO IS TAKING ALL YOUR POINTS SO I DID THE SAME THING TO HER CUZ SHE DID IT TO ME...SO WHY DONT WE ALL
    5·1 answer
  • Help me..and thank you​
    14·1 answer
  • A(n) ________ portal offers a personalized, single point of access through a web browser to employees located inside and outside
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!