This IS NOT homework, but it is a practice that the teacher gave us to help study for a test. But i will not be turning this in to the teacher. (i’m hoping that is allowed)
He wants a user to input a grade and have it assigned a letter grade. It would be easy with if statements, but i can’t use ANY! I have to use the switch method.
Here is my functional class:
public class Grade {
public double getGrade(int input)
{
double inputGrade;
switch(input)
{
case 1: inputGrade >= 90;
break;
case 2: inputGrade >= 80;
break;
case 3: inputGrade >= 70;
break;
default:
grade = 60;
}
return grade;
}
}
Here is my test class:
import java.util.*;
public class TestGrade
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
Grade lGrade = new Grade();
double finalGrade = lGrade.getGradeSwitch(input);
System.out.println("Your toll is $" + finalGrade);
}
}
I just haven’t been programming enough to have this analytical mind. I HAVE tried to complete it, i just haven’t found a way to covert the user input (int) into a letter grade (string) without if statements.
I know this is incomplete, but this is as far as I could go without making errors.
EDIT:WOW! Thanks guys, i hate to pick a single correct answer, because a lot of you helped me!
This is what i ended up with (that worked :D)
public String getGrade(int input)
{
String letterGrade;
switch(input/10)
{
case 9: letterGrade = "A";
break;
case 8: letterGrade = "B";
break;
case 7: letterGrade = "C";
break;
case 6: letterGrade = "D";
default:
letterGrade = "F";
}
return letterGrade;
}
OK, my assumption is that you will enter a numeric grade 0-100, and that the letter grades you want go as “90-100: A; 80-89: B; …; 60-69: D; 0-59: F”.
If that is not the case, your first lesson to be learned about programming is: the most important part of coding is clearly writing out specifications and requirements
Having said that, there are smart and dumb approaches.
Dumb approach:
Use a switch on the “input” as you do now
Since values 90, 91… 100 are all supposed to be treated the same, put them all in as separate switch cases but only do the work in the last one (known as fall-through; see SwitchDemo2 on this page for how that works)
For the last value (say, 90 if you start from 100 and go down), the switch statement would be letterGrade = “A”; break;
once you reach case of 89, you repeat the same logic as you did for 100 through 90
Lather, Rinse, Repeat, till 60
Handle Fs as a default case.
This will be very long, quite dumb (as in, brute force) and very annoying to write, and a brilliant teaching tool for why you never want to write dumb code again if you can help it.
More elegant solution (insert appropriate Star Wars quote here)
Right, it’s the fact that, aside from 100, they all are 2-diit #s starting with 9.
So, to handle them all in 1 case statement, wouldn’t it be wonderful to switch on some value that gives you “9” for all #s between 90 and 99?
I hope you know the answer right away… hint below…
Yes, just divide whole by 10!
So, you need to have a switch on your numeric grade diveded by 10,
with cases of 10, 9, 8, 7, 6 and default mapping to F.
Hope this covers enough ground for you to come up with working solution, while showing how the analysis might work as well.