import java.util.Scanner;
class Student {
String name;
Mark mark;
Student(String name, int mk1, int mk2, int mk3) {
this.name = name;
this.mark = new Mark(mk1, mk2, mk3);
}
}
class Mark {
int mk1;
int mk2;
int mk3;
int tot;
int avg;
String grade(){
if (avg >= 90 && avg <= 100) {
return "A";
} else if (avg >= 80 && avg <= 89) {
return "B";
} else if (avg >= 70 && avg <= 79) {
return "C";
} else if (avg >= 60 && avg <= 69) {
return "D";
} else {
return "F";
}
};
Mark(int mk1, int mk2, int mk3){
this.mk1 = mk1;
this.mk2 = mk2;
this.mk3 = mk3;
this.tot = mk1 + mk2 + mk3;
this.avg = tot /3;
this.grade();
}
}
class studentManage {
public static void printStudentInfo(Student[] studentList){
System.out.println("Name\tMark1\tMark2\tMark3\tAvg\tGrade");
for (int i = 0; i < studentList.length; i++) {
System.out.print(studentList[i].name + "\t");
System.out.print(studentList[i].mark.mk1 + "\t");
System.out.print(studentList[i].mark.mk2 + "\t");
System.out.print(studentList[i].mark.mk3 + "\t");
System.out.print(studentList[i].mark.avg + "\t");
System.out.println(studentList[i].mark.grade() + "\t");
}
}
public static void reversePrintStudentInfo(Student[] studentList){
System.out.println("Name\tMark1\tMark2\tMark3\tAvg\tGrade");
for (int i = studentList.length-1; i >= 0; i--) {
System.out.print(studentList[i].name + "\t");
System.out.print(studentList[i].mark.mk1 + "\t");
System.out.print(studentList[i].mark.mk2 + "\t");
System.out.print(studentList[i].mark.mk3 + "\t");
System.out.print(studentList[i].mark.avg + "\t");
System.out.println(studentList[i].mark.grade() + "\t");
}
}
public static void showMenu() {
System.out.println("===========================================================");
System.out.println("1: Print the entire list");
System.out.println("2: Sort and print the list alphabetically");
System.out.println("3: Sort and print the list in descending order based on the average");
System.out.println("4: Search for the student who has that average");
System.out.println("5: Find the student who has the minimum average");
System.out.println("6: Print the grade distribution");
System.out.println("0: Exit");
System.out.println("==========================================================");
System.out.print("Enter your choice? ");
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of students ? ");
int numOfStudents = in.nextInt();
Student[] studentList = new Student[numOfStudents];
String name;
int mk1, mk2, mk3;
for(int i = 0; i < studentList.length; i++){
System.out.print("Enter name and 3 marks for student " + (i+1) +"? ");
name = in.next();
mk1 = in.nextInt();
mk2 = in.nextInt();
mk3 = in.nextInt();
studentList[i] = new Student(name, mk1, mk2, mk3);
}
int sel;
while(true){
showMenu();
sel = in.nextInt();
switch(sel){
case 1:
printStudentInfo(studentList);
break;
case 2:
for (int i = 0; i < studentList.length; i++) {
Student Min= studentList[i];
int Index = i;
for (int j = i + 1; j < studentList.length; j++) {
if (Min.name.compareTo(studentList[j].name) > 0) {
Min = studentList[j];
Index = j;
}
}
if (Index != i) {
studentList[Index] = studentList[i];
studentList[i] = Min;
}
}
printStudentInfo(studentList);
break;
case 3:
for (int i = 1; i < studentList.length; i++) {
Student currentElement = studentList[i];
int k;
for (k = i - 1; k >= 0 && studentList[k].mark.avg > currentElement.mark.avg; k--) {
studentList[k + 1] = studentList[k];
}
studentList[k + 1] = currentElement;
}
reversePrintStudentInfo(studentList);
break;
case 4:
System.out.print("Enter the average to search:");
int searchAvg = in.nextInt();
int low = 0;
int high = studentList.length - 1;
int mid = (low + high) / 2;
while (studentList[high].mark.avg >= studentList[low].mark.avg) {
if (searchAvg == studentList[mid].mark.avg){
System.out.println("The student who has that average: "+ studentList[mid].name);
break;}
else if (searchAvg < studentList[mid].mark.avg)
studentList[high] = studentList[mid - 1];
else {
studentList[low] = studentList[mid + 1];}
}
System.out.println("The student who has that average: "+ studentList[mid].name);
break;
case 5:
Student min = studentList[0];
for (int j = 0; j < studentList.length; j++) {
if (studentList[j].mark.avg < min.mark.avg) {
min = studentList[j];
}
}
System.out.println("Student with min average is: " + min.name + " Average= " + min.mark.avg);
break;
case 6:
int cntA = 0;
int cntB = 0;
int cntC = 0;
int cntD = 0;
int cntF = 0;
for(int i = 0; i < studentList.length; i++){
String grade = (studentList[i].mark.grade());
if(grade == "A") cntA += 1;
else if(grade == "B") cntB += 1;
else if(grade == "C") cntC += 1;
else if(grade == "D") cntD += 1;
else { cntF += 1;}
}
System.out.print("Grade distribution: ");
System.out.print(" A=" + cntA);
System.out.print(" B=" + cntB);
System.out.print(" C=" + cntC);
System.out.print(" D=" + cntD);
System.out.println(" F=" + cntF);
break;
case 0:
System.out.println("This is the end of the program...Thank you");
return;
}
}
}
}