2

Java Program to Calculate Grade Point Based on Credit Hour and Display Loan and Claa

Q: Write a simple java program that will collect grade points and credit hours of courses to calculate the CGPA. The program should collect grade point and credit hours of five courses into an array gp[] and crhr[]. The CGPA is assigned to a class. The following are the CGPA and the classes:


•4.0 – First Class

•3.9 – 3.0 – Second Class

•2.9 – 2.0 – Third Class

•1.9 – 1.0 – Pass


Using switch, display the class and CGPA of individual students. Base on the individual CGPA a ban policy is given as follows:


•4.0 – 3.2 – 20% of tuition fees

•3.1 – 2.5 15% of tuition

•2.4 – 2.0 – 10% of tuition

•1.9 – 1.5 – 5% of tuition


Using if-else-if statement, calculate the loan where tuition fee must be collected from the keyboard.


Solutions:


import java.util.Scanner;

class Cgpa{

public static void main(String arg[]){

Scanner get_values = new Scanner(System.in);

float gp[] = new float[5];

int crhr[] = new int[5];

for(int x = 0; x < 5;x++){

System.out.println("Enter Grade Point: ");

gp[x] = get_values.nextFloat();

System.out.println("Enter Credit Hour: ");

crhr[x] = get_values.nextInt();

}

double cgpa1 = (gp[0] * crhr[0]) + (gp[1] * crhr[1]) + (gp[2] * crhr[2]) + (gp[3] * crhr[3]) + (gp[4] * crhr[4]);

double tCredit = crhr[0] + crhr[1] + crhr[2] + crhr[3] + crhr[4];

double cgpa = cgpa1/tCredit;

int switchCgpa = (int)cgpa;

switch(switchCgpa){

case 4:

System.out.println("Class: First Class!");

break;

case 3:

System.out.println("Class: Second Class!");

break;

case 2:

System.out.println("Class: Third Class!");

break;

case 1:

System.out.println("Class: Pass!");

break;

default:

System.out.println("Class: Sorry, you are classless!");

}

System.out.println("Enter your tuition fee: ");

double loan;

double tuition = get_values.nextDouble();

if(cgpa >= 3.2){

loan = 0.20 * tuition;

System.out.println(" Loan: " + loan);

}

else if(cgpa >= 2.5 && cgpa < 3.2){

loan = 0.15 * tuition;

System.out.println(" Loan: " + loan);

}

else if(cgpa >= 2.0 && cgpa < 2.5){

loan = 0.1 * tuition;

System.out.println(" Loan: " + loan);

}

else if(cgpa >= 1.5 && cgpa < 2.0){

loan = 0.05 * tuition;

System.out.println(" Loan: " + loan);

}

else {

System.out.println("There is no loan for you!");

}

}

}