Member Avatar for Codeslinger

PLEASE MODIFY MY CODE TO COMPUTE FOR THE AVERAGE OF FIRST GRADING, SECOND GRADING, THIRD GRADING AND FOURTH GRADING PERIOD, THE FORMULA IS THIS

AVERAGE COMPUTATION

First Grading Period:
Average = Sum of grades / Number of subjects

Second Grading to Fourth Grading Period:
Average = (30% of the Average of the previous grading period) + (70% of the Average of the current grading period)

//THIS IS MY CODE:

import java.io.*;

public class TestCase2 {
    public static void main(String[] args)throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        float[][] arr = new float[4][5];
        float ave = 0;
        String[] outData = {"First", "Second", "Third", "Fourth"};
        String studentName, gradeLevel;
        int ctr = 0;

        System.out.print("Enter name: ");
        studentName = in.readLine();
        System.out.print("Grade level: ");
        gradeLevel = in.readLine();

        for(int i=0; i<4; i++) {
            float sum = 0; 
            int c = 0;

            String[] inData = {"first", "second", "third", "fourth", "fifth"};
            System.out.println();
            System.out.println("============================");
            System.out.println("Enter " + outData[ctr] + " Grading Grades   ");
            System.out.println("============================");
            ctr++;

            for(int j=0; j<5; j++) {
                System.out.print("Enter " + inData[c] + " grade: ");
                c++;

                sum += arr[i][j] = Float.parseFloat(in.readLine());         
            }

            //ave = sum / 5;
            //System.out.print("Grading Period Average: " + ave + "n" + "n");               
        }
        System.exit(0);
    }
}

Recommended Answers

All 11 Replies

Please use the code-tags when posting code and define where exactly do you get stuck? what does not work?

Member Avatar for Codeslinger

/* PLEASE MODIFY MY CODE TO COMPUTE FOR THE AVERAGE OF FIRST GRADING, SECOND GRADING, THIRD GRADING AND FOURTH GRADING PERIOD, THE FORMULA IS THIS :

--------------------------------------------
AVERAGE COMPUTATION
--------------------------------------------
First Grading Period:
Average = Sum of grades / Number of subjects

Second Grading to Fourth Grading Period:
Average = (30% of the Average of the previous grading period) + (70% of the Average of the current grading period) */

//THIS IS MY CODE:

import java.io.*;

public class TestCase2 {
public static void main(String[] args)throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

float[][] arr = new float[4][5];
float ave = 0;
String[] outData = {"First", "Second", "Third", "Fourth"};
String studentName, gradeLevel;
int ctr = 0;

System.out.print("Enter name: ");
studentName = in.readLine();
System.out.print("Grade level: ");
gradeLevel = in.readLine();

for(int i=0; i<4; i++) {
float sum = 0;
int c = 0;

String[] inData = {"first", "second", "third", "fourth", "fifth"};
System.out.println();
System.out.println("============================");
System.out.println("Enter " + outData[ctr] + " Grading Grades ");
System.out.println("============================");
ctr++;

for(int j=0; j<5; j++) {
System.out.print("Enter " + inData[c] + " grade: ");
c++;

sum += arr[i][j] = Float.parseFloat(in.readLine());
}

//ave = sum / 5;
//System.out.print("Grading Period Average: " + ave + "\n" + "\n");
}
System.exit(0);
}
}
Member Avatar for Codeslinger

My problem is how to compute the average with the given formulas .. Please help me ! Thanks.

Again: define where exactly do you get stuck? what does not work as expected?

Member Avatar for Codeslinger

The program is running, my only problem is how to apply the formula given in the loop .. THANKS!

You have the formulas - after the loop, enter the averages according the formulas, what is the problem?

Sorry, my mistake

Sorry, my mistake

Forgive my previous post. I clicked the Post button accidentally.

I took your code and made a few changes in order to help you understand it a little bit better

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
        int PERIODS = 4;
        int GRADES = 5; // That is also the number of subjects
    
        float[][] arr = new float[PERIODS][GRADES];

        // This is necessary in order to remember the previous average
        float [] averages = new float[PERIODS];
        
        String studentName, gradeLevel;
    
        System.out.print("Enter name: ");
        studentName = in.readLine();
        System.out.print("Grade level: ");
        gradeLevel = in.readLine();
    
        for(int period=0; period<PERIODS; period++) {
            
            System.out.println();
            System.out.println("============================");
            System.out.println("Enter Grading Grades for period: " + (period+1));
            System.out.println("============================");
        
            float sum = 0;
            for(int grade=0; grade<GRADES; grade++) {
                System.out.print("Enter Grading Grade: " + (grade+1));
            
                arr[period][grade] = Float.parseFloat(in.readLine());
                sum += arr[period][grade];
            }
            float currentAvg = sum/GRADES;
            System.out.println("Current Average for period: "+(period+1) + " is: " + currentAvg);
            
            // TODO ADD YOUR CODE HERE
            //averages[period] = ?;
            
            System.out.println("Final Average for period: "+(period+1) + " is: " + averages[period]);
        }

Try to run that code and see what happens. Now about your formula. It says for the first period do some stuff, for the rest periods do something else. That sounds like if statements:

if (period==0) {
 // Average = Sum of grades / Number of subjects
 averages[period] = Average; // period here is 0
} else {
  // get Average of the previous grading period: IT IS SAVED IN THE averages I DECLARED FOR YOU. YOU JUST NEED TO TAKE THE PREVIOUS ONE SAVED
  // get the Average of the current grading period: ALREADY CALCULATED. IT IS THE CURRENT

  // Average = (30% of the Average of the previous grading period) + (70% of the Average of the current grading period) 
  // SAVE THAT AVERAGE IN THE ARRAY averages
}
Member Avatar for Codeslinger

Thanks bro !

Member Avatar for Codeslinger

I get it ! Once again, thanks bro !

Please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.