/* Class Grading
* Created by Brendan Curran-Johnson
* Made in COMP3040
* Calculates a persons Grade
*/
import javax.swing.*; //needed for JOptionPane
import java.lang.Math;//needed for Min Max
public class Grade
{
public static void main (String args[])
{
// These variables are used for input
String input;
int grade;
// Get Number grade
input = JOptionPane.showInputDialog(null, "Please enter the grade");
// Convert string to int
//number grades can have decimal places, so read in as a double first to avoid errors
//Hopefully your teacher is nice like this program and rounds grades instead of truncating them
grade = (int)Math.round(Double.parseDouble(input));
//This is the heart of the program. The great thing about chars is that you can
//add and subtract int values from them. This means you can decrement the letter
//grade for every threshhold value, and the grade will go from F -> A.
char letter = 'F';
//D
//This code, convoluted as it may be, is the trick for actually working out whether
//the grade should be decremented. You can't conditionally subtract something from
//the grade (without using conditional statements, which you haven't learned yet),
//so you have to manipulate values so that you are subtracting 0 if the grade is
//less than a D, and 1 if the grade is atleast a D (technically, for a D, you will
//use the values 0 or 2, as there is no E grade, but for the other cases, you will
//use the values 0 or 1)
int temp = grade - 49;//50% is a D. right now, temp will be <= 0 if the grade is less
//than a D, and >= 1 if the grade is atleast a D
//make all values greater than 1 equal to 1
temp = Math.min(1,temp); //temp is 1 if grade is a D or better, otherwise, it is 0-
//make all values less than 0 equal to 0
temp = Math.max(0,temp); //temp is 1 if grade is a D or better, otherwise, it is 0
//subtract temp from letter to turn the grade into an F or D
letter -= temp*2; //The times 2 is because there isn't an E in the letter grade system
//C
temp = grade - 59; //60% = C
temp = Math.min(1,temp); //make all values greater than 1 equal to 1
temp = Math.max(0,temp); //make all values less than 0 equal to 0
letter -= temp; //Decrement grade
//Getting plusses to show up is slightly more tricky than letter grades, but the
//basic concept is the same. We are going to start with the plus value showing up
//as a space, and if the person's grade warrants a plus, we will increment the character
//to a +. We are going to use 3 plus values (C+, B+, A+), and if the grade is within
//the correct range for that value, it will equal 1, otherwise it will equal zero.
//Later, we will add the 3 values together. This will result in 1 if there needs to be
//a plus, and 0 otherwise.
char plus = ' ';
int isPlus = 0;
//C+
//We have to check two things, is the grade above a C, and is the grade below a B. the
//'easy' way to do this is to have a counter value that we increment if the grade is above
//a C+, and decrement if the grade is below a B. This way, the value will only = 1 if the
//grade is in range
temp = grade - 64; //65% is a C+. righ now, temp will be <= 0 if the grade is less
//than a C+, and >= 1 if the grade is atleast a C+
//make all values greater than 1 equal to 1
temp = Math.min(1,temp); //temp is 1 if grade is a D+ or better, otherwise, it is 0-
//make all values less than 0 equal to 0
temp = Math.max(0,temp); //temp is 1 if grade is a D+ or better, otherwise, it is 0
//increment C+
isPlus += temp;
//B
temp = grade - 69; //70% = B
temp = Math.min(1,temp); //make all values greater than 1 equal to 1
temp = Math.max(0,temp); //make all values less than 0 equal to 0
letter -= temp; //Decrement grade
isPlus -= temp; //Decrement C+
//B+
temp = grade - 74; //75% = B+
temp = Math.min(1,temp); //make all values greater than 1 equal to 1
temp = Math.max(0,temp); //make all values less than 0 equal to 0
isPlus += temp; //increment B+
//A
temp = grade - 79; //80% = A
temp = Math.min(1,temp); //make all values greater than 1 equal to 1
temp = Math.max(0,temp); //make all values less than 0 equal to 0
letter -= temp; //Decrement grade
isPlus -= temp; //Decrement B+
//A+
int ap = 0;
temp = grade - 89; //90% = A+
temp = Math.min(1,temp); //make all values greater than 1 equal to 1
temp = Math.max(0,temp); //make all values less than 0 equal to 0
isPlus += temp; //increment A+. A+ will never be decremented, it is the highest grade
//More fun with adding and subtracting chars. In this case we are working
//out the gap between + and the space character. We multiply by 1 or 0, and
//then add it to plus, so that it is only incremented if a plus is warrented
plus += ('+' - ' ')*isPlus;
//output solution
System.out.println(grade + "% is equal to: " + letter + plus);
//there will be a space after the letter grade if it is not a +, you can fix
//this by replacing ' ' with the number 0 (the ascii value for the null char)
//when plus is created and incremented. This sample code doesn't do this
//merely because you might not have learned about null characters yet, and
//this code is confusing enough without values you've never seen before
}
}