java. For this exercice, you will need to write a method that asks 10 numbers to
ID: 3782133 • Letter: J
Question
java.
For this exercice, you will need to write a method that asks 10 numbers to the user in the main method. These numbers represent 10 student’s grade. Once the 10 numbers are obtained, you will need to call the four other methods passing the 10 numbers as an argument. The method calculateAverage needs to calculate and then print the average of the grades. The method calculateMedian calculates and prints the median of the grades. The method calculateNumberFailed calculates and prints the number of students that failed and calculateNumberPassed calculates and prints the number of students that passes. The passing grade is 50%.
LabExercice6f class public static void main St ring[] args //your code here public static void calculat (double notes lateAverage //your code here public static void calcu lateMedian (double notes //your code here public static void calcu Failed (double[] notes //your code here public static void calcu lateNumbe r Passed (double[] notes //your code hereExplanation / Answer
LabExercise6.java
import java.util.Scanner;
public class LabExercise6 {
public static void main(String[] args) {
//Declaring a double type array of size 10
double notes[]=new double[10];
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
/* This for loop will get the grades entered by the user
* and populate those values into an array named notes
*/
for(int i=0;i<10;i++)
{
//Getting the grade entered by the user
System.out.print("Enter Grade "+(i+1)+":");
notes[i]=sc.nextDouble();
}
//Calling the methods
calculateAverage(notes);
calculateMedian(notes);
calculateNumberFailed(notes);
calculateNumberPassed(notes);
}
//This method will find the no of students passed and display
private static void calculateNumberPassed(double[] notes) {
int passed=0;
for(int i=0;i<notes.length;i++)
{
if(notes[i]>=50)
//Counting the number of students passed
passed++;
}
//Displaying the number of students passed
System.out.println("The Number passed is "+passed);
}
private static void calculateNumberFailed(double[] notes) {
int failed=0;
for(int i=0;i<notes.length;i++)
{
if(notes[i]<50)
//This method will find the no of students failed and display
failed++;
}
//Displaying the number of students failed
System.out.println("The Number Failed is "+failed);
}
private static void calculateMedian(double[] notes) {
int size=notes.length;
double temp;
double median = 0;
//This for loop is used to sort the array in an ascending order.
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (notes[i] > notes[j])
{
temp = notes[i];
notes[i] = notes[j];
notes[j] = temp;
}
}
}
/* if the size of an array is even the if block will be executed
* An find the median value.
*/
if(size%2==0)
{
int a=size/2;
int b=a+1;
median=(notes[a]+notes[b])/2;
}
//Displaying the median of students grades array
System.out.println("Median of the array is :"+median);
}
//This method will find the average of students grade
private static void calculateAverage(double[] notes) {
//Declaring the variables
double sum=0.0,average;
//This for loop will find the sum of student grades
for(int i=0;i<notes.length;i++)
{
sum+=notes[i];
}
//Calculating the average
average=sum/notes.length;
//Displaying the average
System.out.println("The Average Grade is :"+average);
}
}
____________________________
Output:
Enter Grade 1:45
Enter Grade 2:33
Enter Grade 3:78
Enter Grade 4:89
Enter Grade 5:90
Enter Grade 6:87
Enter Grade 7:77
Enter Grade 8:66
Enter Grade 9:83
Enter Grade 10:32
The Average Grade is :68.0
Median of the array is :80.5
The Number Failed is 3
The Number passed is 7
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.