Write a program that calculates the total grade for three classroom exercises as a percentage. Use the DecimalFormat class to output the value as a percent. The scores should be summarized in a table. Input the assignment information in this order: name of assignment (may include spaces), points earned (integer), and total points possible (integer). The percentage is the sum of the total points earned divided by the total points possible. Sample input and output is shown as follows:
Name of exercise 1:
Group Project
Score received for exercise 1:
10
Total points possible for exercise 1:
10
Name of exercise 2:
Homework
Score received for exercise 2:
7
Total points possible for exercise 2:
12
Name of exercise 3:
Presentation
Score received for exercise 3:
5
Total points possible for exercise 3:
8
Exercise Score Total Possible
Group Project 10 10
Homework 7 12
Presentation 5 8
Total 22 30
Your total is 22 out of 30, or 73.33%.
import java.util.Scanner;
import java.text.DecimalFormat;
public class ClassroomExercises {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
DecimalFormat percent= new DecimalFormat("##.00%");
String exercise1,exercise2,exercise3;
int score1,score2,score3,totalpossiblepoints1,totalpossiblepoints2,totalpossiblepoints3,marks,grandtotal;
System.out.println("Name of exercise 1:");
exercise1=input.nextLine();
System.out.println("Score received for exercise 1:");
score1=input.nextInt();
System.out.println("Total points possible for exercise 1:");
totalpossiblepoints1=input.nextInt();;
String junk = input.nextLine(); /*To get rid of '\n'*/
System.out.println("Name of exercise 2:");
exercise2=input.nextLine();
System.out.println("Score received for exercise 2 :");
score2=input.nextInt();
System.out.println("Total points possible for exercise 2 :");
totalpossiblepoints2=input.nextInt();;
String junk2 = input.nextLine(); /*To get rid of '\n'*/
System.out.println("Name of exercise 3:");
exercise3=input.nextLine();
System.out.println("Score received for exercise 3:");
score3=input.nextInt();
System.out.println("Total points possible for exercise 3:");
totalpossiblepoints3=input.nextInt();;
String junk3 = input.nextLine(); /*To get rid of '\n'*/
marks=score1+score2+score3;
grandtotal=totalpossiblepoints1+totalpossiblepoints2+totalpossiblepoints3;
System.out.printf("%-30s %-10s %-15s %n","Exercise","Score","Total Possible");
System.out.printf("%-30s %-10d %-15d %n",exercise1,score1,totalpossiblepoints1);
System.out.printf("%-30s %-10d %-15d %n",exercise2,score2,totalpossiblepoints2);
System.out.printf("%-30s %-10d %-15d %n",exercise3,score3,totalpossiblepoints3);
System.out.printf("%-30s %-10d %-15d %n","Total",marks,grandtotal);
System.out.println("Your total "+marks+" out of "+ grandtotal+", or "+percent.format((double)marks /grandtotal )+".");
}
}