Sort Pass Students by Name

                                  Sort Pass Students by Name

 

A list of N students name and also their marks in three subjects are passed as the input. The average of the 3 subjects must be greater than or equal to 40 and also the total marks must be greater than or equal to 150 for the student to pass. Print the names of the students who have passed, with the names sorted in ascending order.
Input Format:
The first line contains N, the number of students.
The following N lines contains Name and also marks of each student in three subjects separated by space.
Output Format:
Print the names of the students who passed (with their names sorted in ascending order).

Example Input/Output 1:

Input:
3
Ram 45 65 45
Geetha 60 30 50
Soundarya 80 90 80
Output:
Ram
Soundarya


Code:

import java.util.*;
public class Hello {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int m1,m2,m3,n,i,tot,avg;
String nm;
n=sc.nextInt();
List<String> li=new ArrayList<String>();
for(i=0;i<n;i++)
{
nm=sc.next();
m1=sc.nextInt();
m2=sc.nextInt();
m3=sc.nextInt();
tot=m1+m2+m3;
avg=tot/3;
if(tot>=150&&avg>=40)
li.add(nm);
}
Collections.sort(li);
for(String s:li)
System.out.println(s);

}
}

Explanation:

1 : Create a class.

2 : Create an object for Scanner class to get the input from user.

3 : Get n value - number of students.

4 : Create an object ArrayList Collection with String content within it.

5 : Generate a loop running n times.For each iteration do the following.

6 : Get the details for each students such as student name and their marks in three subjects.

7 : Calculate the Total marks and also Average marks for each student.

8 : Check for the pass constraint.

The average of three subjects must be greater than or equal to 40 and also the total marks must be greater than or equal to 150.

9 : If the pass constraints get satisfied then add the students name within the list.

10 : Sort the ArrayList using the Sort function because students name should be displayed in ascending order.

11 : Finally!! Parse through the ArrayList and display the sorted List of students name.

 

 

Please do comment If u have any Queries!

Add a Comment

Your email address will not be published. Required fields are marked *