Difference between revisions of "Ifless Grade Calculator"
(optimized code) |
(Changed to utilize CodeBlock template) |
||
Line 66: | Line 66: | ||
'''Hint 5:''' | '''Hint 5:''' | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
+ | //C | ||
temp = grade - 59; //60% = C | temp = grade - 59; //60% = C | ||
Line 72: | Line 74: | ||
temp = Math.max(0,temp); //make all values less than 0 equal to 0 | temp = Math.max(0,temp); //make all values less than 0 equal to 0 | ||
− | letter -= temp; //Decrement grade | + | letter -= temp; //Decrement grade |
+ | }} | ||
Revision as of 16:16, 4 December 2011
Back to the Program-A-Day homepage
ProblemSunday 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:
If you can get that working, then add plus grades:
Don't worry about plus grades until you have normal letter grades working
You will need the following:
|
...by studentsMike 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. | |
---|---|---|
SolutionHint 1: You can increment and decrement char values, just like you can integers
//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
| ||
Code |