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
What is one difference between a web page and a web application? Web pages provide information, while web applications allow the
Assoli18 [71]

Webpage is a page of a site, such as https://example.com/test, where the bolded text is the page.

On the other hand, web applications, or simply web apps, are found in many websites, such as here and also in Connexus. They end in either aspx or jsp, which stand for asp.net appx and javascript page, respectively.


So, your answer would be A: Web pages provide information, while web applications allow the user to do something.

Hope this was answer you were looking for. Have a nice day!

7 0
2 years ago
What is the main advantage of Ram<br><br>​
andre [41]

Answer:

to get milk

Explanation:

gjvuvuvjvvjvjvvjvjvhvjvub

7 0
3 years ago
WHO LOVES THE KREW LOOK IT UP ON YOU_TUBE IF UR NOT SURE BUT PLZ SUBSCRIBE TO HER SHE DA BEST "YOOT" the one with pink and golde
kodGreya [7K]

Answer:

i like her mate :3 glad im not alone!

Explanation:

7 0
2 years ago
Read 3 more answers
Write a family database program. Create a class to represent a person and to store references to the person’s mother, father, an
Gennadij [26K]

Do you go to BASIS?

Sorry I don't have an answer for you, but we have the same assignment in our AP Comp sci class.

Just wondering.

8 0
2 years ago
Data is best described as
viktelen [127]

Answer:

The answer is B

Explanation:

TOOK THE TEST

5 0
3 years ago
Read 2 more answers
Other questions:
  • Array A is not a heap. Clearly explain why does above tree not a heap? b) Using build heap procedure discussed in the class, con
    15·1 answer
  • How is a ink pen better than a digital pen.
    8·1 answer
  • What is the term for the error caused when one end of an unshielded twisted-pair (UTP) cable is terminated in the T568A configur
    6·1 answer
  • ARP request is send to
    12·1 answer
  • A list is sorted in ascending order if it is empty or each item except the last one is less than or equal to its successor.
    14·1 answer
  • MLB The Show 17, which simulates professional baseball games, and Madden Football, which simulates professional football games,
    5·1 answer
  • Need help finding the totals and with the empty spots
    9·1 answer
  • 3.5 Lesson practice quiz: Edhesive Question 5
    8·1 answer
  • Explain why a holiday on a cruise liner will be an ideal holiday​
    12·1 answer
  • Write a Pascal program that will prompt the user to enter the radius of a circle.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!