Question: In Java, The process of finding a specific piece of data that is in among a bunch of other pieces of data is a common

In Java, The process of finding a specific piece of data that is in among a bunch of other pieces of data is a common computer

application. For example, Google and other search engines have to search very quickly through mountains of data. In

this application, you need to write code that will find the largest and smallest number among a set of numbers, and also

find the average of all the numbers.

When the code is running it will prompt the user to enter a real number one at a time. The user stops entering numbers

by typing q for quit. After all numbers have been entered, the code will print out what the biggest number is, the

smallest number is, as well as the average of all the numbers. The output to the screen will look as follows:

Welcome to Number Finder!

Please enter digits. Enter q to quit.

>> 3.1

>> 4.2

>> 5.0

>> q

Smallest Number = 3.1

Largest Number = 5.0

Average = 12.3

Your program will need to use, at least, three variables. Declare them to be doubles and use the following variable

names.

1. largestNumber, The largest number entered so far.

2. smallestNumber, The smallest number entered so far.

3. sum, A number that keeps a running sum (for calculating the average at the end).

Well have to read a String from the keyboard in order to allow the user to enter the character q. If the user enters a

number well have to convert the String to a double before we can perform any operations on it.

To read in a String from a Scanner object use the method next(), rather than nextDouble(). Then compare the

users input with the letter q by using the String method equals(). An example may look like this:

String ans = input.next(); // read in user input as String and put in the variable ans

ans.equals(q) // would equal false if ans is not equal to q, true otherwise.

// can use as the sentinel for a while loop possibly

double num = Double.parseDouble(ans); // if not quitting, convert ans to a double with the

// parseDouble() method of the Double class

For your code to be robust think of ways that the user could crash your code and program accordingly. Will your code

work with q and Q to force a quit? What if the user enters no numbers, but only enters a q to quit, what will your

code print out? How are you going to initialize all the variables? For instance, what do you initialize smallest and

largest to?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!