-2

I am having problem with my getGPA() method it will only return a 0.00 value. I need for the function to calculate the GPA by dividing totalgradepoints by numberofclasses. I've tried everything and have no idea what the problem is. I am a complete beginner and trying to learn as I go this is for a class assignment so this is what we were instructed by our professor. I appreciate any advice on this thank you everyone.

import java.util.Scanner;
import java.text.DecimalFormat;

public class StudentGPAInfo {
    static DecimalFormat f = new DecimalFormat ("0.00");
    private double gpa;
    private int totalgradepoints;
    private int numberofclasses;
    private String studentname;

    public StudentGPAInfo() {
        gpa = 0;
        totalgradepoints = 0;
        numberofclasses = 0;
        studentname = null;

    }

    public void setName(String studentID){
        studentname = studentID;

    } 

    public void addClass(int classes, String gradepoint){
        numberofclasses = numberofclasses += classes;
            if (gradepoint.equalsIgnoreCase("A")) {
                totalgradepoints += 4;
            } else if (gradepoint.equalsIgnoreCase("B")) {
                totalgradepoints += 3;
            } else if (gradepoint.equalsIgnoreCase("C")) {
                totalgradepoints += 2;
            } else if (gradepoint.equalsIgnoreCase("D")) {
                totalgradepoints += 1;
            } else if (!gradepoint.equalsIgnoreCase("F")) {
                System.err.println("Invalid input: " + gradepoint);

            }


    }       



    public String getName(){

        return studentname;
    }

    public double getGPA(){

    gpa = totalgradepoints / numberofclasses;

    return gpa;
    }

    public void displayStudent(){
        System.out.println("Student Name: " + studentname);
        System.out.println("Total Grade Points: " + totalgradepoints);
        System.out.println("Number of Class Credits: " + numberofclasses);
        System.out.println(studentname + " Your GPA Average is: " + f.format(gpa));
    }

}

public class GPAtest {
    static Scanner input = new Scanner (System.in);
    public static void main(String[] args) {
    String sName;
    String grade;
    int sClass;
    String aClass;
    StudentGPAInfo student = new StudentGPAInfo();

    System.out.print("Please Enter Student Name: "); 
    sName = input.nextLine();
    student.setName(sName);
    do {
        System.out.print("Please Enter Class credits: ");
        sClass = Integer.parseInt(input.nextLine());
        System.out.print("Please Enter Letter Grade: ");    
        grade = input.nextLine();
        System.out.print("Another Class? ");
        aClass = input.nextLine();
        student.addClass(sClass, grade);
        }
    while (aClass.equalsIgnoreCase("y"));
        student.displayStudent();



    }
}
MrMiyagi
  • 39
  • 5

1 Answers1

0

Please read how integer division works. if the numerator is less than the denominator, the result is zero. Make the numerator a double by casting.

Change this:

gpa = totalgradepoints / numberofclasses;

to this:

gpa = (double) totalgradepoints / numberofclasses;
duffymo
  • 305,152
  • 44
  • 369
  • 561