I have 3 classes: Course, CourseEntry and Transcript. In transcript, I have a function to add courses, like that:
public class Transcript {
CourseEntry coursestaken[] = new CourseEntry[6];
public void addCourse(Course course)
{
coursestaken[lastIndexOf(getCoursestaken())] = new CourseEntry(course);
}
(lastIndexOf gives me the empty array index - it's working on)
And in my CourseEntry:
public class CourseEntry {
Course course;
char grade = 'I';
public CourseEntry(Course course)
{
this.course = course;
}
And in my Course:
public class Course {
int courseNumber,credits;
String courseName;
public Course addNewCourse(int courseNumber, int credits, String courseName)
{
this.courseNumber = courseNumber;
this.credits = credits;
this.courseName = courseName;
return this;
}
In my main:
Transcript t = new Transcript();
Course course = new Course();
Course matematik = course.addNewCourse(1, 2, "Matematik");
t.addCourse(matematik);
Course turkce = course.addNewCourse(1, 4, "Türkçe");
t.addCourse(turkce);
But if I loop coursestaken array, it prints the last inserted index for all.
How can I solve that?
Thanks
Course.addNewCoursemutates/changes/updates the current object (and doesn't "add" it to anything). Instead, remove that method and usednew Course(the constructor should be updated to take courseNumber, credits, courseName) and then add the new Course object to the Transcript.