1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
yarga [219]
3 years ago
6

Write a Java Program that can compute the mean, median, the highest, and the lowest of the students’ scores in a class. First, t

he user inputs the number of students in the class and you capture this number. Then the user inputs the grades of all the students in the class. After capturing all the inputs, your program should output the mean, the median, the highest, and the lowest.
Computers and Technology
1 answer:
slava [35]3 years ago
4 0

Answer:

import java.util.Arrays; // The Array class allows for array operations

import java.util.Scanner; // The Scanner class allows for user's inputs

public class Statistics {

public static void main(String[] args) {

 // create an object of the Scanner class

 Scanner input = new Scanner(System.in);

 System.out.println("Please enter the number of students");

 // Using "input" which is an object of the Scanner class,

 // get the number of students.

 int noOfStudents = input.nextInt();

 // Create an array of scores whose length is equal to the number of students

 // Array is of type double as students' scores could be floating point numbers

 double[] scores = new double[noOfStudents];

 // Create a loop that cycles as many times as the number of students.

 // Each loops asks the user to enter the score of each student

 // Initialize the scores array with each score at every loop

 int i = 0; // This is a counter for the loop

 while (i < noOfStudents) {

  System.out.println("Enter score for student " + (i + 1));

  scores[i] = input.nextDouble();

  i++; // increment counter at every loop

 }

 // At the end of the loop above, the scores array should be completely

 // initialized

 // You might want to print out the scores before sorting

 System.out.println(Arrays.toString(scores));

 // Sort the scores array so as to be able to calculate the median

 Arrays.sort(scores);

 // Print out the results by calling the necessary methods

 System.out.println("Mean score : " + mean(scores));

 System.out.println("Median score is : " + median(scores));

 System.out.println("Highest score : " + highest(scores));

 System.out.println("Lowest score : " + lowest(scores));

}

// Method to calculate the mean of the scores in the scores array

// Initialize a variable, sum, to zero to allow for cumulative addition of the

// array elements.

// Loop through the scores array adding up the array elements cumulatively

// Divide the result of the sum by the length of the array to get the

// mean(average).

// In this case, scores array is represented by arr

public static double mean(double[] arr) {

 double sum = 0;

 for (int i = 0; i < arr.length; i++) {

  sum += arr[i];

 }

 return sum / arr.length; // Return the mean (average).

 

} // End of the mean method  

 

 

// Method to calculate the median of the scores in the scores array

// Median is the middle element in a sorted set of elements

// If the length(number of elements) of the sorted set of elements is even

// there will be two elements representing the median.

// To break this tie, the average of the two elements is found.

// In this case, scores array is represented by arr

public static double median(double[] arr) {

 //Check if the length of the array is even or not.

 //This will help to break the tie if it occurs.

 

 if (arr.length % 2 != 0) {

  int mid = (arr.length + 1) / 2;   //mid represents the middle number

  return arr[mid - 1];    // Because arrays are indexed from zero(0), 1 is subtracted from mid

 } else {

  int mid = (arr.length) / 2;

  return (arr[mid] + arr[mid + 1]) / 2.0;

 }

} // End of the method median

 

 

// Method to calculate the highest of the scores in the scores array

// In this case, the scores array is represented by arr

public static double highest(double[] arr) {

 

 //Declare and initialize a temp variable to hold the first element in the array

 double temp = arr[0];

 //Loop through the array starting at the second position since the value in the first position has been stored in a temp variable

 for (int i = 1; i < arr.length; i++) {

  //At every loop, check if there is a number greater than the one stored in the temp variable

  //If there is, replace temp with such number

  if (arr[i] > temp) {

   temp = arr[i];

  }

 }

 return temp; //Return the highest number

} // End of the method highest

 

 

// Method to calculate the lowest of the scores in the scores array

// In this case the scores array is represented by arr

public static double lowest(double[] arr) {

 

 //Declare and initialize a temp variable to hold the first element in the array

 double temp = arr[0];

 //Loop through the array starting at the second position since the value in the first position has been stored in a temp variable

 for (int i = 1; i < arr.length; i++) {

  //At every loop, check if there is a number less than the one stored in the temp variable

  //If there is, replace temp with such number

  if (arr[i] < temp) {

   temp = arr[i];

  }

 }

 return temp; //Return the lowest number

} // End of the method lowest

}

Explanation:

Explanations are explicitly written in the answer above in comments.

The source code file (Statistics.java) has been attached to this answer.

Hope this helps!.

Download java
You might be interested in
To rename a worksheet, you change the text on the ? HELP ASAP
prohojiy [21]
If this is Excel, it would be C. Sheet tab
5 0
3 years ago
The set of specific, sequential steps that describe exactly what a computer program must do to complete the work is called a(n)
STatiana [176]

Answer:

algorithm

Explanation:

7 0
3 years ago
If you experience a denial-of-service attack, you can use firewall logs to determine the _______ from which the attack originate
suter [353]
<span>The answer is IP address.  If you experience a denial-of-service attack, you can use firewall logs to determine the IP address from which the attack originated.</span>
6 0
3 years ago
Read 2 more answers
"""
Vinvika [58]

head1 = "Number: "
head2 = "Multiplied by 2: "
head3 = "Multiplied by 10: "
NUM_LOOPS = 10 # Constant used to control loop.

print("0 through 10 multiplied by 2 and by 10" + "\n")

# Initialize loop control variable.
# Write your counter controlled while loop here
# Multiply by 10.
# Multiply by 2.
x = 0
while(x<11):
print(head1 + str(x))
print(head2 + str(x*2))
print(head3 + str(x*10))
x += 1
# Next number.

4 0
2 years ago
Which of the following is typically an advantage of configuring your own hosting server over using an ISP or a cloud service pro
Inessa [10]

Answer:

D that's the answer it has to be

5 0
4 years ago
Other questions:
  • The sound cards is a digital to____________ converter.
    11·1 answer
  • What are possible consequences for cyberbullying?
    11·2 answers
  • PLEASE HELP!!!!!!!!!!!!!!!!!!
    6·1 answer
  • How do you create a logo on Adobe illustrator
    8·1 answer
  • Witch of the following is a valid why a scientist might a scientific theory
    13·1 answer
  • Please answer please
    12·2 answers
  • Select the correct answer.
    7·2 answers
  • Can someone suggest how to manage a group when you are the group leader?​
    10·2 answers
  • Within how many days can a deleted view be restored?
    6·1 answer
  • PLEASE HELP!!! NO LINKS!! I'LL GIVE BRAINLIEST!!
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!