Question: Write a class named PrintAverage.java The main method of the class should only contain a call to a void method named userInterface() which takes no

Write a class named PrintAverage.java

The main method of the class should only contain a call to a void method named userInterface() which takes no parameters. The userInterface() method must use a sentinel loop to repeatedly prompt the user for integers. If the user enters anything other than an integer, display a new prompt that says "Not an integer, try again: " (use a while loop and the Scanner hasNextInt() method for this).

Once the user types any negative number (sentinel), the method should display the average of all the non-negative numbers typed. Display the average as a double. Assume inputs do not contain spaces.

Here is a sample dialogue with the user:

csc$ java PrintAverage

Type an integer (negative number to quit): 4 Type an integer (negative number to quit): i Not an integer, try again: L Not an integer, try again: % Not an integer, try again: 7 Type an integer (negative number to quit): 16 Type an integer (negative number to quit): -8 Average was 9.0

csc$ If the first number that the user types is negative (count of non-negative integers entered is 0), do not print an average. Sample user dialog for this case:

csc$ java PrintAverage

Type an integer (negative number to quit): -2

csc$

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!