Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im trying to develop a Simple Statistical Tool in Java. the program shall displa

ID: 654461 • Letter: I

Question

Im trying to develop a Simple Statistical Tool in Java.

the program shall display the prompt asking the user to enter a positive number (to continue) or a negative number (to stop input and display the results). The input shall be entered on the same line, not on the next line.

If the user enters a negative number at the first iteration, the program shall exit immediately (use System.exit()).

If the user enters a negative number at any following iteration, the program shall display the smallest and the largest previously entered number and the mean of the previously entered numbers. The program then shall terminate. The most recently entered negative number shall not be included in the computations.

The program shall not be robust against incorrect inputs. (In other words, it shall assume that the input is always a valid number.)

.

Explanation / Answer

/*Acheved every thing except single line input just check*/(solution is Robust for test cases)

import java.util.Scanner;

public class StatisticalTool {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int input, max=0, min=0, sum = 0, count = -1;
       while ((input = sc.nextInt()) >= 0) {
           if (count == -1) {
               max = min = input;
               count=0;
           }
           if(max<input)
               max=input;
           if(min>input)
               min=input;
           sum = sum + input;
           count++;
       }
       sc.close();
       if(count==-1)
           System.exit(0);
       else{
           System.out.println("minimum number="+ min);
           System.out.println("maximum number="+ max);
           System.out.println("mean="+ (double)sum/count);
       }
   }
}

sample output:

123
12
16
18
45
26
72
999
12
444
555
666
-9
minimum number=12
maximum number=999
mean=249.0