Ifless Grade Calculator

From CompSciWiki
Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Sunday Stumpers are the Sunday New York Times Crossword Puzzle of Program A Day. The problems are intentionally difficult, and the average student probably won't be able to get the whole solution by themselves. They will never contain anything that the students haven't learned, but they may apply things the students have learned in unorthodox ways. The solution will contain several hints and the actual code.

Next week you are going to be learning conditional statements (like if statements). This will allow you to make much more powerful programs. One problem that you are going to be asked to do next week, is to make a program that converts number grades into letter grades. Technically, it is possible to write this program without a single if statement.

Create a program that will allow the user to enter in a decimal letter grade, and will output the letter grade. The number grade should be entered using consecutive JOptionPane.showInputDialog, and the result should be given using System.out.println. Assume valid inputs. Don't use ifs, loops, arrays or anything else that have not yet been discussed.

The grades breakdown is as follows:

  • F: less than 50%
  • D: at least 50%, less than 60%
  • C: at least 60%, less than 70%
  • B: at least 70%, less than 80%
  • A: at least 80%

If you can get that working, then add plus grades:

  • F: less than 50%
  • D: at least 50%, less than 60%
  • C: at least 60%, less than 65%
  • C+: at least 65%, less than 70%
  • B: at least 70%, less than 75%
  • B+: at least 75%, less than 80%
  • A: at least 80%, less than 90%
  • A+: at least 90%

Don't worry about plus grades until you have normal letter grades working


You will need the following:

 

...By Students

"Mike Domaratzki, my 1010 professor, asked my class this problem on the second week of class. Of about 100 students, only 2 of us solved it. I remembered sitting through my class after 1010, furiously trying to solve the problem by hand. When I coded the program up, it took me an hour to debug. When I finally got it working, the sense of accomplishment was overwhelming."

"This is a difficult problem, one the would probably stump many fourth year students. Even though I know the trick for solving this problem, adding the plus grades was a bit difficult for me as well."

Solution

Hint 1: You can increment and decrement char values, just like you can integers.

  • 'B' - 1 = 'A'
  • You can have a char variable that represents the letter grade.
  • Start the letter grade variable at 'F' and decriment it if the grade is higher.


Hint 2: You will need to have a line of code that decrements the grade for each possible letter (except F, the default grade).

  • If the grade is a C, letter grade will be decriments twice:
    • Once by the D increment code.
    • Once by the C increment code.


Hint 3: If there is a decrement section for every letter grade, then the decrement value must be variable.

  • Specifically, every section should either decrement 0 or 1.


Hint 4: Min and Max functions can be used to shape the decrement value so that is 0 or 1.

  • Start with a temporary value that represents number_grade - (minimum_for_letter_grade -1)
    • If the threshold for the letter grade is met, temp will be at least 1.
    • If the threshold for the letter grade is not met, temp will be no more than 0.


Hint 5:

 //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 


Hint 6: You can deal with '+' by using a second character variable.

  • The variable should equal either ' ' or '+'.


Hint 7: You will need range checks for each set of possible + grades.

  • Increment a plus value if the grade is greater than the lower threshold.
  • Decrement a plus value if the grade is greater than the next threshold.

Code

Solution Code

Back to the Program-A-Day homepage