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]
3 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]3 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
A Web ____ is a software program that retrieves the page and displays it. Select one:
kari74 [83]
A. browser Web browsers are programs like internet explorer, safari, chrome. You use them to access web pages on the internet.
4 0
3 years ago
_______ allow(s) you to apply colorful, eye catching designs to a presentation all at once. A. Themes B. Slide orientation C. Th
hram777 [196]
Its A. Themes 
theme is a set of unified design elements that u can apply to a eye catching headline.
6 0
3 years ago
Read 2 more answers
Explain how communication has helped to the staff in organization to reach their maximum delivery of service with efficience and
Gwar [14]

Answer:

i don't know

Explanation:

6 0
2 years ago
What commonly predefined alias is configured to run the ls âl command?
coldgirl [10]
<span>The l</span><span>l command is the commonly predefined alias that is configured to run the ls âl command. The command ls stands for list. So instead of writing list, in Linux you only write the command ls.
The alias are </span>shortcuts and time-savers. By typing ll we'll get the current directory's listing, in long format, including hidden directories.


6 0
3 years ago
To create a formula in ___ you would first click in one of the cells. A.word b.excel c.facebook d.powerpoint
guajiro [1.7K]
The correct anwser is excel 
7 0
3 years ago
Other questions:
  • What is the primary criticism against a national identification system based on biometric data?
    13·1 answer
  • What is the term for the process of gathering information through images taken at a distance?
    13·1 answer
  • Im trying to learn c# for unity does anyone know any good sources
    10·1 answer
  • What are some programs that you have used that have condition-controlled loops and count-controlled loops?
    10·1 answer
  • list six external parts or peripherals of a computer system and identify which are output and which are input devices
    8·1 answer
  • Josh's boss asked him to write a letter to their customers explaining some upcoming price increases. But Josh was in a hurry to
    8·2 answers
  • A company that provides finance services wants to shift to a mobile platform. What is the major advantage that the users of the
    10·2 answers
  • In an URL what does the domain name describe?
    14·1 answer
  • "If a user on a laptop complains that they are unable to sign into Windows even though they are certain they are entering the co
    12·1 answer
  • Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!