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
Aika has several labeled folders for different parts of his research. What is the MOST likely reason he does this? He wants to m
kobusy [5.1K]

Answer:

<u>He wants to easily and quickly find specific bits of research that cover certain topics.</u>

<u>Explanation:</u>

Remember, a research work typically has these 6 (six) components:

  • The Abstract.
  • Introduction
  • Methodology
  • Results
  • Discussion
  • References.

Thus, Aika's decision to label several folders for different parts of his research would make it easier and quicker for him to find specific bits of research that cover certain topics, instead of spending time looking for topics.

4 0
3 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
What is the outlined area called?
LenKa [72]

Answer:

If you're talking about perimeter (the length of the outer edges)

Explanation:

6 0
4 years ago
The ability for new computers to be automatically connected to the network is provided by the:________
Brrunno [24]

Answer:

DHCP server

Explanation:

5 0
2 years ago
Cute - or - ugly ???
maria [59]

Answer:

ur not ugly

Explanation:

6 0
3 years ago
Read 2 more answers
Other questions:
  • Andy, a developer, is designing a new program. Which tool should Andy use to help him complete his task?
    6·1 answer
  • Which keyboard command can be used to delete an image from one slide while adding it to the clipboard so that it can be added in
    5·2 answers
  • Which of the following is not anadvantage of simulation?
    14·1 answer
  • Fazer um programa em C++ que contenha 3 funções sendo:
    11·1 answer
  • What is a half note + a 8th note in band? plz helppp
    11·2 answers
  • Write a function shampoo_instructions() with parameter num_cycles. If num_cycles is less than 1, print "Too few.". If more than
    9·1 answer
  • From the philosophical standpoint, especially in the discussion of moral philosophy or ethics, why do we consider “murder” or “k
    9·1 answer
  • How to run angular project from github.
    7·1 answer
  • Collection activities acquire and extract data from the operational environment such that Processing and Exploitation can conver
    14·1 answer
  • What are the uses of navigation keys​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!