Write a java program to Calculate Grades

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 name is string, mark is float.
You decide to build a hashmap. The hashmap contains name as key and mark as value.

BUSINESS RULE:
1. If Mark is less than 60, then grade is FAIL.
2. If Mark is greater than or equal to 60, then grade is PASS.
Note: FAIL/PASS should be in uppercase.
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

import java.util.HashMap;
package com.demo;
 
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
 
import java.util.Scanner;
 
public class Main {
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 >= 80)
tm.put(id, "GOLD");
else if (mark < 80 && mark >= 60)
tm.put(id, "SILVER");
else if (mark < 60 && mark >= 45)
tm.put(id, "BRONZE");
else
tm.put(id, "FAIL");
}
return tm;
}
}

Add a Comment

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

19 + 20 =