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
¿como influye la tecnologia en su vida cotidiana?
ANEK [815]

Answer:

La tecnología afecta la forma en que hablamos, aprendemos y pensamos. También nos hemos vuelto dependientes de la tecnología.

Explanation:

3 0
3 years ago
Internet Security: How can you work securely on the internet? What actions should be avoided and what are the pitfalls?
Cerrena [4.2K]

Answer:First of all, to work safely at the internet you should avoid any not secure page, and if you can’t do that, avoid entering stuff such as credit cards or passwords on this sites. Now, to work safely it’s recomendable to have a VPN(virtual private network), that hides your IP direction.

Explanation:

8 0
3 years ago
how to get into harvard stanford mit ivy league guaranteed upper middle class need aid 9th grade, computer science major have no
Murrr4er [49]

Answer:

Colleges care a lot more about your math skills than computer stuff, even for computer science. Make sure you still do some computer science stuff (esp. if it's extracurricular) but focus on improving your math.

8 0
3 years ago
What are the data type(s) will be used to declare the variable(s) needed to
postnew [5]
The three primitive data types used in this course are int (integer numbers), double (decimal numbers), and boolean (true or false). Each variable has associated memory that is used to hold its value.
4 0
3 years ago
You acquire a network vulnerability-scanning tool and try it out on a network address segment belonging to people at your univer
inna [77]

Answer and Explanation:

  • The speculates on either the morality of transmitting vulnerabilities to an individual over all the internet. The node is a very possible target of a criminal charge, these are highly recommended in terms of the problem not just ethical.
  • The question argues the etiquette of telling a compromised network infrastructure to something like a domain admins or security guard. Throughout this case the primary admin issue is power. Informing individuals about both the potential problem is prudent or legal, which is also preferable to recommend the future course of action.
  • The speculates on either the moral values of leveraging the infrastructure for a mild vulnerability. This same proprietor including its node is truly likely to be victims of a prospective infringement, and therefore it is advantageous to notify him including its problem that the equitable access is considered to become an ethical manipulate susceptibility for possessor data as well as potential threats to understanding.
  • The theories a small flaw throughout the channel's ethics. The device's leader is the likely guilty party of even a future offense to notify him of both the actual problem. The law is ridiculous as well as comparable to trying to hack without permission vulnerability it's immoral vulnerability.
  • The content upon the ethical principles of manipulating the channel's small susceptibility. The device's owner seems to be the likely casualty of such a future offense to instruct him including its subject. As well as trying to sell him much farther documents socially responsible borders. It's the holder who has so far notified the weakness she perhaps she has just one option to obtain products and services. Having clear data on the sale still seems to be ethical.
  • The issue argues mostly on ethics with repairing security flaws without channel assent. Although the controlled variable of the modules has been the true likely target of such a future infringement, exploiting susceptibility without permission is appropriate as well as unethical, this same objective being honorable as well as noble.
8 0
3 years ago
Other questions:
  • PLEASE HELP PROGRAMMING WILL GIVE BRAINLIEST
    7·1 answer
  • Question 1 of 20 :
    15·2 answers
  • Write a program that removes all spaces from the given input. Ex: If the input is: Hello my name is John. the output is: Hellomy
    11·1 answer
  • Digital dashboards provide the decision makers with a quick overview of key performance indicators and other key operational sta
    13·1 answer
  • A _____ is often used in Access to present a menu with command buttons that end users can click to perform various database acti
    13·1 answer
  • A computer with 5 stage pipeline like the one descrive in class delas with conditional branches by stalling for the next three c
    5·1 answer
  • (1) Output a menu of automotive services and the corresponding cost of each service. (2 pts)Ex:Davy's auto shop servicesOil chan
    9·1 answer
  • Implement the Dining Philosophers problem (described on pages 167-170 in the textbook (chapter 2.5.1)). Create a Graphical User
    13·1 answer
  • Anyone know what I’m supposed to do?
    11·1 answer
  • You are configuring a network and have been assigned the network address of 221.12.12.0. You want to subnet the network to allow
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!