-1

I have an assignment to make a student class with setter and getter methods followed by a course class where we make an array of student objects that we use to test the methods from that student class. I am clearly doing something wrong though.

class Course{
    
    static Student[] Roll = new Student[10];
    
    public static void main(String[] args) {
        Roll[0] = new Student("Burris","Adam", 20221483, "CompSci", 4.0, 2, 8);
        Roll[1] = new Student("Blanton","Emily", 21221484, "Finance",3.0, 2, 6);
        Student Student3 = new Student("Allred","Adam", 22221485, "Kinesiology",3.8, 20, 76);
        Student Student4 = new Student("Johnston", "Josh", 23221486, "CompSci", 3.0, 20, 60);
        Student Student5 = new Student("Perryman", "Noah", 24221487, "BCIS", 2.79, 43, 120);
        
        System.out.println(Student3.getName());
        Student3.setFullName("Emily Burris");
        System.out.println(Student3.getName());
        System.out.println(Roll[1].getName());
    }
}

Result:

Noah Perryman
Emily Burris
Emily Burris

I'm not understanding why the getName() is only pulling the last reference to names. First it is the bottom student then it is my setFullName method.

Student class:

class Student{
    private static String fullName;
    private static int studentID;
    private static String major;
    private static double GPA;
    private static int classesTaken;
    private static double scores;
    
    
    public Student (String lastName, String firstName, int studentID, String major, 
            double GPA, int classesTaken, double  scores) {
        
        Student.fullName = firstName + " "+ lastName;
        this.studentID = studentID;
        this.major = major;
        this.GPA = GPA;
        this.classesTaken = classesTaken;
        this.scores = scores;
    }
    
    public String getName() {
        return fullName;
    }
    
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    
    public int getStudentID() {
        return studentID;
    }
    
    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }
    
    public String getMajor() {
        return major;
    }
    
    public void setMajor(String major) {
        this.major = major;
    }
    
    public void completed(double newScore) {
        classesTaken = classesTaken + 1;
        scores = scores + newScore;
        GPA = scores / classesTaken;
    }
    
    public double getGPA() {
        return GPA;
    }
    
    public int getClassesTaken() {
        return classesTaken;
    }
    
    public double getScores() {
        return scores;
    }
    
}
  • 7
    There is probably a bug in `Student`. At a guess, you made `name` static. – Elliott Frisch Jul 28 '21 at 23:38
  • 2
    Right, we'll need to see the full source code for `Student` class please. – markspace Jul 28 '21 at 23:42
  • 1
    Yeah looks like you are using class static variables to store the full name and that all student instances will always return the last value set. Try it print Student4, and 5 too. – Rob Jul 28 '21 at 23:46
  • The keyword `static` _means_ "belonging to the class and not to any individual instance". You should not use it unless you can explain why you need it. – chrylis -cautiouslyoptimistic- Jul 29 '21 at 00:14
  • Related question: [Static vs Instance Variables: Difference?](https://stackoverflow.com/questions/21204589/static-vs-instance-variables-difference) – geocodezip Jul 29 '21 at 00:23

1 Answers1

1

I offer the following observations and suggestions. But they need to be taken in conjunction with your design goals and requirements.

None of the following should be declare static. They should be instance fields so that they can hold different values per instance of the Student class. Otherwise, the values will be shared among all instances and only the last one set will be used for any of them.

    private static String fullName;
    private static int studentID;
    private static String major;
    private static double GPA;
    private static int classesTaken;
    private static double scores;

And change the following...

 Student.fullName = firstName + " "+ lastName;

to

fullName = firstName + " " + lastName;

And changing someone's name and student id might be useful if entered incorrectly but unless you also allow the scores and classes taken to change it could be a recipe for disaster. You might consider not allowing identifying information to change.

Finally, why are you passing the GPA to the constructor when you are also passing the scores and classes taken? Would it not be better to just do the following in the constructor?

GPA = scores/classesTaken;
WJS
  • 36,363
  • 4
  • 24
  • 39