import java.util.Scanner;
public class RAnsariStudentPercentage
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
//Initialize the 2 SDA
String names[] = new String[35];
double percentage[] = new double[35];
/*
* Accept student details from user and store
* in corresponding SDA
*/
for (int i = 0; i < names.length; i++) {
System.out.print("Enter name of student "
+ (i + 1) + ": ");
names[i] = in.nextLine();
System.out.print("Enter percentage of student "
+ (i + 1) + ": ");
percentage[i] = in.nextDouble();
in.nextLine();
}
//Selection Sort in Descending Order
for (int i = 0; i < percentage.length - 1; i++) {
int maxIdx = i;
for (int j = i + 1; j < percentage.length; j++) {
if (percentage[j] > percentage[maxIdx])
maxIdx = j;
}
double t = percentage[i];
percentage[i] = percentage[maxIdx];
percentage[maxIdx] = t;
String name = names[i];
names[i] = names[maxIdx];
names[maxIdx] = name;
}
//Display first ten toppers
System.out.println("Name\tPercentage");
for (int i = 0; i < 10; i++) {
System.out.println(names[i] + '\t' + percentage[i]);
}
}
}
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.