0

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

3
  • 5
    Course.addNewCourse mutates/changes/updates the current object (and doesn't "add" it to anything). Instead, remove that method and used new Course (the constructor should be updated to take courseNumber, credits, courseName) and then add the new Course object to the Transcript. Commented Jan 15, 2013 at 21:02
  • please include the loop code as well as the stack trace Commented Jan 15, 2013 at 21:03
  • wow reading that code really hurt my brain Commented Jan 15, 2013 at 21:07

2 Answers 2

7

You need to create a new Course object for each course, your addNewCourse method only mutates the current Course object. Modify Course like so:

public class Course {
    private final int courseNumber;
    private final int credits;
    private final String courseName;

    public Course(int courseNumber, int credits, String courseName) {
        this.courseNumber = courseNumber;
        this.credits = credits;
        this.courseName = courseName;
    }

    public int getCourseNumber() {
        return courseNumber;
    }

    public int getCredits() {
        return credits;
    }

    public String getCourseName() {
        return courseName;
    }
}

And then use the following:

Transcript t = new Transcript();

Course matematik = new Course(1, 2, "Matematik");
t.addCourse(matematik);

Course turkce = new Course(1, 4, "Türkçe");
t.addCourse(turkce);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for all , really thanks. But , i wonder that, with use that new so that create a new object, will make space more on ram or wherever ? This is the best way ( for performance ) or easiest way ?
@MuhammetArslan There is no other way to do what you are describing except to allocate an object per course.
3

Objects are references in Java, that is, pointers to the objects. So when you do:

Object a = new Object();
Object b = a;

You're NOT copying the whole object a to b, but copying the reference to a to b (the memory address). So both a and b are references to the object created by new.

Let's follow your code so you see what's happening:

Course course = new Course();
Course matematik = course.addNewCourse(1, 2, "Matematik");
    this.courseNumber = courseNumber;
    this.credits = credits;
    this.courseName = courseName;
    return this;

Here you modified course object. matematik now is also the same as course because it points to same object.

Course turkce = course.addNewCourse(1, 4, "Türkçe");

Here you modify course again. Now course, turkce and matematik are all referencing the same object, which you created first with Course course = new Course();.

I think most easy way to fix this is that you create a constructor with parameters:

public class Course {
...
     public Course(int courseNumber,int credits,String courseName) {
           this.courseNumber = courseNumber;
           this.credits = credits;
           this.courseName = courseName;
     }
}

and then

  Course matematik = new Course(1, 2, "Matematik");
  t.addCourse(matematik);

  Course turkce = new Course(1, 4, "Türkçe");
  t.addCourse(turkce);

2 Comments

Thanks for all , really thanks. But , i wonder that, with use that new so that create a new object, will make space more on ram or wherever ? This is the best way ( for performance ) or easiest way ?
You're welcome. This is a common mistake when learning Java. Yes, it takes more RAM and you should avoid doing new if you can. In your case, however, you cannot avoid creating a new object because you want to store different data each time and keep it, so the more data you add, the more RAM you will use, obviously :) And don't worry, computers this days can take a lot more than that ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.