Write a java program to Calculate Grades.
A School wants to give assign grades to its students based on their marks. You have been assigned as the programmer to automate this process. You would like to showcase your skills by creating a quick prototype.
The prototype consists of the following steps:
- Read student details from the User. The details would include name, mark in the given order. The datatype for the name is a string, the mark is float.
- You decide to build a hashmap. The hashmap contains the name as key and mark as the value.
BUSINESS RULE:
1. If Mark is less than 60, then the grade is FAIL.
2. If Mark is greater than or equal to 60, then the grade is PASS. Note: FAIL/PASS should be in uppercase.
3. Store the result in a new Hashmap with name as Key and grade as value.
4. You decide to write a function calculateGrade which takes the above hashmap as input and returns the hashmap as output.
Input and Output Format:
- Input consists of student details. The first number indicates the size of the students. The next two values indicate the name, mark.
- Output consists of a name and corresponding grade for each student.
Refer sample output for formatting specifications:
Sample Input 1:
3
Avi
76
Sunil
68
Raja
36
Sample Output 1:
Avi
PASS
Sunil
PASS
Raja
FAIL
Java program to Calculate Grades
Following are the steps to calculate grades in Java:
- Input the size of the students from the user. Create an empty HashMap to store the name and marks of the students. Add name and marks as key and value to the Map. Pass the Map and size to display() method.
- Inside the method, construct a new empty treemap. Iterate over the map and get all the keys and store it in the id of type string. Now, pass the id to the get() method that would return the value to which the specified key is mapped.
- Next, check if the value is greater than or equal to 60, then add “PASS” to the newly created treemap otherwise if the value is less than 60 then put “FAIL” in the map and return them.
- At last, just iterate over the map and get the key and value pair i.e (name and corresponding marks) and print them.
package com.demo; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; import java.util.TreeMap; public class GradesCalculater { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s = sc.nextInt(); HashMap<String, Integer> hm = new HashMap<String, Integer>(); for (int i = 0; i < s; i++) { String name = sc.next(); int number = sc.nextInt(); hm.put(name, number); } TreeMap<String, String> tm = new TreeMap<String, String>(); tm = display(hm); Iterator<String> it = tm.keySet().iterator(); for (int i = 0; i < s; i++) { String n = it.next(); String fac = tm.get(n); System.out.println(n); System.out.println(fac); } } public static TreeMap<String, String> display(HashMap<String, Integer> hm) { TreeMap<String, String> tm = new TreeMap<String, String>(); Iterator<String> it = hm.keySet().iterator(); while (it.hasNext()) { String id = it.next(); int mark = hm.get(id); if (mark >= 60) tm.put(id, "PASS"); else if(mark < 60) tm.put(id, "FAIL"); else tm.put(id, "FAIL"); } return tm; } }
Output
Thus, in this way, we learn how to calculate grades in Java using HashMap and TreeMap.