I need to write a program that calculates the total grade for N classroom exerci
ID: 3543026 • Letter: I
Question
I need to write a program that calculates the total grade for N classroom exercises as a percentage. I need to use the Decimal Format class to output the value as a percent. The user should input the value for N followed by each of the N scores and totals. Calculate the overall percentage (sum of the total points earned divided by the total points possible) and output it using the Decimal Format Class. sample input (in bold) and output (in italics) is shown below.
How many exercises to input? 3
Score received for exercise 1: 10
Total points possible for exercise 1: 10
Score received for exercise 2: 7
Total points possible for exercise 2: 12
Score received for exercise 3: 5
Total points possible for exercise 3: 8
Your total is 22 out of 30, or 73.33%.
Explanation / Answer
Sample Output
How many exercises to input? 3
Score received for exercise 1: 10
Total points possible for exercise 1: 10
Score received for exercise 2: 7
Total points possible for exercise 2: 12
Score received for exercise 3: 5
Total points possible for exercise 3: 8
Your total is 22.0 out of 30.0, or 73.33%
Code
import java.text.DecimalFormat;
import java.util.Scanner;
/*How many exercises to input? 3
Score received for exercise 1: 10
Total points possible for exercise 1: 10
Score received for exercise 2: 7
Total points possible for exercise 2: 12
Score received for exercise 3: 5
Total points possible for exercise 3: 8*/
public class Exercises {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("How many exercises to input? ");
int countOfExercises = scanner.nextInt();
System.out.println();
int count = 1;
double scoreAchieved = 0, MaxTotal = 0, totalObtained = 0, total = 0;
for (int i = 0; i < countOfExercises; i++)
{
System.out.print("Score received for exercise "+count+": ");
scoreAchieved = scanner.nextDouble();
totalObtained += scoreAchieved;
System.out.print("Total points possible for exercise "+count+": ");
MaxTotal = scanner.nextDouble();
total += MaxTotal;
System.out.println();
count++;
}
double percentage = ((double)totalObtained/ (double)total) * 100;
DecimalFormat decimal = new DecimalFormat("#.00");
System.out.println(" Your total is "+totalObtained+" out of "+total+", or "+decimal.format(percentage)+"%");
}
}