Difference between revisions of "Ifless Grade Calculator"

From CompSciWiki
Jump to: navigation, search
(Added the sunday stumper, an incredibly hard problem. This one is a number grade to letter grade converter that doesn't use if statements)
 
(Capitalized bullets. Some were, some weren't - had to make them consistent.)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{1010PrAD|Ifless Grade Calculator=The name of the program
 
{{1010PrAD|Ifless Grade Calculator=The name of the program
  
|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.
+
|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.''
  
THIS IS A STUB, DON'T EDIT ME. I'm going to ask them to make a grade calculator without using ifs, arrays, etc.
+
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.
  
IT WILL TELL THEM WHAT THEY SHOULD BE USING, ETC.
+
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.
  
|SideSectionTitle=...by students
+
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%
  
|SideSection=I WILL TALK ABOUT MIKE D GIVING MY CLASS THIS PROBLEM
+
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'''
  
|Solution=THERE WILL BE HINTS HERE
 
  
|SolutionCode=<pre>
+
 
 +
You will need the following:
 +
*Min, Max, Round using [[Math Methods]]
 +
*[[Increment and Decrement Operators]]
 +
*[[Common_Primitive_Variables#Primitive_Type_char|Chars]]
 +
*Ingenuity
 +
 
 +
|SideSectionTitle=...By Students
 +
 
 +
|SideSection="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:'''
 +
{{CodeBlock
 +
|Code=
 +
//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.
 +
 
 +
|SolutionCode=
 
/* Class Grading
 
/* Class Grading
 
  * Created by Brendan Curran-Johnson
 
  * Created by Brendan Curran-Johnson
Line 25: Line 100:
 
public class Grade  
 
public class Grade  
 
{
 
{
 
+
 
public static void main (String args[])  
 
public static void main (String args[])  
 
{
 
{
Line 34: Line 109:
 
// Get Number grade
 
// Get Number grade
 
input = JOptionPane.showInputDialog(null, "Please enter the grade");
 
input = JOptionPane.showInputDialog(null, "Please enter the grade");
 
+
 
// Convert string to int
 
// Convert string to int
 
//number grades can have decimal places, so read in as a double first to avoid errors
 
//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
 
//Hopefully your teacher is nice like this program and rounds grades instead of truncating them
 
grade = (int)Math.round(Double.parseDouble(input));
 
grade = (int)Math.round(Double.parseDouble(input));
 
+
 
//This is the heart of the program. The great thing about chars is that you can
 
//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
 
//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.
 
//grade for every threshhold value, and the grade will go from F -> A.
 
char letter = 'F';
 
char letter = 'F';
 
+
 
//D
 
//D
 
//This code, convoluted as it may be, is the trick for actually working out whether
 
//This code, convoluted as it may be, is the trick for actually working out whether
Line 65: Line 140:
 
//subtract temp from letter to turn the grade into an F or D
 
//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
 
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
 
//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
 
//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
 
//as a space, and if the person's grade warrants a plus, we will increment the character
//to a +. We are going to use 4 plus values (D+, C+, B+, A+), and if the grade is within
+
//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.
 
//the correct range for that value, it will equal 1, otherwise it will equal zero.
//Later, we will add the 4 values together. This will result in 1 if there needs to be
+
//Later, we will add the 3 values together. This will result in 1 if there needs to be
 
//a plus, and 0 otherwise.
 
//a plus, and 0 otherwise.
 
char plus = ' ';
 
char plus = ' ';
 
+
int  isPlus = 0;
//D+
+
//We have to check two things, is the grade above a D, and is the grade below a C. the
+
//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
 
//'easy' way to do this is to have a counter value that we increment if the grade is above
//a D, and decrement if the grade is below a C. This way, the value will only = 1 if the
+
//a C+, and decrement if the grade is below a B. This way, the value will only = 1 if the
 
//grade is in range
 
//grade is in range
int dp = 0;
+
temp = grade - 64; //65% is a C+. righ now, temp will be <= 0 if the grade is less
temp = grade - 54; //55% is a D+. righ now, temp will be <= 0 if the grade is lesss
+
//than a C+, and >= 1 if the grade is atleast a C+
//than a D+, and >= 1 if the grade is atleast a D+
+
 
+
 
//make all values greater than 1 equal to 1
 
//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-
 
temp = Math.min(1,temp); //temp is 1 if grade is a D+ or better, otherwise, it is 0-
Line 90: Line 173:
 
temp = Math.max(0,temp); //temp is 1 if grade is a D+ or better, otherwise, it is 0
 
temp = Math.max(0,temp); //temp is 1 if grade is a D+ or better, otherwise, it is 0
 
 
//increment D+
+
//increment C+
dp += temp;
+
isPlus += temp;  
 
+
//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
+
dp -= temp; //Decrement D+
+
+
//c+
+
int cp = 0;
+
temp = grade - 64; //65% = 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
+
cp += temp; //increment C+
+
 
 
 
//B
 
//B
Line 117: Line 183:
 
 
 
letter -= temp; //Decrement grade
 
letter -= temp; //Decrement grade
cp -= temp; //Decrement C+
+
isPlus -= temp; //Decrement C+
 
 
 
//B+
 
//B+
int bp = 0;
 
 
temp = grade - 74; //75% = B+
 
temp = grade - 74; //75% = B+
 
 
 
temp = Math.min(1,temp); //make all values greater than 1 equal to 1
 
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
 
temp = Math.max(0,temp); //make all values less than 0  equal to 0
bp += temp; //increment B+
+
isPlus += temp; //increment B+
 
+
 
//A
 
//A
 
temp = grade - 79; //80% = A
 
temp = grade - 79; //80% = A
Line 134: Line 199:
 
 
 
letter -= temp; //Decrement grade
 
letter -= temp; //Decrement grade
bp -= temp; //Decrement B+
+
isPlus -= temp; //Decrement B+
 
 
 
//A+
 
//A+
Line 142: Line 207:
 
temp = Math.min(1,temp); //make all values greater than 1 equal to 1
 
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
 
temp = Math.max(0,temp); //make all values less than 0  equal to 0
ap += temp; //increment A+. A+ will never be decremented, it is the highest grade
+
isPlus += temp; //increment A+. A+ will never be decremented, it is the highest grade
 
+
//calculate whether a plus is needed
+
int isPlus = ap + bp + cp + dp;
+
 
+
 
//More fun with adding and subtracting chars. In this case we are working
 
//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
 
//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
 
//then add it to plus, so that it is only incremented if a plus is warrented
 
plus += ('+' - ' ')*isPlus;
 
plus += ('+' - ' ')*isPlus;
 
+
 
//output solution
 
//output solution
 
System.out.println(grade + "% is equal to: " + letter + plus);
 
System.out.println(grade + "% is equal to: " + letter + plus);
Line 160: Line 222:
 
//this code is confusing enough without values you've never seen before
 
//this code is confusing enough without values you've never seen before
 
}
 
}
}</pre>
+
}
 
+
  
 
}}
 
}}

Latest revision as of 23:11, 8 December 2011

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