1. Ask the user to enter their test 1 score (from 100) 2. Ask the user to enter
ID: 3874502 • Letter: 1
Question
1. Ask the user to enter their test 1 score (from 100) 2. Ask the user to enter their test 2 score (from 100) 3. Ask the user to enter their Final test score (from 100) 4. Ask the user to enter their 8 lab scores (each one is a Pass/Fail) Ask the user to enter P for Pass or F for Fail Save the data in an array of char 5. Call the following method and remember its returned value: public static double calculateLabGrade (char[] labGrades) this method will calculate the average for the labs and returns the result out of 100 score. 6. Call the following method and remember its returned value: public static double calculateNumericalGrade (double testl, double test2, double finalTest, double labs) this method will takes in 4 numbers and will calculate and return the numericalGrade based on the following: Testl 20%, test2 20%, final-Test 40%, Labs20% 7. Call the following method and remember its returned value: public static char calculateLetterGrade (double nGrade) this method takes in a numerical grade and returns a letter Grade: B+: 87-89 A: 94-100 C+: 77-79 D-: 60-63 A-: 90-93 B:84- 86, B: 80-83 C:74-76 C-: 70-73 D+: 67- 69 D: 64-66 F: Below 60 8. Print out testl, test2, the 8 lab scores, and their average 9. Print out the numerical grade for the class and the letter from 100 and the final exam. grade.Explanation / Answer
Hello, I have a solution for you. Everything is implemented as per the needs. Note that there was a mistake in one of the method signatures. i.e method to calculate the letter grade cannot be used to return a single char, as there can be two characters (A-, B+ etc) in a letter grade.So I have changed it to return a String of letter grade. No other changes have been made.
Created a class Grade, with a main function, defined a Scanner object to get user input, defined appropriate variables to store test scores and lab results. Implemented methods to calculate lab grade in 100, numerical grade and letter grade as per the requirements. Comments are included wherever necessary, feel free to drop a comment if you have any doubts. Thanks.
//Grades.java file
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
/**
Scanner object to receive user input */
Scanner scanner = new Scanner(System.in);
/**
Variables to store input and results */
double test1, test2, finaltest, labgrade;
char[] labs = new char[8];
/**
Getting inputs and handling errors appropriately */
try {
System.out.println("Enter test 1 score");
test1 = Double.parseDouble(scanner.nextLine());
if (test1 < 0 || test1 > 100) {
/** Invalid entry- test scores must be in between 0-100 */
throw new Exception();
}
System.out.println("Enter test 2 score");
test2 = Double.parseDouble(scanner.nextLine());
if (test2 < 0 || test2 > 100) {
/** Invalid entry- test scores must be in between 0-100 */
throw new Exception();
}
System.out.println("Enter final test score");
finaltest = Double.parseDouble(scanner.nextLine());
if (finaltest < 0 || finaltest > 100) {
/** Invalid entry- test scores must be in between 0-100 */
throw new Exception();
}
/**
Reading lab results */
for (int i = 0; i < 8; i++) {
System.out.println("Enter lab " + (i + 1) + " result, P/F");
char c = scanner.nextLine().charAt(0);
if (c == 'p' || c == 'P' || c == 'f' || c == 'F') {
labs[i] = c;
} else {
/** Invalid entry for lab exam results */
throw new Exception();
}
}
/**
Calculating lab grade, numerical grade and letter grade */
labgrade = calculateLabGrade(labs);
double numericalGrade= calculateNumericalGrade(test1,test2,finaltest,labgrade);
String letterGrade=calculateLetterGrade(numericalGrade);
/**
Displaying the stats */
System.out.println("...Results...");
System.out.println("Test1 score: "+test1);
System.out.println("Test2 score: "+test2);
System.out.println("Final Test score: "+finaltest);
for(int i=0;i<labs.length;i++){
System.out.println("Lab"+(i+1)+": "+labs[i]);
}
System.out.println("Lab average: "+labgrade);
System.out.println("Numerical Grade: "+numericalGrade);
System.out.println("Final grade: "+letterGrade);
} catch (Exception e) {
System.out.println("Invalid input");
}
}
/**
method to calculate average lab grade */
public static double calculateLabGrade(char[] labgrades) {
double grade = 0;
int passcount = 0;
for (int i = 0; i < labgrades.length; i++) {
if (labgrades[i] == 'P' || labgrades[i] == 'p') {
passcount++;
}
}
/**
average= number of passes / total number of labs * 100 */
grade = (double) passcount / labgrades.length;
return grade * 100;
}
/**
method to calculate numerical grade */
public static double calculateNumericalGrade(
double test1, double test2, double finaltest, double labs) {
double numGrade = 0;
/**
As the inputs are already out of 100, we just need to multiply it with
the requred percentages (20/100, 40/100 etc) and add them all */
numGrade = test1 * (.2d) + test2 * (.2d) + finaltest * (.4d) + labs * (.2d);
return numGrade;
}
/**
method to calculate letter grade */
public static String calculateLetterGrade(double ngrade){
if(ngrade >=94){
return "A";
}else if(ngrade>=90 && ngrade<=93){
return "A-";
}else if(ngrade>=87 && ngrade<=89){
return "B+";
}else if(ngrade>=84 && ngrade<=86){
return "B";
}else if(ngrade>=80 && ngrade<=83){
return "B-";
}else if(ngrade>=77 && ngrade<=79){
return "C+";
}else if(ngrade>=74 && ngrade<=76){
return "C";
}else if(ngrade>=70 && ngrade<=73){
return "C-";
}else if(ngrade>=67 && ngrade<=69){
return "D+";
}else if(ngrade>=64 && ngrade<=66){
return "D";
}else if(ngrade>=60 && ngrade<=63){
return "D-";
}else{
return "F";
}
}
}
/*OUTPUT*/
Enter test 1 score
100
Enter test 2 score
80
Enter final test score
90
Enter lab 1 result, P/F
P
Enter lab 2 result, P/F
P
Enter lab 3 result, P/F
P
Enter lab 4 result, P/F
P
Enter lab 5 result, P/F
P
Enter lab 6 result, P/F
F
Enter lab 7 result, P/F
F
Enter lab 8 result, P/F
F
...Results...
Test1 score: 100.0
Test2 score: 80.0
Final Test score: 90.0
Lab1: P
Lab2: P
Lab3: P
Lab4: P
Lab5: P
Lab6: F
Lab7: F
Lab8: F
Lab average: 62.5
Numerical Grade: 84.5
Final grade: B
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.