Question

I'm a college freshmen and I'm having trouble with my programming homework. The homework I got from my lecturer was for me to write a program in Java to take in a student's info, and allow the student to choose how many subject the student takes, and input marks and credit hours, followed by a formula to calculate the grade and subject grade points. In the end of the program, the program would be able to output the student info (name, ID etc) and the total subject grade point for all of the subjects entered, total credit hours for all the subjects, and the cumulative grade point average (CGPA).

However, I have three problems here

  1. I have an issue with the loop that I have set in order to read how many subjects the user wants to key in.
  2. When I tried to print "Grade = " + subjectGrade); my compiler says it hasn't been initialized. Same goes to the GradePoint and subjectCreditHour.
  3. And I couldn't figure out how to get the program to calculate the Total Subject Grade Points, Total Credit Hours, and CGPA. Because depending on how many numbers of subjects the user wants, I can't figure out how to get the program to take in the user's input and sum them up together

My code goes like this :

package javaquiz1;

import java.util.Scanner;

/**
 *
 * @author jerem_000
 */
public class JavaQuiz1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner (System.in);

        String name;
        int ID;
        int tel;
        String email;
        int subjects;

        String subjectName;
        int subjectCreditHour;
        int subjectMark;
        String subjectGrade;
        double GradePoint;

        double subjectGradePoint;
        double CGPA;
        double totalSubjectGP;
        int totalCreditHour;

        System.out.print("Please input student's name : ");
        name = input.nextLine();
        System.out.print("Please input student's ID : ");
        ID = input.nextInt();
        System.out.print("Please input student's telephone number : ");
        tel = input.nextInt();
        System.out.print("Please input student's email : ");
        email = input.next();
        System.out.print("Please input number of subjects : ");
        subjects = input.nextInt();

        for (int i = 1; i >= subjects ; i++) {  //I'm having an issue with this loop 
        System.out.println("Subject " + i +  " : Please input the following"); //I placed the variable i there in order to make the program print something like "Subject 1 , Subject 2, Subject 3 etc". Depending on the user's number of subjects input
        System.out.print("Subject name : ");
        subjectName = input.next(); 
        System.out.print("Credit Hour : ");
        subjectCreditHour = input.nextInt();
        System.out.print("Mark : ");
        subjectMark = input.nextInt();

        if ( subjectMark >= 80 ) {
            subjectGrade = "A";
            GradePoint = 4.0;
        } else if (subjectMark < 80) {
            subjectGrade = "B+";
            GradePoint = 3.5;
        } else if (subjectMark < 70) {
            subjectGrade = "B";
            GradePoint = 3.0;
        } else if (subjectMark < 65) {
            subjectGrade = "C+";
            GradePoint = 2.5;
        } else if (subjectMark < 55) {
            subjectGrade = "C";
            GradePoint = 2.0;
        } else if (subjectMark < 50) {
            subjectGrade = "D";
            GradePoint = 1.0;
        } else  {
            subjectGrade = "F";
            GradePoint = 0.0;
        } 
    }

        System.out.println("Grade = " + subjectGrade); 
        System.out.println("Subject Grade Point = " + (GradePoint * subjectCreditHour)); //I'm having a problem with the subjectGrade, GradePoint, and subjectCreditHour, it says variable might have not been initialized 

        System.out.println("Name : " + name);
        System.out.println("ID : " + ID);
        System.out.println("Tel : " + tel);
        System.out.println("email : " + email);

        System.out.print("Total subject Grade Points = "  ); 
        System.out.print("Total Credit Hours = " );
        System.out.print("Cumulative Grade Point Average =");  //On this 3 system.out.prints, I can't seem to think of a way to read the Grade Point, Total Credit Hours, and CGPA, and add them all together  

    }
}

I also have a sample output on how the program is supposed to be:

Please input student's name : James Cook 
Please input student's ID : 0106578
Please input student's tel : 010783938
Please input student's e-mail : jcook@gmail.com
Please input number of subjects : 3 

Subject 1 : Please input the following
Subject name : Fundamentals of Programming
Credit Hour : 4 
Mark : 78
Grade : B+
Subject Grade Point : 14.0 

Subject 2 : Please input the following
Subject name : English
Credit Hour : 3 
Mark : 85
Grade : A
Subject Grade Point : 12.0 

Subject 3 : Please input the following
Subject name : Computer Fundamentals
Credit Hour : 3 
Mark : 78
Grade : B+
Subject Grade Point : 10.5 

Name : James Cook
ID : 0106578 
tel :  010783938 
e-mail : jcook@gmail.com

