I decided to tackle a HackerRank challenge. I selected the Grading Students challenge. If interested please follow the link and take a look at the requirements.
Following is the console capture of the Eclipse IDE using the sample input:
4 73 67 38 33 75 67 40 33
I decided to use a function to compute the rounded grade given that there are two conditions that would require it. My code in Java 8 follows:
import java.util.Scanner; public class Solution { /** * Round up grade. */ static int round(int grade) { // System.out.println("round <<< grade: " + grade); // **** next multiple of 5 **** int next = ((grade / 5) + 1) * 5; // System.out.println("round <<< next: " + next); // **** **** int diff = next - grade; // System.out.println("round <<< diff: " + diff); // **** round up (if needed) **** if (diff < 3) { grade = next; } // **** **** return grade; } /** * Test code. */ public static void main(String[] args) { // **** open scanner **** Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int a0 = 0; a0 < n; a0++) { int grade = in.nextInt(); // System.out.println("main <<< grade: " + grade); // **** failing grade **** if (grade < 40) { // **** rounding required **** if (grade >= 38) { grade = round(grade); } } // **** passing grade **** else { grade = round(grade); } // **** print the grade **** System.out.println(grade); } // **** close scanner **** in.close(); } }
The challenge was ranked easy. I always like to start simple and walk my way up.
I commented out some lines of code that I used to test the program as it was being developed. I could have removed them but decided to leave them in to show how I like to develop software.
If you have comments or questions please let me know. I will not use your name unless you explicitly allow me to do so.
John
john.canessa@gmail.com
Follow me on Twitter: @john_canessa