Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want do display the Quizzes,Labs, Lab attendance, Midterms like its shown below.
Quizzes: 66%
Labs: 88%
Lab attendance: 81%
Midterms: 91%
Final: Not applicable
Overall Average: 85%.
But I'm getting

output:

66.0
88.0
81.0
91.0



Java
import java.io.*;
    import java.util.*;
    
    public class FindGrade {
        public static final int NUM_SCORE_TYPES = 5;
    
        public static void main(String[] args) {
            Scanner scan = null;
            int[] quizArray = null;
            int[] labArray = null;
            int[] attendance = null;
            int[] midterms = null;
            int quizgrade = 0;
            int labgrade = 0;
            int attendance_1 = 0;
            int midterms_1 = 0;
            String name;
    
    
            try {
                scan = new Scanner(new File("input.txt"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return;
            }
    
            // each iteration is for single exam type (ie: Quizzes is the 1st one)
            for (int i = 0; i < NUM_SCORE_TYPES; i++) {
    
                name = scan.next();
                int numScores = scan.nextInt();
                int maxGrade = scan.nextInt();
    
                if (name.equals("Quizzes")) {
                    quizArray = new int[numScores];
                    readScores(quizArray, numScores, scan,maxGrade);
    
    
                } else if (name.equals("Labs")) {
                    labArray = new int[numScores];
                    readScores(labArray, numScores, scan,maxGrade);
    
                } else if (name.equals("Lab_attendance")) {
                    attendance = new int[numScores];
                    readScores(attendance, numScores, scan,maxGrade);
    
                } else if (name.equals("Midterms")) {
                    midterms = new int[numScores];
                    readScores(midterms, numScores, scan,maxGrade);
    
                }
    
            }
    
        }
    
    
        public static void readScores(int[] scoreArray, int numScores, Scanner scan, int maxGrade) {
            for (int i = 0; i < numScores; i++) {
                scoreArray[i] = scan.nextInt();
            }
            average(scoreArray, numScores, maxGrade);
        }
    
        public static int normalize(int[] scoreArray, int maxGrade) {
            int total = 0;
            for (int i = 0; i < scoreArray.length; i++) {
                total += scoreArray[i];
            }
            int percent = Math.round(total * 100 / maxGrade);
            return percent;
        }
    
        public static double average(double[] scoreArray, int numScores,int maxGrade) {
            double sum = 0;
            for (int i = 0; i < scoreArray.length; i++) {
                sum += scoreArray[i];
            }
            double average = Math.round((sum / numScores)*100/maxGrade);
    
            return average;



}

input file:

Quizzes 8 10
5 8 9 10 4 0 10 7
Labs 6 100
95 90 100 87 63 92
Lab_attendance 16 1
1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1
Midterms 2 100
87 94
Final 0 100
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900