import java.util.Scanner; // program uses class scanner public class MainClass {
ID: 3632004 • Letter: I
Question
import java.util.Scanner; // program uses class scannerpublic class MainClass {
public double mean(double[] m) {
double sum1 = 0;
for (int i = 0; i < m.length; i++) {
sum1 += m[i];
}
return sum1 / m.length;
}
// find the mode
public static double mode(double b[]) {
double maxValue=0;
double maxCount = 0;
for (int i = 0; i < b.length; ++i) {
int count = 0;
for (int j = 0; j < b.length; ++j) {
if (b[j] == b[i])
count++;
}
if (count > maxCount) {
maxCount = count;
maxValue = b[i];
}
}
return maxValue;
}
// the array double[] m MUST BE SORTED
public static double median(double[] m) {
int middle = m.length/2;
if (m.length % 2 == 1) {
return m[middle];
} else {
return (m[middle-1] + m[middle]) / 2.0;
}
}
public static void main(String args[])
{
// create Scanner to obtain list size
Scanner input = new Scanner(System.in);
Test method = new Test();
int listSize;
System.out.println("Enter the amount of (double)numbers: ");// prompt
listSize=input.nextInt(); // read in number
double [] scores = new double[listSize];
int n = 0; // current number of values in scores
Scanner in = new Scanner(System.in);
System.out.println("Enter the input: ");// prompt for the array
while(in.hasNextDouble()) {
scores[n] = in.nextDouble();
n++;
}
// Code below is to find the minimum value
int firstCounter = 0;
double min = 0;
int sizeChecker = 0;
while(firstCounter<scores.length)
{
if(min<=scores[sizeChecker]){
min = scores[sizeChecker];
sizeChecker++;
firstCounter++;
}
}
// Code below to is to find the maximum value
int secondCounter = 0;
double max = 0;
int sizeChecker2=0;
while(secondCounter<scores.length)
{
if(max>=scores[sizeChecker2]){
max=scores[sizeChecker2];
sizeChecker2++;
secondCounter++;
}
}
// Code below is to find the sum
int thirdCounter = 0;
double sum = 0;
int sizeChecker3=0;
while(thirdCounter<scores.length)
{
sum=sum+scores[sizeChecker3];
sizeChecker3++;
thirdCounter++;
}
System.out.println("Result: %.2f Result: %.2f Result: %.2f Result: %.2f Result: %.2f Result: %.2f ",min,max,sum,
mean(scores),median(scores),mode(scores));
}
}
Explanation / Answer
Please Rate --------------------------------------------- import java.util.Scanner; // program uses class scanner public class MainClass { public static double mean(double[] m) { double sum1 = 0; for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.