Answer to Question #288497 in Java | JSP | JSF for Zeref

Question #288497

Write a program to create two single dimension arrays to store names and marks of 25 students. Then display the name of the students who secured more than 90 as per their marks in descending order and also display how many such students found.

Hint: Use Linear search and Bubble sorting.


1
Expert's answer
2022-01-19T01:55:07-0500
import java.util.Arrays;
import java.util.Random;

public class Main {

    public static void main(String[] args) {
        String[] names = generateNames();
        int[] marks = generateMarks();
        bubbleSort(marks, names);

        System.out.println("Names: " + Arrays.toString(names));
        System.out.println("Marks: " + Arrays.toString(marks));

        String[] cleverStudents = getCleverStudents(names, marks);
        System.out.println("Clever students: " + Arrays.toString(cleverStudents));
    }

    private static String[] generateNames() {
        String[] array = new String[25];

        for (int i = 0; i < array.length; i++) {
            array[i] = "Name" + i;
        }

        return array;
    }

    private static int[] generateMarks() {
        Random random = new Random();
        int[] array = new int[25];

        for (int i = 0; i < 25; i++) {
            array[i] = random.nextInt(30) + 71;
        }

        return array;
    }

    private static void bubbleSort(int[] marks, String[] names) {
        int temp;
        String temp2;

        for (int i = 0; i < marks.length; i++) {
            for (int j = 1; j < (marks.length - i); j++) {
                if (marks[j - 1] < marks[j]) {
                    //swap elements
                    temp = marks[j - 1];
                    marks[j - 1] = marks[j];
                    marks[j] = temp;

                    temp2 = names[j - 1];
                    names[j - 1] = names[j];
                    names[j] = temp2;
                }
            }
        }
    }

    private static String[] getCleverStudents(String[] names, int[] marks) {
        int size = 0;
        for (int i = 0; i < marks.length; i++) {
            if (marks[i] > 90) {
                size++;
            }
        }

        String[] cleverStudents = new String[size];
        for (int i = 0, counter = 0; i < names.length; i++) {
            if (marks[i] > 90) {
                cleverStudents[counter] = names[i];
                counter++;
            }
        }
        return cleverStudents;
    }
}

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
New on Blog