Answer to Question #348913 in Java | JSP | JSF for Dunga

Question #348913

Write a program that initially requests


the user to enter the number of students


in a class. The program then proceeds to request the user to enter the marks (double) of these


students; these are to be inserted into an


array. Once all the marks have been entered,


your program shall have and use the below


functions


1. public static double sum(int [] array). Calculate & return sum of all marks.


2. Public static double[] sort(int []


array). Sort array containing marks.


3. public static double max(int []


array). Return highest mark.


4. public static double min(int [] array). Return lowest mark.

1
Expert's answer
2022-06-07T17:44:26-0400
import java.util.Arrays;
import java.util.Scanner;

class Main {
    public static double sum(double[] array) {
        double s = 0;
        for (double element : array)
            s += element;
        return s;
    }

    public static double[] sort(double[] array) {
        double[] arrayCopy = array.clone();
        Arrays.sort(arrayCopy);
        return arrayCopy;
    }

    public static double max(double[] array) {
        double m = 0;
        for (double element : array)
            if (element > m)
                m = element;
        return m;
    }

    public static double min(double[] array) {
        double m = Double.MAX_VALUE;
        for (double element : array)
            if (element < m)
                m = element;
        return m;
    }

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter the number of students in a class: ");
            int numberOfStudents = scanner.nextInt();
            double[] marks = new double[numberOfStudents];
            System.out.print("Enter the marks of students separating by spaces: ");
            for (int i = 0; i < numberOfStudents; i++)
                marks[i] = scanner.nextDouble();
            System.out.println("1. The sum of all marks: " + sum(marks));
            System.out.print("2. Sorted array of marks: ");
            for (double mark : sort(marks))
                System.out.print(mark + " ");
            System.out.println();
            System.out.println("3. The highest mark: " + max(marks));
            System.out.println("4. The lowest mark: " + min(marks));
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS