ThinkVireo
  • Home (current)
  • About
  • TCS IPA/DCA(new)
    TCS DCA TCS IPA Python TCS IPA JAVA
  • FAQs
    TCS 2025
  • Contact
  • India
  • Leetcode

OOPs:Scholar Grade

Create a class Scholar with attributes:

  • ScholarId - Number (representing a unique id for each student)
  • ScholarName - String (represents the name of the student)
  • State - String (representing the state of the student)
  • Marks - List (storing the marks of 3 subjects, obtained marks from each subject is out of​ 100)

Define the constructor which takes parameters in the above sequence and sets the​ values for attributes.​ ​ Create another class ScholarResult with following attribute and methods​ ScholarGrade - list of dictionaries (It will contain the record for each student which will​ have ScholarId, ScholarName, TotalMarks, Grade and State)​ Define the constructor which sets the values for above mentioned attribute.​ Define the methods in the ScholarResult class as below​
1. First method will take the list of Scholar objects and Grade as an input parameters.​ Firstly, you need to calculate the percentage for each student (based on the marks​ obtained in 3 subjects).​ To calculate the percentage use the following formula:​
Percentage = (Subject1Marks + Subject2Marks + Subject3Marks) / 3)​ Note: Percentage must be an integer value and rounds the value. That means, if​ Percentage >=40.0% or <40.5% then it will be considered as a 40% and,​ if percentage >=40.5% or <=41.0% then it will be considered as a 41%.​ After calculating the percentage, you need to assign the grades as per the following​ given conditions:​ ​

If student’s percentage >=80, then assign Grade “A”​ If student’s percentage >= 60 and < 80 then assign Grade “B”​ If student’s percentage >= 50 and < 60 then assign Grade “C”​ If student’s percentage <50 then assign Grade “D”​ ​ After calculating the grades, make a dictionary which contains ScholarId, ScholarName,​ TotalMarks, Grade and State and append it to the ScholarGrade (list of dictionaries)​ which is an attribute of ScholarResult class.​ Secondly, your method will return the list of dictionaries according to the Grade passed​ as parameter. This list of dictionaries will be sorted based on their TotalMarks in​ descending order.​ For Example:​ [{'ScholarId': 106, 'ScholarName': 'palak', 'TotalMarks': 282, 'Grade': 'A', 'State': 'gujarat'},​ {'ScholarId': 104, 'ScholarName': 'ramesh', 'TotalMarks': 275, 'Grade': 'A', 'State':​ 'rajasthan'},​ 'ScholarId': 102, 'ScholarName': 'ram', ' TotalMarks': 270, 'Grade': 'A', 'State': 'delhi'}]​ Assumption: No scholars would have same TotalMarks​ Method should return None, in the following cases:​ if search is unsuccessful​ if no records are found, or​ Grade is any other string apart from “A”, “B”, ”C” or “D”

2. Second method will calculate the pass:fail ratio for each state and will return the list​ of states with pass:fail ratio. This list will be sorted in ascending order based on state's​ name.​ For example: We have 2 states i.e. delhi and rajasthan. Output would be delhi first and​ then rajasthan.​ A student who has acquired either Grade “A”, “B” or “C” will be considered as a Pass​ and a student with Grade “D” will be considered as a Fail.​ ​ Formula to calculate the Passing and Failing Percentage:​ PassingPercentage = (Number of Scholars Passed / Total Scholars) * 100)​ FailingPercentage = (Number of Scholars Failed / Total Scholars) * 100)​ Note: Percentage must be an integer value and rounds the value. That means, if​ Percentage >=40.0% or <40.5% then it will be considered as a 40% and,​ if percentage >=40.5% or <=41.0% then it will be considered as a 41%.​ ​ For Example-​ Your returning list must follow the format as per the below sample example:​ ​ [['delhi', '60:40'], ['gujarat', '100:0'],['rajasthan', '70:30']]​ If no records are found then method should return None.

