AaßbCcDdEe AaBbCcDdEe AaBbCcDc AaBbCcDdE !le=tiLi.jde | Normal | No Spacing Head
ID: 3593725 • Letter: A
Question
AaßbCcDdEe AaBbCcDdEe AaBbCcDc AaBbCcDdE !le=tiLi.jde | Normal | No Spacing Heading 1 Heading Lab 5: Working with Sentinel-controlled Loop Problem Statement: Write a java program that reads a set of integer data values from console, one at a time, auntil a sentinel value (which in this case is 9999) is given as input. The program is expected to compute statistics of input values read such as count, average, max, and min and print them with proper labels. You are expected to develop this program and test it in various stages as suggested below. Develop the program in stages as shown below, testing each stage thoroughly before proceeding to next stage. Submit only the finalwjaxa file on Lab5 assignment on or before deadline to receive S point credit Stage 1: Set up a sentinel-controlled loop- read and print input values until sentinel value Develop java code using the following guidelines: Define a Scanner object such as input/scnx/atdin for reading console input Declare type variables such as: nun , un, max , min, count Declare double type variable such as: average and initialize its value to. Set up a priming stage of the loop as follows: (is.befos the loop) . * . . o o o Print a prompt statement such as: Enter an integer Read the input value and store in a variable num Assign a zero value to count variable here value, or -9999 to quit Set up sentinel controlled while loop here as follows while (num-9999) /I bodx of loop Here is what you need to add to the body of the loop in Stage 1 Increment the count now (count++a-Loop body is entered only if the sumber read is the real data, and a not a sentinel value. Variable count is used to keep track of exact number of data values read 2. Print the value of the count and the value of the num (separated by a tab) 3. Now we need to read another data item into aum before end of the loop Print a prompt statement such as: Enter an integer value, or 9999 to quit Read the input valuc and store in a variable num Note java code in the last step is exactly simlar to the code that appears in priming stage Test the loop you have written in Stage I with S or 6 data values one at a time, end it with a sentinel value (-9999) to signify end of data. Make sure that the output is as expected Congratulations! You have successfully implemented a sentinel-controlled loop to read print data values ge 2: Now we are ready to compute the sum of all numbers read, calculate average and print results Add the following statements before the leop (in priming stage) Add the following statements in the body of the loep, after printing input value: Add the following statements after the loop Assign a zero value for the variable sum aum-sum+num W add num value to sum and store it back in sum for accumalation Syatem.outprintia(unmber of input data values read t count) Syatem.out pxintia ("Sun of all data valuest+sum) Pages: 10f 2 Words: 96 of 923 :Explanation / Answer
The required Java program is given below ( with comment in bold and italics) :
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc= new Scanner(System.in); //Defining Scanner Object to read console input
int num,sum,max,min,count; //Declaring Integer variables
double average =0.00; //Declaring Double variable
System.out.println("Enter an integer value, or -9999 to quit"); //priming stage
num=sc.nextInt();
max = num; //first value read is set to max
min = num; //first value read is set to min
count=0;
sum=0;
while (num != -9999){ //start of loop
count++;
System.out.println(count + " " + num);
sum=sum+num; //add num value to sum and store it back in sum for accumulation
if (num > max)
max = num; // adjust max value
if (num <min)
min = num; // adjust min value
System.out.println("Enter an integer value, or -9999 to quit");
num=sc.nextInt();
} //end of while loop
System.out.println("Number of input data values read : " + count); //printing no. of values
System.out.println("Sum of all data values : " + sum); //printing sum of all values
if (count != 0)
average = (double) sum / (double) count;
System.out.print("Average = ");
System.out.printf("%10.4f", average); //format with 4 decimals
System.out.println();
System.out.println("Max value = " + max); //printing max value
System.out.println("Min value = " + min); //printing min value
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.