DownLoadFiles Programming Example CGPA
DownLoadFiles Programming Example CGPA
CGPA Write a program to calculate the CGPA got by a student. CGPA will be calculated on the basis of SGPA and number of semester. CGPA, which is the cumulative grade point average is calculated on the basis of SGPA, which is the semester grade point average. CGPA is nothing but SUM of (SGPA * no of credits in that semester) / SUM of all the credits.
Constraints
All the sgpas are non negative, otherwise return -1 All the credits are greater than zero, otherwise return -1 The arraylength of sgpa and credits are equal to noOfSem.
Example 1
Input sgpa = {0.8, 0.7, 0.6, 0.5}, credits = {50, 100, 50, 100}, noOfSem = 4 Output The function calcCGPA() returns 0.633. Explanation CGPA = (SUM (sgpa * credits)) / SUM (Credits) = (0.8 * 50 + 0.7 * 100 + 0.6 * 50 + 0.5 * 100) / 300
Example 2
Input sgpa = {7.2, 8.32, 7.5}, credits = {50, 100, 50}, noOfSem = 3 Output The function calcCGPA() returns 7.835
Example 3
Input sgpa = {7.2, 8.32, 7.5} credits = {50, 100, 0} noOfSem = 3 Output The function calcCGPA() returns -1
General Instructions The package names, class names, method signatures to be used are mentioned in the problem statement.
Do not use your own names or change the method signatures and fields. You can add any number of additional methods.
Pseudo Code
CGPA
1. 2. 3. 4. Check for the constraint, all the sgpas are non negative, otherwise return -1 Check for the constraint, All the credits are greater than zero, otherwise return -1 Calculate the CGPA. Return the CGPA from the method.
Program Solution
package test.cgpa; public class CGPA { float calcCGPA(float[] sgpa, int[] credits, int noOfSem) { float cgpa = 0.0f; float sgpaC = 0.0f; int totalCredits = 0; for(int i = 0; i<noOfSem; i++) { if(sgpa[i] < 0 || credits[i] <= 0) return -1; sgpaC += sgpa[i] * credits[i]; totalCredits += credits[i]; } cgpa = sgpaC / totalCredits; return cgpa; }
public static void main(String[] args) { //TestCase 1 try { float[] sgpa = {0.8f, 0.7f, 0.6f, 0.5f}; int[] credites = {50, 100, 50, 100}; int noOfSem = 4;