传递正确的参数后,为什么我的GPA方法无法正确计算?

时间:2019-05-27 01:07:44

标签: java arrays class constructor

大家好,我都想解决这个课堂上的JAVA作业,下午我大部分时间都在努力,试图让我的GPA构造函数为我提供正确的结果。

我需要输出的是学生的姓名及其GPA。目前,“名字”和“姓氏”已正确打印,但是对于GPA,我已经得到了空值,而现在,我似乎只是得到了参考。

这是我的主要课程代码

package assignment2;

/**
*
* @author javyc
*/
import java.text.NumberFormat;
import java.util.HashSet;
public class Assignment2 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Classes[]courseArray = new Classes[2];
    courseArray[0] = new Classes("Javier", "Ruiz", "Winter 2018", 3);
    courseArray[1] = new Classes("Gabriela", "Ruiz", "Fall 2018", 3);

    double[] gpa = new double[2];
        gpa[0] = 4.00;
        gpa[1] = 4.00;

        Students ArizonaStudent = new Students("Javier", "Ruiz","MIS", "2020", courseArray, gpa);
        gpa = ArizonaStudent.getGpa();
        NumberFormat formatting = NumberFormat.getNumberInstance();
        formatting.setMaximumFractionDigits(2);

        System.out.println(ArizonaStudent.getStudentFirstName()+ " " + ArizonaStudent.getStudentLastName() + "" + gpa);
        //System.out.println(ArizonaStudent.getCourses(1));
}

}

这是我的学生班级代码

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment2;

/**
 *
 * @author javyc
*/
public class Students {
private String studentFirstName;
private String studentLastName;
private String department;
private String gradYear;

private Classes[] courses;
private double[] gpa1;

public Students(String studentFN, String studentLN, String dept, String graduationYear, Classes[]coursesList, double[]gpaList){
    studentFirstName = studentFN;
    studentLastName = studentLN;
    department = dept;
    gradYear = graduationYear;
    setCourses(coursesList);
    setGPA(gpaList);
}

public String getStudentFirstName(){
    return studentFirstName;
}
public String getStudentLastName(){
    return studentLastName;
}
public String getStudentDepartment(){
    return department;
}
public String getStudentGraduationYear(){
    return gradYear;
}
public Classes[]getCourses(){
    return courses;
}
public double[]getGpa(){
    return gpa1;
}


public void setStudentFirstName(String studentFN){
    studentFirstName = studentFN;
}
public void setStudentLastName(String studentLN){
    studentLastName = studentLN;
}
public void setDepartment(String department){
    this.department = department;
}
public void setGraduationYear(String gradYear){
    this.gradYear = gradYear;
}
public void setCourses(Classes[]courses){
    this.courses=courses;
}
public void setGPA(double[]gpa1){
    this.gpa1=gpa1;
}
 public class GPA{
public double gradePointAverage(){
    int totalCreds = 0;
    double gpaTotal = 0.0;
    for (int i = 0; i<courses.length;i++){
        totalCreds += courses[i].getCredits();
        gpaTotal += courses[i].getCredits()* gpa1[i];  
        return gpaTotal = gpaTotal/totalCreds;
    }
    return gpaTotal = gpaTotal/totalCreds;
}
}
}

这是我的课程代码

/*
 * To change this license header, choose License Headers in Project     Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment2;

/**
 *
 * @author javyc
 */
public class Classes {
//We will start by declaring the attributes of the Classes offered at UA
private String professorFirstName;
private String professorLastName;
private String semesterOffered;
private int creditTotal = 0;

public Classes(String profMainName, String profLastName, String semester, int credits) 
    {
    professorFirstName = profMainName;
    professorLastName = profLastName;
    semesterOffered = semester;
    creditTotal = credits;
    }
public String returnFirstName(){
    return professorFirstName;
}
public String returnLastName(){
    return professorLastName;
}
public String returnSemester(){
    return semesterOffered;
}
public int getCredits(){
    return creditTotal;
}
public void establishedSemester(String semesterOffered){
    this.semesterOffered = semesterOffered;
}
public void establishFirstName(String professorFirstName){
    this.professorFirstName = professorFirstName;
}
public void establishLastName(String professorLastName){
    this.professorLastName = professorLastName;
}
public void establishCredits(Integer creditTotal){
    this.creditTotal = creditTotal;
}

}

1 个答案:

答案 0 :(得分:0)

有几个设计问题。我不确定最初的作业要求是什么,但是您的班级应该是学生还是学生?然后,每个学生对象将代表一个学生。所以,那个学生不应该只有一个GPA吗?

如果要为学生设置GPA,我假设您应该将对象的setGPA方法传递给double而不是double数组。这有道理吗?