Total subject Grade Point = 36.5
Total Credit Hours = 10
CGPA = 3.65 

The formula to calculate the subject grade point is subjectGradePoint = GradePoint * CreditHour

And the formula to calculate the CGPA (cumulative grade point average) is CGPA = totalSubjectGP / totalCreditHours

Correction, criticism, advice will be welcomed for future improvement. Thanks in advance!

Était-ce utile?

La solution

1) I have an issue with the loop that I have set in order to read how many subjects the user wants to key in.

Already answered by @Nishan in a comment. Just replace for (int i=1;i >= subjects; i++) by for (int i=1;i <= subjects; i++).


2) When I tried to print "Grade = " + subjectGrade); my compiler says it hasn't been initialized. Same goes to the GradePoint and subjectCreditHour.

Already answered.


3) And I couldn't figure out how to get the program to calculate the Total Subject Grade Points, Total Credit Hours, and CGPA. Because depending on how many numbers of subjects the user wants, I can't figure out how to get the program to take in the user's input and sum them up together

You already are on the right way since you have the accumulators you need:

double subjectGradePoint = 0d;
double CGPA = 0d;
double totalSubjectGP = 0d;
int totalCreditHour = 0;

Whitin your loop and after nested if-else blocks, you need to update subjectGradePoint, totalSubjectGP and totalCreditHour variables in each iteration:

subjectGradePoint = GradePoint * CreditHour;
totalSubjectGP += subjectGradePoint;
totalCreditHour += CreditHour;

Finally, after your loop calculate CGPA:

CGPA = totalSubjectGP / totalCreditHour;

Autres conseils

Java mandates method variables to be initialized before use, just initialize String value and it shall work fine. Please refer to below code extract.

    String subjectName=null;
    int subjectCreditHour=0; 
    String subjectGrade=null;

java variable must intialized before use

import java.util.Scanner;

/**
 *
* @author jerem_000
*/

public class JavaQuiz1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Scanner input = new Scanner (System.in);

    String name;
    int ID;
    int tel;
    String email;
    int subjects;

    String subjectName;
    int subjectCreditHour=0;
    int subjectMark;
    String subjectGrade="";
    double GradePoint=0;

    double subjectGradePoint=0;
    double CGPA;
    double totalSubjectGP;
    int totalCreditHour;

    System.out.print("Please input student's name : ");
    name = input.nextLine();
    System.out.print("Please input student's ID : ");
    ID = input.nextInt();
    System.out.print("Please input student's telephone number : ");
    tel = input.nextInt();
    System.out.print("Please input student's email : ");
    email = input.next();
    System.out.print("Please input number of subjects : ");
    subjects = input.nextInt();

    for (int i = 1; i >= subjects ; i++) {  //I'm having an issue with this loop 
    System.out.println("Subject " + i +  " : Please input the following"); //I placed    the        variable i there in order to make the program print something like "Subject 1 , Subject 2, Subject 3 etc". Depending on the user's number of subjects input
    System.out.print("Subject name : ");
    subjectName = input.next(); 
    System.out.print("Credit Hour : ");
    subjectCreditHour = input.nextInt();
    System.out.print("Mark : ");
    subjectMark = input.nextInt();

    if ( subjectMark >= 80 ) {
        subjectGrade = "A";
        GradePoint = 4.0;
    } else if (subjectMark < 80) {
        subjectGrade = "B+";
        GradePoint = 3.5;
    } else if (subjectMark < 70) {
        subjectGrade = "B";
        GradePoint = 3.0;
    } else if (subjectMark < 65) {
        subjectGrade = "C+";
        GradePoint = 2.5;
    } else if (subjectMark < 55) {
        subjectGrade = "C";
        GradePoint = 2.0;
    } else if (subjectMark < 50) {
        subjectGrade = "D";
        GradePoint = 1.0;
    } else  {
        subjectGrade = "F";
        GradePoint = 0.0;
    } 
}

    System.out.println("Grade = " + subjectGrade); 
    System.out.println("Subject Grade Point = " + (GradePoint * subjectCreditHour)); //I'm having a problem with the subjectGrade, GradePoint, and subjectCreditHour, it says variable might have not been initialized 

    System.out.println("Name : " + name);
    System.out.println("ID : " + ID);
    System.out.println("Tel : " + tel);
    System.out.println("email : " + email);

    System.out.print("Total subject Grade Points = "  ); 
    System.out.print("Total Credit Hours = " );
    System.out.print("Cumulative Grade Point Average =");  //On this 3 system.out.prints, I can't seem to think of a way to read the Grade Point, Total Credit Hours, and CGPA, and add them all together  

}

}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top