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.

Computer Science & Information Technology

You might also like to view...

Why would a database administrator choose to split a database?

What will be an ideal response?

Computer Science & Information Technology

The Insert Endnote icon can be found on the Pictures Tools tab

Indicate whether the statement is true or false

Computer Science & Information Technology

Internet Connection Sharing is a simplified form of Network Address Translation

Indicate whether the statement is true or false

Computer Science & Information Technology

Some smartphones support a(n) ______ keyboard app, on which users enter words by tracing a path on an on-screen keyboard.

A. touch B. swipe C. finger D. virtual

Computer Science & Information Technology