Using Java 1.Declare and initialize a Scanner to read from the keyboard. 2.Decla
ID: 671666 • Letter: U
Question
Using Java
1.Declare and initialize a Scanner to read from the keyboard.
2.Declare an integer variable called value and initialize it to 0.
3.Declare an integer variable called count and initialize it to 0.
4.Declare a double variable called mean and initialize it to 0.0.
5.Declare an integer variable called minimum and set it equal to Integer.MAX_VALUE.
6.Declare an integer variable called maximum and set it equal to Integer.MIN_VALUE;
7.Write a while loop that never terminates!
8.Inside the loop:
Read in an integer value from the keyboard, without prompting.
If the value equals the sentinel (for example, -1), exit the loop with a break statement.
If value is smaller than minimum, assign the value to minimum.
If value is larger than maximum, assign the value to maximum.
Add the value to the mean.
Increment the count.
9.After the loop has completed, divide the mean by the count.
10.Print "Count: " and the value of count, with a newline.
11.Print "Average: " and the value of mean, with a newline.
12.Print "Maximum: " and the value of maximum, with a newline.
13.Print "Minimum: " and the value of minimum, with a newline.
Explanation / Answer
public static void computeStatistics(int sentinel)
{
Scanner sc = new Scanner(System.in);
int value=0;
int count =0;
double mean =0;
double av;
int minimum = Integer.MAX_VALUE;
int maximum = Integer.MIN_VALUE;
while(1)
{
value=sc.nextInt();
if(value==sentinel)
{
break;
}
elseif(value<minimum)
{
value=minimum;
}
else
{
value=maximum;
}
mean=mean+value;
count++;
}
av=mean/count;
System.out.println("Count: "+count);
System.out.println("Average: "+av);
System.out.println("Maximum: "+maximum);
System.out.println("Minimum: "+minimum);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.