好的,所以这是一个与家庭作业相关的问题,所以你们都知道。
问题:为什么我的arrayList中的对象有不同的课程名称(就像他们应该的那样) 但是所有的字母等级都与arrayList中最后添加的课程相同(它们应该是调用addCourse方法时作为参数传递的等级)?
如果您对我为什么以我的方式写作方式有任何其他问题,那么你们可以提供的任何帮助都会非常有用。这个地方就像一个智慧的字体,我在这里尽我所能。
我知道有些方法没有完成但是在我弄清楚如何解决这个arrayList问题之前我实际上无法完成:)
我的输出如下:
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();
}
}
答案 0 :(得分:5)
您现在正在了解为什么我们建议您避免过度使用静态字段,因为这会导致您的问题。解决方案:不要使用静态字段来保存对象的状态。
将静态字段视为该类的所有对象共享的字段。如果你为所有对象改变它就改变它(实际上它甚至不属于对象而是属于类本身)。
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;
您的代码中还有其他一些问题,包括调用compute(...)
并忽略返回的答案,但我认为这是另一个问题的讨论。