gpt4 book ai didi

java - 添加到我的 arrayList 中的对象正在被添加的最后一个对象覆盖?为什么是这样?

转载 作者:行者123 更新时间:2023-11-29 07:58:09 25 4
gpt4 key购买 nike

<分区>

好吧,这是一个与家庭作业相关的问题,让你们都知道。

问题:为什么我的 arrayList 中的对象有不同的类(class)名称(就像它们应该的那样)但是所有字母成绩都与数组列表中最后添加的类(class)相同(它们应该是调用 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]

类(class).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;
}



}

成绩单.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();
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com