Consider the previous question, but include + or - letter grades. A+ is 4.25, A- is 3.75, B+ is 3.25, B- is 2.75, and so on.
a.Why can’t we use one switch statement with no other conditionals to convert these additional letter grades?
b. Write a fragment of code that will do the conversion using a multibranch ifelse statement.
c. Write a fragment of code that will do the conversion using nested switch statements.
a) The grade A+ would be a string, but we cannot use switch on a string.
b) ```
if(enhancedLetterGrade.equals("A+"))
gradeValue = 4.25;
else if(enhancedLetterGrade.equals("A"))
gradeValue = 4.0;
else if(enhancedLetterGrade.equals("A-"))
gradeValue = 3.75;
else if(enhancedLetterGrade.equals("B+"))
gradeValue = 3.25;
else if(enhancedLetterGrade.equals("B"))
gradeValue = 3.0;
else if(enhancedLetterGrade.equals("B-"))
gradeValue = 2.75;
else if(enhancedLetterGrade.equals("C+"))
gradeValue = 2.25;
else if(enhancedLetterGrade.equals("C"))
gradeValue = 2.0;
else if(enhancedLetterGrade.equals("C-"))
gradeValue = 1.75;
else if(enhancedLetterGrade.equals("D+"))
gradeValue = 1.25;
else if(enhancedLetterGrade.equals("D"))
gradeValue = 1.0;
else if(enhancedLetterGrade.equals("D-"))
gradeValue = 0.75;
else if(enhancedLetterGrade.equals("F+"))
gradeValue = 0.25;
else if(enhancedLetterGrade.equals("F"))
gradeValue = 0.0;
else {
gradeValue = 0.0;
System.out.println("The grade "
+ enhancedLetterGrade + " is not valid");
}
```
c) ```
char letterPart = enhancedLetterGrade.charAt(0);
char plusPart = '0';
if(enhancedLetterGrade.length()>1)
plusPart = enhancedLetterGrade.charAt(1);
switch(letterPart){
case 'A':
gradeValue = 4.0;
switch(plusPart){
case '+':
gradeValue += 0.25;
break;
case '-':
gradeValue -= 0.25;
break;
}
break;
case 'B':
gradeValue = 3.0;
switch(plusPart){
case '+':
gradeValue += 0.25;
break;
case '-':
gradeValue -= 0.25;
break;
}
break;
case 'C':
gradeValue = 2.0;
switch(plusPart){
case '+':
gradeValue += 0.25;
break;
case '-':
gradeValue -= 0.25;
break;
}
break;
case 'D':
gradeValue = 1.0;
switch(plusPart){
case '+':
gradeValue += 0.25;
break;
case '-':
gradeValue -= 0.25;
break;
}
break;
case 'F':
gradeValue = 0.0;
switch(plusPart){
case '+':
gradeValue += 0.25;
break;
}
break;
default:
gradeValue = 0.0;
System.out.println("The grade " + letter
+ " is not valid");
}
```
This code is in Fragments.java.
You might also like to view...
A(n) ____________________ installs, upgrades, and configures software.
Fill in the blank(s) with the appropriate word(s).
An online application suite designed to help users manage, supplement, visualize, and analyze data.
What will be an ideal response?