Java An experiment was conducted and resulted in a series of
Java
An experiment was conducted and resulted in a series of pH measurements. The number of samples is known ahead of time. Have your program prompt the user to enter that number of measurements. Validate the number to ensure it\'s in the range of 2 to 100 measurements, inclusive. If it is out range, display an error message, Error: valid range is 2 to 100 If it\'s within the range, then use the number of measurements to drive a counter-controlled while loop. During each iteration of the loop, prompt the user to enter a pH sample, one value at a time. When all data has been input, calculate and display the average (mean) pH value. Before calculating the mean, be sure to check for division by 0. The input dialog and output must conform to these examples: Good range: Enter the number of samples: 4 Sample 1: 4.3 Sample 2: 10.1 Sample 3: 7.2 Sample 4: 8.7 Mean pH: 7.58 Bad range: Enter the number of samples: 1 Error: valid range is 2 to 100
Solution
import java.util.*;
import java.lang.*;
import java.io.*;
class Samples
{
public static void main (String[] args) throws java.lang.Exception
{
int samples;
int count=0;
double pH,mean,sum;
sum=0;
mean=0;
Scanner keyboard = new Scanner(System.in);
System.out.println(\"Enter the number of samples\");
samples =keyboard.nextInt();
if(samples < 2 || samples >100) //check number of samples range
System.out.println(\"Error: valid range is 2 to 100 \");
else
{
while(count<samples)
{
System.out.println(\"Sample \"+ (count+1));
pH = keyboard.nextDouble();
count++;
sum = sum + pH; //sum the pH values to get the mean
}
}
if(count ==0) //check for counter value
System.out.println(\"Error: can\'t do division by 0\");
else
mean = sum/count;
System.out.println(\"Mean pH = \"+mean);
}
}
output:
Success time: 0.07 memory: 711680 signal:0
