Flow of Control in Java When processing data, it is necessary to make comparison
ID: 3600365 • Letter: F
Question
Flow of Control in Java
When processing data, it is necessary to make comparisons and validate the information entered. This week those skills will be learned by understanding the if-else and looping statements (while, do-while, and for-loop).
Assignment:
Part I:
Modify the program in Example 5.4 on page 225-226 to implement the new grading system below. Change the program by making the variable letterGrade a String variable instead of a character (char) variable, and change the if-else statement to assign the appropriate grade for the test score entered.
Part II: Read section 6.5, Type-Safe Input Using Scanner, on page 310. Review the code in Example 6.10 on page 313 and modify the input in the above program to make sure the test score entered is an integer.
Run the program and enter 75.1, J, and 75.
Make a screenshot of the output and include it as an attachment to the Week 3 Assignment. Also, include the program code in a text file to run on another computer. Make sure to add comments to your code for full credit (see page 45).
Grade Minimum Percentage A+ 97 A 93 A- 90 B+ 87 B 83 B- 80 C+ 77 C 73 C- 70 D+ 67 D 65 D- 60 F 0Explanation / Answer
CalculateGradeLetter.java
import java.util.InputMismatchException;
import java.util.Scanner;
public class CalculateGradeLetter {
public static void main(String[] args) {
//Declaring variables
int grade;
String finalGrade;
int flag = 0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner input = new Scanner(System.in);
/* This while loop continues to execute
* until the user enters a valid number or -1
*/
while (true) {
try {
System.out.print("Enter a grade between (0-100) or -1 to quit: ");
grade = input.nextInt();
while (true) {
flag = 0;
if (grade != -1) {
System.out.print(grade);
finalGrade = findLetterGrade(grade);
System.out.printf("%s%d%s%s%n", " The letter grade is for ", grade, " is ", finalGrade);
System.out.print("Enter a grade between (0-100) or -1 to quit: ");
grade = input.nextInt();
continue;
} else
break;
}
} catch (InputMismatchException e) {
System.out.println("Invalid,Must be an Integer");
input.nextLine();
flag = 1;
continue;
}
if (flag == 0)
break;
}
} //end method
//This method will return the grade letter
public static String findLetterGrade(int grade) //
{
String outputGrade = null;
if (grade >= 97 && grade <= 100)
outputGrade = "A+";
else if (grade >= 93 && grade < 97)
outputGrade = "A";
else if (grade >= 90 && grade < 93)
outputGrade = "A-";
else if (grade >= 87 && grade < 90)
outputGrade = "B+";
else if (grade >= 83 && grade < 87)
outputGrade = "B";
else if (grade >= 80 && grade < 83)
outputGrade = "B-";
else if (grade >= 77 && grade < 80)
outputGrade = "C+";
else if (grade >= 73 && grade < 77)
outputGrade = "C";
else if (grade >= 70 && grade < 73)
outputGrade = "C-";
else if (grade >= 67 && grade < 70)
outputGrade = "D+";
else if (grade >= 65 && grade < 67)
outputGrade = "D";
else if (grade >= 60 && grade < 65)
outputGrade = "D-";
else if (grade >= 0 && grade < 60)
outputGrade = "F";
return outputGrade;
}
}
_________________
Output:
Enter a grade between (0-100) or -1 to quit: 75.1
Invalid,Must be an Integer
Enter a grade between (0-100) or -1 to quit: J
Invalid,Must be an Integer
Enter a grade between (0-100) or -1 to quit: 75
75 The letter grade is for 75 is C
Enter a grade between (0-100) or -1 to quit: 97
97 The letter grade is for 97 is A+
Enter a grade between (0-100) or -1 to quit: 93
93 The letter grade is for 93 is A
Enter a grade between (0-100) or -1 to quit: -1
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.