1

Okay so this is a homework related question just so you all know.

Question: Why do my objects in my arrayList have different Course names (like they should) but all the Letter Grades are the same as the last added Course in the arrayList (they should be whatever Grade that was passed as a parameter when the addCourse method was called)?

Any help you guys can provide would be extremely helpful, if you have any other questions about why I wrote things the way I did then feel free to ask. This place is like a font of wisdom and I'm here to learn everything I can.

I know some of the methods are not completed but I can't actually finish until I figure out how to fix this arrayList issue :)

My output looks like this:

Student ID: 11234
Student Name: Cody

[
Course: COP2013
Letter Grade: A
GPA in course: 3.67, 
Course: COP2014
Letter Grade: A
GPA in course: 3.67, 
Course: COP2015
Letter Grade: A
GPA in course: 3.67, 
Course: COP2016
Letter Grade: A
GPA in course: 3.67, 
Course: COP2017
Letter Grade: A
GPA in course: 3.67]

Course.java:

 import java.lang.String.*;

public class Course {

//global variables
private String courseID;
private static String letterGrade;
private static Double numberGrade;

//CONSTRUCTORS
//param
public Course(String id, String letter){
courseID = id;
letterGrade=letter;
compute(letter);
}
//default
public Course(){
    courseID = "COP2053";
    numberGrade = 4.0;
    letterGrade = "A";
    compute(getLetterGrade());
}


//COMPUTING GRADES AND UPDATING
private static Double compute(String letter){
    Double a = 0.0;

    if(letter == "A+"){a = 4.0;}
    else if(letter == "A"){a = 3.67;}
    else if(letter == "A-"){a = 3.33;}
    else if(letter == "B+"){a = 3.00;}
    else if(letter == "B"){a = 2.67;}
    else if(letter == "B-"){a = 2.33;}
    else if(letter == "C+"){a = 2.00;}
    else if(letter == "C"){a = 1.67;}
    else if(letter == "C-"){a = 1.33;}
    else if(letter == "D+"){a = 1.0;}
    else if(letter == "D"){a = .67;}
    else if(letter == "D-"){a = .33;}
    else if(letter == "F"){a = 0.0;}


    return a;

}

public static void updateGrade(String newGrade){
    letterGrade=newGrade;
    numberGrade=compute(newGrade);
}

@Override
public String toString(){
    String a = "\nCourse: "+getCourseID()+"\nLetter Grade: "+getLetterGrade()
            +"\nGPA in course: "+compute(getLetterGrade());
    return a;
}




//GETTERS
public String getCourseID(){
    return courseID;
}

public String getLetterGrade(){
    return letterGrade;
}

public Double getNumberGrade(){
    return numberGrade;
}



}

Transcript.java:

import java.util.ArrayList;


public class Transcript {

private ArrayList<Course> classesTaken = new ArrayList<Course>();

public Transcript(String studentID, String studentName){
 System.out.println("\nStudent ID: "+studentID+"\nStudent Name: "+studentName);
}

public void addCourse(String courseID, String letterGrade){
    Course myCourse = new Course(courseID,letterGrade);
    classesTaken.add(myCourse);
}

public void updateCourse(String courseID, String newLetterGrade){

    for(int i=0; i<classesTaken.size(); i++){

        if(classesTaken.contains(courseID)){
        Course.updateGrade(newLetterGrade);
        }
    }
}

public void getGPA(){
calculateGPA();
}

private Double calculateGPA(){
    for(int i=0; i<classesTaken.size(); i++){
    }


    return 0.0;
}

public void printArray(){
    System.out.println("\n"+classesTaken);
}



}

transcriptTester.java:

public class TranscriptTester {

public static void main(String[] args){
    Transcript test = new Transcript("11234", "Cody");
    test.addCourse("COP2013","A+");
    test.addCourse("COP2014","B+");
    test.addCourse("COP2015","B+");
    test.addCourse("COP2016","D+");
    test.addCourse("COP2017","A");
    test.updateCourse("COP2013", "B-");
    test.updateCourse("COP2014", "A+");
    test.getGPA();
    test.printArray();
}
}
Cody Mallery
  • 131
  • 3
  • 12

1 Answers1

5

You are learning now why we recommend that you avoid over-use of static fields since this is causing your problems. The solution: Don't use static fields to hold an object's state.

Think of static fields as a field shared by all objects of the class. If you change it once it changes for all objects (actually it doesn't even belong to the object but to the class itself).

public class Course {

  //global variables
  private String courseID;

  // the fields below are shared by all the instances of Course.
  private static String letterGrade;
  private static Double numberGrade;

You have some other problems in your code including calling compute(...) and ignoring the answer that is returned, but that's for discussion in another question, I suppose.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373