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

Question #348914

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-08T14:27:32-0400
import java.util.Arrays;
import java.util.Scanner;

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

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

    public static double max(double[] array) {
        double m = array[0];
        for (int i = 1; i < array.length; i++)
            if (array[i] > m)
                m = array[i];
        return m;
    }

    public static double min(double[] array) {
        double m = array[0];
        for (int i = 1; i < array.length; i++)
            if (array[i] < m)
                m = array[i];
        return m;
    }

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter the number of students: ");
            int n = scanner.nextInt();
            double[] marks = new double[n];
            System.out.println("Enter their marks:");
            for (int i = 0; i < n; i++)
                marks[i] = scanner.nextDouble();
            System.out.println("1. Sum of all marks = " + sum(marks));
            System.out.println("2. Sorted marks:");
            double[] sortedMarks = sort(marks);
            for (double mark : sortedMarks)
                System.out.println("\t" + mark);
            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