According to the grading policy, final grade is determined by the average of four test scores. Create a program to read student’s first name, last name, and four test scores. The program outputs the student’s last name, four test scores, and the average of test scores of four different students. Find the best student also and display the details of best student



package lab0;

import java.util.Scanner;

public class Ques_2 {
public static void main(String[] args) {
String f_name[] = new String[4];
String l_name[] = new String[4];
float t1_score[] = new float[4];
float t2_score[] = new float[4];
float t3_score[] = new float[4];
float t4_score[] = new float[4];
float avg[] = new float[4];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 4; i++) {
System.out.print("Enter the first name of Student " + (i + 1) + " :");
f_name[i] = sc.nextLine();
System.out.print("Enter the last name of Student " + (i + 1) + " :");
l_name[i] = sc.nextLine();
System.out.print("Enter the Test 1 score of Student " + (i + 1) + " :");
t1_score[i] = Float.parseFloat(sc.nextLine());
System.out.print("Enter the Test 2 score of Student " + (i + 1) + " :");
t2_score[i] = Float.parseFloat(sc.nextLine());
System.out.print("Enter the Test 3 score of Student " + (i + 1) + " :");
t3_score[i] = Float.parseFloat(sc.nextLine());

System.out.print("Enter the Test 4 score of Student " + (i + 1) + " :");
t4_score[i] = Float.parseFloat(sc.nextLine());
}
sc.close();
for (int i = 0; i < 4; i++) {
avg[i] = (t1_score[i] + t2_score[i] + t3_score[i] + t4_score[i]) / 4;
System.out.println(
l_name[i] + " Test1 Score : " + t1_score[i] + " Test2 Score : " + t2_score[i] + " Test3 Score : "
+ t3_score[i] + " Test4 Score : " + t4_score[i] + " Average Score : " + avg[i]);
System.out.println();
}
if (avg[0] > avg[1] && avg[0] > avg[2] && avg[0] > avg[3]) {
System.out.println("\nStudent 1 is the best student\n");
System.out.println("Student name : " + f_name[0] + " " + l_name[0]);
System.out.println(f_name[0] + " " + l_name[0] + " score in Test1 : " + t1_score[0]);
System.out.println(f_name[0] + " " + l_name[0] + " score in Test2 : " + t2_score[0]);
System.out.println(f_name[0] + " " + l_name[0] + " score in Test3 : " + t3_score[0]);
System.out.println(f_name[0] + " " + l_name[0] + " score in Test4 : " + t4_score[0]);
System.out.println(f_name[0] + " " + l_name[0] + " average score : " + avg[0]);
} else if (avg[1] > avg[0] && avg[1] > avg[2] && avg[1] > avg[3]) {
System.out.println("\nStudent 2 is the best student\n");
System.out.println("Student name : " + f_name[1] + " " + l_name[1]);
System.out.println(f_name[1] + " " + l_name[1] + " score in Test1 : " + t1_score[1]);
System.out.println(f_name[1] + " " + l_name[1] + " score in Test2 : " + t2_score[1]);
System.out.println(f_name[1] + " " + l_name[1] + " score in Test3 : " + t3_score[1]);
System.out.println(f_name[1] + " " + l_name[1] + " score in Test4 : " + t4_score[1]);
System.out.println(f_name[1] + " " + l_name[1] + " average score : " + avg[1]);
} else if (avg[2] > avg[0] && avg[2] > avg[1] && avg[2] > avg[3]) {
System.out.println("\nStudent 3 is the best student\n");
System.out.println("Student name : " + f_name[2] + " " + l_name[2]);
System.out.println(f_name[2] + " " + l_name[2] + " score in Test1 : " + t1_score[2]);
System.out.println(f_name[2] + " " + l_name[2] + " score in Test2 : " + t2_score[2]);
System.out.println(f_name[2] + " " + l_name[2] + " score in Test3 : " + t3_score[2]);
System.out.println(f_name[2] + " " + l_name[2] + " score in Test4 : " + t4_score[2]);
System.out.println(f_name[2] + " " + l_name[2] + " average score : " + avg[2]);
} else if (avg[3] > avg[0] && avg[3] > avg[1] && avg[3] > avg[2]) {
System.out.println("\nStudent 4 is the best student\n");
System.out.println("Student name : " + f_name[3] + " " + l_name[3]);
System.out.println(f_name[3] + " " + l_name[3] + " score in Test1 : " + t1_score[3]);
System.out.println(f_name[3] + " " + l_name[3] + " score in Test2 : " + t2_score[3]);
System.out.println(f_name[3] + " " + l_name[3] + " score in Test3 : " + t3_score[3]);
System.out.println(f_name[3] + " " + l_name[3] + " score in Test4 : " + t4_score[3]);
System.out.println(f_name[3] + " " + l_name[3] + " average score : " + avg[3]);
} else {
System.out.println("More than 1 student have same score");
}
}
}

OUTPUT




Post a Comment

0 Comments