Instructions to write main section of the code:​ 1. You would require to write the main section completely, hence please follow the​ below instructions for the same.​ 2. You would be required to write the main program which is inline to the "sample input​ description section" mentioned below and to read the data in the same sequence.​ 3. Create the respective objects(Scholar and ScholarResult) with the given sequence​ of arguments to fulfill the constructor requirement defined in the respective classes​ referring to the below instructions.​ i. Create a list of Scholar Objects.​ To create the list of objects:​ ​ a. Read the count of Scholar objects you want to create.​ b. Create a Scholar object after reading the data related to it and add the​ object to the list of Scholar objects which will be provided to the​ ScholarResult object. This point repeats for the number of Scholar​ objects (considered in the first line of input, point #3.i.a) .​ ​ 4. Read the Grade as an input from the user.​ 5. Call the first method which has a list of Scholar’s objects and Grade (either “A”, “B”,​ “C” or “D”) as the arguments and return the list of dictionaries as required.​ Format of the output would be:

Format of the output would be:​ “ScholarId ScholarName TotalMarks Grade State” (excluding the quotes).​ If the first method is returning None, then display “No Record Found” (excluding the​ quotes).​ For more clarity please refer the sample testcase.​ 6. Call the second method which will return a list of states with pass:fail ratio.​ Format of the output would be:​ “State PassingPercentage:FailingPercentage” (excluding the quotes).​ If the second method is returning None, then display “No Record Found” (excluding the​ quotes).​ For more clarity please refer the sample testcase.​ Note: All the inputs and searches will be case insensitive. Display State and​ ScholarName in the lowercase.

Not to be circulated​ You can use/refer the below given sample input and output to verify your solution.​ Input Format:​ 1. First input is the integer that reads values for n Scholar objects.​ 2. The next set of inputs are ScholarId, ScholarName, State, Subject1Marks,​ Subject2Marks and Subject3Marks.​ 3. For each Scholar object repeat point#2 and this point is repeated for n number of​ Scholar objects given in the first line of input.​ 4. Last line of input represents a string which is a Grade. It can be either “A”, “B”, “C” or​ “D”.

You can consider below sample input and output to verify your implementation before​ submitting.​
Sample Input1:​
5​
101​
Tanmay​
delhi​
90​
88​
93​
102​
Sunil​
delhi​
90​
95​
90​
103​
Karvi​
maharashtra​
70​
45​
50
104​
monika​
tamilnadu​
20​
35​
40​
105​
Ram​
tamilnadu​
90​
65​
50​
a

​
Sample Output1:​
102 sunil 275 A delhi​
101 tanmay 271 A delhi​
delhi 100:0​
maharashtra 100:0​
tamilnadu 50:50

  1. JAVA Code
    
        import java.util.ArrayList;
        import java.util.Arrays;
        import java.util.Collections;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;
        import java.util.Scanner;
        
        class Scholar {
            int scholar_id;
            String scholar_name;
            String state;
            int[] marks;
            
            public Scholar(int scholar_id, String scholar_name, String state, int[] marks) {
                this.scholar_id = scholar_id;
                this.scholar_name = scholar_name;
                this.state = state;
                this.marks = marks;
            }
        }
        
        class ScholarGrade {
            List> scholar_grade;
            
            public ScholarGrade() {
                this.scholar_grade = new ArrayList<>();
            }
            
            public List> calculate_grade_return(List scholar_list, String grade) {
                List grade_list = Arrays.asList("A", "B", "C", "D");
                List key_list = Arrays.asList("scholar_id", "scholar_name", "total_marks", "grade", "state");
                if (scholar_list.size() > 0) {
                    for (Scholar scholar : scholar_list) {
                        String assign_grade = "";
                        int total_marks = Arrays.stream(scholar.marks).sum();
                        int percentage = Math.round(total_marks / 3.0f);
                        if (percentage >= 80) {
                            assign_grade = grade_list.get(0);
                        } else if (60 <= percentage && percentage < 80) {
                            assign_grade = grade_list.get(1);
                        } else if (50 <= percentage && percentage < 60) {
                            assign_grade = grade_list.get(2);
                        } else {
                            assign_grade = grade_list.get(3);
                        }
                        
                        Map student_record = new HashMap<>();
                        student_record.put(key_list.get(0), scholar.scholar_id);
                        student_record.put(key_list.get(1), scholar.scholar_name);
                        student_record.put(key_list.get(2), total_marks);
                        student_record.put(key_list.get(3), assign_grade);
                        student_record.put(key_list.get(4), scholar.state);
                        
                        this.scholar_grade.add(student_record);
                    }
                    
                    List> find_scholar = new ArrayList<>();
                    for (Map rec : this.scholar_grade) {
                        if (rec.get("grade").equals(grade)) {
                            find_scholar.add(rec);
                        }
                    }
                    Collections.sort(find_scholar, (Map a, Map b) -> ((Integer)b.get("total_marks")).compareTo((Integer)a.get("total_marks")));
                    return find_scholar;
                } else {
                    return null;
                }
            }
            
            public List> display_statewise_result() {
                if (this.scholar_grade.size() == 0) {
                    return null;
                } else {
                    List> state_pass_fail_ratio = new ArrayList<>();
                    List states = new ArrayList<>();
                    for (Map each_record : this.scholar_grade) {
                        String state = (String)each_record.get("state");
                        if (!states.contains(state)) {
                            states.add(state);
                        }
                    }
                    Collections.sort(states);
                    for (String item : states) {
                        int count_scholars = 0;
                        int count_fail = 0;
                        for (Map each_record : this.scholar_grade) {
                            if (each_record.get("state").equals(item)) {
                                countFail += 1;
    }
    }
    double failPercentage = Math.round(((double) countFail / countScholars) * 100);
    int countPass = countScholars - countFail;
    double passPercentage = Math.round(((double) countPass / countScholars) * 100);
    String oneStateRecord = state + " " + passPercentage + ":" + failPercentage;
    statePassFailRatio.add(oneStateRecord);
    }
    return statePassFailRatio;
    } else {
    return null;
    }
    }
    }
    
    public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int scholarCount = Integer.parseInt(sc.nextLine());
    List scholarList = new ArrayList<>();
    for (int i = 0; i < scholarCount; i++) {
    int scholarId = Integer.parseInt(sc.nextLine());
    String scholarName = sc.nextLine().toLowerCase();
    String state = sc.nextLine().toLowerCase();
    int sub1Marks = Integer.parseInt(sc.nextLine());
    int sub2Marks = Integer.parseInt(sc.nextLine());
    int sub3Marks = Integer.parseInt(sc.nextLine());
    int[] marks = {sub1Marks, sub2Marks, sub3Marks};
    Scholar scholarObj = new Scholar(scholarId, scholarName, state, marks);
    scholarList.add(scholarObj);
    }
    String grade = sc.nextLine().toUpperCase();
    ScholarGrade scholarGradeObj = new ScholarGrade();
    List> studentListWithGrade = scholarGradeObj.calculateGradeReturn(scholarList, grade);
    if (studentListWithGrade == null || studentListWithGrade.isEmpty()) {
    System.out.println("No Record Found");
    } else {
    for (Map record : studentListWithGrade) {
    for (Object v : record.values()) {
    System.out.print(v + " ");
    }
    System.out.println();
    }
    }
    List passFailRatioStatewise = scholarGradeObj.displayStatewiseResult();
    if (passFailRatioStatewise == null) {
    System.out.println("No Record Found");
    } else {
    for (String record : passFailRatioStatewise) {
    System.out.println(record);
    }
    }
    }
    }
        
    
                    
                
Shubham Shrivas


ThinkVireo

ThinkVireo

TCS IPA Leetcode Python Java Partners Careers
FAQ Contact About Support Blog Services
Privacy & Policy

©ThinkVireo 2025 . All rights reserved.