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.
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;
}
}
Comments
Leave a comment