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
Huan wants to enter the science fair at his school. He has a list of ideas for his project. Which questions could be answered th
never [62]

<u>Answer:</u>

<em>Science fair always has experiments which prove science concepts.</em> Students will bring apparatus or an lab items and show experiments as a magic. There are only certain things which a Science can prove and show to other.

<em>From the given question, the following can be proved: </em>

  • <em>Does pressure have an effect on the volume of a gas? </em>
  • <em>Which brand of soap is the best for cleaning grease off dishes? </em>
  • <em>Which laboratory experiment is the most fun to perform?</em>

<em> The below cannot be proved but can be explained </em>

  • <em>Is the information on the periodic table difficult to understand? </em>
  • <em>Which physicist was the smartest?</em>
4 0
3 years ago
Utilizando o padrão de medidas e unidades do National Institute of Standards and Technology, em que 1 KB é igual a 1.000 Bytes,
Tanya [424]

Segundo o padrão da  National Institute of Standards and Technology a  alternativa correta é a letra E) 400 músicas.  Segundo o padrão disposto acima, dois Giga Bytes equivalem à dois mil Mega Bytes, assim, se cada música tem exatos 5MB, podemos chegar ao total de músicas que podem ser gravadas em um PenDrive de 2GB livres do seguinte modo :

x=\frac{2000 MB}{5MB};x=400

 A evolução da microeletrônica possibilitou uma capacidade de armazenamento cada vez maior a cada ano. O desenvolvimento de novas tecnologias na área permite também que os produtores possam produzir músicas, fotos e vídeos com cada vez mais qualidade (consequentemente mais pesada).

5 0
2 years ago
Which element of presentation software can you use to add callouts and banners?
Harrizon [31]
<span><span>1.)From the Insert tab, select the Shapes command. A drop-down menu will appear.
</span><span>2.)Select the desired shape
3.)</span></span>Click and drag<span> the slide to create the shape. You may need to move or resize the shape so it points to the desired part of the image.
4.)</span><span>f you want your callout to contain text, start typing while the shape is selected.
5.)</span><span>From the </span>Format<span> tab, you can use the options in the </span>Shape Styles<span> group to customize the appearance of the shape. You can also adjust the font from the Home tab</span>
7 0
2 years ago
Read 2 more answers
Programming CRe-type the code and fix any errors. The code should convert non-positive numbers to 1.
LUCKY_DIMON [66]

Answer:

Given

The above lines of code

Required

Rearrange.

The code is re-arrange d as follows;.

#include<iostream>

int main()

{

int userNum;

scanf("%d", &userNum);

if (userNum > 0)

{

printf("Positive.\n");

}

else

{

printf("Non-positive, converting to 1.\n");

userNum = 1;

printf("Final: %d\n", userNum);

}

return 0;

}

When rearranging lines of codes. one has to be mindful of the programming language, the syntax of the language and control structures in the code;

One should take note of the variable declarations and usage

See attachment for .cpp file

Download cpp
5 0
3 years ago
PLEASE HURRY!!
PtichkaEL [24]

Answer: I would say the first one

Explanation: I have never seen an interactive button for use in autoformatting at least on a presentation

8 0
2 years ago
Other questions:
  • Tim has an old server computer that his company uses as a backup. One of the hard drives has gone bad and needs to be replaced.
    13·1 answer
  • What is key for evaluating internal controls such as human resources, logistics, information technology, risk, legal, marketing
    11·1 answer
  • What is the Slide Sorter View used for?
    12·2 answers
  • Describe the process of sorting the following list using the above algorithm:
    7·1 answer
  • You are in the windows power shell window and decide to encrypt folder which of the following command do you use
    7·1 answer
  • Your _______ can help block inappropriate content online.<br> 1. web browser<br> 2. Password
    14·1 answer
  • Rosa is building a website for a multimedia company. She wants to add a drop-down menu functionality to the website's main page.
    12·1 answer
  • When a ____________ file is opened, it appears full-screen, in slideshow mode, rather than in edit mode.
    6·1 answer
  • What do we call the software which programmers use to program?​
    6·1 answer
  • Which of the following digital communicatio media does not require a connection to the internet
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!